| 1 | // Ported from: |
| 2 | // |
| 3 | // https://github.com/llvm/llvm-project/commit/d674d96bc56c0f377879d01c9d8dfdaaa7859cdb/compiler-rt/test/builtins/Unit/divdf3_test.c |
| 4 | |
| 5 | const std = @import("std"); |
| 6 | const math = std.math; |
| 7 | const testing = std.testing; |
| 8 | |
| 9 | const div_f64 = @import("divdf3.zig").div_f64; |
| 10 | |
| 11 | const nanRep: u64 = @as(u64, @bitCast(math.nan(f64))); |
| 12 | const infRep: u64 = @as(u64, @bitCast(math.inf(f64))); |
| 13 | const negInfRep: u64 = @as(u64, @bitCast(-math.inf(f64))); |
| 14 | |
| 15 | fn compareResultD(result: f64, expected: u64) bool { |
| 16 | const rep: u64 = @bitCast(result); |
| 17 | |
| 18 | if (rep == expected) { |
| 19 | return true; |
| 20 | } |
| 21 | // test other possible NaN representation(signal NaN) |
| 22 | else if (expected == nanRep) { |
| 23 | if ((rep & 0x7ff0000000000000) == 0x7ff0000000000000 and |
| 24 | (rep & 0xfffffffffffff) > 0) |
| 25 | { |
| 26 | return true; |
| 27 | } |
| 28 | } |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | fn test__divdf3(a: f64, b: f64, expected: u64) !void { |
| 33 | const x = div_f64(a, b); |
| 34 | const ret = compareResultD(x, expected); |
| 35 | try testing.expect(ret == true); |
| 36 | } |
| 37 | |
| 38 | test "divdf3" { |
| 39 | try test__divdf3(1.0, 3.0, 0x3fd5555555555555); |
| 40 | try test__divdf3(4.450147717014403e-308, 2.0, 0x10000000000000); |
| 41 | try test__divdf3(1.0, 0x1.fffffffffffffp-1, 0x3ff0000000000001); |
| 42 | |
| 43 | try test__divdf3(math.nan(f64), 1.0, nanRep); |
| 44 | try test__divdf3(1.0, math.nan(f64), nanRep); |
| 45 | |
| 46 | try test__divdf3(math.inf(f64), 1.0, infRep); |
| 47 | try test__divdf3(-math.inf(f64), 1.0, negInfRep); |
| 48 | try test__divdf3(1.0, math.inf(f64), 0x0000000000000000); |
| 49 | try test__divdf3(1.0, -math.inf(f64), 0x8000000000000000); |
| 50 | |
| 51 | try test__divdf3(math.inf(f64), math.inf(f64), nanRep); |
| 52 | try test__divdf3(0.0, 0.0, nanRep); |
| 53 | try test__divdf3(-0.0, 0.0, nanRep); |
| 54 | |
| 55 | try test__divdf3(0.0, 1.0, 0x0000000000000000); |
| 56 | try test__divdf3(-0.0, 1.0, 0x8000000000000000); |
| 57 | try test__divdf3(1.0, 0.0, infRep); |
| 58 | try test__divdf3(1.0, -0.0, negInfRep); |
| 59 | |
| 60 | try test__divdf3(0x1p-1022, 0x1p52, 0x0000000000000001); |
| 61 | try test__divdf3(-0x1p-1022, 0x1p52, 0x8000000000000001); |
| 62 | try test__divdf3(0x1p-1022, -0x1p52, 0x8000000000000001); |
| 63 | |
| 64 | try test__divdf3(1.0, 0x1p1023, 0x0008000000000000); |
| 65 | try test__divdf3(-1.0, 0x1p1023, 0x8008000000000000); |
| 66 | } |