//! Ported from musl, which is licensed under the MIT license:
//! https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
//!
//! https://git.musl-libc.org/cgit/musl/tree/src/math/log10f.c
//! https://git.musl-libc.org/cgit/musl/tree/src/math/log10.c

const std = @import("std");
const math = std.math;
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const maxInt = std.math.maxInt;

const compiler_rt = @import("../compiler_rt.zig");
const symbol = @import("../compiler_rt.zig").symbol;

comptime {
    symbol(&__log10h, "__log10h");
    symbol(&log10f, "log10f");
    symbol(&log10, "log10");
    symbol(&__log10x, "__log10x");
    if (compiler_rt.want_ppc_abi) {
        symbol(&log10q, "log10f128");
    }
    symbol(&log10q, "log10q");
    symbol(&log10l, "log10l");
}

pub fn __log10h(a: f16) callconv(.c) f16 {
    // TODO: more efficient implementation
    return @floatCast(log10f(a));
}

pub fn log10f(x_: f32) callconv(.c) f32 {
    const ivln10hi: f32 = 4.3432617188e-01;
    const ivln10lo: f32 = -3.1689971365e-05;
    const log10_2hi: f32 = 3.0102920532e-01;
    const log10_2lo: f32 = 7.9034151668e-07;
    const Lg1: f32 = 0xaaaaaa.0p-24;
    const Lg2: f32 = 0xccce13.0p-25;
    const Lg3: f32 = 0x91e9ee.0p-25;
    const Lg4: f32 = 0xf89e26.0p-26;

    var x = x_;
    var u: u32 = @bitCast(x);
    var ix = u;
    var k: i32 = 0;

    // x < 2^(-126)
    if (ix < 0x00800000 or ix >> 31 != 0) {
        // log(+-0) = -inf
        if (ix << 1 == 0) {
            return if (compiler_rt.want_float_exceptions) -1 / (x * x) else -std.math.inf(f64);
        }
        // log(-#) = nan
        if (ix >> 31 != 0) {
            return if (compiler_rt.want_float_exceptions) (x - x) / 0.0 else math.nan(f64);
        }

        k -= 25;
        x *= 0x1.0p25;
        ix = @bitCast(x);
    } else if (ix >= 0x7F800000) {
        return x;
    } else if (ix == 0x3F800000) {
        return 0;
    }

    // x into [sqrt(2) / 2, sqrt(2)]
    ix += 0x3F800000 - 0x3F3504F3;
    k += @as(i32, @intCast(ix >> 23)) - 0x7F;
    ix = (ix & 0x007FFFFF) + 0x3F3504F3;
    x = @bitCast(ix);

    const f = x - 1.0;
    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;

    var hi = f - hfsq;
    u = @bitCast(hi);
    u &= 0xFFFFF000;
    hi = @bitCast(u);
    const lo = f - hi - hfsq + s * (hfsq + R);
    const dk: f32 = @floatFromInt(k);

    return dk * log10_2lo + (lo + hi) * ivln10lo + lo * ivln10hi + hi * ivln10hi + dk * log10_2hi;
}

pub fn log10(x_: f64) callconv(.c) f64 {
    const ivln10hi: f64 = 4.34294481878168880939e-01;
    const ivln10lo: f64 = 2.50829467116452752298e-11;
    const log10_2hi: f64 = 3.01029995663611771306e-01;
    const log10_2lo: f64 = 3.69423907715893078616e-13;
    const Lg1: f64 = 6.666666666666735130e-01;
    const Lg2: f64 = 3.999999999940941908e-01;
    const Lg3: f64 = 2.857142874366239149e-01;
    const Lg4: f64 = 2.222219843214978396e-01;
    const Lg5: f64 = 1.818357216161805012e-01;
    const Lg6: f64 = 1.531383769920937332e-01;
    const Lg7: f64 = 1.479819860511658591e-01;

    var x = x_;
    var ix: u64 = @bitCast(x);
    var hx: u32 = @intCast(ix >> 32);
    var k: i32 = 0;

    if (hx < 0x00100000 or hx >> 31 != 0) {
        // log(+-0) = -inf
        if (ix << 1 == 0) {
            return if (compiler_rt.want_float_exceptions) -1 / (x * x) else -std.math.inf(f64);
        }
        // log(-#) = nan
        if (hx >> 31 != 0) {
            return if (compiler_rt.want_float_exceptions) (x - x) / 0.0 else math.nan(f64);
        }

        // subnormal, scale x
        k -= 54;
        x *= 0x1.0p54;
        hx = @intCast(@as(u64, @bitCast(x)) >> 32);
    } else if (hx >= 0x7FF00000) {
        return x;
    } else if (hx == 0x3FF00000 and ix << 32 == 0) {
        return 0;
    }

    // x into [sqrt(2) / 2, sqrt(2)]
    hx += 0x3FF00000 - 0x3FE6A09E;
    k += @as(i32, @intCast(hx >> 20)) - 0x3FF;
    hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
    ix = (@as(u64, hx) << 32) | (ix & 0xFFFFFFFF);
    x = @bitCast(ix);

    const f = x - 1.0;
    const hfsq = 0.5 * f * f;
    const s = f / (2.0 + f);
    const z = s * s;
    const w = z * z;
    const t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
    const t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
    const R = t2 + t1;

    // hi + lo = f - hfsq + s * (hfsq + R) ~ log(1 + f)
    var hi = f - hfsq;
    var hii: u64 = @bitCast(hi);
    hii &= @as(u64, maxInt(u64)) << 32;
    hi = @bitCast(hii);
    const lo = f - hi - hfsq + s * (hfsq + R);

    // val_hi + val_lo ~ log10(1 + f) + k * log10(2)
    var val_hi = hi * ivln10hi;
    const dk: f64 = @floatFromInt(k);
    const y = dk * log10_2hi;
    var val_lo = dk * log10_2lo + (lo + hi) * ivln10lo + lo * ivln10hi;

    // Extra precision multiplication
    const ww = y + val_hi;
    val_lo += (y - ww) + val_hi;
    val_hi = ww;

    return val_lo + val_hi;
}

pub fn __log10x(a: f80) callconv(.c) f80 {
    // TODO: more efficient implementation
    return @floatCast(log10q(a));
}

pub fn log10q(a: f128) callconv(.c) f128 {
    // TODO: more correct implementation
    return log10(@floatCast(a));
}

pub fn log10l(x: c_longdouble) callconv(.c) c_longdouble {
    switch (@typeInfo(c_longdouble).float.bits) {
        16 => return __log10h(x),
        32 => return log10f(x),
        64 => return log10(x),
        80 => return __log10x(x),
        128 => return log10q(x),
        else => @compileError("unreachable"),
    }
}

test "log10f() special" {
    try expectEqual(log10f(0.0), -math.inf(f32));
    try expectEqual(log10f(-0.0), -math.inf(f32));
    try expect(math.isPositiveZero(log10f(1.0)));
    try expectEqual(log10f(10.0), 1.0);
    try expectEqual(log10f(0.1), -1.0);
    try expectEqual(log10f(math.inf(f32)), math.inf(f32));
    try expect(math.isNan(log10f(-1.0)));
    try expect(math.isNan(log10f(-math.inf(f32))));
    try expect(math.isNan(log10f(math.nan(f32))));
    try expect(math.isNan(log10f(math.snan(f32))));
}

test "log10f() sanity" {
    try expect(math.isNan(log10f(-0x1.0223a0p+3)));
    try expectEqual(log10f(0x1.161868p+2), 0x1.46a9bcp-1);
    try expect(math.isNan(log10f(-0x1.0c34b4p+3)));
    try expect(math.isNan(log10f(-0x1.a206f0p+2)));
    try expectEqual(log10f(0x1.288bbcp+3), 0x1.ef1300p-1);
    try expectEqual(log10f(0x1.52efd0p-1), -0x1.6ee6dcp-3); // Disagrees with GCC in last bit
    try expect(math.isNan(log10f(-0x1.a05cc8p-2)));
    try expectEqual(log10f(0x1.1f9efap-1), -0x1.0075ccp-2);
    try expectEqual(log10f(0x1.8c5db0p-1), -0x1.c75df8p-4);
    try expect(math.isNan(log10f(-0x1.5b86eap-1)));
}

test "log10f() boundary" {
    try expectEqual(log10f(0x1.fffffep+127), 0x1.344136p+5); // Max input value
    try expectEqual(log10f(0x1p-149), -0x1.66d3e8p+5); // Min positive input value
    try expect(math.isNan(log10f(-0x1p-149))); // Min negative input value
    try expectEqual(log10f(0x1.000002p+0), 0x1.bcb7b0p-25); // Last value before result reaches +0
    try expectEqual(log10f(0x1.fffffep-1), -0x1.bcb7b2p-26); // Last value before result reaches -0
    try expectEqual(log10f(0x1p-126), -0x1.2f7030p+5); // First subnormal
    try expect(math.isNan(log10f(-0x1p-126))); // First negative subnormal
}

test "log10() special" {
    try expectEqual(log10(0.0), -math.inf(f64));
    try expectEqual(log10(-0.0), -math.inf(f64));
    try expect(math.isPositiveZero(log10(1.0)));
    try expectEqual(log10(10.0), 1.0);
    try expectEqual(log10(0.1), -1.0);
    try expectEqual(log10(math.inf(f64)), math.inf(f64));
    try expect(math.isNan(log10(-1.0)));
    try expect(math.isNan(log10(-math.inf(f64))));
    try expect(math.isNan(log10(math.nan(f64))));
    try expect(math.isNan(log10(math.snan(f64))));
}

test "log10() sanity" {
    try expect(math.isNan(log10(-0x1.02239f3c6a8f1p+3)));
    try expectEqual(log10(0x1.161868e18bc67p+2), 0x1.46a9bd1d2eb87p-1);
    try expect(math.isNan(log10(-0x1.0c34b3e01e6e7p+3)));
    try expect(math.isNan(log10(-0x1.a206f0a19dcc4p+2)));
    try expectEqual(log10(0x1.288bbb0d6a1e6p+3), 0x1.ef12fff994862p-1);
    try expectEqual(log10(0x1.52efd0cd80497p-1), -0x1.6ee6db5a155cbp-3);
    try expect(math.isNan(log10(-0x1.a05cc754481d1p-2)));
    try expectEqual(log10(0x1.1f9ef934745cbp-1), -0x1.0075cda79d321p-2);
    try expectEqual(log10(0x1.8c5db097f7442p-1), -0x1.c75df6442465ap-4);
    try expect(math.isNan(log10(-0x1.5b86ea8118a0ep-1)));
}

test "log10() boundary" {
    try expectEqual(log10(0x1.fffffffffffffp+1023), 0x1.34413509f79ffp+8); // Max input value
    try expectEqual(log10(0x1p-1074), -0x1.434e6420f4374p+8); // Min positive input value
    try expect(math.isNan(log10(-0x1p-1074))); // Min negative input value
    try expectEqual(log10(0x1.0000000000001p+0), 0x1.bcb7b1526e50dp-54); // Last value before result reaches +0
    try expectEqual(log10(0x1.fffffffffffffp-1), -0x1.bcb7b1526e50fp-55); // Last value before result reaches -0
    try expectEqual(log10(0x1p-1022), -0x1.33a7146f72a42p+8); // First subnormal
    try expect(math.isNan(log10(-0x1p-1022))); // First negative subnormal
}
