33 static constexpr int32_t threshold = 28000;
36 static inline int32_t clipper(int32_t x)
38 static_assert(m > 0,
"Clipper range must be a positive value");
40 if (likely(x < threshold))
43 constexpr double max_val =
static_cast<double>(m);
44 constexpr double t = threshold / max_val;
45 constexpr double a = 1. - t;
46 constexpr double b = 1. / a;
48 double value =
static_cast<double>(x - threshold) / max_val;
49 value = a * std::tanh(b * value);
50 return static_cast<int32_t
>(threshold + (value * max_val));
56 static inline int32_t softClipImpl(int32_t x)
58 return x < 0 ? -clipper<32768>(-x) : clipper<32767>(x);
65 static inline int16_t softClip(int32_t x) {
return static_cast<int16_t
>(softClipImpl(x)); }