fn log1p_32(x: f32) f32 x: f3230
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101fn log1p_32(x: f32) f32 {
const ln2_hi = 6.9313812256e-01;
const ln2_lo = 9.0580006145e-06;
const Lg1: f32 = 0xaaaaaa.0p-24;
const Lg2: f32 = 0xccce13.0p-25;
const Lg3: f32 = 0x91e9ee.0p-25;
const Lg4: f32 = 0xf89e26.0p-26;
const u: u32 = @bitCast(x);
const ix = u;
var k: i32 = 1;
var f: f32 = undefined;
var c: f32 = undefined;
// 1 + x < sqrt(2)+
if (ix < 0x3ED413D0 or ix >> 31 != 0) {
// x = -1.0
if (ix == 0xBF800000)
// log1p(-1) = -inf
return x / 0.0;
// x < -1.0
if (ix > 0xBF800000)
// log1p(x < -1) = nan
return (x - x) / 0.0;
// |x| < 2^(-24)
if ((ix << 1) < (0x33800000 << 1)) {
// underflow if subnormal
if (ix & 0x7F800000 == 0) {
mem.doNotOptimizeAway(x * x);
}
return x;
}
// sqrt(2) / 2- <= 1 + x < sqrt(2)+
if (ix <= 0xBE95F619) {
k = 0;
c = 0;
f = x;
}
} else if (ix >= 0x7F800000) {
return x;
}
if (k != 0) {
const uf = 1 + x;
var iu = @as(u32, @bitCast(uf));
iu += 0x3F800000 - 0x3F3504F3;
k = @as(i32, @intCast(iu >> 23)) - 0x7F;
// correction to avoid underflow in c / u
if (k < 25) {
c = if (k >= 2) 1 - (uf - x) else x - (uf - 1);
c /= uf;
} else {
c = 0;
}
// u into [sqrt(2)/2, sqrt(2)]
iu = (iu & 0x007FFFFF) + 0x3F3504F3;
f = @as(f32, @bitCast(iu)) - 1;
}
const s = f / (2.0 + f);
const z = s * s;
const w = z * z;
const t1 = w * (Lg2 + w * Lg4);
const t2 = z * (Lg1 + w * Lg3);
const R = t2 + t1;
const hfsq = 0.5 * f * f;
const dk = @as(f32, @floatFromInt(k));
return s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi;
}