2 #ifndef _MA_MATH_SCALARS_H_
3 #define _MA_MATH_SCALARS_H_
11 constexpr
inline float fastLn(
float x) {
15 static_assert(
sizeof(
unsigned int) ==
sizeof(
float));
16 static_assert(
sizeof(
float) == 4);
18 if (*
reinterpret_cast<unsigned int*
>(&x) & 0x80000000) [[unlikely]] {
19 return -std::numeric_limits<float>::quiet_NaN();
20 }
else if (!((*
reinterpret_cast<unsigned int*
>(&x) & 0x7FFFFFFF) ^ 0x00000000)) [[unlikely]] {
21 return -std::numeric_limits<float>::infinity();
24 auto bx{*
reinterpret_cast<unsigned int*
>(&x)};
26 const auto t{
static_cast<signed int>(ex) -
static_cast<signed int>(127)};
28 bx = 1065353216 | (bx & 8388607);
29 x = *
reinterpret_cast<float*
>(&bx);
30 return static_cast<float>(-1.49278 + (2.11263 + (-0.729104 + 0.10969 * x) * x) * x + 0.6931471806 * t);
37 static_assert(
sizeof(
float) == 4);
39 x = (12102203.1608621 * x) + 1064986823.01029;
41 const float c{8388608.f};
42 const float d{2139095040.f};
44 if ((x < c) | (x > d)) x = (x < c) ? 0.0f : d;
46 uint32_t n =
static_cast<uint32_t
>(x);
47 x = *
reinterpret_cast<float*
>(&n);
52 constexpr
inline float sigmoid(
float x) {
return 1.0f / (1.0f + std::exp(-x)); }
57 float denominator = 1.0f - x;
59 if (std::abs(denominator) < std::numeric_limits<float>::epsilon()) {
60 denominator = std::numeric_limits<float>::epsilon();
63 return std::log(x / denominator);
66 constexpr
inline int32_t
quantizeValue(
float value,
float scale, int32_t zero_point) {
67 return static_cast<int32_t
>(std::round(value / scale) + zero_point);
71 return static_cast<int32_t
>(std::floor(value / scale) + zero_point);
74 constexpr
inline float dequantizeValue(int32_t value,
float scale, int32_t zero_point) {
75 return static_cast<float>(value - zero_point) * scale;
Definition: ma_math_scalars.h:9
constexpr float inverseSigmoid(float x)
Definition: ma_math_scalars.h:56
constexpr float fastExp(float x)
Definition: ma_math_scalars.h:33
constexpr float fastSigmoid(float x)
Definition: ma_math_scalars.h:54
constexpr float sigmoid(float x)
Definition: ma_math_scalars.h:52
constexpr float dequantizeValue(int32_t value, float scale, int32_t zero_point)
Definition: ma_math_scalars.h:74
constexpr int32_t quantizeValue(float value, float scale, int32_t zero_point)
Definition: ma_math_scalars.h:66
constexpr float fastLn(float x)
Definition: ma_math_scalars.h:11
constexpr int32_t quantizeValueFloor(float value, float scale, int32_t zero_point)
Definition: ma_math_scalars.h:70