authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-13 12:25:53-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-13 12:25:53-08:00
log12e6ac2e8fb034f823390e5d7f1680411c903e7c
treeb8b0268088b056e295422c115fdd5f36df253653
parent8901d38b7d3eab22af87bba9962f5a749fb31661

std.math.atan: simplify type logic


1 files changed, 20 insertions(+), 26 deletions(-)

lib/std/math/atan.zig+20-26
......@@ -23,21 +23,21 @@ const testing = std.testing;
2323/// - atan(+-inf) = +-pi/2
2424pub fn atan(x: anytype) @TypeOf(x) {
2525 const T = @TypeOf(x);
26 switch (T) {
27 f16 => return atanBinary16(x),
28 f32 => return atanBinary32(x),
29 f64 => return atanBinary64(x),
30 f80 => return atanExtended80(x),
31 f128 => return atanBinary128(x),
32 else => {
33 const info = @typeInfo(T);
34 if (info == .vector) {
35 const vec_type = info.vector.child;
36 if (vec_type == f32) return atanBinary32Vec(x);
37 if (vec_type == f64) return atanBinary64Vec(x);
38 }
39 @compileError("atan not implemented for " ++ @typeName(T));
26 switch (@typeInfo(T)) {
27 .float => |info| switch (info.bits) {
28 16 => return atanBinary16(x),
29 32 => return atanBinary32(x),
30 64 => return atanBinary64(x),
31 80 => return atanExtended80(x),
32 128 => return atanBinary128(x),
33 else => comptime unreachable,
34 },
35 .vector => |info| switch (info.child) {
36 f32 => return atanBinary32Vec(info.len, x),
37 f64 => return atanBinary64Vec(info.len, x),
38 else => @compileError("unimplemented"),
4039 },
40 else => comptime unreachable,
4141 }
4242}
4343
......@@ -594,10 +594,7 @@ test "atanBinary128" {
594594 try testing.expectApproxEqAbs(atanBinary128(-0x1.0264fb9f3d50e4f0f966f0686064p1), -0x1.1c617825f97512b7f38656ab12cdp0, math.floatEpsAt(f128, -0x1.1c617825f97512b7f38656ab12cdp0));
595595}
596596
597fn atanBinary32Vec(x: anytype) @TypeOf(x) {
598 const type_info = @typeInfo(@TypeOf(x));
599 comptime std.debug.assert(type_info.vector.child == f32);
600 const vec_len = type_info.vector.len;
597fn atanBinary32Vec(comptime vec_len: comptime_int, x: @Vector(vec_len, f32)) @TypeOf(x) {
601598 const sign_mask: @Vector(vec_len, u32) = @splat(0x80000000);
602599 const neg_one: @Vector(vec_len, f32) = @splat(-1.0);
603600 const pi_over_2: @Vector(vec_len, u32) = @splat(0x3fc90fdb);
......@@ -630,10 +627,7 @@ fn atanBinary32Vec(x: anytype) @TypeOf(x) {
630627 return @mulAdd(@Vector(vec_len, f32), z3, p0_7, shift + z);
631628}
632629
633fn atanBinary64Vec(x: anytype) @TypeOf(x) {
634 const type_info = @typeInfo(@TypeOf(x));
635 comptime std.debug.assert(type_info.vector.child == f64);
636 const vec_len = type_info.vector.len;
630fn atanBinary64Vec(comptime vec_len: comptime_int, x: @Vector(vec_len, f64)) @TypeOf(x) {
637631 const sign_mask: @Vector(vec_len, u64) = @splat(0x8000000000000000);
638632 const neg_one: @Vector(vec_len, f64) = @splat(-1.0);
639633 const pi_over_2: @Vector(vec_len, u64) = @splat(0x3ff921fb54442d18);
......@@ -701,7 +695,7 @@ test "atanBinary32Vec.special" {
701695 -math.inf(f32),
702696 math.nan(f32),
703697 };
704 const output = atanBinary32Vec(input);
698 const output = atanBinary32Vec(7, input);
705699 try testing.expectEqual(output[0], 0x0p+0);
706700 try testing.expectEqual(output[1], -0x0p+0);
707701 try testing.expectApproxEqAbs(output[2], 0x1.921fb6p-1, math.floatEpsAt(f32, 0x1.921fb6p-1));
......@@ -724,7 +718,7 @@ test "atanBinary32Vec" {
724718 0x1.299d54p1,
725719 -0x1.0264fcp1,
726720 };
727 const output = atanBinary32Vec(input);
721 const output = atanBinary32Vec(10, input);
728722 try testing.expectApproxEqAbs(output[0], -0x1.74c62p-2, math.floatEpsAt(f32, -0x1.74c62p-2));
729723 try testing.expectApproxEqAbs(output[1], -0x1.375fd8p0, math.floatEpsAt(f32, -0x1.375fd8p0));
730724 try testing.expectApproxEqAbs(output[2], -0x1.11b8aep0, math.floatEpsAt(f32, -0x1.11b8aep0));
......@@ -747,7 +741,7 @@ test "atanBinary64Vec.special" {
747741 -math.inf(f64),
748742 math.nan(f64),
749743 };
750 const output = atanBinary64Vec(input);
744 const output = atanBinary64Vec(7, input);
751745 try testing.expectEqual(output[0], 0x0p+0);
752746 try testing.expectEqual(output[1], -0x0p+0);
753747 try testing.expectApproxEqAbs(output[2], 0x1.921fb54442d18p-1, math.floatEpsAt(f64, 0x1.921fb54442d18p-1));
......@@ -770,7 +764,7 @@ test "atanBinary64Vec" {
770764 0x1.299d54ac7d6bp1,
771765 -0x1.0264fb9f3d50ep1,
772766 };
773 const output = atanBinary64Vec(input);
767 const output = atanBinary64Vec(10, input);
774768 try testing.expectApproxEqAbs(output[0], -0x1.74c61f4377016p-2, math.floatEpsAt(f64, -0x1.74c61f4377016p-2));
775769 try testing.expectApproxEqAbs(output[1], -0x1.375fd7987cc2p0, math.floatEpsAt(f64, -0x1.375fd7987cc2p0));
776770 try testing.expectApproxEqAbs(output[2], -0x1.11b8adeba5616p0, math.floatEpsAt(f64, -0x1.11b8adeba5616p0));