authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-04-29 09:00:29+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-05-01 16:28:08+02:00
log236dba8ba926c3214e759df23d20f2f5880b6235
tree880b7dd6c8dd9832df784e6636089a25610067fd
parent9f6edfd2016177883c952cd72bbcfab87025703c

std: Implement copysign for f128


1 files changed, 18 insertions(+), 0 deletions(-)

lib/std/math/copysign.zig+18
......@@ -20,6 +20,7 @@ pub fn copysign(comptime T: type, x: T, y: T) T {
2020 f16 => copysign16(x, y),
2121 f32 => copysign32(x, y),
2222 f64 => copysign64(x, y),
23 f128 => copysign128(x, y),
2324 else => @compileError("copysign not implemented for " ++ @typeName(T)),
2425 };
2526}
......@@ -51,10 +52,20 @@ fn copysign64(x: f64, y: f64) f64 {
5152 return @bitCast(f64, h1 | h2);
5253}
5354
55fn copysign128(x: f128, y: f128) f128 {
56 const ux = @bitCast(u128, x);
57 const uy = @bitCast(u128, y);
58
59 const h1 = ux & (maxInt(u128) / 2);
60 const h2 = uy & (@as(u128, 1) << 127);
61 return @bitCast(f128, h1 | h2);
62}
63
5464test "math.copysign" {
5565 expect(copysign(f16, 1.0, 1.0) == copysign16(1.0, 1.0));
5666 expect(copysign(f32, 1.0, 1.0) == copysign32(1.0, 1.0));
5767 expect(copysign(f64, 1.0, 1.0) == copysign64(1.0, 1.0));
68 expect(copysign(f128, 1.0, 1.0) == copysign128(1.0, 1.0));
5869}
5970
6071test "math.copysign16" {
......@@ -77,3 +88,10 @@ test "math.copysign64" {
7788 expect(copysign64(-5.0, -1.0) == -5.0);
7889 expect(copysign64(-5.0, 1.0) == 5.0);
7990}
91
92test "math.copysign128" {
93 expect(copysign128(5.0, 1.0) == 5.0);
94 expect(copysign128(5.0, -1.0) == -5.0);
95 expect(copysign128(-5.0, -1.0) == -5.0);
96 expect(copysign128(-5.0, 1.0) == 5.0);
97}