9#ifndef IMPULSE_MATH_OPS_H
10#define IMPULSE_MATH_OPS_H
101 return (uint32_t)__popcnt(x);
105 return _BitScanReverse(&idx, x) ? (31 - idx) : 32;
109 return _BitScanForward(&idx, x) ? idx : 32;
112#if defined(_M_X64) || defined(_M_ARM64)
113 return (uint32_t)__popcnt64(x);
115 return (uint32_t)(__popcnt((uint32_t)x) + __popcnt((uint32_t)(x >> 32)));
119#if defined(_M_X64) || defined(_M_ARM64)
121 return _BitScanReverse64(&idx, x) ? (63 - idx) : 64;
123 uint32_t high = (uint32_t)(x >> 32);
129#if defined(_M_X64) || defined(_M_ARM64)
131 return _BitScanForward64(&idx, x) ? idx : 64;
133 uint32_t low = (uint32_t)x;
140 return (uint32_t)__builtin_popcount((
unsigned int)x);
143 return x == 0 ? 32 : (uint32_t)__builtin_clz((
unsigned int)x);
146 return x == 0 ? 32 : (uint32_t)__builtin_ctz((
unsigned int)x);
149 return (uint32_t)__builtin_popcountll((
unsigned long long)x);
152 return x == 0 ? 64 : (uint32_t)__builtin_clzll((
unsigned long long)x);
155 return x == 0 ? 64 : (uint32_t)__builtin_ctzll((
unsigned long long)x);
163 if (isnan(x) || x <= 0.0f)
return 0u;
164 if ((
double)x >= 4294967295.0)
return 0xFFFFFFFFu;
165 return (uint32_t)trunc((
double)x);
172 if (isnan(x) || x <= 0.0)
return 0ULL;
173 if (x >= 18446744073709551616.0)
return 0xFFFFFFFFFFFFFFFFULL;
174 return (uint64_t)trunc(x);
181 if (isnan(y) || isinf(y) || y <= -9007199254740992.0 || y >= 9007199254740992.0)
return 0u;
182 int64_t sy = (int64_t)trunc(y);
183 return ((uint32_t)sy) & mask;
209 case MATH_FUNC_SINC:
return isnan(x) ? x : ((fabsf(x) < 1e-7f) ? 1.0f : (sinf(x) / x));
220 case MATH_FUNC_RELU:
return isnan(x) ? x : ((x > 0.0f) ? x : 0.0f);
224 if (isnan(x))
return x;
225 if (isinf(x))
return (x > 0.0f) ? x : 0.0f;
226 if (x <= -15.0f)
return 0.0f;
227 if (x >= 15.0f)
return x;
228 const float k0 = 0.7978845608f;
229 const float k1 = 0.044715f;
230 return 0.5f * x * (1.0f + tanhf(k0 * (x + k1 * x * x * x)));
233 if (isnan(x))
return x;
234 if (isinf(x))
return (x > 0.0f) ? x : 0.0f;
235 return x / (1.0f + expf(-x));
238 if (isnan(x))
return x;
239 if (isinf(x))
return (x > 0.0f) ? x : 0.0f;
240 if (x > 20.0f)
return x;
241 return log1pf(expf(x));
246 if (isnan(x))
return x;
247 if (isinf(x))
return (
float)INFINITY;
248 if (x <= 0.0f && floorf(x) == x)
return (
float)INFINITY;
284 case MATH_FUNC_SINC:
return isnan(x) ? x : ((fabs(x) < 1e-15) ? 1.0 : (sin(x) / x));
299 if (isnan(x))
return x;
300 if (isinf(x))
return (x > 0.0) ? x : 0.0;
301 if (x <= -15.0)
return 0.0;
302 if (x >= 15.0)
return x;
303 const double k0 = 0.7978845608028654;
304 const double k1 = 0.044715;
305 return 0.5 * x * (1.0 + tanh(k0 * (x + k1 * x * x * x)));
308 if (isnan(x))
return x;
309 if (isinf(x))
return (x > 0.0) ? x : 0.0;
310 return x / (1.0 + exp(-x));
313 if (isnan(x))
return x;
314 if (isinf(x))
return (x > 0.0) ? x : 0.0;
315 if (x > 40.0)
return x;
316 return log1p(exp(x));
321 if (isnan(x))
return x;
322 if (isinf(x))
return (
double)INFINITY;
323 if (x <= 0.0 && floor(x) == x)
return (
double)INFINITY;
341 case 0:
return x + y;
342 case 1:
return x - y;
343 case 2:
return x * y;
344 case 3:
return isnan(x) ? x : ((y == 0.0f || isnan(y)) ? 0.0f : (x / y));
345 case 4:
return powf(x, y);
349 case MATH_FUNC_COPYSIGN:
return (isnan(x) || isnan(y)) ? (float)NAN : copysignf(x, y);
351 case MATH_FUNC_LEAKY_RELU:
return (isnan(x) || isnan(y)) ? (float)NAN : ((x > 0.0f) ? x : (y * x));
352 case MATH_FUNC_SAFE_DIV:
return isnan(x) ? x : ((y == 0.0f || isnan(y)) ? 0.0f : (x / y));
354 if (isnan(x) || isnan(y))
return (
float)NAN;
357 return (s == 0u) ? (float)ux : (float)((ux << s) | (ux >> (32u - s)));
360 if (isnan(x) || isnan(y))
return (
float)NAN;
363 return (s == 0u) ? (float)ux : (float)((ux >> s) | (ux << (32u - s)));
374 case 0:
return x + y;
375 case 1:
return x - y;
376 case 2:
return x * y;
377 case 3:
return isnan(x) ? x : ((y == 0.0 || isnan(y)) ? 0.0 : (x / y));
378 case 4:
return pow(x, y);
382 case MATH_FUNC_COPYSIGN:
return (isnan(x) || isnan(y)) ? (double)NAN : copysign(x, y);
384 case MATH_FUNC_LEAKY_RELU:
return (isnan(x) || isnan(y)) ? (double)NAN : ((x > 0.0) ? x : (y * x));
385 case MATH_FUNC_SAFE_DIV:
return isnan(x) ? x : ((y == 0.0 || isnan(y)) ? 0.0 : (x / y));
387 if (isnan(x) || isnan(y))
return (
double)NAN;
390 return (s == 0u) ? (double)ux : (double)((ux << s) | (ux >> (64u - s)));
393 if (isnan(x) || isnan(y))
return (
double)NAN;
396 return (s == 0u) ? (double)ux : (double)((ux >> s) | (ux << (64u - s)));
408 case 1:
return fmaf(x, y, z);
410 case MATH_FUNC_CLAMP:
return (isnan(x) || isnan(y) || isnan(z)) ? (float)NAN : ((x < y) ? y : ((x > z) ? z : x));
411 case MATH_FUNC_SAFE_DIV:
return isnan(x) ? x : ((y == 0.0f || isnan(y)) ? z : (x / y));
422 case 1:
return fma(x, y, z);
424 case MATH_FUNC_CLAMP:
return (isnan(x) || isnan(y) || isnan(z)) ? (double)NAN : ((x < y) ? y : ((x > z) ? z : x));
425 case MATH_FUNC_SAFE_DIV:
return isnan(x) ? x : ((y == 0.0 || isnan(y)) ? z : (x / y));
434 #pragma omp parallel for schedule(static, 1024) if (count > 2048)
435 for (
size_t i = 0; i < count; ++i) {
441 #pragma omp parallel for schedule(static, 1024) if (count > 2048)
442 for (
size_t i = 0; i < count; ++i) {
451 #pragma omp parallel for schedule(static, 1024) if (count > 2048)
452 for (
size_t i = 0; i < count; ++i) {
458 #pragma omp parallel for schedule(static, 1024) if (count > 2048)
459 for (
size_t i = 0; i < count; ++i) {
468 #pragma omp parallel for schedule(static, 1024) if (count > 2048)
469 for (
size_t i = 0; i < count; ++i) {
475 #pragma omp parallel for schedule(static, 1024) if (count > 2048)
476 for (
size_t i = 0; i < count; ++i) {
static uint32_t impulse_builtin_popcount64(uint64_t x)
Definition impulse_math_ops.h:148
static double impulse_math_unary_f64(uint8_t func_id, double x)
Evaluates a scalar unary math operation on double.
Definition impulse_math_ops.h:264
static float impulse_math_ternary_f32(uint8_t func_id, float x, float y, float z)
Evaluates a scalar ternary math operation on float.
Definition impulse_math_ops.h:405
static uint32_t impulse_builtin_clz32(uint32_t x)
Definition impulse_math_ops.h:142
static void impulse_vector_math_ternary_f32(uint8_t func_id, float *dst, const float *src1, const float *src2, const float *src3, size_t count)
Vectorized Ternary Vector Math Evaluation.
Definition impulse_math_ops.h:467
static uint32_t impulse_builtin_ctz32(uint32_t x)
Definition impulse_math_ops.h:145
static double impulse_math_ternary_f64(uint8_t func_id, double x, double y, double z)
Evaluates a scalar ternary math operation on double.
Definition impulse_math_ops.h:419
static void impulse_vector_math_binary_f32(uint8_t func_id, float *dst, const float *src1, const float *src2, size_t count)
Vectorized Binary Vector Math Evaluation.
Definition impulse_math_ops.h:450
static float impulse_math_unary_f32(uint8_t func_id, float x)
Evaluates a scalar unary math operation on float.
Definition impulse_math_ops.h:189
static void impulse_vector_math_binary_f64(uint8_t func_id, double *dst, const double *src1, const double *src2, size_t count)
Definition impulse_math_ops.h:457
static double impulse_math_binary_f64(uint8_t func_id, double x, double y)
Evaluates a scalar binary math operation on double.
Definition impulse_math_ops.h:372
impulse_math_func_id_t
Canonical 42 Vector Math Function Identifiers (func_id).
Definition impulse_math_ops.h:24
@ MATH_FUNC_COPYSIGN
Definition impulse_math_ops.h:68
@ MATH_FUNC_COS
Definition impulse_math_ops.h:46
@ MATH_FUNC_SAFE_DIV
Definition impulse_math_ops.h:92
@ MATH_FUNC_ATAN
Definition impulse_math_ops.h:50
@ MATH_FUNC_ACOS
Definition impulse_math_ops.h:49
@ MATH_FUNC_HYPOT
Definition impulse_math_ops.h:31
@ MATH_FUNC_LOG10
Definition impulse_math_ops.h:41
@ MATH_FUNC_FMOD
Definition impulse_math_ops.h:69
@ MATH_FUNC_TANH
Definition impulse_math_ops.h:57
@ MATH_FUNC_CLZ
Definition impulse_math_ops.h:86
@ MATH_FUNC_COSH
Definition impulse_math_ops.h:56
@ MATH_FUNC_CEIL
Definition impulse_math_ops.h:64
@ MATH_FUNC_CBRT
Definition impulse_math_ops.h:29
@ MATH_FUNC_SIN
Definition impulse_math_ops.h:45
@ MATH_FUNC_SIGMOID
Definition impulse_math_ops.h:74
@ MATH_FUNC_ATANH
Definition impulse_math_ops.h:60
@ MATH_FUNC_EXP2
Definition impulse_math_ops.h:36
@ MATH_FUNC_RELU
Definition impulse_math_ops.h:72
@ MATH_FUNC_TRUNC
Definition impulse_math_ops.h:65
@ MATH_FUNC_FLOOR
Definition impulse_math_ops.h:63
@ MATH_FUNC_GELU
Definition impulse_math_ops.h:75
@ MATH_FUNC_ROUND
Definition impulse_math_ops.h:66
@ MATH_FUNC_CLAMP
Definition impulse_math_ops.h:67
@ MATH_FUNC_ROTL
Definition impulse_math_ops.h:88
@ MATH_FUNC_SILU
Definition impulse_math_ops.h:76
@ MATH_FUNC_LOG2
Definition impulse_math_ops.h:40
@ MATH_FUNC_ASIN
Definition impulse_math_ops.h:48
@ MATH_FUNC_SINC
Definition impulse_math_ops.h:52
@ MATH_FUNC_TAN
Definition impulse_math_ops.h:47
@ MATH_FUNC_ATAN2
Definition impulse_math_ops.h:51
@ MATH_FUNC_ACOSH
Definition impulse_math_ops.h:59
@ MATH_FUNC_POW
Definition impulse_math_ops.h:30
@ MATH_FUNC_SQRT
Definition impulse_math_ops.h:27
@ MATH_FUNC_POPCOUNT
Definition impulse_math_ops.h:85
@ MATH_FUNC_EXP
Definition impulse_math_ops.h:35
@ MATH_FUNC_EXPM1
Definition impulse_math_ops.h:38
@ MATH_FUNC_ISFINITE
Definition impulse_math_ops.h:95
@ MATH_FUNC_LEAKY_RELU
Definition impulse_math_ops.h:73
@ MATH_FUNC_ERF
Definition impulse_math_ops.h:80
@ MATH_FUNC_ROTR
Definition impulse_math_ops.h:89
@ MATH_FUNC_LOG
Definition impulse_math_ops.h:39
@ MATH_FUNC_LERP
Definition impulse_math_ops.h:32
@ MATH_FUNC_ASINH
Definition impulse_math_ops.h:58
@ MATH_FUNC_ABS
Definition impulse_math_ops.h:26
@ MATH_FUNC_ERFC
Definition impulse_math_ops.h:81
@ MATH_FUNC_LOG1P
Definition impulse_math_ops.h:42
@ MATH_FUNC_EXP10
Definition impulse_math_ops.h:37
@ MATH_FUNC_SINH
Definition impulse_math_ops.h:55
@ MATH_FUNC_ISNAN
Definition impulse_math_ops.h:93
@ MATH_FUNC_CTZ
Definition impulse_math_ops.h:87
@ MATH_FUNC_RSQRT
Definition impulse_math_ops.h:28
@ MATH_FUNC_ISINF
Definition impulse_math_ops.h:94
@ MATH_FUNC_LGAMMA
Definition impulse_math_ops.h:82
@ MATH_FUNC_SOFTPLUS
Definition impulse_math_ops.h:77
static void impulse_vector_math_ternary_f64(uint8_t func_id, double *dst, const double *src1, const double *src2, const double *src3, size_t count)
Definition impulse_math_ops.h:474
static uint32_t impulse_shift_amount_f64(double y, uint32_t mask)
UB-free masked shift count extraction from double.
Definition impulse_math_ops.h:180
static void impulse_vector_math_unary_f32(uint8_t func_id, float *dst, const float *src, size_t count)
Vectorized Unary Vector Math Evaluation.
Definition impulse_math_ops.h:433
static uint32_t impulse_builtin_popcount32(uint32_t x)
Definition impulse_math_ops.h:139
static float impulse_math_binary_f32(uint8_t func_id, float x, float y)
Evaluates a scalar binary math operation on float.
Definition impulse_math_ops.h:339
static uint64_t impulse_f64_to_u64_clamped(double x)
UB-free double-to-uint64 saturating truncation helper.
Definition impulse_math_ops.h:171
static void impulse_vector_math_unary_f64(uint8_t func_id, double *dst, const double *src, size_t count)
Definition impulse_math_ops.h:440
static uint32_t impulse_builtin_clz64(uint64_t x)
Definition impulse_math_ops.h:151
static uint32_t impulse_f32_to_u32_clamped(float x)
UB-free float-to-uint32 saturating truncation helper.
Definition impulse_math_ops.h:162
static uint32_t impulse_builtin_ctz64(uint64_t x)
Definition impulse_math_ops.h:154