From 236dba8ba926c3214e759df23d20f2f5880b6235 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Thu, 29 Apr 2021 09:00:29 +0200 Subject: [PATCH] std: Implement copysign for f128 --- lib/std/math/copysign.zig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/std/math/copysign.zig b/lib/std/math/copysign.zig index 2804d10495..7861cda8eb 100644 --- a/lib/std/math/copysign.zig +++ b/lib/std/math/copysign.zig @@ -20,6 +20,7 @@ pub fn copysign(comptime T: type, x: T, y: T) T { f16 => copysign16(x, y), f32 => copysign32(x, y), f64 => copysign64(x, y), + f128 => copysign128(x, y), else => @compileError("copysign not implemented for " ++ @typeName(T)), }; } @@ -51,10 +52,20 @@ fn copysign64(x: f64, y: f64) f64 { return @bitCast(f64, h1 | h2); } +fn copysign128(x: f128, y: f128) f128 { + const ux = @bitCast(u128, x); + const uy = @bitCast(u128, y); + + const h1 = ux & (maxInt(u128) / 2); + const h2 = uy & (@as(u128, 1) << 127); + return @bitCast(f128, h1 | h2); +} + test "math.copysign" { expect(copysign(f16, 1.0, 1.0) == copysign16(1.0, 1.0)); expect(copysign(f32, 1.0, 1.0) == copysign32(1.0, 1.0)); expect(copysign(f64, 1.0, 1.0) == copysign64(1.0, 1.0)); + expect(copysign(f128, 1.0, 1.0) == copysign128(1.0, 1.0)); } test "math.copysign16" { @@ -77,3 +88,10 @@ test "math.copysign64" { expect(copysign64(-5.0, -1.0) == -5.0); expect(copysign64(-5.0, 1.0) == 5.0); } + +test "math.copysign128" { + expect(copysign128(5.0, 1.0) == 5.0); + expect(copysign128(5.0, -1.0) == -5.0); + expect(copysign128(-5.0, -1.0) == -5.0); + expect(copysign128(-5.0, 1.0) == 5.0); +}