Edge AI Add-on API 3.0.0
Loading...
Searching...
No Matches
nrf_dsp_c_intrinsics.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
6#ifndef _NRF_DSP_C_INTRINSICS_H_
7#define _NRF_DSP_C_INTRINSICS_H_
8
10#include "nrf_dsp_defs.h"
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16#define __NRF_DSP_CLZ __CLZ
17#define __NRF_DSP_SSAT __SSAT
18#define __NRF_DSP_USAT __USAT
19#define __NRF_DSP_ROR __ROR
20
26__NRF_DSP_STATIC_FORCEINLINE uint8_t __CLZ(uint32_t data)
27{
28 if (data == 0U) {
29 return 32U;
30 }
31
32 uint32_t count = 0U;
33 uint32_t mask = 0x80000000U;
34
35 while ((data & mask) == 0U) {
36 count += 1U;
37 mask = mask >> 1U;
38 }
39 return count;
40}
41
49__NRF_DSP_STATIC_FORCEINLINE int32_t __SSAT(int32_t val, uint32_t sat)
50{
51 if ((sat >= 1U) && (sat <= 32U)) {
52 const int32_t max = (int32_t)((1U << (sat - 1U)) - 1U);
53 const int32_t min = -1 - max;
54
55 if (val > max) {
56 return max;
57 } else if (val < min) {
58 return min;
59 }
60 }
61 return val;
62}
63
67#define __SSAT16(x) CLIP_MINMAX((x), NRF_DSP_INT16_MIN, NRF_DSP_INT16_MAX)
68
76__NRF_DSP_STATIC_FORCEINLINE uint32_t __USAT(int32_t val, uint32_t sat)
77{
78 if (sat <= 31U) {
79 const uint32_t max = ((1U << sat) - 1U);
80
81 if (val > (int32_t)max) {
82 return max;
83 } else if (val < 0) {
84 return 0U;
85 }
86 }
87 return (uint32_t)val;
88}
89
98__NRF_DSP_STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
99{
100 op2 %= 32U;
101
102 return (op2 == 0U) ? op1 : ((op1 >> op2) | (op1 << (32U - op2)));
103}
104
110__NRF_DSP_STATIC_FORCEINLINE int32_t clip_q63_to_q31(int64_t x)
111{
112 bool overflow = ((int32_t)(x >> 32) != ((int32_t)x >> 31));
113
114 return overflow ? ((0x7FFFFFFF ^ ((int32_t)(x >> 63)))) : (int32_t)x;
115}
116
123__NRF_DSP_STATIC_FORCEINLINE int16_t clip_q63_to_q15(int64_t x)
124{
125 bool overflow = ((int32_t)(x >> 32) != ((int32_t)x >> 31));
126
127 return overflow ? ((0x7FFF ^ ((int16_t)(x >> 63)))) : (int16_t)(x >> 15);
128}
129
135__NRF_DSP_STATIC_FORCEINLINE int8_t clip_q31_to_q7(int32_t x)
136{
137 bool overflow = ((int32_t)(x >> 24) != ((int32_t)x >> 23));
138
139 return overflow ? ((0x7F ^ ((int8_t)(x >> 31)))) : (int8_t)x;
140}
141
147__NRF_DSP_STATIC_FORCEINLINE int16_t clip_q31_to_q15(int32_t x)
148{
149 bool overflow = ((int32_t)(x >> 16) != ((int32_t)x >> 15));
150
151 return overflow ? ((0x7FFF ^ ((int16_t)(x >> 31)))) : (int16_t)x;
152}
153
154#ifdef __cplusplus
155}
156#endif
157
158#endif /* _NRF_DSP_C_INTRINSICS_H_ */
__NRF_DSP_STATIC_FORCEINLINE int16_t clip_q31_to_q15(int32_t x)
Clips a Q31 value to Q15 range.
Definition nrf_dsp_c_intrinsics.h:147
__NRF_DSP_STATIC_FORCEINLINE int8_t clip_q31_to_q7(int32_t x)
Clips a Q31 value to Q7 range.
Definition nrf_dsp_c_intrinsics.h:135
__NRF_DSP_STATIC_FORCEINLINE int32_t clip_q63_to_q31(int64_t x)
Clips a Q63 value to Q31 range.
Definition nrf_dsp_c_intrinsics.h:110
__NRF_DSP_STATIC_FORCEINLINE int16_t clip_q63_to_q15(int64_t x)
Clips a Q63 value to Q15 range.
Definition nrf_dsp_c_intrinsics.h:123