NMSIS-Core  Version 1.2.0
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 
189 #if defined(__DSP_PRESENT) && (__DSP_PRESENT == 1)
190 #define __RBIT(value) __RV_BITREVI((value), 31)
191 #else
192 __STATIC_FORCEINLINE uint32_t __RBIT(uint32_t value)
193 {
194  uint32_t result;
195  uint32_t s = (4U /*sizeof(v)*/ * 8U) - 1U; /* extra shift needed at end */
196 
197  result = value; /* r will be reversed bits of v; first get LSB of v */
198  for (value >>= 1U; value != 0U; value >>= 1U) {
199  result <<= 1U;
200  result |= value & 1U;
201  s--;
202  }
203  result <<= s; /* shift when v's highest bits are zero */
204  return result;
205 }
206 #endif /* defined(__DSP_PRESENT) && (__DSP_PRESENT == 1) */
207 
214 #if defined(__DSP_PRESENT) && (__DSP_PRESENT == 1)
215 #define __CLZ(data) __RV_CLZ32(data)
216 #else
217 __STATIC_FORCEINLINE uint8_t __CLZ(uint32_t data)
218 {
219  uint8_t ret = 0;
220  uint32_t temp = ~data;
221  while (temp & 0x80000000) {
222  temp <<= 1;
223  ret++;
224  }
225  return ret;
226 }
227 #endif /* defined(__DSP_PRESENT) && (__DSP_PRESENT == 1) */
228 
235 __STATIC_FORCEINLINE unsigned long __CTZ(unsigned long data)
236 {
237  unsigned long ret = 0;
238 
239  while (!(data & 1UL)) {
240  ret++;
241  data = data >> 1;
242  }
243 
244  return ret;
245 }
246 
253 #if __RISCV_XLEN == 32
254 #define __EXPD_BYTE(x) ((unsigned long)(((unsigned long)(x) << 0) | \
255  ((unsigned long)(x) << 8) | \
256  ((unsigned long)(x) << 16) | \
257  ((unsigned long)(x) << 24)))
258 #elif __RISCV_XLEN == 64
259 #define __EXPD_BYTE(x) ((unsigned long)(((unsigned long)(x) << 0) | \
260  ((unsigned long)(x) << 8) | \
261  ((unsigned long)(x) << 16) | \
262  ((unsigned long)(x) << 24) | \
263  ((unsigned long)(x) << 32) | \
264  ((unsigned long)(x) << 40) | \
265  ((unsigned long)(x) << 48) | \
266  ((unsigned long)(x) << 56)))
267 #endif
268  /* End of Doxygen Group NMSIS_Core_ARMCompatiable_Functions */
270 
271 #ifdef __cplusplus
272 }
273 #endif
274 #endif /* __CORE_COMPATIABLE_H__ */
__CTZ
__STATIC_FORCEINLINE unsigned long __CTZ(unsigned long data)
Count tailing zero.
Definition: core_compatiable.h:235
__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:190
__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:215
__REVSH
__STATIC_FORCEINLINE int16_t __REVSH(int16_t value)
Reverse byte order (16 bit)
Definition: core_compatiable.h:159