32 static constexpr int threshold = 28000;
35 static inline int clipper(
int x)
37 static_assert(m > 0,
"Clipper range must be a positive value");
39 if (likely(x < threshold))
42 constexpr double max_val =
static_cast<double>(m);
43 constexpr double t = threshold / max_val;
44 constexpr double a = 1. - t;
45 constexpr double b = 1. / a;
47 double value =
static_cast<double>(x - threshold) / max_val;
48 value = a * std::tanh(b * value);
49 return static_cast<int>(threshold + (value * max_val));
55 static inline int softClipImpl(
int x)
57 return x < 0 ? -clipper<32768>(-x) : clipper<32767>(x);
64 static inline short softClip(
int x) {
return static_cast<short>(softClipImpl(x)); }