authorgravatar for info@bnoordhuis.nlBen Noordhuis <info@bnoordhuis.nl> 2018-06-30 01:44:54+02:00
committergravatar for info@bnoordhuis.nlBen Noordhuis <info@bnoordhuis.nl> 2018-06-30 01:58:17+02:00
logca444e6191138e6f9977cf2fbceb071134ed6aff
tree8e77c7cdd549ab72d8cd3fcd876552b684a10446
parentd293f1a0edf0e2e42f03ad416c7f5d649776ae88

add std.math f16 copysign support

refs #1122

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

std/math/copysign.zig+18
......@@ -4,12 +4,22 @@ const assert = std.debug.assert;
44
55pub fn copysign(comptime T: type, x: T, y: T) T {
66 return switch (T) {
7 f16 => copysign16(x, y),
78 f32 => copysign32(x, y),
89 f64 => copysign64(x, y),
910 else => @compileError("copysign not implemented for " ++ @typeName(T)),
1011 };
1112}
1213
14fn copysign16(x: f16, y: f16) f16 {
15 const ux = @bitCast(u16, x);
16 const uy = @bitCast(u16, y);
17
18 const h1 = ux & (@maxValue(u16) / 2);
19 const h2 = uy & (u16(1) << 15);
20 return @bitCast(f16, h1 | h2);
21}
22
1323fn copysign32(x: f32, y: f32) f32 {
1424 const ux = @bitCast(u32, x);
1525 const uy = @bitCast(u32, y);
......@@ -29,10 +39,18 @@ fn copysign64(x: f64, y: f64) f64 {
2939}
3040
3141test "math.copysign" {
42 assert(copysign(f16, 1.0, 1.0) == copysign16(1.0, 1.0));
3243 assert(copysign(f32, 1.0, 1.0) == copysign32(1.0, 1.0));
3344 assert(copysign(f64, 1.0, 1.0) == copysign64(1.0, 1.0));
3445}
3546
47test "math.copysign16" {
48 assert(copysign16(5.0, 1.0) == 5.0);
49 assert(copysign16(5.0, -1.0) == -5.0);
50 assert(copysign16(-5.0, -1.0) == -5.0);
51 assert(copysign16(-5.0, 1.0) == 5.0);
52}
53
3654test "math.copysign32" {
3755 assert(copysign32(5.0, 1.0) == 5.0);
3856 assert(copysign32(5.0, -1.0) == -5.0);