28#include "siddefs-fp.h"
38 constexpr int32_t threshold = 28000;
41 inline int32_t clipper(int32_t x)
43 static_assert(m > 0,
"Clipper range must be a positive value");
45 if (likely(x < threshold))
48 constexpr double max_val =
static_cast<double>(m);
49 constexpr double max_val_inv = 1. /
static_cast<double>(m);
50 constexpr double t = threshold / max_val;
51 constexpr double a = 1. - t;
52 constexpr double b = 1. / a;
54 double value =
static_cast<double>(x - threshold) * max_val_inv;
55 value = a * std::tanh(b * value);
56 return static_cast<int32_t
>(threshold + (value * max_val));
62 inline int32_t softClipImpl(int32_t x)
64 return x < 0 ? -clipper<32768>(-x) : clipper<32767>(x);
71 inline int16_t softClip(int32_t x) {
return static_cast<int16_t
>(softClipImpl(x)); }