| 1 | // Ported from: |
| 2 | // |
| 3 | // https://github.com/llvm/llvm-project/blob/02d85149a05cb1f6dc49f0ba7a2ceca53718ae17/compiler-rt/test/builtins/Unit/addtf3_test.c |
| 4 | // https://github.com/llvm/llvm-project/blob/02d85149a05cb1f6dc49f0ba7a2ceca53718ae17/compiler-rt/test/builtins/Unit/subtf3_test.c |
| 5 | |
| 6 | const std = @import("std"); |
| 7 | const builtin = @import("builtin"); |
| 8 | const math = std.math; |
| 9 | const qnan128: f128 = @bitCast(@as(u128, 0x7fff800000000000) << 64); |
| 10 | |
| 11 | const impl = @import("addf3.zig"); |
| 12 | const add_f128 = impl.add_f128; |
| 13 | const add_f80 = impl.add_f80; |
| 14 | const sub_f128 = impl.sub_f128; |
| 15 | |
| 16 | fn test__addtf3(a: f128, b: f128, expected_hi: u64, expected_lo: u64) !void { |
| 17 | const x = add_f128(a, b); |
| 18 | |
| 19 | const rep: u128 = @bitCast(x); |
| 20 | const hi: u64 = @intCast(rep >> 64); |
| 21 | const lo: u64 = @truncate(rep); |
| 22 | |
| 23 | if (hi == expected_hi and lo == expected_lo) { |
| 24 | return; |
| 25 | } |
| 26 | // test other possible NaN representation (signal NaN) |
| 27 | else if (expected_hi == 0x7fff800000000000 and expected_lo == 0x0) { |
| 28 | if ((hi & 0x7fff000000000000) == 0x7fff000000000000 and |
| 29 | ((hi & 0xffffffffffff) > 0 or lo > 0)) |
| 30 | { |
| 31 | return; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | return error.TestFailed; |
| 36 | } |
| 37 | |
| 38 | test "addtf3" { |
| 39 | try test__addtf3(qnan128, 0x1.23456789abcdefp+5, 0x7fff800000000000, 0x0); |
| 40 | |
| 41 | // NaN + any = NaN |
| 42 | try test__addtf3(@as(f128, @bitCast((@as(u128, 0x7fff000000000000) << 64) | @as(u128, 0x800030000000))), 0x1.23456789abcdefp+5, 0x7fff800000000000, 0x0); |
| 43 | |
| 44 | // inf + inf = inf |
| 45 | try test__addtf3(math.inf(f128), math.inf(f128), 0x7fff000000000000, 0x0); |
| 46 | |
| 47 | // inf + any = inf |
| 48 | try test__addtf3(math.inf(f128), 0x1.2335653452436234723489432abcdefp+5, 0x7fff000000000000, 0x0); |
| 49 | |
| 50 | // any + any |
| 51 | try test__addtf3(0x1.23456734245345543849abcdefp+5, 0x1.edcba52449872455634654321fp-1, 0x40042afc95c8b579, 0x61e58dd6c51eb77c); |
| 52 | try test__addtf3(0x1.edcba52449872455634654321fp-1, 0x1.23456734245345543849abcdefp+5, 0x40042afc95c8b579, 0x61e58dd6c51eb77c); |
| 53 | } |
| 54 | |
| 55 | fn test__subtf3(a: f128, b: f128, expected_hi: u64, expected_lo: u64) !void { |
| 56 | const x = sub_f128(a, b); |
| 57 | |
| 58 | const rep: u128 = @bitCast(x); |
| 59 | const hi: u64 = @intCast(rep >> 64); |
| 60 | const lo: u64 = @truncate(rep); |
| 61 | |
| 62 | if (hi == expected_hi and lo == expected_lo) { |
| 63 | return; |
| 64 | } |
| 65 | // test other possible NaN representation (signal NaN) |
| 66 | else if (expected_hi == 0x7fff800000000000 and expected_lo == 0x0) { |
| 67 | if ((hi & 0x7fff000000000000) == 0x7fff000000000000 and |
| 68 | ((hi & 0xffffffffffff) > 0 or lo > 0)) |
| 69 | { |
| 70 | return; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return error.TestFailed; |
| 75 | } |
| 76 | |
| 77 | test "subtf3" { |
| 78 | // qNaN - any = qNaN |
| 79 | try test__subtf3(qnan128, 0x1.23456789abcdefp+5, 0x7fff800000000000, 0x0); |
| 80 | |
| 81 | // NaN + any = NaN |
| 82 | try test__subtf3(@as(f128, @bitCast((@as(u128, 0x7fff000000000000) << 64) | @as(u128, 0x800030000000))), 0x1.23456789abcdefp+5, 0x7fff800000000000, 0x0); |
| 83 | |
| 84 | // inf - any = inf |
| 85 | try test__subtf3(math.inf(f128), 0x1.23456789abcdefp+5, 0x7fff000000000000, 0x0); |
| 86 | |
| 87 | // any + any |
| 88 | try test__subtf3(0x1.234567829a3bcdef5678ade36734p+5, 0x1.ee9d7c52354a6936ab8d7654321fp-1, 0x40041b8af1915166, 0xa44a7bca780a166c); |
| 89 | try test__subtf3(0x1.ee9d7c52354a6936ab8d7654321fp-1, 0x1.234567829a3bcdef5678ade36734p+5, 0xc0041b8af1915166, 0xa44a7bca780a166c); |
| 90 | } |
| 91 | |
| 92 | const qnan80: f80 = @bitCast(@as(u80, @bitCast(math.nan(f80))) | (1 << (math.floatFractionalBits(f80) - 1))); |
| 93 | |
| 94 | fn test__addxf3(a: f80, b: f80, expected: u80) !void { |
| 95 | const x = add_f80(a, b); |
| 96 | const rep: u80 = @bitCast(x); |
| 97 | |
| 98 | if (rep == expected) |
| 99 | return; |
| 100 | |
| 101 | if (math.isNan(@as(f80, @bitCast(expected))) and math.isNan(x)) |
| 102 | return; // We don't currently test NaN payload propagation |
| 103 | |
| 104 | return error.TestFailed; |
| 105 | } |
| 106 | |
| 107 | test "addxf3" { |
| 108 | // NaN + any = NaN |
| 109 | try test__addxf3(qnan80, 0x1.23456789abcdefp+5, @as(u80, @bitCast(qnan80))); |
| 110 | try test__addxf3(@as(f80, @bitCast(@as(u80, 0x7fff_8000_8000_3000_0000))), 0x1.23456789abcdefp+5, @as(u80, @bitCast(qnan80))); |
| 111 | |
| 112 | // any + NaN = NaN |
| 113 | try test__addxf3(0x1.23456789abcdefp+5, qnan80, @as(u80, @bitCast(qnan80))); |
| 114 | try test__addxf3(0x1.23456789abcdefp+5, @as(f80, @bitCast(@as(u80, 0x7fff_8000_8000_3000_0000))), @as(u80, @bitCast(qnan80))); |
| 115 | |
| 116 | // NaN + inf = NaN |
| 117 | try test__addxf3(qnan80, math.inf(f80), @as(u80, @bitCast(qnan80))); |
| 118 | |
| 119 | // inf + NaN = NaN |
| 120 | try test__addxf3(math.inf(f80), qnan80, @as(u80, @bitCast(qnan80))); |
| 121 | |
| 122 | // inf + inf = inf |
| 123 | try test__addxf3(math.inf(f80), math.inf(f80), @as(u80, @bitCast(math.inf(f80)))); |
| 124 | |
| 125 | // inf + -inf = NaN |
| 126 | try test__addxf3(math.inf(f80), -math.inf(f80), @as(u80, @bitCast(qnan80))); |
| 127 | |
| 128 | // -inf + inf = NaN |
| 129 | try test__addxf3(-math.inf(f80), math.inf(f80), @as(u80, @bitCast(qnan80))); |
| 130 | |
| 131 | // inf + any = inf |
| 132 | try test__addxf3(math.inf(f80), 0x1.2335653452436234723489432abcdefp+5, @as(u80, @bitCast(math.inf(f80)))); |
| 133 | |
| 134 | // any + inf = inf |
| 135 | try test__addxf3(0x1.2335653452436234723489432abcdefp+5, math.inf(f80), @as(u80, @bitCast(math.inf(f80)))); |
| 136 | |
| 137 | // any + any |
| 138 | try test__addxf3(0x1.23456789abcdp+5, 0x1.dcba987654321p+5, 0x4005_BFFFFFFFFFFFC400); |
| 139 | try test__addxf3(0x1.23456734245345543849abcdefp+5, 0x1.edcba52449872455634654321fp-1, 0x4004_957E_4AE4_5ABC_B0F3); |
| 140 | try test__addxf3(0x1.ffff_ffff_ffff_fffcp+0, 0x1.0p-63, 0x3FFF_FFFFFFFFFFFFFFFF); // exact |
| 141 | try test__addxf3(0x1.ffff_ffff_ffff_fffep+0, 0x0.0p0, 0x3FFF_FFFFFFFFFFFFFFFF); // exact |
| 142 | try test__addxf3(0x1.ffff_ffff_ffff_fffcp+0, 0x1.4p-63, 0x3FFF_FFFFFFFFFFFFFFFF); // round down |
| 143 | try test__addxf3(0x1.ffff_ffff_ffff_fffcp+0, 0x1.8p-63, 0x4000_8000000000000000); // round up to even |
| 144 | try test__addxf3(0x1.ffff_ffff_ffff_fffcp+0, 0x1.cp-63, 0x4000_8000000000000000); // round up |
| 145 | try test__addxf3(0x1.ffff_ffff_ffff_fffcp+0, 0x2.0p-63, 0x4000_8000000000000000); // exact |
| 146 | try test__addxf3(0x1.ffff_ffff_ffff_fffcp+0, 0x2.1p-63, 0x4000_8000000000000000); // round down |
| 147 | try test__addxf3(0x1.ffff_ffff_ffff_fffcp+0, 0x3.0p-63, 0x4000_8000000000000000); // round down to even |
| 148 | try test__addxf3(0x1.ffff_ffff_ffff_fffcp+0, 0x3.1p-63, 0x4000_8000000000000001); // round up |
| 149 | try test__addxf3(0x1.ffff_ffff_ffff_fffcp+0, 0x4.0p-63, 0x4000_8000000000000001); // exact |
| 150 | |
| 151 | try test__addxf3(0x1.0fff_ffff_ffff_fffep+0, 0x1.0p-63, 0x3FFF_8800000000000000); // exact |
| 152 | try test__addxf3(0x1.0fff_ffff_ffff_fffep+0, 0x1.7p-63, 0x3FFF_8800000000000000); // round down |
| 153 | try test__addxf3(0x1.0fff_ffff_ffff_fffep+0, 0x1.8p-63, 0x3FFF_8800000000000000); // round down to even |
| 154 | try test__addxf3(0x1.0fff_ffff_ffff_fffep+0, 0x1.9p-63, 0x3FFF_8800000000000001); // round up |
| 155 | try test__addxf3(0x1.0fff_ffff_ffff_fffep+0, 0x2.0p-63, 0x3FFF_8800000000000001); // exact |
| 156 | try test__addxf3(0x0.ffff_ffff_ffff_fffcp-16382, 0x0.0000_0000_0000_0002p-16382, 0x0000_7FFFFFFFFFFFFFFF); // exact |
| 157 | try test__addxf3(0x0.1fff_ffff_ffff_fffcp-16382, 0x0.0000_0000_0000_0002p-16382, 0x0000_0FFFFFFFFFFFFFFF); // exact |
| 158 | } |