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;...@@ -23,21 +23,21 @@ const testing = std.testing;
23/// - atan(+-inf) = +-pi/223/// - atan(+-inf) = +-pi/2
24pub fn atan(x: anytype) @TypeOf(x) {24pub fn atan(x: anytype) @TypeOf(x) {
25 const T = @TypeOf(x);25 const T = @TypeOf(x);
26 switch (T) {26 switch (@typeInfo(T)) {
27 f16 => return atanBinary16(x),27 .float => |info| switch (info.bits) {
28 f32 => return atanBinary32(x),28 16 => return atanBinary16(x),
29 f64 => return atanBinary64(x),29 32 => return atanBinary32(x),
30 f80 => return atanExtended80(x),30 64 => return atanBinary64(x),
31 f128 => return atanBinary128(x),31 80 => return atanExtended80(x),
32 else => {32 128 => return atanBinary128(x),
33 const info = @typeInfo(T);33 else => comptime unreachable,
34 if (info == .vector) {34 },
35 const vec_type = info.vector.child;35 .vector => |info| switch (info.child) {
36 if (vec_type == f32) return atanBinary32Vec(x);36 f32 => return atanBinary32Vec(info.len, x),
37 if (vec_type == f64) return atanBinary64Vec(x);37 f64 => return atanBinary64Vec(info.len, x),
38 }38 else => @compileError("unimplemented"),
39 @compileError("atan not implemented for " ++ @typeName(T));
40 },39 },
40 else => comptime unreachable,
41 }41 }
42}42}
4343
...@@ -594,10 +594,7 @@ test "atanBinary128" {...@@ -594,10 +594,7 @@ test "atanBinary128" {
594 try testing.expectApproxEqAbs(atanBinary128(-0x1.0264fb9f3d50e4f0f966f0686064p1), -0x1.1c617825f97512b7f38656ab12cdp0, math.floatEpsAt(f128, -0x1.1c617825f97512b7f38656ab12cdp0));594 try testing.expectApproxEqAbs(atanBinary128(-0x1.0264fb9f3d50e4f0f966f0686064p1), -0x1.1c617825f97512b7f38656ab12cdp0, math.floatEpsAt(f128, -0x1.1c617825f97512b7f38656ab12cdp0));
595}595}
596596
597fn atanBinary32Vec(x: anytype) @TypeOf(x) {597fn atanBinary32Vec(comptime vec_len: comptime_int, x: @Vector(vec_len, f32)) @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;
601 const sign_mask: @Vector(vec_len, u32) = @splat(0x80000000);598 const sign_mask: @Vector(vec_len, u32) = @splat(0x80000000);
602 const neg_one: @Vector(vec_len, f32) = @splat(-1.0);599 const neg_one: @Vector(vec_len, f32) = @splat(-1.0);
603 const pi_over_2: @Vector(vec_len, u32) = @splat(0x3fc90fdb);600 const pi_over_2: @Vector(vec_len, u32) = @splat(0x3fc90fdb);
...@@ -630,10 +627,7 @@ fn atanBinary32Vec(x: anytype) @TypeOf(x) {...@@ -630,10 +627,7 @@ fn atanBinary32Vec(x: anytype) @TypeOf(x) {
630 return @mulAdd(@Vector(vec_len, f32), z3, p0_7, shift + z);627 return @mulAdd(@Vector(vec_len, f32), z3, p0_7, shift + z);
631}628}
632629
633fn atanBinary64Vec(x: anytype) @TypeOf(x) {630fn atanBinary64Vec(comptime vec_len: comptime_int, x: @Vector(vec_len, f64)) @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;
637 const sign_mask: @Vector(vec_len, u64) = @splat(0x8000000000000000);631 const sign_mask: @Vector(vec_len, u64) = @splat(0x8000000000000000);
638 const neg_one: @Vector(vec_len, f64) = @splat(-1.0);632 const neg_one: @Vector(vec_len, f64) = @splat(-1.0);
639 const pi_over_2: @Vector(vec_len, u64) = @splat(0x3ff921fb54442d18);633 const pi_over_2: @Vector(vec_len, u64) = @splat(0x3ff921fb54442d18);
...@@ -701,7 +695,7 @@ test "atanBinary32Vec.special" {...@@ -701,7 +695,7 @@ test "atanBinary32Vec.special" {
701 -math.inf(f32),695 -math.inf(f32),
702 math.nan(f32),696 math.nan(f32),
703 };697 };
704 const output = atanBinary32Vec(input);698 const output = atanBinary32Vec(7, input);
705 try testing.expectEqual(output[0], 0x0p+0);699 try testing.expectEqual(output[0], 0x0p+0);
706 try testing.expectEqual(output[1], -0x0p+0);700 try testing.expectEqual(output[1], -0x0p+0);
707 try testing.expectApproxEqAbs(output[2], 0x1.921fb6p-1, math.floatEpsAt(f32, 0x1.921fb6p-1));701 try testing.expectApproxEqAbs(output[2], 0x1.921fb6p-1, math.floatEpsAt(f32, 0x1.921fb6p-1));
...@@ -724,7 +718,7 @@ test "atanBinary32Vec" {...@@ -724,7 +718,7 @@ test "atanBinary32Vec" {
724 0x1.299d54p1,718 0x1.299d54p1,
725 -0x1.0264fcp1,719 -0x1.0264fcp1,
726 };720 };
727 const output = atanBinary32Vec(input);721 const output = atanBinary32Vec(10, input);
728 try testing.expectApproxEqAbs(output[0], -0x1.74c62p-2, math.floatEpsAt(f32, -0x1.74c62p-2));722 try testing.expectApproxEqAbs(output[0], -0x1.74c62p-2, math.floatEpsAt(f32, -0x1.74c62p-2));
729 try testing.expectApproxEqAbs(output[1], -0x1.375fd8p0, math.floatEpsAt(f32, -0x1.375fd8p0));723 try testing.expectApproxEqAbs(output[1], -0x1.375fd8p0, math.floatEpsAt(f32, -0x1.375fd8p0));
730 try testing.expectApproxEqAbs(output[2], -0x1.11b8aep0, math.floatEpsAt(f32, -0x1.11b8aep0));724 try testing.expectApproxEqAbs(output[2], -0x1.11b8aep0, math.floatEpsAt(f32, -0x1.11b8aep0));
...@@ -747,7 +741,7 @@ test "atanBinary64Vec.special" {...@@ -747,7 +741,7 @@ test "atanBinary64Vec.special" {
747 -math.inf(f64),741 -math.inf(f64),
748 math.nan(f64),742 math.nan(f64),
749 };743 };
750 const output = atanBinary64Vec(input);744 const output = atanBinary64Vec(7, input);
751 try testing.expectEqual(output[0], 0x0p+0);745 try testing.expectEqual(output[0], 0x0p+0);
752 try testing.expectEqual(output[1], -0x0p+0);746 try testing.expectEqual(output[1], -0x0p+0);
753 try testing.expectApproxEqAbs(output[2], 0x1.921fb54442d18p-1, math.floatEpsAt(f64, 0x1.921fb54442d18p-1));747 try testing.expectApproxEqAbs(output[2], 0x1.921fb54442d18p-1, math.floatEpsAt(f64, 0x1.921fb54442d18p-1));
...@@ -770,7 +764,7 @@ test "atanBinary64Vec" {...@@ -770,7 +764,7 @@ test "atanBinary64Vec" {
770 0x1.299d54ac7d6bp1,764 0x1.299d54ac7d6bp1,
771 -0x1.0264fb9f3d50ep1,765 -0x1.0264fb9f3d50ep1,
772 };766 };
773 const output = atanBinary64Vec(input);767 const output = atanBinary64Vec(10, input);
774 try testing.expectApproxEqAbs(output[0], -0x1.74c61f4377016p-2, math.floatEpsAt(f64, -0x1.74c61f4377016p-2));768 try testing.expectApproxEqAbs(output[0], -0x1.74c61f4377016p-2, math.floatEpsAt(f64, -0x1.74c61f4377016p-2));
775 try testing.expectApproxEqAbs(output[1], -0x1.375fd7987cc2p0, math.floatEpsAt(f64, -0x1.375fd7987cc2p0));769 try testing.expectApproxEqAbs(output[1], -0x1.375fd7987cc2p0, math.floatEpsAt(f64, -0x1.375fd7987cc2p0));
776 try testing.expectApproxEqAbs(output[2], -0x1.11b8adeba5616p0, math.floatEpsAt(f64, -0x1.11b8adeba5616p0));770 try testing.expectApproxEqAbs(output[2], -0x1.11b8adeba5616p0, math.floatEpsAt(f64, -0x1.11b8adeba5616p0));