fn tanh32(x: f32) f32
fn tanh32(x: f32) f32 {
const u = @as(u32, @bitCast(x));
const ux = u & 0x7FFFFFFF;
const ax = @as(f32, @bitCast(ux));
const sign = (u >> 31) != 0;
var t: f32 = undefined;
// |x| < log(3) / 2 ~= 0.5493 or nan
if (ux > 0x3F0C9F54) {
// |x| > 10
if (ux > 0x41200000) {
t = 1.0 + 0 / x;
} else {
t = math.expm1(2 * ax);
t = 1 - 2 / (t + 2);
}
}
// |x| > log(5 / 3) / 2 ~= 0.2554
else if (ux > 0x3E82C578) {
t = math.expm1(2 * ax);
t = t / (t + 2);
}
// |x| >= 0x1.0p-126
else if (ux >= 0x00800000) {
t = math.expm1(-2 * ax);
t = -t / (t + 2);
}
// |x| is subnormal
else {
mem.doNotOptimizeAway(ax * ax);
t = ax;
}
return if (sign) -t else t;
}