SSCMA-Micro CPP SDK  v2.0.0
SSCMA-Micro is a cross-platform machine learning inference framework designed for embedded devices.
ma_math_scalars.h
Go to the documentation of this file.
1 
2 #ifndef _MA_MATH_SCALARS_H_
3 #define _MA_MATH_SCALARS_H_
4 
5 #include <cmath>
6 #include <cstdint>
7 #include <limits>
8 
9 namespace ma::math {
10 
11 constexpr inline float fastLn(float x) {
12  // "Remez approximation of ln(x)""
13  // https://sites.tufts.edu/atasissa/files/2019/09/remez.pdf
14 
15  static_assert(sizeof(unsigned int) == sizeof(float));
16  static_assert(sizeof(float) == 4);
17 
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();
22  }
23 
24  auto bx{*reinterpret_cast<unsigned int*>(&x)};
25  auto ex{bx >> 23};
26  const auto t{static_cast<signed int>(ex) - static_cast<signed int>(127)};
27 
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);
31 }
32 
33 constexpr inline float fastExp(float x) {
34  // N. Schraudolph, "A Fast, Compact Approximation of the Exponential Function"
35  // https://nic.schraudolph.org/pubs/Schraudolph99.pdf
36 
37  static_assert(sizeof(float) == 4);
38 
39  x = (12102203.1608621 * x) + 1064986823.01029;
40 
41  const float c{8388608.f};
42  const float d{2139095040.f};
43 
44  if ((x < c) | (x > d)) x = (x < c) ? 0.0f : d;
45 
46  uint32_t n = static_cast<uint32_t>(x);
47  x = *reinterpret_cast<float*>(&n);
48 
49  return x;
50 }
51 
52 constexpr inline float sigmoid(float x) { return 1.0f / (1.0f + std::exp(-x)); }
53 
54 constexpr inline float fastSigmoid(float x) { return 1.0f / (1.0f + fastExp(-x)); }
55 
56 constexpr inline float inverseSigmoid(float x) {
57  float denominator = 1.0f - x;
58 
59  if (std::abs(denominator) < std::numeric_limits<float>::epsilon()) {
60  denominator = std::numeric_limits<float>::epsilon();
61  }
62 
63  return std::log(x / denominator);
64 }
65 
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);
68 }
69 
70 constexpr inline int32_t quantizeValueFloor(float value, float scale, int32_t zero_point) {
71  return static_cast<int32_t>(std::floor(value / scale) + zero_point);
72 }
73 
74 constexpr inline float dequantizeValue(int32_t value, float scale, int32_t zero_point) {
75  return static_cast<float>(value - zero_point) * scale;
76 }
77 
78 } // namespace ma::math
79 
80 #endif // _MA_MATH_SCALAR_H
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