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