|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef _ARM_MATH_UTILS_H_ |
|
|
|
|
|
#define _ARM_MATH_UTILS_H_ |
|
|
|
|
|
#include "edge-impulse-sdk/CMSIS/DSP/Include/arm_math_types.h" |
|
|
|
|
|
#ifdef __cplusplus |
|
|
extern "C" |
|
|
{ |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define INDEX_MASK 0x0000003F |
|
|
|
|
|
|
|
|
#define SQ(x) ((x) * (x)) |
|
|
|
|
|
#define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__STATIC_FORCEINLINE uint32_t arm_recip_q31( |
|
|
q31_t in, |
|
|
q31_t * dst, |
|
|
const q31_t * pRecipTable) |
|
|
{ |
|
|
q31_t out; |
|
|
uint32_t tempVal; |
|
|
uint32_t index, i; |
|
|
uint32_t signBits; |
|
|
|
|
|
if (in > 0) |
|
|
{ |
|
|
signBits = ((uint32_t) (__CLZ( in) - 1)); |
|
|
} |
|
|
else |
|
|
{ |
|
|
signBits = ((uint32_t) (__CLZ(-in) - 1)); |
|
|
} |
|
|
|
|
|
|
|
|
in = (in << signBits); |
|
|
|
|
|
|
|
|
index = (uint32_t)(in >> 24); |
|
|
index = (index & INDEX_MASK); |
|
|
|
|
|
|
|
|
out = pRecipTable[index]; |
|
|
|
|
|
|
|
|
|
|
|
for (i = 0U; i < 2U; i++) |
|
|
{ |
|
|
tempVal = (uint32_t) (((q63_t) in * out) >> 31); |
|
|
tempVal = 0x7FFFFFFFu - tempVal; |
|
|
|
|
|
|
|
|
out = clip_q63_to_q31(((q63_t) out * tempVal) >> 30); |
|
|
} |
|
|
|
|
|
|
|
|
*dst = out; |
|
|
|
|
|
|
|
|
return (signBits + 1U); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__STATIC_FORCEINLINE uint32_t arm_recip_q15( |
|
|
q15_t in, |
|
|
q15_t * dst, |
|
|
const q15_t * pRecipTable) |
|
|
{ |
|
|
q15_t out = 0; |
|
|
uint32_t tempVal = 0; |
|
|
uint32_t index = 0, i = 0; |
|
|
uint32_t signBits = 0; |
|
|
|
|
|
if (in > 0) |
|
|
{ |
|
|
signBits = ((uint32_t)(__CLZ( in) - 17)); |
|
|
} |
|
|
else |
|
|
{ |
|
|
signBits = ((uint32_t)(__CLZ(-in) - 17)); |
|
|
} |
|
|
|
|
|
|
|
|
in = (in << signBits); |
|
|
|
|
|
|
|
|
index = (uint32_t)(in >> 8); |
|
|
index = (index & INDEX_MASK); |
|
|
|
|
|
|
|
|
out = pRecipTable[index]; |
|
|
|
|
|
|
|
|
|
|
|
for (i = 0U; i < 2U; i++) |
|
|
{ |
|
|
tempVal = (uint32_t) (((q31_t) in * out) >> 15); |
|
|
tempVal = 0x7FFFu - tempVal; |
|
|
|
|
|
out = (q15_t) (((q31_t) out * tempVal) >> 14); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
*dst = out; |
|
|
|
|
|
|
|
|
return (signBits + 1); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__STATIC_INLINE void arm_norm_64_to_32u(uint64_t in, int32_t * normalized, int32_t *norm) |
|
|
{ |
|
|
int32_t n1; |
|
|
int32_t hi = (int32_t) (in >> 32); |
|
|
int32_t lo = (int32_t) ((in << 32) >> 32); |
|
|
|
|
|
n1 = __CLZ(hi) - 32; |
|
|
if (!n1) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
n1 = __CLZ(lo); |
|
|
if (!n1) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
*norm = -1; |
|
|
*normalized = (((uint32_t) lo) >> 1); |
|
|
} else |
|
|
{ |
|
|
if (n1 == 32) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
*norm = 0; |
|
|
*normalized = 0; |
|
|
} else |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
*norm = n1 - 1; |
|
|
*normalized = lo << *norm; |
|
|
} |
|
|
} |
|
|
} else |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
n1 = 1 - n1; |
|
|
*norm = -n1; |
|
|
|
|
|
|
|
|
|
|
|
*normalized = (((uint32_t) lo) >> n1) | (hi << (32 - n1)); |
|
|
} |
|
|
} |
|
|
|
|
|
__STATIC_INLINE q31_t arm_div_q63_to_q31(q63_t num, q31_t den) |
|
|
{ |
|
|
q31_t result; |
|
|
uint64_t absNum; |
|
|
int32_t normalized; |
|
|
int32_t norm; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
absNum = num > 0 ? num : -num; |
|
|
arm_norm_64_to_32u(absNum, &normalized, &norm); |
|
|
if (norm > 0) |
|
|
|
|
|
|
|
|
|
|
|
result = (q31_t) num / den; |
|
|
else |
|
|
|
|
|
|
|
|
|
|
|
result = (q31_t) (num / den); |
|
|
|
|
|
return result; |
|
|
} |
|
|
|
|
|
|
|
|
#ifdef __cplusplus |
|
|
} |
|
|
#endif |
|
|
|
|
|
#endif |
|
|
|