Edge AI Add-on API 2.2.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) { return 32U; }
29
30 uint32_t count = 0U;
31 uint32_t mask = 0x80000000U;
32
33 while ((data & mask) == 0U)
34 {
35 count += 1U;
36 mask = mask >> 1U;
37 }
38 return count;
39}
40
48__NRF_DSP_STATIC_FORCEINLINE int32_t __SSAT(int32_t val, uint32_t sat)
49{
50 if ((sat >= 1U) && (sat <= 32U))
51 {
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 {
80 const uint32_t max = ((1U << sat) - 1U);
81
82 if (val > (int32_t)max) {
83 return max;
84 } else if (val < 0) {
85 return 0U;
86 }
87 }
88 return (uint32_t)val;
89}
90
98__NRF_DSP_STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
99{
100 op2 %= 32U;
101 return (op2 == 0U) ? op1 : ((op1 >> op2) | (op1 << (32U - op2)));
102}
103
109__NRF_DSP_STATIC_FORCEINLINE int32_t clip_q63_to_q31(int64_t x)
110{
111 return ((int32_t)(x >> 32) != ((int32_t)x >> 31)) ? ((0x7FFFFFFF ^ ((int32_t)(x >> 63)))) :
112 (int32_t)x;
113}
114
121__NRF_DSP_STATIC_FORCEINLINE int16_t clip_q63_to_q15(int64_t x)
122{
123 return ((int32_t)(x >> 32) != ((int32_t)x >> 31)) ? ((0x7FFF ^ ((int16_t)(x >> 63)))) :
124 (int16_t)(x >> 15);
125}
126
132__NRF_DSP_STATIC_FORCEINLINE int8_t clip_q31_to_q7(int32_t x)
133{ return ((int32_t)(x >> 24) != ((int32_t)x >> 23)) ? ((0x7F ^ ((int8_t)(x >> 31)))) : (int8_t)x; }
134
140__NRF_DSP_STATIC_FORCEINLINE int16_t clip_q31_to_q15(int32_t x)
141{
142 return ((int32_t)(x >> 16) != ((int32_t)x >> 15)) ? ((0x7FFF ^ ((int16_t)(x >> 31)))) :
143 (int16_t)x;
144}
145
146#ifdef __cplusplus
147}
148#endif
149
150#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:140
__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:132
__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:109
__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:121