| ... | ... | @@ -0,0 +1,285 @@ |
| 1 | // Ported from: |
| 2 | // |
| 3 | // https://github.com/llvm/llvm-project/blob/2ffb1b0413efa9a24eb3c49e710e36f92e2cb50b/compiler-rt/lib/builtins/fp_mul_impl.inc |
| 4 | |
| 5 | const std = @import("std"); |
| 6 | const builtin = @import("builtin"); |
| 7 | const compiler_rt = @import("../compiler_rt.zig"); |
| 8 | |
| 9 | pub extern fn __multf3(a: f128, b: f128) f128 { |
| 10 | return mulXf3(f128, a, b); |
| 11 | } |
| 12 | pub extern fn __muldf3(a: f64, b: f64) f64 { |
| 13 | return mulXf3(f64, a, b); |
| 14 | } |
| 15 | pub extern fn __mulsf3(a: f32, b: f32) f32 { |
| 16 | return mulXf3(f32, a, b); |
| 17 | } |
| 18 | |
| 19 | fn mulXf3(comptime T: type, a: T, b: T) T { |
| 20 | const Z = @IntType(false, T.bit_count); |
| 21 | |
| 22 | const typeWidth = T.bit_count; |
| 23 | const significandBits = std.math.floatMantissaBits(T); |
| 24 | const exponentBits = std.math.floatExponentBits(T); |
| 25 | |
| 26 | const signBit = (Z(1) << (significandBits + exponentBits)); |
| 27 | const maxExponent = ((1 << exponentBits) - 1); |
| 28 | const exponentBias = (maxExponent >> 1); |
| 29 | |
| 30 | const implicitBit = (Z(1) << significandBits); |
| 31 | const quietBit = implicitBit >> 1; |
| 32 | const significandMask = implicitBit - 1; |
| 33 | |
| 34 | const absMask = signBit - 1; |
| 35 | const exponentMask = absMask ^ significandMask; |
| 36 | const qnanRep = exponentMask | quietBit; |
| 37 | const infRep = @bitCast(Z, std.math.inf(T)); |
| 38 | |
| 39 | const aExponent = @truncate(u32, (@bitCast(Z, a) >> significandBits) & maxExponent); |
| 40 | const bExponent = @truncate(u32, (@bitCast(Z, b) >> significandBits) & maxExponent); |
| 41 | const productSign: Z = (@bitCast(Z, a) ^ @bitCast(Z, b)) & signBit; |
| 42 | |
| 43 | var aSignificand: Z = @bitCast(Z, a) & significandMask; |
| 44 | var bSignificand: Z = @bitCast(Z, b) & significandMask; |
| 45 | var scale: i32 = 0; |
| 46 | |
| 47 | // Detect if a or b is zero, denormal, infinity, or NaN. |
| 48 | if (aExponent -% 1 >= maxExponent -% 1 or bExponent -% 1 >= maxExponent -% 1) { |
| 49 | const aAbs: Z = @bitCast(Z, a) & absMask; |
| 50 | const bAbs: Z = @bitCast(Z, b) & absMask; |
| 51 | |
| 52 | // NaN * anything = qNaN |
| 53 | if (aAbs > infRep) return @bitCast(T, @bitCast(Z, a) | quietBit); |
| 54 | // anything * NaN = qNaN |
| 55 | if (bAbs > infRep) return @bitCast(T, @bitCast(Z, b) | quietBit); |
| 56 | |
| 57 | if (aAbs == infRep) { |
| 58 | // infinity * non-zero = +/- infinity |
| 59 | if (bAbs != 0) { |
| 60 | return @bitCast(T, aAbs | productSign); |
| 61 | } else { |
| 62 | // infinity * zero = NaN |
| 63 | return @bitCast(T, qnanRep); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if (bAbs == infRep) { |
| 68 | //? non-zero * infinity = +/- infinity |
| 69 | if (aAbs != 0) { |
| 70 | return @bitCast(T, bAbs | productSign); |
| 71 | } else { |
| 72 | // zero * infinity = NaN |
| 73 | return @bitCast(T, qnanRep); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // zero * anything = +/- zero |
| 78 | if (aAbs == 0) return @bitCast(T, productSign); |
| 79 | // anything * zero = +/- zero |
| 80 | if (bAbs == 0) return @bitCast(T, productSign); |
| 81 | |
| 82 | // one or both of a or b is denormal, the other (if applicable) is a |
| 83 | // normal number. Renormalize one or both of a and b, and set scale to |
| 84 | // include the necessary exponent adjustment. |
| 85 | if (aAbs < implicitBit) scale +%= normalize(T, &aSignificand); |
| 86 | if (bAbs < implicitBit) scale +%= normalize(T, &bSignificand); |
| 87 | } |
| 88 | |
| 89 | // Or in the implicit significand bit. (If we fell through from the |
| 90 | // denormal path it was already set by normalize( ), but setting it twice |
| 91 | // won't hurt anything.) |
| 92 | aSignificand |= implicitBit; |
| 93 | bSignificand |= implicitBit; |
| 94 | |
| 95 | // Get the significand of a*b. Before multiplying the significands, shift |
| 96 | // one of them left to left-align it in the field. Thus, the product will |
| 97 | // have (exponentBits + 2) integral digits, all but two of which must be |
| 98 | // zero. Normalizing this result is just a conditional left-shift by one |
| 99 | // and bumping the exponent accordingly. |
| 100 | var productHi: Z = undefined; |
| 101 | var productLo: Z = undefined; |
| 102 | wideMultiply(Z, aSignificand, bSignificand << exponentBits, &productHi, &productLo); |
| 103 | |
| 104 | var productExponent: i32 = @bitCast(i32, aExponent +% bExponent) -% exponentBias +% scale; |
| 105 | |
| 106 | // Normalize the significand, adjust exponent if needed. |
| 107 | if ((productHi & implicitBit) != 0) { |
| 108 | productExponent +%= 1; |
| 109 | } else { |
| 110 | productHi = (productHi << 1) | (productLo >> (typeWidth - 1)); |
| 111 | productLo = productLo << 1; |
| 112 | } |
| 113 | |
| 114 | // If we have overflowed the type, return +/- infinity. |
| 115 | if (productExponent >= maxExponent) return @bitCast(T, infRep | productSign); |
| 116 | |
| 117 | if (productExponent <= 0) { |
| 118 | // Result is denormal before rounding |
| 119 | // |
| 120 | // If the result is so small that it just underflows to zero, return |
| 121 | // a zero of the appropriate sign. Mathematically there is no need to |
| 122 | // handle this case separately, but we make it a special case to |
| 123 | // simplify the shift logic. |
| 124 | const shift: u32 = @truncate(u32, Z(1) -% @bitCast(u32, productExponent)); |
| 125 | if (shift >= typeWidth) return @bitCast(T, productSign); |
| 126 | |
| 127 | // Otherwise, shift the significand of the result so that the round |
| 128 | // bit is the high bit of productLo. |
| 129 | wideRightShiftWithSticky(Z, &productHi, &productLo, shift); |
| 130 | } else { |
| 131 | // Result is normal before rounding; insert the exponent. |
| 132 | productHi &= significandMask; |
| 133 | productHi |= Z(@bitCast(u32, productExponent)) << significandBits; |
| 134 | } |
| 135 | |
| 136 | // Insert the sign of the result: |
| 137 | productHi |= productSign; |
| 138 | |
| 139 | // Final rounding. The final result may overflow to infinity, or underflow |
| 140 | // to zero, but those are the correct results in those cases. We use the |
| 141 | // default IEEE-754 round-to-nearest, ties-to-even rounding mode. |
| 142 | if (productLo > signBit) productHi +%= 1; |
| 143 | if (productLo == signBit) productHi +%= productHi & 1; |
| 144 | return @bitCast(T, productHi); |
| 145 | } |
| 146 | |
| 147 | fn wideMultiply(comptime Z: type, a: Z, b: Z, hi: *Z, lo: *Z) void { |
| 148 | switch (Z) { |
| 149 | u32 => { |
| 150 | // 32x32 --> 64 bit multiply |
| 151 | const product = u64(a) * u64(b); |
| 152 | hi.* = @truncate(u32, product >> 32); |
| 153 | lo.* = @truncate(u32, product); |
| 154 | }, |
| 155 | u64 => { |
| 156 | const S = struct { |
| 157 | fn loWord(x: u64) u64 { |
| 158 | return @truncate(u32, x); |
| 159 | } |
| 160 | fn hiWord(x: u64) u64 { |
| 161 | return @truncate(u32, x >> 32); |
| 162 | } |
| 163 | }; |
| 164 | // 64x64 -> 128 wide multiply for platforms that don't have such an operation; |
| 165 | // many 64-bit platforms have this operation, but they tend to have hardware |
| 166 | // floating-point, so we don't bother with a special case for them here. |
| 167 | // Each of the component 32x32 -> 64 products |
| 168 | const plolo: u64 = S.loWord(a) * S.loWord(b); |
| 169 | const plohi: u64 = S.loWord(a) * S.hiWord(b); |
| 170 | const philo: u64 = S.hiWord(a) * S.loWord(b); |
| 171 | const phihi: u64 = S.hiWord(a) * S.hiWord(b); |
| 172 | // Sum terms that contribute to lo in a way that allows us to get the carry |
| 173 | const r0: u64 = S.loWord(plolo); |
| 174 | const r1: u64 = S.hiWord(plolo) +% S.loWord(plohi) +% S.loWord(philo); |
| 175 | lo.* = r0 +% (r1 << 32); |
| 176 | // Sum terms contributing to hi with the carry from lo |
| 177 | hi.* = S.hiWord(plohi) +% S.hiWord(philo) +% S.hiWord(r1) +% phihi; |
| 178 | }, |
| 179 | u128 => { |
| 180 | const Word_LoMask = u64(0x00000000ffffffff); |
| 181 | const Word_HiMask = u64(0xffffffff00000000); |
| 182 | const Word_FullMask = u64(0xffffffffffffffff); |
| 183 | const S = struct { |
| 184 | fn Word_1(x: u128) u64 { |
| 185 | return @truncate(u32, x >> 96); |
| 186 | } |
| 187 | fn Word_2(x: u128) u64 { |
| 188 | return @truncate(u32, x >> 64); |
| 189 | } |
| 190 | fn Word_3(x: u128) u64 { |
| 191 | return @truncate(u32, x >> 32); |
| 192 | } |
| 193 | fn Word_4(x: u128) u64 { |
| 194 | return @truncate(u32, x); |
| 195 | } |
| 196 | }; |
| 197 | // 128x128 -> 256 wide multiply for platforms that don't have such an operation; |
| 198 | // many 64-bit platforms have this operation, but they tend to have hardware |
| 199 | // floating-point, so we don't bother with a special case for them here. |
| 200 | |
| 201 | const product11: u64 = S.Word_1(a) * S.Word_1(b); |
| 202 | const product12: u64 = S.Word_1(a) * S.Word_2(b); |
| 203 | const product13: u64 = S.Word_1(a) * S.Word_3(b); |
| 204 | const product14: u64 = S.Word_1(a) * S.Word_4(b); |
| 205 | const product21: u64 = S.Word_2(a) * S.Word_1(b); |
| 206 | const product22: u64 = S.Word_2(a) * S.Word_2(b); |
| 207 | const product23: u64 = S.Word_2(a) * S.Word_3(b); |
| 208 | const product24: u64 = S.Word_2(a) * S.Word_4(b); |
| 209 | const product31: u64 = S.Word_3(a) * S.Word_1(b); |
| 210 | const product32: u64 = S.Word_3(a) * S.Word_2(b); |
| 211 | const product33: u64 = S.Word_3(a) * S.Word_3(b); |
| 212 | const product34: u64 = S.Word_3(a) * S.Word_4(b); |
| 213 | const product41: u64 = S.Word_4(a) * S.Word_1(b); |
| 214 | const product42: u64 = S.Word_4(a) * S.Word_2(b); |
| 215 | const product43: u64 = S.Word_4(a) * S.Word_3(b); |
| 216 | const product44: u64 = S.Word_4(a) * S.Word_4(b); |
| 217 | |
| 218 | const sum0: u128 = u128(product44); |
| 219 | const sum1: u128 = u128(product34) +% |
| 220 | u128(product43); |
| 221 | const sum2: u128 = u128(product24) +% |
| 222 | u128(product33) +% |
| 223 | u128(product42); |
| 224 | const sum3: u128 = u128(product14) +% |
| 225 | u128(product23) +% |
| 226 | u128(product32) +% |
| 227 | u128(product41); |
| 228 | const sum4: u128 = u128(product13) +% |
| 229 | u128(product22) +% |
| 230 | u128(product31); |
| 231 | const sum5: u128 = u128(product12) +% |
| 232 | u128(product21); |
| 233 | const sum6: u128 = u128(product11); |
| 234 | |
| 235 | const r0: u128 = (sum0 & Word_FullMask) +% |
| 236 | ((sum1 & Word_LoMask) << 32); |
| 237 | const r1: u128 = (sum0 >> 64) +% |
| 238 | ((sum1 >> 32) & Word_FullMask) +% |
| 239 | (sum2 & Word_FullMask) +% |
| 240 | ((sum3 << 32) & Word_HiMask); |
| 241 | |
| 242 | lo.* = r0 +% (r1 << 64); |
| 243 | hi.* = (r1 >> 64) +% |
| 244 | (sum1 >> 96) +% |
| 245 | (sum2 >> 64) +% |
| 246 | (sum3 >> 32) +% |
| 247 | sum4 +% |
| 248 | (sum5 << 32) +% |
| 249 | (sum6 << 64); |
| 250 | }, |
| 251 | else => @compileError("unsupported"), |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | fn normalize(comptime T: type, significand: *@IntType(false, T.bit_count)) i32 { |
| 256 | const Z = @IntType(false, T.bit_count); |
| 257 | const significandBits = std.math.floatMantissaBits(T); |
| 258 | const implicitBit = Z(1) << significandBits; |
| 259 | |
| 260 | const shift = @clz(significand.*) - @clz(implicitBit); |
| 261 | significand.* <<= @intCast(std.math.Log2Int(Z), shift); |
| 262 | return 1 - shift; |
| 263 | } |
| 264 | |
| 265 | fn wideRightShiftWithSticky(comptime Z: type, hi: *Z, lo: *Z, count: u32) void { |
| 266 | const typeWidth = Z.bit_count; |
| 267 | const S = std.math.Log2Int(Z); |
| 268 | if (count < typeWidth) { |
| 269 | const sticky = @truncate(u8, lo.* << @intCast(S, typeWidth -% count)); |
| 270 | lo.* = (hi.* << @intCast(S, typeWidth -% count)) | (lo.* >> @intCast(S, count)) | sticky; |
| 271 | hi.* = hi.* >> @intCast(S, count); |
| 272 | } else if (count < 2 * typeWidth) { |
| 273 | const sticky = @truncate(u8, hi.* << @intCast(S, 2 * typeWidth -% count) | lo.*); |
| 274 | lo.* = hi.* >> @intCast(S, count -% typeWidth) | sticky; |
| 275 | hi.* = 0; |
| 276 | } else { |
| 277 | const sticky = @truncate(u8, hi.* | lo.*); |
| 278 | lo.* = sticky; |
| 279 | hi.* = 0; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | test "import mulXf3" { |
| 284 | _ = @import("mulXf3_test.zig"); |
| 285 | } |