zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

log2.zig (8927B) - Raw


      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/log2f.c
      5 //! https://git.musl-libc.org/cgit/musl/tree/src/math/log2.c
      6 
      7 const std = @import("std");
      8 const builtin = @import("builtin");
      9 const math = std.math;
     10 const expect = std.testing.expect;
     11 const expectEqual = std.testing.expectEqual;
     12 const maxInt = std.math.maxInt;
     13 const arch = builtin.cpu.arch;
     14 const common = @import("common.zig");
     15 
     16 pub const panic = common.panic;
     17 
     18 comptime {
     19     @export(&__log2h, .{ .name = "__log2h", .linkage = common.linkage, .visibility = common.visibility });
     20     @export(&log2f, .{ .name = "log2f", .linkage = common.linkage, .visibility = common.visibility });
     21     @export(&log2, .{ .name = "log2", .linkage = common.linkage, .visibility = common.visibility });
     22     @export(&__log2x, .{ .name = "__log2x", .linkage = common.linkage, .visibility = common.visibility });
     23     if (common.want_ppc_abi) {
     24         @export(&log2q, .{ .name = "log2f128", .linkage = common.linkage, .visibility = common.visibility });
     25     }
     26     @export(&log2q, .{ .name = "log2q", .linkage = common.linkage, .visibility = common.visibility });
     27     @export(&log2l, .{ .name = "log2l", .linkage = common.linkage, .visibility = common.visibility });
     28 }
     29 
     30 pub fn __log2h(a: f16) callconv(.c) f16 {
     31     // TODO: more efficient implementation
     32     return @floatCast(log2f(a));
     33 }
     34 
     35 pub fn log2f(x_: f32) callconv(.c) f32 {
     36     const ivln2hi: f32 = 1.4428710938e+00;
     37     const ivln2lo: f32 = -1.7605285393e-04;
     38     const Lg1: f32 = 0xaaaaaa.0p-24;
     39     const Lg2: f32 = 0xccce13.0p-25;
     40     const Lg3: f32 = 0x91e9ee.0p-25;
     41     const Lg4: f32 = 0xf89e26.0p-26;
     42 
     43     var x = x_;
     44     var u: u32 = @bitCast(x);
     45     var ix = u;
     46     var k: i32 = 0;
     47 
     48     // x < 2^(-126)
     49     if (ix < 0x00800000 or ix >> 31 != 0) {
     50         // log(+-0) = -inf
     51         if (ix << 1 == 0) {
     52             return -math.inf(f32);
     53         }
     54         // log(-#) = nan
     55         if (ix >> 31 != 0) {
     56             return math.nan(f32);
     57         }
     58 
     59         k -= 25;
     60         x *= 0x1.0p25;
     61         ix = @bitCast(x);
     62     } else if (ix >= 0x7F800000) {
     63         return x;
     64     } else if (ix == 0x3F800000) {
     65         return 0;
     66     }
     67 
     68     // x into [sqrt(2) / 2, sqrt(2)]
     69     ix += 0x3F800000 - 0x3F3504F3;
     70     k += @as(i32, @intCast(ix >> 23)) - 0x7F;
     71     ix = (ix & 0x007FFFFF) + 0x3F3504F3;
     72     x = @bitCast(ix);
     73 
     74     const f = x - 1.0;
     75     const s = f / (2.0 + f);
     76     const z = s * s;
     77     const w = z * z;
     78     const t1 = w * (Lg2 + w * Lg4);
     79     const t2 = z * (Lg1 + w * Lg3);
     80     const R = t2 + t1;
     81     const hfsq = 0.5 * f * f;
     82 
     83     var hi = f - hfsq;
     84     u = @bitCast(hi);
     85     u &= 0xFFFFF000;
     86     hi = @bitCast(u);
     87     const lo = f - hi - hfsq + s * (hfsq + R);
     88     return (lo + hi) * ivln2lo + lo * ivln2hi + hi * ivln2hi + @as(f32, @floatFromInt(k));
     89 }
     90 
     91 pub fn log2(x_: f64) callconv(.c) f64 {
     92     const ivln2hi: f64 = 1.44269504072144627571e+00;
     93     const ivln2lo: f64 = 1.67517131648865118353e-10;
     94     const Lg1: f64 = 6.666666666666735130e-01;
     95     const Lg2: f64 = 3.999999999940941908e-01;
     96     const Lg3: f64 = 2.857142874366239149e-01;
     97     const Lg4: f64 = 2.222219843214978396e-01;
     98     const Lg5: f64 = 1.818357216161805012e-01;
     99     const Lg6: f64 = 1.531383769920937332e-01;
    100     const Lg7: f64 = 1.479819860511658591e-01;
    101 
    102     var x = x_;
    103     var ix: u64 = @bitCast(x);
    104     var hx: u32 = @intCast(ix >> 32);
    105     var k: i32 = 0;
    106 
    107     if (hx < 0x00100000 or hx >> 31 != 0) {
    108         // log(+-0) = -inf
    109         if (ix << 1 == 0) {
    110             return -math.inf(f64);
    111         }
    112         // log(-#) = nan
    113         if (hx >> 31 != 0) {
    114             return math.nan(f64);
    115         }
    116 
    117         // subnormal, scale x
    118         k -= 54;
    119         x *= 0x1.0p54;
    120         hx = @intCast(@as(u64, @bitCast(x)) >> 32);
    121     } else if (hx >= 0x7FF00000) {
    122         return x;
    123     } else if (hx == 0x3FF00000 and ix << 32 == 0) {
    124         return 0;
    125     }
    126 
    127     // x into [sqrt(2) / 2, sqrt(2)]
    128     hx += 0x3FF00000 - 0x3FE6A09E;
    129     k += @as(i32, @intCast(hx >> 20)) - 0x3FF;
    130     hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
    131     ix = (@as(u64, hx) << 32) | (ix & 0xFFFFFFFF);
    132     x = @bitCast(ix);
    133 
    134     const f = x - 1.0;
    135     const hfsq = 0.5 * f * f;
    136     const s = f / (2.0 + f);
    137     const z = s * s;
    138     const w = z * z;
    139     const t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
    140     const t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
    141     const R = t2 + t1;
    142 
    143     // hi + lo = f - hfsq + s * (hfsq + R) ~ log(1 + f)
    144     var hi = f - hfsq;
    145     var hii = @as(u64, @bitCast(hi));
    146     hii &= @as(u64, maxInt(u64)) << 32;
    147     hi = @bitCast(hii);
    148     const lo = f - hi - hfsq + s * (hfsq + R);
    149 
    150     var val_hi = hi * ivln2hi;
    151     var val_lo = (lo + hi) * ivln2lo + lo * ivln2hi;
    152 
    153     // spadd(val_hi, val_lo, y)
    154     const y: f64 = @floatFromInt(k);
    155     const ww = y + val_hi;
    156     val_lo += (y - ww) + val_hi;
    157     val_hi = ww;
    158 
    159     return val_lo + val_hi;
    160 }
    161 
    162 pub fn __log2x(a: f80) callconv(.c) f80 {
    163     // TODO: more efficient implementation
    164     return @floatCast(log2q(a));
    165 }
    166 
    167 pub fn log2q(a: f128) callconv(.c) f128 {
    168     // TODO: more correct implementation
    169     return log2(@floatCast(a));
    170 }
    171 
    172 pub fn log2l(x: c_longdouble) callconv(.c) c_longdouble {
    173     switch (@typeInfo(c_longdouble).float.bits) {
    174         16 => return __log2h(x),
    175         32 => return log2f(x),
    176         64 => return log2(x),
    177         80 => return __log2x(x),
    178         128 => return log2q(x),
    179         else => @compileError("unreachable"),
    180     }
    181 }
    182 
    183 test "log2f() special" {
    184     try expectEqual(log2f(0.0), -math.inf(f32));
    185     try expectEqual(log2f(-0.0), -math.inf(f32));
    186     try expect(math.isPositiveZero(log2f(1.0)));
    187     try expectEqual(log2f(2.0), 1.0);
    188     try expectEqual(log2f(math.inf(f32)), math.inf(f32));
    189     try expect(math.isNan(log2f(-1.0)));
    190     try expect(math.isNan(log2f(-math.inf(f32))));
    191     try expect(math.isNan(log2f(math.nan(f32))));
    192     try expect(math.isNan(log2f(math.snan(f32))));
    193 }
    194 
    195 test "log2f() sanity" {
    196     try expect(math.isNan(log2f(-0x1.0223a0p+3)));
    197     try expectEqual(log2f(0x1.161868p+2), 0x1.0f49acp+1);
    198     try expect(math.isNan(log2f(-0x1.0c34b4p+3)));
    199     try expect(math.isNan(log2f(-0x1.a206f0p+2)));
    200     try expectEqual(log2f(0x1.288bbcp+3), 0x1.9b2676p+1);
    201     try expectEqual(log2f(0x1.52efd0p-1), -0x1.30b494p-1); // Disagrees with GCC in last bit
    202     try expect(math.isNan(log2f(-0x1.a05cc8p-2)));
    203     try expectEqual(log2f(0x1.1f9efap-1), -0x1.a9f89ap-1);
    204     try expectEqual(log2f(0x1.8c5db0p-1), -0x1.7a2c96p-2);
    205     try expect(math.isNan(log2f(-0x1.5b86eap-1)));
    206 }
    207 
    208 test "log2f() boundary" {
    209     try expectEqual(log2f(0x1.fffffep+127), 0x1p+7); // Max input value
    210     try expectEqual(log2f(0x1p-149), -0x1.2ap+7); // Min positive input value
    211     try expect(math.isNan(log2f(-0x1p-149))); // Min negative input value
    212     try expectEqual(log2f(0x1.000002p+0), 0x1.715474p-23); // Last value before result reaches +0
    213     try expectEqual(log2f(0x1.fffffep-1), -0x1.715478p-24); // Last value before result reaches -0
    214     try expectEqual(log2f(0x1p-126), -0x1.f8p+6); // First subnormal
    215     try expect(math.isNan(log2f(-0x1p-126))); // First negative subnormal
    216 
    217 }
    218 
    219 test "log2() special" {
    220     try expectEqual(log2(0.0), -math.inf(f64));
    221     try expectEqual(log2(-0.0), -math.inf(f64));
    222     try expect(math.isPositiveZero(log2(1.0)));
    223     try expectEqual(log2(2.0), 1.0);
    224     try expectEqual(log2(math.inf(f64)), math.inf(f64));
    225     try expect(math.isNan(log2(-1.0)));
    226     try expect(math.isNan(log2(-math.inf(f64))));
    227     try expect(math.isNan(log2(math.nan(f64))));
    228     try expect(math.isNan(log2(math.snan(f64))));
    229 }
    230 
    231 test "log2() sanity" {
    232     try expect(math.isNan(log2(-0x1.02239f3c6a8f1p+3)));
    233     try expectEqual(log2(0x1.161868e18bc67p+2), 0x1.0f49ac3838580p+1);
    234     try expect(math.isNan(log2(-0x1.0c34b3e01e6e7p+3)));
    235     try expect(math.isNan(log2(-0x1.a206f0a19dcc4p+2)));
    236     try expectEqual(log2(0x1.288bbb0d6a1e6p+3), 0x1.9b26760c2a57ep+1);
    237     try expectEqual(log2(0x1.52efd0cd80497p-1), -0x1.30b490ef684c7p-1);
    238     try expect(math.isNan(log2(-0x1.a05cc754481d1p-2)));
    239     try expectEqual(log2(0x1.1f9ef934745cbp-1), -0x1.a9f89b5f5acb8p-1);
    240     try expectEqual(log2(0x1.8c5db097f7442p-1), -0x1.7a2c947173f06p-2);
    241     try expect(math.isNan(log2(-0x1.5b86ea8118a0ep-1)));
    242 }
    243 
    244 test "log2() boundary" {
    245     try expectEqual(log2(0x1.fffffffffffffp+1023), 0x1p+10); // Max input value
    246     try expectEqual(log2(0x1p-1074), -0x1.0c8p+10); // Min positive input value
    247     try expect(math.isNan(log2(-0x1p-1074))); // Min negative input value
    248     try expectEqual(log2(0x1.0000000000001p+0), 0x1.71547652b82fdp-52); // Last value before result reaches +0
    249     try expectEqual(log2(0x1.fffffffffffffp-1), -0x1.71547652b82fep-53); // Last value before result reaches -0
    250     try expectEqual(log2(0x1p-1022), -0x1.ffp+9); // First subnormal
    251     try expect(math.isNan(log2(-0x1p-1022))); // First negative subnormal
    252 }