| 1 | // Ported from: |
| 2 | // |
| 3 | // https://github.com/llvm/llvm-project/commit/d674d96bc56c0f377879d01c9d8dfdaaa7859cdb/compiler-rt/test/builtins/Unit/divsf3_test.c |
| 4 | |
| 5 | const std = @import("std"); |
| 6 | const math = std.math; |
| 7 | const testing = std.testing; |
| 8 | |
| 9 | const div_f32 = @import("divsf3.zig").div_f32; |
| 10 | |
| 11 | const nanRep: u32 = @as(u32, @bitCast(math.nan(f32))); |
| 12 | const infRep: u32 = @as(u32, @bitCast(math.inf(f32))); |
| 13 | const negInfRep: u32 = @as(u32, @bitCast(-math.inf(f32))); |
| 14 | |
| 15 | fn compareResultF(result: f32, expected: u32) bool { |
| 16 | const rep: u32 = @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 & 0x7f800000) == 0x7f800000 and |
| 24 | (rep & 0x7fffff) > 0) |
| 25 | { |
| 26 | return true; |
| 27 | } |
| 28 | } |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | fn test__divsf3(a: f32, b: f32, expected: u32) !void { |
| 33 | const x = div_f32(a, b); |
| 34 | const ret = compareResultF(x, expected); |
| 35 | try testing.expect(ret == true); |
| 36 | } |
| 37 | |
| 38 | test "divsf3" { |
| 39 | try test__divsf3(1.0, 3.0, 0x3EAAAAAB); |
| 40 | try test__divsf3(2.3509887e-38, 2.0, 0x00800000); |
| 41 | try test__divsf3(1.0, 0x1.fffffep-1, 0x3f800001); |
| 42 | |
| 43 | try test__divsf3(math.nan(f32), 1.0, nanRep); |
| 44 | try test__divsf3(1.0, math.nan(f32), nanRep); |
| 45 | |
| 46 | try test__divsf3(math.inf(f32), 1.0, infRep); |
| 47 | try test__divsf3(-math.inf(f32), 1.0, negInfRep); |
| 48 | try test__divsf3(1.0, math.inf(f32), 0x00000000); |
| 49 | try test__divsf3(1.0, -math.inf(f32), 0x80000000); |
| 50 | |
| 51 | try test__divsf3(math.inf(f32), math.inf(f32), nanRep); |
| 52 | try test__divsf3(0.0, 0.0, nanRep); |
| 53 | try test__divsf3(-0.0, 0.0, nanRep); |
| 54 | |
| 55 | try test__divsf3(0.0, 1.0, 0x00000000); |
| 56 | try test__divsf3(-0.0, 1.0, 0x80000000); |
| 57 | try test__divsf3(1.0, 0.0, infRep); |
| 58 | try test__divsf3(1.0, -0.0, negInfRep); |
| 59 | |
| 60 | try test__divsf3(0x1p-126, 0x1p23, 0x00000001); |
| 61 | try test__divsf3(-0x1p-126, 0x1p23, 0x80000001); |
| 62 | try test__divsf3(0x1p-126, -0x1p23, 0x80000001); |
| 63 | |
| 64 | try test__divsf3(1.0, 0x1p127, 0x00400000); |
| 65 | try test__divsf3(-1.0, 0x1p127, 0x80400000); |
| 66 | } |