Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sin/cos/tan musl reimplementation #10276

Merged
merged 4 commits into from
Dec 5, 2021
Merged

Conversation

tiehuis
Copy link
Member

@tiehuis tiehuis commented Dec 5, 2021

This reimplements sin, cos and tan functions in the math library and bases them on musl instead of the previous golang port.

I've tested all f32 values against musl and confirmed that the results are bit-identical using the following simple program.

const std = @import("std");
const c = @cImport(@cInclude("math.h"));

pub fn assertEqual(comptime name: []const u8, in: f32, x: f32, y: f32) !void {
    const bin = @bitCast(u32, in);
    const bx = @bitCast(u32, x);
    const by = @bitCast(u32, y);

    if (bx != by) {
        std.debug.print(
            \\name: {s}
            \\in : {b} ({})
            \\expected {}, found {}
            \\zig: {b}
            \\c  : {b}
            \\
        , .{ name, bin, in, x, y, bx, by });
        return error.NotEqual;
    }
}

const start = 0;
const progress = 10_000_000;

const test_sin = true;
const test_cos = true;
const test_tan = true;

pub fn main() !void {
    var x: u32 = start;
    while (true) : (x += 1) {
        if (x % progress == 0) {
            std.debug.print("{}\n", .{x});
        }

        const y = @bitCast(f32, x);

        if (test_sin) {
            const z_sin = std.math.sin(y);
            const c_sin = c.sinf(y);
            assertEqual("sin", y, z_sin, c_sin) catch {};
        }

        if (test_cos) {
            const z_cos = std.math.cos(y);
            const c_cos = c.cosf(y);
            assertEqual("cos", y, z_cos, c_cos) catch {};
        }

        if (test_tan) {
            const z_tan = std.math.tan(y);
            const c_tan = c.tanf(y);
            assertEqual("tan", y, z_tan, c_tan) catch {};
        }

        if (x == std.math.maxInt(u32)) break;
    }
}

Built with

zig build-exe -target x86_64-linux-musl test.zig -lc -D ReleaseFast

I've also tested a few million f64 cases using afl++ and @squeek502's fuzzing code and found no errors so far.

I haven't made an effort to make the code particular idiomatic to zig at this stage.

try expect(math.approxEqAbs(f32, cos_(f32, -1.5), 0.070737, epsilon));
try expect(math.approxEqAbs(f32, cos_(f32, 37.45), 0.969132, epsilon));
try expect(math.approxEqAbs(f32, cos_(f32, 89.123), 0.400798, epsilon));
const epsilon = 0.00001;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to reduce these epsilons since the calculation method changed and the test cases otherwise would need adjustment.

lib/std/math/cos.zig Outdated Show resolved Hide resolved
@andrewrk
Copy link
Member

andrewrk commented Dec 5, 2021

Thanks!

@andrewrk andrewrk merged commit a7828c2 into ziglang:master Dec 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants