extendf_test.zig (8104B) - Raw
1 const std = @import("std"); 2 const math = std.math; 3 const builtin = @import("builtin"); 4 const __extendhfsf2 = @import("extendhfsf2.zig").__extendhfsf2; 5 const __extendhftf2 = @import("extendhftf2.zig").__extendhftf2; 6 const __extendsftf2 = @import("extendsftf2.zig").__extendsftf2; 7 const __extenddftf2 = @import("extenddftf2.zig").__extenddftf2; 8 const __extenddfxf2 = @import("extenddfxf2.zig").__extenddfxf2; 9 const F16T = @import("./common.zig").F16T; 10 11 fn test__extenddfxf2(a: f64, expected: u80) !void { 12 const x = __extenddfxf2(a); 13 14 const rep: u80 = @bitCast(x); 15 if (rep == expected) 16 return; 17 18 // test other possible NaN representation(signal NaN) 19 if (math.isNan(@as(f80, @bitCast(expected))) and math.isNan(x)) 20 return; 21 22 @panic("__extenddfxf2 test failure"); 23 } 24 25 fn test__extenddftf2(a: f64, expected_hi: u64, expected_lo: u64) !void { 26 const x = __extenddftf2(a); 27 28 const rep: u128 = @bitCast(x); 29 const hi: u64 = @intCast(rep >> 64); 30 const lo: u64 = @truncate(rep); 31 32 if (hi == expected_hi and lo == expected_lo) 33 return; 34 35 // test other possible NaN representation(signal NaN) 36 if (expected_hi == 0x7fff800000000000 and expected_lo == 0x0) { 37 if ((hi & 0x7fff000000000000) == 0x7fff000000000000 and 38 ((hi & 0xffffffffffff) > 0 or lo > 0)) 39 { 40 return; 41 } 42 } 43 44 @panic("__extenddftf2 test failure"); 45 } 46 47 fn test__extendhfsf2(a: u16, expected: u32) !void { 48 const x = __extendhfsf2(@as(F16T(f32), @bitCast(a))); 49 const rep: u32 = @bitCast(x); 50 51 if (rep == expected) { 52 if (rep & 0x7fffffff > 0x7f800000) { 53 return; // NaN is always unequal. 54 } 55 if (x == @as(f32, @bitCast(expected))) { 56 return; 57 } 58 } 59 60 return error.TestFailure; 61 } 62 63 fn test__extendsftf2(a: f32, expected_hi: u64, expected_lo: u64) !void { 64 const x = __extendsftf2(a); 65 66 const rep: u128 = @bitCast(x); 67 const hi: u64 = @intCast(rep >> 64); 68 const lo: u64 = @truncate(rep); 69 70 if (hi == expected_hi and lo == expected_lo) 71 return; 72 73 // test other possible NaN representation(signal NaN) 74 if (expected_hi == 0x7fff800000000000 and expected_lo == 0x0) { 75 if ((hi & 0x7fff000000000000) == 0x7fff000000000000 and 76 ((hi & 0xffffffffffff) > 0 or lo > 0)) 77 { 78 return; 79 } 80 } 81 82 return error.TestFailure; 83 } 84 85 test "extenddfxf2" { 86 // qNaN 87 try test__extenddfxf2(makeQNaN64(), 0x7fffc000000000000000); 88 89 // NaN 90 try test__extenddfxf2(makeNaN64(0x7100000000000), 0x7fffe080000000000000); 91 // This is bad? 92 93 // inf 94 try test__extenddfxf2(makeInf64(), 0x7fff8000000000000000); 95 96 // zero 97 try test__extenddfxf2(0.0, 0x0); 98 99 try test__extenddfxf2(0x0.a3456789abcdefp+6, 0x4004a3456789abcdf000); 100 101 try test__extenddfxf2(0x0.edcba987654321fp-8, 0x3ff6edcba98765432000); 102 103 try test__extenddfxf2(0x0.a3456789abcdefp+46, 0x402ca3456789abcdf000); 104 105 try test__extenddfxf2(0x0.edcba987654321fp-44, 0x3fd2edcba98765432000); 106 107 // subnormal 108 try test__extenddfxf2(0x1.8000000000001p-1022, 0x3c01c000000000000800); 109 try test__extenddfxf2(0x1.8000000000002p-1023, 0x3c00c000000000001000); 110 } 111 112 test "extenddftf2" { 113 // qNaN 114 try test__extenddftf2(makeQNaN64(), 0x7fff800000000000, 0x0); 115 116 // NaN 117 try test__extenddftf2(makeNaN64(0x7100000000000), 0x7fff710000000000, 0x0); 118 119 // inf 120 try test__extenddftf2(makeInf64(), 0x7fff000000000000, 0x0); 121 122 // zero 123 try test__extenddftf2(0.0, 0x0, 0x0); 124 125 try test__extenddftf2(0x1.23456789abcdefp+5, 0x400423456789abcd, 0xf000000000000000); 126 127 try test__extenddftf2(0x1.edcba987654321fp-9, 0x3ff6edcba9876543, 0x2000000000000000); 128 129 try test__extenddftf2(0x1.23456789abcdefp+45, 0x402c23456789abcd, 0xf000000000000000); 130 131 try test__extenddftf2(0x1.edcba987654321fp-45, 0x3fd2edcba9876543, 0x2000000000000000); 132 133 // subnormal 134 try test__extenddftf2(0x1.8p-1022, 0x3c01800000000000, 0x0); 135 try test__extenddftf2(0x1.8p-1023, 0x3c00800000000000, 0x0); 136 } 137 138 test "extendhfsf2" { 139 try test__extendhfsf2(0x7e00, 0x7fc00000); // qNaN 140 try test__extendhfsf2(0x7f00, 0x7fe00000); // sNaN 141 // On x86 the NaN becomes quiet because the return is pushed on the x87 142 // stack due to ABI requirements 143 if (builtin.target.cpu.arch != .x86 and builtin.target.os.tag == .windows) 144 try test__extendhfsf2(0x7c01, 0x7f802000); // sNaN 145 146 try test__extendhfsf2(0, 0); // 0 147 try test__extendhfsf2(0x8000, 0x80000000); // -0 148 149 try test__extendhfsf2(0x7c00, 0x7f800000); // inf 150 try test__extendhfsf2(0xfc00, 0xff800000); // -inf 151 152 try test__extendhfsf2(0x0001, 0x33800000); // denormal (min), 2**-24 153 try test__extendhfsf2(0x8001, 0xb3800000); // denormal (min), -2**-24 154 155 try test__extendhfsf2(0x03ff, 0x387fc000); // denormal (max), 2**-14 - 2**-24 156 try test__extendhfsf2(0x83ff, 0xb87fc000); // denormal (max), -2**-14 + 2**-24 157 158 try test__extendhfsf2(0x0400, 0x38800000); // normal (min), 2**-14 159 try test__extendhfsf2(0x8400, 0xb8800000); // normal (min), -2**-14 160 161 try test__extendhfsf2(0x7bff, 0x477fe000); // normal (max), 65504 162 try test__extendhfsf2(0xfbff, 0xc77fe000); // normal (max), -65504 163 164 try test__extendhfsf2(0x3c01, 0x3f802000); // normal, 1 + 2**-10 165 try test__extendhfsf2(0xbc01, 0xbf802000); // normal, -1 - 2**-10 166 167 try test__extendhfsf2(0x3555, 0x3eaaa000); // normal, approx. 1/3 168 try test__extendhfsf2(0xb555, 0xbeaaa000); // normal, approx. -1/3 169 } 170 171 test "extendsftf2" { 172 // qNaN 173 try test__extendsftf2(makeQNaN32(), 0x7fff800000000000, 0x0); 174 // NaN 175 try test__extendsftf2(makeNaN32(0x410000), 0x7fff820000000000, 0x0); 176 // inf 177 try test__extendsftf2(makeInf32(), 0x7fff000000000000, 0x0); 178 // zero 179 try test__extendsftf2(0.0, 0x0, 0x0); 180 try test__extendsftf2(0x1.23456p+5, 0x4004234560000000, 0x0); 181 try test__extendsftf2(0x1.edcbap-9, 0x3ff6edcba0000000, 0x0); 182 try test__extendsftf2(0x1.23456p+45, 0x402c234560000000, 0x0); 183 try test__extendsftf2(0x1.edcbap-45, 0x3fd2edcba0000000, 0x0); 184 } 185 186 fn makeQNaN64() f64 { 187 return @bitCast(@as(u64, 0x7ff8000000000000)); 188 } 189 190 fn makeInf64() f64 { 191 return @bitCast(@as(u64, 0x7ff0000000000000)); 192 } 193 194 fn makeNaN64(rand: u64) f64 { 195 return @bitCast(0x7ff0000000000000 | (rand & 0xfffffffffffff)); 196 } 197 198 fn makeQNaN32() f32 { 199 return @bitCast(@as(u32, 0x7fc00000)); 200 } 201 202 fn makeNaN32(rand: u32) f32 { 203 return @bitCast(0x7f800000 | (rand & 0x7fffff)); 204 } 205 206 fn makeInf32() f32 { 207 return @bitCast(@as(u32, 0x7f800000)); 208 } 209 210 fn test__extendhftf2(a: u16, expected_hi: u64, expected_lo: u64) !void { 211 const x = __extendhftf2(@as(F16T(f128), @bitCast(a))); 212 213 const rep: u128 = @bitCast(x); 214 const hi: u64 = @intCast(rep >> 64); 215 const lo: u64 = @truncate(rep); 216 217 if (hi == expected_hi and lo == expected_lo) 218 return; 219 220 // test other possible NaN representation(signal NaN) 221 if (expected_hi == 0x7fff800000000000 and expected_lo == 0x0) { 222 if ((hi & 0x7fff000000000000) == 0x7fff000000000000 and 223 ((hi & 0xffffffffffff) > 0 or lo > 0)) 224 { 225 return; 226 } 227 } 228 229 return error.TestFailure; 230 } 231 232 test "extendhftf2" { 233 // qNaN 234 try test__extendhftf2(0x7e00, 0x7fff800000000000, 0x0); 235 // NaN 236 try test__extendhftf2(0x7d00, 0x7fff400000000000, 0x0); 237 // inf 238 try test__extendhftf2(0x7c00, 0x7fff000000000000, 0x0); 239 try test__extendhftf2(0xfc00, 0xffff000000000000, 0x0); 240 // zero 241 try test__extendhftf2(0x0000, 0x0000000000000000, 0x0); 242 try test__extendhftf2(0x8000, 0x8000000000000000, 0x0); 243 // denormal 244 try test__extendhftf2(0x0010, 0x3feb000000000000, 0x0); 245 try test__extendhftf2(0x0001, 0x3fe7000000000000, 0x0); 246 try test__extendhftf2(0x8001, 0xbfe7000000000000, 0x0); 247 248 // pi 249 try test__extendhftf2(0x4248, 0x4000920000000000, 0x0); 250 try test__extendhftf2(0xc248, 0xc000920000000000, 0x0); 251 252 try test__extendhftf2(0x508c, 0x4004230000000000, 0x0); 253 try test__extendhftf2(0x1bb7, 0x3ff6edc000000000, 0x0); 254 }