master
  1// Ported from musl, which is licensed under the MIT license:
  2// https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
  3//
  4// https://git.musl-libc.org/cgit/musl/tree/src/math/log1pf.c
  5// https://git.musl-libc.org/cgit/musl/tree/src/math/log1p.c
  6
  7const std = @import("../std.zig");
  8const math = std.math;
  9const mem = std.mem;
 10const expect = std.testing.expect;
 11const expectEqual = std.testing.expectEqual;
 12
 13/// Returns the natural logarithm of 1 + x with greater accuracy when x is near zero.
 14///
 15/// Special Cases:
 16///  - log1p(+inf)  = +inf
 17///  - log1p(+-0)   = +-0
 18///  - log1p(-1)    = -inf
 19///  - log1p(x)     = nan if x < -1
 20///  - log1p(nan)   = nan
 21pub fn log1p(x: anytype) @TypeOf(x) {
 22    const T = @TypeOf(x);
 23    return switch (T) {
 24        f32 => log1p_32(x),
 25        f64 => log1p_64(x),
 26        else => @compileError("log1p not implemented for " ++ @typeName(T)),
 27    };
 28}
 29
 30fn log1p_32(x: f32) f32 {
 31    const ln2_hi = 6.9313812256e-01;
 32    const ln2_lo = 9.0580006145e-06;
 33    const Lg1: f32 = 0xaaaaaa.0p-24;
 34    const Lg2: f32 = 0xccce13.0p-25;
 35    const Lg3: f32 = 0x91e9ee.0p-25;
 36    const Lg4: f32 = 0xf89e26.0p-26;
 37
 38    const u: u32 = @bitCast(x);
 39    const ix = u;
 40    var k: i32 = 1;
 41    var f: f32 = undefined;
 42    var c: f32 = undefined;
 43
 44    // 1 + x < sqrt(2)+
 45    if (ix < 0x3ED413D0 or ix >> 31 != 0) {
 46        // x <= -1.0
 47        if (ix >= 0xBF800000) {
 48            // log1p(-1) = -inf
 49            if (x == -1.0) {
 50                return -math.inf(f32);
 51            }
 52            // log1p(x < -1) = nan
 53            else {
 54                return math.nan(f32);
 55            }
 56        }
 57        // |x| < 2^(-24)
 58        if ((ix << 1) < (0x33800000 << 1)) {
 59            // underflow if subnormal
 60            if (ix & 0x7F800000 == 0) {
 61                mem.doNotOptimizeAway(x * x);
 62            }
 63            return x;
 64        }
 65        // sqrt(2) / 2- <= 1 + x < sqrt(2)+
 66        if (ix <= 0xBE95F619) {
 67            k = 0;
 68            c = 0;
 69            f = x;
 70        }
 71    } else if (ix >= 0x7F800000) {
 72        return x;
 73    }
 74
 75    if (k != 0) {
 76        const uf = 1 + x;
 77        var iu = @as(u32, @bitCast(uf));
 78        iu += 0x3F800000 - 0x3F3504F3;
 79        k = @as(i32, @intCast(iu >> 23)) - 0x7F;
 80
 81        // correction to avoid underflow in c / u
 82        if (k < 25) {
 83            c = if (k >= 2) 1 - (uf - x) else x - (uf - 1);
 84            c /= uf;
 85        } else {
 86            c = 0;
 87        }
 88
 89        // u into [sqrt(2)/2, sqrt(2)]
 90        iu = (iu & 0x007FFFFF) + 0x3F3504F3;
 91        f = @as(f32, @bitCast(iu)) - 1;
 92    }
 93
 94    const s = f / (2.0 + f);
 95    const z = s * s;
 96    const w = z * z;
 97    const t1 = w * (Lg2 + w * Lg4);
 98    const t2 = z * (Lg1 + w * Lg3);
 99    const R = t2 + t1;
100    const hfsq = 0.5 * f * f;
101    const dk = @as(f32, @floatFromInt(k));
102
103    return s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi;
104}
105
106fn log1p_64(x: f64) f64 {
107    const ln2_hi: f64 = 6.93147180369123816490e-01;
108    const ln2_lo: f64 = 1.90821492927058770002e-10;
109    const Lg1: f64 = 6.666666666666735130e-01;
110    const Lg2: f64 = 3.999999999940941908e-01;
111    const Lg3: f64 = 2.857142874366239149e-01;
112    const Lg4: f64 = 2.222219843214978396e-01;
113    const Lg5: f64 = 1.818357216161805012e-01;
114    const Lg6: f64 = 1.531383769920937332e-01;
115    const Lg7: f64 = 1.479819860511658591e-01;
116
117    const ix: u64 = @bitCast(x);
118    const hx: u32 = @intCast(ix >> 32);
119    var k: i32 = 1;
120    var c: f64 = undefined;
121    var f: f64 = undefined;
122
123    // 1 + x < sqrt(2)
124    if (hx < 0x3FDA827A or hx >> 31 != 0) {
125        // x <= -1.0
126        if (hx >= 0xBFF00000) {
127            // log1p(-1) = -inf
128            if (x == -1.0) {
129                return -math.inf(f64);
130            }
131            // log1p(x < -1) = nan
132            else {
133                return math.nan(f64);
134            }
135        }
136        // |x| < 2^(-53)
137        if ((hx << 1) < (0x3CA00000 << 1)) {
138            if ((hx & 0x7FF00000) == 0) {
139                math.raiseUnderflow();
140            }
141            return x;
142        }
143        // sqrt(2) / 2- <= 1 + x < sqrt(2)+
144        if (hx <= 0xBFD2BEC4) {
145            k = 0;
146            c = 0;
147            f = x;
148        }
149    } else if (hx >= 0x7FF00000) {
150        return x;
151    }
152
153    if (k != 0) {
154        const uf = 1 + x;
155        const hu = @as(u64, @bitCast(uf));
156        var iu = @as(u32, @intCast(hu >> 32));
157        iu += 0x3FF00000 - 0x3FE6A09E;
158        k = @as(i32, @intCast(iu >> 20)) - 0x3FF;
159
160        // correction to avoid underflow in c / u
161        if (k < 54) {
162            c = if (k >= 2) 1 - (uf - x) else x - (uf - 1);
163            c /= uf;
164        } else {
165            c = 0;
166        }
167
168        // u into [sqrt(2)/2, sqrt(2)]
169        iu = (iu & 0x000FFFFF) + 0x3FE6A09E;
170        const iq = (@as(u64, iu) << 32) | (hu & 0xFFFFFFFF);
171        f = @as(f64, @bitCast(iq)) - 1;
172    }
173
174    const hfsq = 0.5 * f * f;
175    const s = f / (2.0 + f);
176    const z = s * s;
177    const w = z * z;
178    const t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
179    const t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
180    const R = t2 + t1;
181    const dk = @as(f64, @floatFromInt(k));
182
183    return s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi;
184}
185
186test "log1p_32() special" {
187    try expect(math.isPositiveZero(log1p_32(0.0)));
188    try expect(math.isNegativeZero(log1p_32(-0.0)));
189    try expectEqual(log1p_32(-1.0), -math.inf(f32));
190    try expectEqual(log1p_32(1.0), math.ln2);
191    try expectEqual(log1p_32(math.inf(f32)), math.inf(f32));
192    try expect(math.isNan(log1p_32(-2.0)));
193    try expect(math.isNan(log1p_32(-math.inf(f32))));
194    try expect(math.isNan(log1p_32(math.nan(f32))));
195    try expect(math.isNan(log1p_32(math.snan(f32))));
196}
197
198test "log1p_32() sanity" {
199    try expect(math.isNan(log1p_32(-0x1.0223a0p+3)));
200    try expectEqual(log1p_32(0x1.161868p+2), 0x1.ad1bdcp+0);
201    try expect(math.isNan(log1p_32(-0x1.0c34b4p+3)));
202    try expect(math.isNan(log1p_32(-0x1.a206f0p+2)));
203    try expectEqual(log1p_32(0x1.288bbcp+3), 0x1.2a1ab8p+1);
204    try expectEqual(log1p_32(0x1.52efd0p-1), 0x1.041a4ep-1);
205    try expectEqual(log1p_32(-0x1.a05cc8p-2), -0x1.0b3596p-1);
206    try expectEqual(log1p_32(0x1.1f9efap-1), 0x1.c88344p-2);
207    try expectEqual(log1p_32(0x1.8c5db0p-1), 0x1.258a8ep-1);
208    try expectEqual(log1p_32(-0x1.5b86eap-1), -0x1.22b542p+0);
209}
210
211test "log1p_32() boundary" {
212    try expectEqual(log1p_32(0x1.fffffep+127), 0x1.62e430p+6); // Max input value
213    try expectEqual(log1p_32(0x1p-149), 0x1p-149); // Min positive input value
214    try expectEqual(log1p_32(-0x1p-149), -0x1p-149); // Min negative input value
215    try expectEqual(log1p_32(0x1p-126), 0x1p-126); // First subnormal
216    try expectEqual(log1p_32(-0x1p-126), -0x1p-126); // First negative subnormal
217    try expectEqual(log1p_32(-0x1.fffffep-1), -0x1.0a2b24p+4); // Last value before result is -inf
218    try expect(math.isNan(log1p_32(-0x1.000002p+0))); // First value where result is nan
219}
220
221test "log1p_64() special" {
222    try expect(math.isPositiveZero(log1p_64(0.0)));
223    try expect(math.isNegativeZero(log1p_64(-0.0)));
224    try expectEqual(log1p_64(-1.0), -math.inf(f64));
225    try expectEqual(log1p_64(1.0), math.ln2);
226    try expectEqual(log1p_64(math.inf(f64)), math.inf(f64));
227    try expect(math.isNan(log1p_64(-2.0)));
228    try expect(math.isNan(log1p_64(-math.inf(f64))));
229    try expect(math.isNan(log1p_64(math.nan(f64))));
230    try expect(math.isNan(log1p_64(math.snan(f64))));
231}
232
233test "log1p_64() sanity" {
234    try expect(math.isNan(log1p_64(-0x1.02239f3c6a8f1p+3)));
235    try expectEqual(log1p_64(0x1.161868e18bc67p+2), 0x1.ad1bdd1e9e686p+0); // Disagrees with GCC in last bit
236    try expect(math.isNan(log1p_64(-0x1.0c34b3e01e6e7p+3)));
237    try expect(math.isNan(log1p_64(-0x1.a206f0a19dcc4p+2)));
238    try expectEqual(log1p_64(0x1.288bbb0d6a1e6p+3), 0x1.2a1ab8365b56fp+1);
239    try expectEqual(log1p_64(0x1.52efd0cd80497p-1), 0x1.041a4ec2a680ap-1);
240    try expectEqual(log1p_64(-0x1.a05cc754481d1p-2), -0x1.0b3595423aec1p-1);
241    try expectEqual(log1p_64(0x1.1f9ef934745cbp-1), 0x1.c8834348a846ep-2);
242    try expectEqual(log1p_64(0x1.8c5db097f7442p-1), 0x1.258a8e8a35bbfp-1);
243    try expectEqual(log1p_64(-0x1.5b86ea8118a0ep-1), -0x1.22b5426327502p+0);
244}
245
246test "log1p_64() boundary" {
247    try expectEqual(log1p_64(0x1.fffffffffffffp+1023), 0x1.62e42fefa39efp+9); // Max input value
248    try expectEqual(log1p_64(0x1p-1074), 0x1p-1074); // Min positive input value
249    try expectEqual(log1p_64(-0x1p-1074), -0x1p-1074); // Min negative input value
250    try expectEqual(log1p_64(0x1p-1022), 0x1p-1022); // First subnormal
251    try expectEqual(log1p_64(-0x1p-1022), -0x1p-1022); // First negative subnormal
252    try expectEqual(log1p_64(-0x1.fffffffffffffp-1), -0x1.25e4f7b2737fap+5); // Last value before result is -inf
253    try expect(math.isNan(log1p_64(-0x1.0000000000001p+0))); // First value where result is nan
254}