NMSIS-Core  Version 1.3.1
NMSIS-Core support for Nuclei processor-based devices
core_compatiable.h
1 /*
2  * Copyright (c) 2019 Nuclei Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the License); you may
7  * not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #ifndef __CORE_COMPATIABLE_H__
19 #define __CORE_COMPATIABLE_H__
20 
24 #ifdef __cplusplus
25  extern "C" {
26 #endif
27 
28 #include "core_feature_base.h"
29 
30 /* ===== ARM Compatiable Functions ===== */
42 #define __ISB() __RWMB()
43 
45 #define __DSB() __RWMB()
46 
48 #define __DMB() __RWMB()
49 
51 #define __LDRBT(ptr) __LB((ptr))
52 
53 #define __LDRHT(ptr) __LH((ptr))
54 
55 #define __LDRT(ptr) __LW((ptr))
56 
58 #define __STRBT(val, ptr) __SB((ptr), (val))
59 
60 #define __STRHT(val, ptr) __SH((ptr), (val))
61 
62 #define __STRT(val, ptr) __SW((ptr), (val))
63 
64 /* ===== Saturation Operations ===== */
72 #if defined(__DSP_PRESENT) && (__DSP_PRESENT == 1)
73 #define __SSAT(val, sat) __RV_SCLIP32((val), (sat-1))
74 #else
75 __STATIC_FORCEINLINE int32_t __SSAT(int32_t val, uint32_t sat)
76 {
77  if ((sat >= 1U) && (sat <= 32U)) {
78  const int32_t max = (int32_t)((1U << (sat - 1U)) - 1U);
79  const int32_t min = -1 - max ;
80  if (val > max) {
81  return max;
82  } else if (val < min) {
83  return min;
84  }
85  }
86  return val;
87 }
88 #endif
89 
97 #if defined(__DSP_PRESENT) && (__DSP_PRESENT == 1)
98 #define __USAT(val, sat) __RV_UCLIP32((val), (sat))
99 #else
100 __STATIC_FORCEINLINE uint32_t __USAT(int32_t val, uint32_t sat)
101 {
102  if (sat <= 31U) {
103  const uint32_t max = ((1U << sat) - 1U);
104  if (val > (int32_t)max) {
105  return max;
106  } else if (val < 0) {
107  return 0U;
108  }
109  }
110  return (uint32_t)val;
111 }
112 #endif
113 
114 /* ===== Data Processing Operations ===== */
122 __STATIC_FORCEINLINE uint32_t __REV(uint32_t value)
123 {
124  uint32_t result;
125 
126  result = ((value & 0xff000000) >> 24)
127  | ((value & 0x00ff0000) >> 8 )
128  | ((value & 0x0000ff00) << 8 )
129  | ((value & 0x000000ff) << 24);
130  return result;
131 }
132 
140 __STATIC_FORCEINLINE uint32_t __REV16(uint32_t value)
141 {
142  uint32_t result;
143  result = ((value & 0xff000000) >> 8)
144  | ((value & 0x00ff0000) << 8 )
145  | ((value & 0x0000ff00) >> 8 )
146  | ((value & 0x000000ff) << 8) ;
147 
148  return result;
149 }
150 
159 __STATIC_FORCEINLINE int16_t __REVSH(int16_t value)
160 {
161  int16_t result;
162  result = ((value & 0xff00) >> 8) | ((value & 0x00ff) << 8);
163  return result;
164 }
165 
174 __STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
175 {
176  op2 = op2 & 0x1F;
177  if (op2 == 0U) {
178  return op1;
179  }
180  return (op1 >> op2) | (op1 << (32U - op2));
181 }
182 
191 __STATIC_FORCEINLINE uint64_t __ROR64(uint64_t op1, uint32_t op2)
192 {
193  op2 = op2 & 0x1F;
194  if (op2 == 0U) {
195  return op1;
196  }
197  uint32_t tmp1 = (uint32_t)op1;
198  uint32_t tmp2 = (uint32_t)(op1 >> 32);
199  return (uint64_t)((tmp1 >> op2) | (tmp1 << (32U - op2)))
200  | ((uint64_t)((tmp2 >> op2) | (tmp2 << (32U - op2))) << 32);
201 }
202 
209 #if defined(__DSP_PRESENT) && (__DSP_PRESENT == 1)
210 #define __RBIT(value) __RV_BITREVI((value), 31)
211 #else
212 __STATIC_FORCEINLINE uint32_t __RBIT(uint32_t value)
213 {
214  uint32_t result;
215  uint32_t s = (4U /*sizeof(v)*/ * 8U) - 1U; /* extra shift needed at end */
216 
217  result = value; /* r will be reversed bits of v; first get LSB of v */
218  for (value >>= 1U; value != 0U; value >>= 1U) {
219  result <<= 1U;
220  result |= value & 1U;
221  s--;
222  }
223  result <<= s; /* shift when v's highest bits are zero */
224  return result;
225 }
226 #endif /* defined(__DSP_PRESENT) && (__DSP_PRESENT == 1) */
227 
234 #if defined(__DSP_PRESENT) && (__DSP_PRESENT == 1)
235 #define __CLZ(data) __RV_CLZ32(data)
236 #else
237 __STATIC_FORCEINLINE uint8_t __CLZ(uint32_t data)
238 {
239  uint8_t ret = 0;
240  uint32_t temp = ~data;
241  while (temp & 0x80000000) {
242  temp <<= 1;
243  ret++;
244  }
245  return ret;
246 }
247 #endif /* defined(__DSP_PRESENT) && (__DSP_PRESENT == 1) */
248 
255 __STATIC_FORCEINLINE unsigned long __CTZ(unsigned long data)
256 {
257  unsigned long ret = 0;
258 
259  while (!(data & 1UL)) {
260  ret++;
261  data = data >> 1;
262  }
263 
264  return ret;
265 }
266 
273 #if __RISCV_XLEN == 32
274 #define __EXPD_BYTE(x) ((unsigned long)(((unsigned long)(x) << 0) | \
275  ((unsigned long)(x) << 8) | \
276  ((unsigned long)(x) << 16) | \
277  ((unsigned long)(x) << 24)))
278 #elif __RISCV_XLEN == 64
279 #define __EXPD_BYTE(x) ((unsigned long)(((unsigned long)(x) << 0) | \
280  ((unsigned long)(x) << 8) | \
281  ((unsigned long)(x) << 16) | \
282  ((unsigned long)(x) << 24) | \
283  ((unsigned long)(x) << 32) | \
284  ((unsigned long)(x) << 40) | \
285  ((unsigned long)(x) << 48) | \
286  ((unsigned long)(x) << 56)))
287 #endif
288  /* End of Doxygen Group NMSIS_Core_ARMCompatiable_Functions */
290 
291 #ifdef __cplusplus
292 }
293 #endif
294 #endif /* __CORE_COMPATIABLE_H__ */
__CTZ
__STATIC_FORCEINLINE unsigned long __CTZ(unsigned long data)
Count tailing zero.
Definition: core_compatiable.h:255
__REV
__STATIC_FORCEINLINE uint32_t __REV(uint32_t value)
Reverse byte order (32 bit)
Definition: core_compatiable.h:122
__ROR
__STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
Rotate Right in unsigned value (32 bit)
Definition: core_compatiable.h:174
__SSAT
#define __SSAT(val, sat)
Signed Saturate.
Definition: core_compatiable.h:73
__STATIC_FORCEINLINE
#define __STATIC_FORCEINLINE
Define a static function that should be always inlined by the compiler.
Definition: nmsis_gcc.h:70
__USAT
#define __USAT(val, sat)
Unsigned Saturate.
Definition: core_compatiable.h:98
__RBIT
#define __RBIT(value)
Reverse bit order of value.
Definition: core_compatiable.h:210
__REV16
__STATIC_FORCEINLINE uint32_t __REV16(uint32_t value)
Reverse byte order (16 bit)
Definition: core_compatiable.h:140
__CLZ
#define __CLZ(data)
Count leading zeros.
Definition: core_compatiable.h:235
__REVSH
__STATIC_FORCEINLINE int16_t __REVSH(int16_t value)
Reverse byte order (16 bit)
Definition: core_compatiable.h:159
__ROR64
__STATIC_FORCEINLINE uint64_t __ROR64(uint64_t op1, uint32_t op2)
Rotate Right in uint32x2 value (64 bit)
Definition: core_compatiable.h:191