authorgravatar for hi@dnydxn.moealice <hi@dnydxn.moe> 2022-05-17 21:55:22+01:00
committergravatar for hi@dnydxn.moealice <hi@dnydxn.moe> 2022-05-17 21:55:22+01:00
log70b6b98e91fb9d1d5575da4d59c5c523864a8f0e
treea728394816c426f6e4b2c1af33fc4ee3e49add14
parentceeec8d19f36bf0a2cb132d35c0e119c4fec476d
signaturelock-open Commit is signed but in an unrecognized format.

Simplify `Copysign`


9 files changed, 51 insertions(+), 118 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/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 {