| author | |
| committer | |
| log | be861a85c8a1adc1f82c523136e6d6b18992c372 |
| tree | 7d1e5bb947d059b31995649174d7f76483f46fd8 |
| parent | 8e68d43ad373e643797209d59e6f10aa12b4c038 |
Allows addition/subtraction of f128 and narrowing casts to f16 from
larger float types.6 files changed, 353 insertions(+), 0 deletions(-)
CMakeLists.txt+1| ... | ... | @@ -608,6 +608,7 @@ set(ZIG_STD_FILES |
| 608 | 608 | "special/bootstrap_lib.zig" |
| 609 | 609 | "special/build_runner.zig" |
| 610 | 610 | "special/builtin.zig" |
| 611 | "special/compiler_rt/addXf3.zig" | |
| 611 | 612 | "special/compiler_rt/aulldiv.zig" |
| 612 | 613 | "special/compiler_rt/aullrem.zig" |
| 613 | 614 | "special/compiler_rt/comparetf2.zig" |
std/special/compiler_rt/addXf3.zig created+191| ... | ... | @@ -0,0 +1,191 @@ |
| 1 | // Ported from: | |
| 2 | // | |
| 3 | // https://github.com/llvm-mirror/compiler-rt/blob/92f7768ce940f6437b32ecc0985a1446cd040f7a/lib/builtins/fp_add_impl.inc | |
| 4 | ||
| 5 | const std = @import("std"); | |
| 6 | const builtin = @import("builtin"); | |
| 7 | const compiler_rt = @import("index.zig"); | |
| 8 | ||
| 9 | pub extern fn __addtf3(a: f128, b: f128) f128 { | |
| 10 | return addXf3(f128, a, b); | |
| 11 | } | |
| 12 | ||
| 13 | pub extern fn __subtf3(a: f128, b: f128) f128 { | |
| 14 | const neg_b = @bitCast(f128, @bitCast(u128, b) ^ (u128(1) << 127)); | |
| 15 | return addXf3(f128, a, neg_b); | |
| 16 | } | |
| 17 | ||
| 18 | inline fn normalize(comptime T: type, significand: *@IntType(false, T.bit_count)) i32 { | |
| 19 | const Z = @IntType(false, T.bit_count); | |
| 20 | const significandBits = std.math.floatMantissaBits(T); | |
| 21 | const implicitBit = Z(1) << significandBits; | |
| 22 | ||
| 23 | const shift = @clz(significand.*) - @clz(implicitBit); | |
| 24 | significand.* <<= @intCast(u7, shift); | |
| 25 | return 1 - shift; | |
| 26 | } | |
| 27 | ||
| 28 | inline fn addXf3(comptime T: type, a: T, b: T) T { | |
| 29 | const Z = @IntType(false, T.bit_count); | |
| 30 | ||
| 31 | const typeWidth = T.bit_count; | |
| 32 | const significandBits = std.math.floatMantissaBits(T); | |
| 33 | const exponentBits = std.math.floatExponentBits(T); | |
| 34 | ||
| 35 | const signBit = (Z(1) << (significandBits + exponentBits)); | |
| 36 | const maxExponent = ((1 << exponentBits) - 1); | |
| 37 | const exponentBias = (maxExponent >> 1); | |
| 38 | ||
| 39 | const implicitBit = (Z(1) << significandBits); | |
| 40 | const quietBit = implicitBit >> 1; | |
| 41 | const significandMask = implicitBit - 1; | |
| 42 | ||
| 43 | const absMask = signBit - 1; | |
| 44 | const exponentMask = absMask ^ significandMask; | |
| 45 | const qnanRep = exponentMask | quietBit; | |
| 46 | ||
| 47 | var aRep = @bitCast(Z, a); | |
| 48 | var bRep = @bitCast(Z, b); | |
| 49 | const aAbs = aRep & absMask; | |
| 50 | const bAbs = bRep & absMask; | |
| 51 | ||
| 52 | const negative = (aRep & signBit) != 0; | |
| 53 | const exponent = @intCast(i32, aAbs >> significandBits) - exponentBias; | |
| 54 | const significand = (aAbs & significandMask) | implicitBit; | |
| 55 | ||
| 56 | const infRep = @bitCast(Z, std.math.inf(T)); | |
| 57 | ||
| 58 | // Detect if a or b is zero, infinity, or NaN. | |
| 59 | if (aAbs - Z(1) >= infRep - Z(1) or | |
| 60 | bAbs - Z(1) >= infRep - Z(1)) | |
| 61 | { | |
| 62 | // NaN + anything = qNaN | |
| 63 | if (aAbs > infRep) return @bitCast(T, @bitCast(Z, a) | quietBit); | |
| 64 | // anything + NaN = qNaN | |
| 65 | if (bAbs > infRep) return @bitCast(T, @bitCast(Z, b) | quietBit); | |
| 66 | ||
| 67 | if (aAbs == infRep) { | |
| 68 | // +/-infinity + -/+infinity = qNaN | |
| 69 | if ((@bitCast(Z, a) ^ @bitCast(Z, b)) == signBit) { | |
| 70 | return @bitCast(T, qnanRep); | |
| 71 | } | |
| 72 | // +/-infinity + anything remaining = +/- infinity | |
| 73 | else { | |
| 74 | return a; | |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | // anything remaining + +/-infinity = +/-infinity | |
| 79 | if (bAbs == infRep) return b; | |
| 80 | ||
| 81 | // zero + anything = anything | |
| 82 | if (aAbs == 0) { | |
| 83 | // but we need to get the sign right for zero + zero | |
| 84 | if (bAbs == 0) { | |
| 85 | return @bitCast(T, @bitCast(Z, a) & @bitCast(Z, b)); | |
| 86 | } else { | |
| 87 | return b; | |
| 88 | } | |
| 89 | } | |
| 90 | ||
| 91 | // anything + zero = anything | |
| 92 | if (bAbs == 0) return a; | |
| 93 | } | |
| 94 | ||
| 95 | // Swap a and b if necessary so that a has the larger absolute value. | |
| 96 | if (bAbs > aAbs) { | |
| 97 | const temp = aRep; | |
| 98 | aRep = bRep; | |
| 99 | bRep = temp; | |
| 100 | } | |
| 101 | ||
| 102 | // Extract the exponent and significand from the (possibly swapped) a and b. | |
| 103 | var aExponent = @intCast(i32, (aRep >> significandBits) & maxExponent); | |
| 104 | var bExponent = @intCast(i32, (bRep >> significandBits) & maxExponent); | |
| 105 | var aSignificand = aRep & significandMask; | |
| 106 | var bSignificand = bRep & significandMask; | |
| 107 | ||
| 108 | // Normalize any denormals, and adjust the exponent accordingly. | |
| 109 | if (aExponent == 0) aExponent = normalize(T, &aSignificand); | |
| 110 | if (bExponent == 0) bExponent = normalize(T, &bSignificand); | |
| 111 | ||
| 112 | // The sign of the result is the sign of the larger operand, a. If they | |
| 113 | // have opposite signs, we are performing a subtraction; otherwise addition. | |
| 114 | const resultSign = aRep & signBit; | |
| 115 | const subtraction = (aRep ^ bRep) & signBit != 0; | |
| 116 | ||
| 117 | // Shift the significands to give us round, guard and sticky, and or in the | |
| 118 | // implicit significand bit. (If we fell through from the denormal path it | |
| 119 | // was already set by normalize( ), but setting it twice won't hurt | |
| 120 | // anything.) | |
| 121 | aSignificand = (aSignificand | implicitBit) << 3; | |
| 122 | bSignificand = (bSignificand | implicitBit) << 3; | |
| 123 | ||
| 124 | // Shift the significand of b by the difference in exponents, with a sticky | |
| 125 | // bottom bit to get rounding correct. | |
| 126 | const @"align" = @intCast(Z, aExponent - bExponent); | |
| 127 | if (@"align" != 0) { | |
| 128 | if (@"align" < typeWidth) { | |
| 129 | const sticky = if (bSignificand << @intCast(u7, typeWidth - @"align") != 0) Z(1) else 0; | |
| 130 | bSignificand = (bSignificand >> @truncate(u7, @"align")) | sticky; | |
| 131 | } else { | |
| 132 | bSignificand = 1; // sticky; b is known to be non-zero. | |
| 133 | } | |
| 134 | } | |
| 135 | if (subtraction) { | |
| 136 | aSignificand -= bSignificand; | |
| 137 | // If a == -b, return +zero. | |
| 138 | if (aSignificand == 0) return @bitCast(T, Z(0)); | |
| 139 | ||
| 140 | // If partial cancellation occured, we need to left-shift the result | |
| 141 | // and adjust the exponent: | |
| 142 | if (aSignificand < implicitBit << 3) { | |
| 143 | const shift = @intCast(i32, @clz(aSignificand)) - @intCast(i32, @clz(implicitBit << 3)); | |
| 144 | aSignificand <<= @intCast(u7, shift); | |
| 145 | aExponent -= shift; | |
| 146 | } | |
| 147 | } else { // addition | |
| 148 | aSignificand += bSignificand; | |
| 149 | ||
| 150 | // If the addition carried up, we need to right-shift the result and | |
| 151 | // adjust the exponent: | |
| 152 | if (aSignificand & (implicitBit << 4) != 0) { | |
| 153 | const sticky = aSignificand & 1; | |
| 154 | aSignificand = aSignificand >> 1 | sticky; | |
| 155 | aExponent += 1; | |
| 156 | } | |
| 157 | } | |
| 158 | ||
| 159 | // If we have overflowed the type, return +/- infinity: | |
| 160 | if (aExponent >= maxExponent) return @bitCast(T, infRep | resultSign); | |
| 161 | ||
| 162 | if (aExponent <= 0) { | |
| 163 | // Result is denormal before rounding; the exponent is zero and we | |
| 164 | // need to shift the significand. | |
| 165 | const shift = @intCast(Z, 1 - aExponent); | |
| 166 | const sticky = if (aSignificand << @intCast(u7, typeWidth - shift) != 0) Z(1) else 0; | |
| 167 | aSignificand = aSignificand >> @intCast(u7, shift | sticky); | |
| 168 | aExponent = 0; | |
| 169 | } | |
| 170 | ||
| 171 | // Low three bits are round, guard, and sticky. | |
| 172 | const roundGuardSticky = aSignificand & 0x7; | |
| 173 | ||
| 174 | // Shift the significand into place, and mask off the implicit bit. | |
| 175 | var result = (aSignificand >> 3) & significandMask; | |
| 176 | ||
| 177 | // Insert the exponent and sign. | |
| 178 | result |= @intCast(Z, aExponent) << significandBits; | |
| 179 | result |= resultSign; | |
| 180 | ||
| 181 | // Final rounding. The result may overflow to infinity, but that is the | |
| 182 | // correct result in that case. | |
| 183 | if (roundGuardSticky > 0x4) result += 1; | |
| 184 | if (roundGuardSticky == 0x4) result += result & 1; | |
| 185 | ||
| 186 | return @bitCast(T, result); | |
| 187 | } | |
| 188 | ||
| 189 | test "import addXf3" { | |
| 190 | _ = @import("addXf3_test.zig"); | |
| 191 | } |
std/special/compiler_rt/addXf3_test.zig created+85| ... | ... | @@ -0,0 +1,85 @@ |
| 1 | // Ported from: | |
| 2 | // | |
| 3 | // https://github.com/llvm-mirror/compiler-rt/blob/92f7768ce940f6437b32ecc0985a1446cd040f7a/test/builtins/Unit/addtf3_test.c | |
| 4 | // https://github.com/llvm-mirror/compiler-rt/blob/92f7768ce940f6437b32ecc0985a1446cd040f7a/test/builtins/Unit/subtf3_test.c | |
| 5 | ||
| 6 | const qnan128 = @bitCast(f128, u128(0x7fff800000000000) << 64); | |
| 7 | const inf128 = @bitCast(f128, u128(0x7fff000000000000) << 64); | |
| 8 | ||
| 9 | const __addtf3 = @import("addXf3.zig").__addtf3; | |
| 10 | ||
| 11 | fn test__addtf3(a: f128, b: f128, expected_hi: u64, expected_lo: u64) void { | |
| 12 | const x = __addtf3(a, b); | |
| 13 | ||
| 14 | const rep = @bitCast(u128, x); | |
| 15 | const hi = @intCast(u64, rep >> 64); | |
| 16 | const lo = @truncate(u64, rep); | |
| 17 | ||
| 18 | if (hi == expected_hi and lo == expected_lo) { | |
| 19 | return; | |
| 20 | } | |
| 21 | // test other possible NaN representation (signal NaN) | |
| 22 | else if (expected_hi == 0x7fff800000000000 and expected_lo == 0x0) { | |
| 23 | if ((hi & 0x7fff000000000000) == 0x7fff000000000000 and | |
| 24 | ((hi & 0xffffffffffff) > 0 or lo > 0)) | |
| 25 | { | |
| 26 | return; | |
| 27 | } | |
| 28 | } | |
| 29 | ||
| 30 | @panic("__addtf3 test failure"); | |
| 31 | } | |
| 32 | ||
| 33 | test "addtf3" { | |
| 34 | test__addtf3(qnan128, 0x1.23456789abcdefp+5, 0x7fff800000000000, 0x0); | |
| 35 | ||
| 36 | // NaN + any = NaN | |
| 37 | test__addtf3(@bitCast(f128, (u128(0x7fff000000000000) << 64) | u128(0x800030000000)), 0x1.23456789abcdefp+5, 0x7fff800000000000, 0x0); | |
| 38 | ||
| 39 | // inf + inf = inf | |
| 40 | test__addtf3(inf128, inf128, 0x7fff000000000000, 0x0); | |
| 41 | ||
| 42 | // inf + any = inf | |
| 43 | test__addtf3(inf128, 0x1.2335653452436234723489432abcdefp+5, 0x7fff000000000000, 0x0); | |
| 44 | ||
| 45 | // any + any | |
| 46 | test__addtf3(0x1.23456734245345543849abcdefp+5, 0x1.edcba52449872455634654321fp-1, 0x40042afc95c8b579, 0x61e58dd6c51eb77c); | |
| 47 | } | |
| 48 | ||
| 49 | const __subtf3 = @import("addXf3.zig").__subtf3; | |
| 50 | ||
| 51 | fn test__subtf3(a: f128, b: f128, expected_hi: u64, expected_lo: u64) void { | |
| 52 | const x = __subtf3(a, b); | |
| 53 | ||
| 54 | const rep = @bitCast(u128, x); | |
| 55 | const hi = @intCast(u64, rep >> 64); | |
| 56 | const lo = @truncate(u64, rep); | |
| 57 | ||
| 58 | if (hi == expected_hi and lo == expected_lo) { | |
| 59 | return; | |
| 60 | } | |
| 61 | // test other possible NaN representation (signal NaN) | |
| 62 | else if (expected_hi == 0x7fff800000000000 and expected_lo == 0x0) { | |
| 63 | if ((hi & 0x7fff000000000000) == 0x7fff000000000000 and | |
| 64 | ((hi & 0xffffffffffff) > 0 or lo > 0)) | |
| 65 | { | |
| 66 | return; | |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | @panic("__subtf3 test failure"); | |
| 71 | } | |
| 72 | ||
| 73 | test "subtf3" { | |
| 74 | // qNaN - any = qNaN | |
| 75 | test__subtf3(qnan128, 0x1.23456789abcdefp+5, 0x7fff800000000000, 0x0); | |
| 76 | ||
| 77 | // NaN + any = NaN | |
| 78 | test__subtf3(@bitCast(f128, (u128(0x7fff000000000000) << 64) | u128(0x800030000000)), 0x1.23456789abcdefp+5, 0x7fff800000000000, 0x0); | |
| 79 | ||
| 80 | // inf - any = inf | |
| 81 | test__subtf3(inf128, 0x1.23456789abcdefp+5, 0x7fff000000000000, 0x0); | |
| 82 | ||
| 83 | // any + any | |
| 84 | test__subtf3(0x1.234567829a3bcdef5678ade36734p+5, 0x1.ee9d7c52354a6936ab8d7654321fp-1, 0x40041b8af1915166, 0xa44a7bca780a166c); | |
| 85 | } |
std/special/compiler_rt/index.zig+4| ... | ... | @@ -21,6 +21,9 @@ comptime { |
| 21 | 21 | |
| 22 | 22 | @export("__unordtf2", @import("comparetf2.zig").__unordtf2, linkage); |
| 23 | 23 | |
| 24 | @export("__addtf3", @import("addXf3.zig").__addtf3, linkage); | |
| 25 | @export("__subtf3", @import("addXf3.zig").__subtf3, linkage); | |
| 26 | ||
| 24 | 27 | @export("__floattitf", @import("floattitf.zig").__floattitf, linkage); |
| 25 | 28 | @export("__floattidf", @import("floattidf.zig").__floattidf, linkage); |
| 26 | 29 | @export("__floattisf", @import("floattisf.zig").__floattisf, linkage); |
| ... | ... | @@ -37,6 +40,7 @@ comptime { |
| 37 | 40 | @export("__extendhfsf2", @import("extendXfYf2.zig").__extendhfsf2, linkage); |
| 38 | 41 | |
| 39 | 42 | @export("__truncsfhf2", @import("truncXfYf2.zig").__truncsfhf2, linkage); |
| 43 | @export("__truncdfhf2", @import("truncXfYf2.zig").__truncdfhf2, linkage); | |
| 40 | 44 | @export("__trunctfdf2", @import("truncXfYf2.zig").__trunctfdf2, linkage); |
| 41 | 45 | @export("__trunctfsf2", @import("truncXfYf2.zig").__trunctfsf2, linkage); |
| 42 | 46 |
std/special/compiler_rt/truncXfYf2.zig+4| ... | ... | @@ -4,6 +4,10 @@ pub extern fn __truncsfhf2(a: f32) u16 { |
| 4 | 4 | return @bitCast(u16, truncXfYf2(f16, f32, a)); |
| 5 | 5 | } |
| 6 | 6 | |
| 7 | pub extern fn __truncdfhf2(a: f64) u16 { | |
| 8 | return @bitCast(u16, truncXfYf2(f16, f64, a)); | |
| 9 | } | |
| 10 | ||
| 7 | 11 | pub extern fn __trunctfsf2(a: f128) f32 { |
| 8 | 12 | return truncXfYf2(f32, f128, a); |
| 9 | 13 | } |
std/special/compiler_rt/truncXfYf2_test.zig+68| ... | ... | @@ -63,6 +63,74 @@ test "truncsfhf2" { |
| 63 | 63 | test__truncsfhf2(0x33000000, 0x0000); // 0x1.0p-25 -> zero |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | const __truncdfhf2 = @import("truncXfYf2.zig").__truncdfhf2; | |
| 67 | ||
| 68 | fn test__truncdfhf2(a: f64, expected: u16) void { | |
| 69 | const rep = @bitCast(u16, __truncdfhf2(a)); | |
| 70 | ||
| 71 | if (rep == expected) { | |
| 72 | return; | |
| 73 | } | |
| 74 | // test other possible NaN representation(signal NaN) | |
| 75 | else if (expected == 0x7e00) { | |
| 76 | if ((rep & 0x7c00) == 0x7c00 and (rep & 0x3ff) > 0) { | |
| 77 | return; | |
| 78 | } | |
| 79 | } | |
| 80 | ||
| 81 | @panic("__truncdfhf2 test failure"); | |
| 82 | } | |
| 83 | ||
| 84 | fn test__truncdfhf2_raw(a: u64, expected: u16) void { | |
| 85 | const actual = __truncdfhf2(@bitCast(f64, a)); | |
| 86 | ||
| 87 | if (actual == expected) { | |
| 88 | return; | |
| 89 | } | |
| 90 | ||
| 91 | @panic("__truncdfhf2 test failure"); | |
| 92 | } | |
| 93 | ||
| 94 | test "truncdfhf2" { | |
| 95 | test__truncdfhf2_raw(0x7ff8000000000000, 0x7e00); // qNaN | |
| 96 | test__truncdfhf2_raw(0x7ff0000000008000, 0x7e00); // NaN | |
| 97 | ||
| 98 | test__truncdfhf2_raw(0x7ff0000000000000, 0x7c00); //inf | |
| 99 | test__truncdfhf2_raw(0xfff0000000000000, 0xfc00); // -inf | |
| 100 | ||
| 101 | test__truncdfhf2(0.0, 0x0); // zero | |
| 102 | test__truncdfhf2_raw(0x80000000 << 32, 0x8000); // -zero | |
| 103 | ||
| 104 | test__truncdfhf2(3.1415926535, 0x4248); | |
| 105 | test__truncdfhf2(-3.1415926535, 0xc248); | |
| 106 | ||
| 107 | test__truncdfhf2(0x1.987124876876324p+1000, 0x7c00); | |
| 108 | test__truncdfhf2(0x1.987124876876324p+12, 0x6e62); | |
| 109 | test__truncdfhf2(0x1.0p+0, 0x3c00); | |
| 110 | test__truncdfhf2(0x1.0p-14, 0x0400); | |
| 111 | ||
| 112 | // denormal | |
| 113 | test__truncdfhf2(0x1.0p-20, 0x0010); | |
| 114 | test__truncdfhf2(0x1.0p-24, 0x0001); | |
| 115 | test__truncdfhf2(-0x1.0p-24, 0x8001); | |
| 116 | test__truncdfhf2(0x1.5p-25, 0x0001); | |
| 117 | ||
| 118 | // and back to zero | |
| 119 | test__truncdfhf2(0x1.0p-25, 0x0000); | |
| 120 | test__truncdfhf2(-0x1.0p-25, 0x8000); | |
| 121 | ||
| 122 | // max (precise) | |
| 123 | test__truncdfhf2(65504.0, 0x7bff); | |
| 124 | ||
| 125 | // max (rounded) | |
| 126 | test__truncdfhf2(65519.0, 0x7bff); | |
| 127 | ||
| 128 | // max (to +inf) | |
| 129 | test__truncdfhf2(65520.0, 0x7c00); | |
| 130 | test__truncdfhf2(-65520.0, 0xfc00); | |
| 131 | test__truncdfhf2(65536.0, 0x7c00); | |
| 132 | } | |
| 133 | ||
| 66 | 134 | const __trunctfsf2 = @import("truncXfYf2.zig").__trunctfsf2; |
| 67 | 135 | |
| 68 | 136 | fn test__trunctfsf2(a: f128, expected: u32) void { |