NMSIS-Core  Version 1.0.0-HummingBird
NMSIS-Core support for HummingBird RISC-V 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 
26 #ifdef __cplusplus
27  extern "C" {
28 #endif
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 __STATIC_FORCEINLINE int32_t __SSAT(int32_t val, uint32_t sat)
73 {
74  if ((sat >= 1U) && (sat <= 32U)) {
75  const int32_t max = (int32_t)((1U << (sat - 1U)) - 1U);
76  const int32_t min = -1 - max ;
77  if (val > max) {
78  return max;
79  } else if (val < min) {
80  return min;
81  }
82  }
83  return val;
84 }
85 
93 __STATIC_FORCEINLINE uint32_t __USAT(int32_t val, uint32_t sat)
94 {
95  if (sat <= 31U) {
96  const uint32_t max = ((1U << sat) - 1U);
97  if (val > (int32_t)max) {
98  return max;
99  } else if (val < 0) {
100  return 0U;
101  }
102  }
103  return (uint32_t)val;
104 }
105 
106 /* ===== Data Processing Operations ===== */
114 __STATIC_FORCEINLINE uint32_t __REV(uint32_t value)
115 {
116  uint32_t result;
117 
118  result = ((value & 0xff000000) >> 24)
119  | ((value & 0x00ff0000) >> 8 )
120  | ((value & 0x0000ff00) << 8 )
121  | ((value & 0x000000ff) << 24);
122  return result;
123 }
124 
132 __STATIC_FORCEINLINE uint32_t __REV16(uint32_t value)
133 {
134  uint32_t result;
135  result = ((value & 0xff000000) >> 8)
136  | ((value & 0x00ff00000) << 8 )
137  | ((value & 0x0000ff00) >> 8 )
138  | ((value & 0x000000ff) << 8) ;
139 
140  return result;
141 }
142 
151 __STATIC_FORCEINLINE int16_t __REVSH(int16_t value)
152 {
153  int16_t result;
154  result = ((value & 0xff00) >> 8) | ((value & 0x00ff) << 8);
155  return result;
156 }
157 
166 __STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
167 {
168  op2 = op2 & 0x1F;
169  if (op2 == 0U) {
170  return op1;
171  }
172  return (op1 >> op2) | (op1 << (32U - op2));
173 }
174 
181 __STATIC_FORCEINLINE uint32_t __RBIT(uint32_t value)
182 {
183  uint32_t result;
184  uint32_t s = (4U /*sizeof(v)*/ * 8U) - 1U; /* extra shift needed at end */
185 
186  result = value; /* r will be reversed bits of v; first get LSB of v */
187  for (value >>= 1U; value != 0U; value >>= 1U) {
188  result <<= 1U;
189  result |= value & 1U;
190  s--;
191  }
192  result <<= s; /* shift when v's highest bits are zero */
193  return result;
194 }
195 
202 __STATIC_FORCEINLINE uint8_t __CLZ(uint32_t data)
203 {
204  uint8_t ret = 0;
205  uint32_t temp = ~data;
206  while (temp & 0x80000000) {
207  temp <<= 1;
208  ret++;
209  }
210  return ret;
211 }
212 
213 /* ===== Macros used in NMSIS-DSP Library ===== */
215 #define __PKHBT(ARG1,ARG2,ARG3) ( ((((uint32_t)(ARG1)) ) & 0x0000FFFFUL) | \
216  ((((uint32_t)(ARG2)) << (ARG3)) & 0xFFFF0000UL) )
217 
218 #define __PKHTB(ARG1,ARG2,ARG3) ( ((((uint32_t)(ARG1)) ) & 0xFFFF0000UL) | \
219  ((((uint32_t)(ARG2)) >> (ARG3)) & 0x0000FFFFUL) )
220  /* End of Doxygen Group NMSIS_Core_ARMCompatiable_Functions */
222 
223 #ifdef __cplusplus
224 }
225 #endif
226 #endif /* __CORE_COMPATIABLE_H__ */
__REV
__STATIC_FORCEINLINE uint32_t __REV(uint32_t value)
Reverse byte order (32 bit)
Definition: core_compatiable.h:114
__ROR
__STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
Rotate Right in unsigned value (32 bit)
Definition: core_compatiable.h:166
__RBIT
__STATIC_FORCEINLINE uint32_t __RBIT(uint32_t value)
Reverse bit order of value.
Definition: core_compatiable.h:181
__SSAT
__STATIC_FORCEINLINE int32_t __SSAT(int32_t val, uint32_t sat)
Signed Saturate.
Definition: core_compatiable.h:72
__STATIC_FORCEINLINE
#define __STATIC_FORCEINLINE
Define a static function that should be always inlined by the compiler.
Definition: nmsis_gcc.h:74
__REV16
__STATIC_FORCEINLINE uint32_t __REV16(uint32_t value)
Reverse byte order (16 bit)
Definition: core_compatiable.h:132
__CLZ
__STATIC_FORCEINLINE uint8_t __CLZ(uint32_t data)
Count leading zeros.
Definition: core_compatiable.h:202
__USAT
__STATIC_FORCEINLINE uint32_t __USAT(int32_t val, uint32_t sat)
Unsigned Saturate.
Definition: core_compatiable.h:93
__REVSH
__STATIC_FORCEINLINE int16_t __REVSH(int16_t value)
Reverse byte order (16 bit)
Definition: core_compatiable.h:151