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))
45  x = (x < c) ? 0.0f : d;
46 
47  uint32_t n = static_cast<uint32_t>(x);
48  x = *reinterpret_cast<float*>(&n);
49 
50  return x;
51 }
52 
53 constexpr inline float sigmoid(float x) {
54  return 1.0f / (1.0f + std::exp(-x));
55 }
56 
57 constexpr inline float fastSigmoid(float x) {
58  return 1.0f / (1.0f + fastExp(-x));
59 }
60 
61 constexpr inline float inverseSigmoid(float x) {
62  float denominator = 1.0f - x;
63 
64  if (std::abs(denominator) < std::numeric_limits<float>::epsilon()) {
65  denominator = std::numeric_limits<float>::epsilon();
66  }
67 
68  return std::log(x / denominator);
69 }
70 
71 constexpr inline int32_t quantizeValue(float value, float scale, int32_t zero_point) {
72  return static_cast<int32_t>(std::round(value / scale) + zero_point);
73 }
74 
75 constexpr inline int32_t quantizeValueFloor(float value, float scale, int32_t zero_point) {
76  return static_cast<int32_t>(std::floor(value / scale) + zero_point);
77 }
78 
79 constexpr inline float dequantizeValue(int32_t value, float scale, int32_t zero_point) {
80  return static_cast<float>(value - zero_point) * scale;
81 }
82 
83 } // namespace ma::math
84 
85 #endif // _MA_MATH_SCALAR_H
Definition: ma_math_matrix.cpp:6
constexpr float inverseSigmoid(float x)
Definition: ma_math_scalars.h:61
constexpr float fastExp(float x)
Definition: ma_math_scalars.h:33
constexpr float fastSigmoid(float x)
Definition: ma_math_scalars.h:57
constexpr float sigmoid(float x)
Definition: ma_math_scalars.h:53
constexpr float dequantizeValue(int32_t value, float scale, int32_t zero_point)
Definition: ma_math_scalars.h:79
constexpr int32_t quantizeValue(float value, float scale, int32_t zero_point)
Definition: ma_math_scalars.h:71
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:75