| ... | ... | @@ -20,6 +20,7 @@ pub fn copysign(comptime T: type, x: T, y: T) T { |
| 20 | 20 | f16 => copysign16(x, y), |
| 21 | 21 | f32 => copysign32(x, y), |
| 22 | 22 | f64 => copysign64(x, y), |
| 23 | f128 => copysign128(x, y), |
| 23 | 24 | else => @compileError("copysign not implemented for " ++ @typeName(T)), |
| 24 | 25 | }; |
| 25 | 26 | } |
| ... | ... | @@ -51,10 +52,20 @@ fn copysign64(x: f64, y: f64) f64 { |
| 51 | 52 | return @bitCast(f64, h1 | h2); |
| 52 | 53 | } |
| 53 | 54 | |
| 55 | fn 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 | |
| 54 | 64 | test "math.copysign" { |
| 55 | 65 | expect(copysign(f16, 1.0, 1.0) == copysign16(1.0, 1.0)); |
| 56 | 66 | expect(copysign(f32, 1.0, 1.0) == copysign32(1.0, 1.0)); |
| 57 | 67 | 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)); |
| 58 | 69 | } |
| 59 | 70 | |
| 60 | 71 | test "math.copysign16" { |
| ... | ... | @@ -77,3 +88,10 @@ test "math.copysign64" { |
| 77 | 88 | expect(copysign64(-5.0, -1.0) == -5.0); |
| 78 | 89 | expect(copysign64(-5.0, 1.0) == 5.0); |
| 79 | 90 | } |
| 91 | |
| 92 | test "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 | } |