authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-05-19 21:11:45-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-05-19 21:11:45-04:00
log85a9bd932f5fd77afecf2443a291f82f0f4a6eaa
tree0431daa515c3bf2b64a4d02e6fcd2c8116c14067
parent1d532f12b568c924baead9de78701f66a526e16b
parent951ab802a38ede9f5329aeae1fed00161321d558
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11491 from poinwn/copysign-signbit

std.math: simplify signbit and copysign

14 files changed, 67 insertions(+), 191 deletions(-)

lib/compiler_rt/fma.zig+2-2
......@@ -57,7 +57,7 @@ pub fn fma(x: f64, y: f64, z: f64) callconv(.C) f64 {
5757 if (spread <= 53 * 2) {
5858 zs = math.scalbn(zs, -spread);
5959 } else {
60 zs = math.copysign(f64, math.floatMin(f64), zs);
60 zs = math.copysign(math.floatMin(f64), zs);
6161 }
6262
6363 const xy = dd_mul(xs, ys);
......@@ -116,7 +116,7 @@ pub fn fmaq(x: f128, y: f128, z: f128) callconv(.C) f128 {
116116 if (spread <= 113 * 2) {
117117 zs = math.scalbn(zs, -spread);
118118 } else {
119 zs = math.copysign(f128, math.floatMin(f128), zs);
119 zs = math.copysign(math.floatMin(f128), zs);
120120 }
121121
122122 const xy = dd_mul128(xs, ys);
lib/std/math/atanh.zig+2-2
......@@ -33,7 +33,7 @@ fn atanh_32(x: f32) f32 {
3333 var y = @bitCast(f32, i); // |x|
3434
3535 if (y == 1.0) {
36 return math.copysign(f32, math.inf(f32), x);
36 return math.copysign(math.inf(f32), x);
3737 }
3838
3939 if (u < 0x3F800000 - (1 << 23)) {
......@@ -62,7 +62,7 @@ fn atanh_64(x: f64) f64 {
6262 var y = @bitCast(f64, u & (maxInt(u64) >> 1)); // |x|
6363
6464 if (y == 1.0) {
65 return math.copysign(f64, math.inf(f64), x);
65 return math.copysign(math.inf(f64), x);
6666 }
6767
6868 if (e < 0x3FF - 1) {
lib/std/math/complex/cosh.zig+10-10
......@@ -45,13 +45,13 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
4545 if (ix < 0x42b17218) {
4646 // x < 88.7: exp(|x|) won't overflow
4747 const h = @exp(@fabs(x)) * 0.5;
48 return Complex(f32).init(math.copysign(f32, h, x) * @cos(y), h * @sin(y));
48 return Complex(f32).init(math.copysign(h, x) * @cos(y), h * @sin(y));
4949 }
5050 // x < 192.7: scale to avoid overflow
5151 else if (ix < 0x4340b1e7) {
5252 const v = Complex(f32).init(@fabs(x), y);
5353 const r = ldexp_cexp(v, -1);
54 return Complex(f32).init(r.re, r.im * math.copysign(f32, 1, x));
54 return Complex(f32).init(r.re, r.im * math.copysign(@as(f32, 1.0), x));
5555 }
5656 // x >= 192.7: result always overflows
5757 else {
......@@ -61,14 +61,14 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
6161 }
6262
6363 if (ix == 0 and iy >= 0x7f800000) {
64 return Complex(f32).init(y - y, math.copysign(f32, 0, x * (y - y)));
64 return Complex(f32).init(y - y, math.copysign(@as(f32, 0.0), x * (y - y)));
6565 }
6666
6767 if (iy == 0 and ix >= 0x7f800000) {
6868 if (hx & 0x7fffff == 0) {
69 return Complex(f32).init(x * x, math.copysign(f32, 0, x) * y);
69 return Complex(f32).init(x * x, math.copysign(@as(f32, 0.0), x) * y);
7070 }
71 return Complex(f32).init(x, math.copysign(f32, 0, (x + x) * y));
71 return Complex(f32).init(x, math.copysign(@as(f32, 0.0), (x + x) * y));
7272 }
7373
7474 if (ix < 0x7f800000 and iy >= 0x7f800000) {
......@@ -113,13 +113,13 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
113113 if (ix < 0x40862e42) {
114114 // x < 710: exp(|x|) won't overflow
115115 const h = @exp(@fabs(x)) * 0.5;
116 return Complex(f64).init(h * @cos(y), math.copysign(f64, h, x) * @sin(y));
116 return Complex(f64).init(h * @cos(y), math.copysign(h, x) * @sin(y));
117117 }
118118 // x < 1455: scale to avoid overflow
119119 else if (ix < 0x4096bbaa) {
120120 const v = Complex(f64).init(@fabs(x), y);
121121 const r = ldexp_cexp(v, -1);
122 return Complex(f64).init(r.re, r.im * math.copysign(f64, 1, x));
122 return Complex(f64).init(r.re, r.im * math.copysign(@as(f64, 1.0), x));
123123 }
124124 // x >= 1455: result always overflows
125125 else {
......@@ -129,14 +129,14 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
129129 }
130130
131131 if (ix | lx == 0 and iy >= 0x7ff00000) {
132 return Complex(f64).init(y - y, math.copysign(f64, 0, x * (y - y)));
132 return Complex(f64).init(y - y, math.copysign(@as(f64, 0.0), x * (y - y)));
133133 }
134134
135135 if (iy | ly == 0 and ix >= 0x7ff00000) {
136136 if ((hx & 0xfffff) | lx == 0) {
137 return Complex(f64).init(x * x, math.copysign(f64, 0, x) * y);
137 return Complex(f64).init(x * x, math.copysign(@as(f64, 0.0), x) * y);
138138 }
139 return Complex(f64).init(x * x, math.copysign(f64, 0, (x + x) * y));
139 return Complex(f64).init(x * x, math.copysign(@as(f64, 0.0), (x + x) * y));
140140 }
141141
142142 if (ix < 0x7ff00000 and iy >= 0x7ff00000) {
lib/std/math/complex/proj.zig+1-1
......@@ -9,7 +9,7 @@ pub fn proj(z: anytype) Complex(@TypeOf(z.re)) {
99 const T = @TypeOf(z.re);
1010
1111 if (math.isInf(z.re) or math.isInf(z.im)) {
12 return Complex(T).init(math.inf(T), math.copysign(T, 0, z.re));
12 return Complex(T).init(math.inf(T), math.copysign(@as(T, 0.0), z.re));
1313 }
1414
1515 return Complex(T).init(z.re, z.im);
lib/std/math/complex/sinh.zig+8-8
......@@ -45,13 +45,13 @@ fn sinh32(z: Complex(f32)) Complex(f32) {
4545 if (ix < 0x42b17218) {
4646 // x < 88.7: exp(|x|) won't overflow
4747 const h = @exp(@fabs(x)) * 0.5;
48 return Complex(f32).init(math.copysign(f32, h, x) * @cos(y), h * @sin(y));
48 return Complex(f32).init(math.copysign(h, x) * @cos(y), h * @sin(y));
4949 }
5050 // x < 192.7: scale to avoid overflow
5151 else if (ix < 0x4340b1e7) {
5252 const v = Complex(f32).init(@fabs(x), y);
5353 const r = ldexp_cexp(v, -1);
54 return Complex(f32).init(r.re * math.copysign(f32, 1, x), r.im);
54 return Complex(f32).init(r.re * math.copysign(@as(f32, 1.0), x), r.im);
5555 }
5656 // x >= 192.7: result always overflows
5757 else {
......@@ -61,14 +61,14 @@ fn sinh32(z: Complex(f32)) Complex(f32) {
6161 }
6262
6363 if (ix == 0 and iy >= 0x7f800000) {
64 return Complex(f32).init(math.copysign(f32, 0, x * (y - y)), y - y);
64 return Complex(f32).init(math.copysign(@as(f32, 0.0), x * (y - y)), y - y);
6565 }
6666
6767 if (iy == 0 and ix >= 0x7f800000) {
6868 if (hx & 0x7fffff == 0) {
6969 return Complex(f32).init(x, y);
7070 }
71 return Complex(f32).init(x, math.copysign(f32, 0, y));
71 return Complex(f32).init(x, math.copysign(@as(f32, 0.0), y));
7272 }
7373
7474 if (ix < 0x7f800000 and iy >= 0x7f800000) {
......@@ -112,13 +112,13 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
112112 if (ix < 0x40862e42) {
113113 // x < 710: exp(|x|) won't overflow
114114 const h = @exp(@fabs(x)) * 0.5;
115 return Complex(f64).init(math.copysign(f64, h, x) * @cos(y), h * @sin(y));
115 return Complex(f64).init(math.copysign(h, x) * @cos(y), h * @sin(y));
116116 }
117117 // x < 1455: scale to avoid overflow
118118 else if (ix < 0x4096bbaa) {
119119 const v = Complex(f64).init(@fabs(x), y);
120120 const r = ldexp_cexp(v, -1);
121 return Complex(f64).init(r.re * math.copysign(f64, 1, x), r.im);
121 return Complex(f64).init(r.re * math.copysign(@as(f64, 1.0), x), r.im);
122122 }
123123 // x >= 1455: result always overflows
124124 else {
......@@ -128,14 +128,14 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
128128 }
129129
130130 if (ix | lx == 0 and iy >= 0x7ff00000) {
131 return Complex(f64).init(math.copysign(f64, 0, x * (y - y)), y - y);
131 return Complex(f64).init(math.copysign(@as(f64, 0.0), x * (y - y)), y - y);
132132 }
133133
134134 if (iy | ly == 0 and ix >= 0x7ff00000) {
135135 if ((hx & 0xfffff) | lx == 0) {
136136 return Complex(f64).init(x, y);
137137 }
138 return Complex(f64).init(x, math.copysign(f64, 0, y));
138 return Complex(f64).init(x, math.copysign(@as(f64, 0.0), y));
139139 }
140140
141141 if (ix < 0x7ff00000 and iy >= 0x7ff00000) {
lib/std/math/complex/sqrt.zig+6-6
......@@ -43,9 +43,9 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {
4343 // sqrt(-inf + i nan) = nan +- inf i
4444 // sqrt(-inf + iy) = 0 + inf i
4545 if (math.signbit(x)) {
46 return Complex(f32).init(@fabs(x - y), math.copysign(f32, x, y));
46 return Complex(f32).init(@fabs(x - y), math.copysign(x, y));
4747 } else {
48 return Complex(f32).init(x, math.copysign(f32, y - y, y));
48 return Complex(f32).init(x, math.copysign(y - y, y));
4949 }
5050 }
5151
......@@ -65,7 +65,7 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {
6565 const t = @sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5);
6666 return Complex(f32).init(
6767 @floatCast(f32, @fabs(y) / (2.0 * t)),
68 @floatCast(f32, math.copysign(f64, t, y)),
68 @floatCast(f32, math.copysign(t, y)),
6969 );
7070 }
7171}
......@@ -94,9 +94,9 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
9494 // sqrt(-inf + i nan) = nan +- inf i
9595 // sqrt(-inf + iy) = 0 + inf i
9696 if (math.signbit(x)) {
97 return Complex(f64).init(@fabs(x - y), math.copysign(f64, x, y));
97 return Complex(f64).init(@fabs(x - y), math.copysign(x, y));
9898 } else {
99 return Complex(f64).init(x, math.copysign(f64, y - y, y));
99 return Complex(f64).init(x, math.copysign(y - y, y));
100100 }
101101 }
102102
......@@ -116,7 +116,7 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
116116 result = Complex(f64).init(t, y / (2.0 * t));
117117 } else {
118118 const t = @sqrt((-x + math.hypot(f64, x, y)) * 0.5);
119 result = Complex(f64).init(@fabs(y) / (2.0 * t), math.copysign(f64, t, y));
119 result = Complex(f64).init(@fabs(y) / (2.0 * t), math.copysign(t, y));
120120 }
121121
122122 if (scale) {
lib/std/math/complex/tanh.zig+4-4
......@@ -34,7 +34,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) {
3434 }
3535 const xx = @bitCast(f32, hx - 0x40000000);
3636 const r = if (math.isInf(y)) y else @sin(y) * @cos(y);
37 return Complex(f32).init(xx, math.copysign(f32, 0, r));
37 return Complex(f32).init(xx, math.copysign(@as(f32, 0.0), r));
3838 }
3939
4040 if (!math.isFinite(y)) {
......@@ -45,7 +45,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) {
4545 // x >= 11
4646 if (ix >= 0x41300000) {
4747 const exp_mx = @exp(-@fabs(x));
48 return Complex(f32).init(math.copysign(f32, 1, x), 4 * @sin(y) * @cos(y) * exp_mx * exp_mx);
48 return Complex(f32).init(math.copysign(@as(f32, 1.0), x), 4 * @sin(y) * @cos(y) * exp_mx * exp_mx);
4949 }
5050
5151 // Kahan's algorithm
......@@ -77,7 +77,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
7777
7878 const xx = @bitCast(f64, (@as(u64, hx - 0x40000000) << 32) | lx);
7979 const r = if (math.isInf(y)) y else @sin(y) * @cos(y);
80 return Complex(f64).init(xx, math.copysign(f64, 0, r));
80 return Complex(f64).init(xx, math.copysign(@as(f64, 0.0), r));
8181 }
8282
8383 if (!math.isFinite(y)) {
......@@ -88,7 +88,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
8888 // x >= 22
8989 if (ix >= 0x40360000) {
9090 const exp_mx = @exp(-@fabs(x));
91 return Complex(f64).init(math.copysign(f64, 1, x), 4 * @sin(y) * @cos(y) * exp_mx * exp_mx);
91 return Complex(f64).init(math.copysign(@as(f64, 1.0), x), 4 * @sin(y) * @cos(y) * exp_mx * exp_mx);
9292 }
9393
9494 // Kahan's algorithm
lib/std/math/copysign.zig+17-84
......@@ -1,92 +1,25 @@
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/copysignf.c
5// https://git.musl-libc.org/cgit/musl/tree/src/math/copysign.c
6
71const std = @import("../std.zig");
82const math = std.math;
93const expect = std.testing.expect;
10const maxInt = std.math.maxInt;
11
12/// Returns a value with the magnitude of x and the sign of y.
13pub fn copysign(comptime T: type, x: T, y: T) T {
14 return switch (T) {
15 f16 => copysign16(x, y),
16 f32 => copysign32(x, y),
17 f64 => copysign64(x, y),
18 f128 => copysign128(x, y),
19 else => @compileError("copysign not implemented for " ++ @typeName(T)),
20 };
21}
224
23fn copysign16(x: f16, y: f16) f16 {
24 const ux = @bitCast(u16, x);
25 const uy = @bitCast(u16, y);
26
27 const h1 = ux & (maxInt(u16) / 2);
28 const h2 = uy & (@as(u16, 1) << 15);
29 return @bitCast(f16, h1 | h2);
30}
31
32fn copysign32(x: f32, y: f32) f32 {
33 const ux = @bitCast(u32, x);
34 const uy = @bitCast(u32, y);
35
36 const h1 = ux & (maxInt(u32) / 2);
37 const h2 = uy & (@as(u32, 1) << 31);
38 return @bitCast(f32, h1 | h2);
39}
40
41fn copysign64(x: f64, y: f64) f64 {
42 const ux = @bitCast(u64, x);
43 const uy = @bitCast(u64, y);
44
45 const h1 = ux & (maxInt(u64) / 2);
46 const h2 = uy & (@as(u64, 1) << 63);
47 return @bitCast(f64, h1 | h2);
48}
49
50fn copysign128(x: f128, y: f128) f128 {
51 const ux = @bitCast(u128, x);
52 const uy = @bitCast(u128, y);
53
54 const h1 = ux & (maxInt(u128) / 2);
55 const h2 = uy & (@as(u128, 1) << 127);
56 return @bitCast(f128, h1 | h2);
5/// Returns a value with the magnitude of `magnitude` and the sign of `sign`.
6pub fn copysign(magnitude: anytype, sign: @TypeOf(magnitude)) @TypeOf(magnitude) {
7 const T = @TypeOf(magnitude);
8 const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
9 const sign_bit_mask = @as(TBits, 1) << (@bitSizeOf(T) - 1);
10 const mag = @bitCast(TBits, magnitude) & ~sign_bit_mask;
11 const sgn = @bitCast(TBits, sign) & sign_bit_mask;
12 return @bitCast(T, mag | sgn);
5713}
5814
5915test "math.copysign" {
60 try expect(copysign(f16, 1.0, 1.0) == copysign16(1.0, 1.0));
61 try expect(copysign(f32, 1.0, 1.0) == copysign32(1.0, 1.0));
62 try expect(copysign(f64, 1.0, 1.0) == copysign64(1.0, 1.0));
63 try expect(copysign(f128, 1.0, 1.0) == copysign128(1.0, 1.0));
64}
65
66test "math.copysign16" {
67 try expect(copysign16(5.0, 1.0) == 5.0);
68 try expect(copysign16(5.0, -1.0) == -5.0);
69 try expect(copysign16(-5.0, -1.0) == -5.0);
70 try expect(copysign16(-5.0, 1.0) == 5.0);
71}
72
73test "math.copysign32" {
74 try expect(copysign32(5.0, 1.0) == 5.0);
75 try expect(copysign32(5.0, -1.0) == -5.0);
76 try expect(copysign32(-5.0, -1.0) == -5.0);
77 try expect(copysign32(-5.0, 1.0) == 5.0);
78}
79
80test "math.copysign64" {
81 try expect(copysign64(5.0, 1.0) == 5.0);
82 try expect(copysign64(5.0, -1.0) == -5.0);
83 try expect(copysign64(-5.0, -1.0) == -5.0);
84 try expect(copysign64(-5.0, 1.0) == 5.0);
85}
86
87test "math.copysign128" {
88 try expect(copysign128(5.0, 1.0) == 5.0);
89 try expect(copysign128(5.0, -1.0) == -5.0);
90 try expect(copysign128(-5.0, -1.0) == -5.0);
91 try expect(copysign128(-5.0, 1.0) == 5.0);
16 inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| {
17 try expect(copysign(@as(T, 1.0), @as(T, 1.0)) == 1.0);
18 try expect(copysign(@as(T, 2.0), @as(T, -2.0)) == -2.0);
19 try expect(copysign(@as(T, -3.0), @as(T, 3.0)) == 3.0);
20 try expect(copysign(@as(T, -4.0), @as(T, -4.0)) == -4.0);
21 try expect(copysign(@as(T, 5.0), @as(T, -500.0)) == -5.0);
22 try expect(copysign(math.inf(T), @as(T, -0.0)) == -math.inf(T));
23 try expect(copysign(@as(T, 6.0), -math.nan(T)) == -6.0);
24 }
9225}
lib/std/math/isfinite.zig+1-4
......@@ -5,10 +5,7 @@ const expect = std.testing.expect;
55/// Returns whether x is a finite value.
66pub fn isFinite(x: anytype) bool {
77 const T = @TypeOf(x);
8 const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));
9 if (@typeInfo(T) != .Float) {
10 @compileError("isFinite not implemented for " ++ @typeName(T));
11 }
8 const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
129 const remove_sign = ~@as(TBits, 0) >> 1;
1310 return @bitCast(TBits, x) & remove_sign < @bitCast(TBits, math.inf(T));
1411}
lib/std/math/isinf.zig+1-4
......@@ -5,10 +5,7 @@ const expect = std.testing.expect;
55/// Returns whether x is an infinity, ignoring sign.
66pub fn isInf(x: anytype) bool {
77 const T = @TypeOf(x);
8 const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));
9 if (@typeInfo(T) != .Float) {
10 @compileError("isInf not implemented for " ++ @typeName(T));
11 }
8 const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
129 const remove_sign = ~@as(TBits, 0) >> 1;
1310 return @bitCast(TBits, x) & remove_sign == @bitCast(TBits, math.inf(T));
1411}
lib/std/math/isnormal.zig+1-4
......@@ -5,10 +5,7 @@ const expect = std.testing.expect;
55/// Returns whether x is neither zero, subnormal, infinity, or NaN.
66pub fn isNormal(x: anytype) bool {
77 const T = @TypeOf(x);
8 const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));
9 if (@typeInfo(T) != .Float) {
10 @compileError("isNormal not implemented for " ++ @typeName(T));
11 }
8 const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
129
1310 const increment_exp = 1 << math.floatMantissaBits(T);
1411 const remove_sign = ~@as(TBits, 0) >> 1;
lib/std/math/ldexp.zig+1-4
......@@ -15,10 +15,7 @@ pub fn ldexp(x: anytype, n: i32) @TypeOf(x) {
1515 var shift = n;
1616
1717 const T = @TypeOf(base);
18 const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));
19 if (@typeInfo(T) != .Float) {
20 @compileError("ldexp not implemented for " ++ @typeName(T));
21 }
18 const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
2219
2320 const mantissa_bits = math.floatMantissaBits(T);
2421 const exponent_min = math.floatExponentMin(T);
lib/std/math/pow.zig+1-1
......@@ -60,7 +60,7 @@ pub fn pow(comptime T: type, x: T, y: T) T {
6060 if (y < 0) {
6161 // pow(+-0, y) = +- 0 for y an odd integer
6262 if (isOddInteger(y)) {
63 return math.copysign(T, math.inf(T), x);
63 return math.copysign(math.inf(T), x);
6464 }
6565 // pow(+-0, y) = +inf for y an even integer
6666 else {
lib/std/math/signbit.zig+12-57
......@@ -5,64 +5,19 @@ const expect = std.testing.expect;
55/// Returns whether x is negative or negative 0.
66pub fn signbit(x: anytype) bool {
77 const T = @TypeOf(x);
8 return switch (T) {
9 f16 => signbit16(x),
10 f32 => signbit32(x),
11 f64 => signbit64(x),
12 f80 => signbit80(x),
13 f128 => signbit128(x),
14 else => @compileError("signbit not implemented for " ++ @typeName(T)),
15 };
16}
17
18fn signbit16(x: f16) bool {
19 const bits = @bitCast(u16, x);
20 return bits >> 15 != 0;
21}
22
23fn signbit32(x: f32) bool {
24 const bits = @bitCast(u32, x);
25 return bits >> 31 != 0;
26}
27
28fn signbit64(x: f64) bool {
29 const bits = @bitCast(u64, x);
30 return bits >> 63 != 0;
31}
32
33fn signbit80(x: f80) bool {
34 const bits = @bitCast(u80, x);
35 return bits >> 79 != 0;
36}
37
38fn signbit128(x: f128) bool {
39 const bits = @bitCast(u128, x);
40 return bits >> 127 != 0;
8 const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
9 return @bitCast(TBits, x) >> (@bitSizeOf(T) - 1) != 0;
4110}
4211
4312test "math.signbit" {
44 try expect(signbit(@as(f16, 4.0)) == signbit16(4.0));
45 try expect(signbit(@as(f32, 4.0)) == signbit32(4.0));
46 try expect(signbit(@as(f64, 4.0)) == signbit64(4.0));
47 try expect(signbit(@as(f128, 4.0)) == signbit128(4.0));
48}
49
50test "math.signbit16" {
51 try expect(!signbit16(4.0));
52 try expect(signbit16(-3.0));
53}
54
55test "math.signbit32" {
56 try expect(!signbit32(4.0));
57 try expect(signbit32(-3.0));
58}
59
60test "math.signbit64" {
61 try expect(!signbit64(4.0));
62 try expect(signbit64(-3.0));
63}
64
65test "math.signbit128" {
66 try expect(!signbit128(4.0));
67 try expect(signbit128(-3.0));
13 inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| {
14 try expect(!signbit(@as(T, 0.0)));
15 try expect(!signbit(@as(T, 1.0)));
16 try expect(signbit(@as(T, -2.0)));
17 try expect(signbit(@as(T, -0.0)));
18 try expect(!signbit(math.inf(T)));
19 try expect(signbit(-math.inf(T)));
20 try expect(!signbit(math.nan(T)));
21 try expect(signbit(-math.nan(T)));
22 }
6823}