| 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); |
| 3 | |
| 4 | const compiler_rt = @import("../compiler_rt.zig"); |
| 5 | const symbol = compiler_rt.symbol; |
| 6 | const normalize = compiler_rt.normalize; |
| 7 | const wideMultiply = compiler_rt.wideMultiply; |
| 8 | |
| 9 | comptime { |
| 10 | if (compiler_rt.want_ppc_abi) { |
| 11 | symbol(&__divtf3, "__divkf3"); |
| 12 | } else if (compiler_rt.want_sparc64_abi) { |
| 13 | symbol(&_Qp_div, "_Qp_div"); |
| 14 | } else if (compiler_rt.want_sparc32_abi) { |
| 15 | symbol(&__divtf3, "_Q_div"); |
| 16 | } else { |
| 17 | symbol(&__divtf3, "__divtf3"); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | fn __divtf3(a: compiler_rt.f128.Abi, b: compiler_rt.f128.Abi) callconv(.c) compiler_rt.f128.Abi { |
| 22 | return compiler_rt.f128.toAbi(div_f128(compiler_rt.f128.fromAbi(a), compiler_rt.f128.fromAbi(b))); |
| 23 | } |
| 24 | |
| 25 | fn _Qp_div(c: *f128, a: *const f128, b: *const f128) callconv(.c) void { |
| 26 | c.* = div_f128(a.*, b.*); |
| 27 | } |
| 28 | |
| 29 | pub fn div_f128(a: f128, b: f128) f128 { |
| 30 | const Z = @Int(.unsigned, 128); |
| 31 | |
| 32 | const significandBits = std.math.floatMantissaBits(f128); |
| 33 | const exponentBits = std.math.floatExponentBits(f128); |
| 34 | |
| 35 | const signBit = (@as(Z, 1) << (significandBits + exponentBits)); |
| 36 | const maxExponent = ((1 << exponentBits) - 1); |
| 37 | const exponentBias = (maxExponent >> 1); |
| 38 | |
| 39 | const implicitBit = (@as(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 | const infRep: Z = @bitCast(std.math.inf(f128)); |
| 47 | |
| 48 | const aExponent: u32 = @truncate((@as(Z, @bitCast(a)) >> significandBits) & maxExponent); |
| 49 | const bExponent: u32 = @truncate((@as(Z, @bitCast(b)) >> significandBits) & maxExponent); |
| 50 | const quotientSign: Z = (@as(Z, @bitCast(a)) ^ @as(Z, @bitCast(b))) & signBit; |
| 51 | |
| 52 | var aSignificand: Z = @as(Z, @bitCast(a)) & significandMask; |
| 53 | var bSignificand: Z = @as(Z, @bitCast(b)) & significandMask; |
| 54 | var scale: i32 = 0; |
| 55 | |
| 56 | // Detect if a or b is zero, denormal, infinity, or NaN. |
| 57 | if (aExponent -% 1 >= maxExponent - 1 or bExponent -% 1 >= maxExponent - 1) { |
| 58 | const aAbs: Z = @as(Z, @bitCast(a)) & absMask; |
| 59 | const bAbs: Z = @as(Z, @bitCast(b)) & absMask; |
| 60 | |
| 61 | // NaN / anything = qNaN |
| 62 | if (aAbs > infRep) return @bitCast(@as(Z, @bitCast(a)) | quietBit); |
| 63 | // anything / NaN = qNaN |
| 64 | if (bAbs > infRep) return @bitCast(@as(Z, @bitCast(b)) | quietBit); |
| 65 | |
| 66 | if (aAbs == infRep) { |
| 67 | // infinity / infinity = NaN |
| 68 | if (bAbs == infRep) { |
| 69 | return @bitCast(qnanRep); |
| 70 | } |
| 71 | // infinity / anything else = +/- infinity |
| 72 | else { |
| 73 | return @bitCast(aAbs | quotientSign); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // anything else / infinity = +/- 0 |
| 78 | if (bAbs == infRep) return @bitCast(quotientSign); |
| 79 | |
| 80 | if (aAbs == 0) { |
| 81 | // zero / zero = NaN |
| 82 | if (bAbs == 0) { |
| 83 | return @bitCast(qnanRep); |
| 84 | } |
| 85 | // zero / anything else = +/- zero |
| 86 | else { |
| 87 | return @bitCast(quotientSign); |
| 88 | } |
| 89 | } |
| 90 | // anything else / zero = +/- infinity |
| 91 | if (bAbs == 0) return @bitCast(infRep | quotientSign); |
| 92 | |
| 93 | // one or both of a or b is denormal, the other (if applicable) is a |
| 94 | // normal number. Renormalize one or both of a and b, and set scale to |
| 95 | // include the necessary exponent adjustment. |
| 96 | if (aAbs < implicitBit) scale +%= normalize(f128, &aSignificand); |
| 97 | if (bAbs < implicitBit) scale -%= normalize(f128, &bSignificand); |
| 98 | } |
| 99 | |
| 100 | // Set the implicit significand bit. If we fell through from the |
| 101 | // denormal path it was already set by normalize( ), but setting it twice |
| 102 | // won't hurt anything. |
| 103 | aSignificand |= implicitBit; |
| 104 | bSignificand |= implicitBit; |
| 105 | var quotientExponent: i32 = @as(i32, @bitCast(aExponent -% bExponent)) +% scale; |
| 106 | |
| 107 | // Align the significand of b as a Q63 fixed-point number in the range |
| 108 | // [1, 2.0) and get a Q64 approximate reciprocal using a small minimax |
| 109 | // polynomial approximation: reciprocal = 3/4 + 1/sqrt(2) - b/2. This |
| 110 | // is accurate to about 3.5 binary digits. |
| 111 | const q63b: u64 = @truncate(bSignificand >> 49); |
| 112 | var recip64 = @as(u64, 0x7504f333F9DE6484) -% q63b; |
| 113 | // 0x7504f333F9DE6484 / 2^64 + 1 = 3/4 + 1/sqrt(2) |
| 114 | |
| 115 | // Now refine the reciprocal estimate using a Newton-Raphson iteration: |
| 116 | // |
| 117 | // x1 = x0 * (2 - x0 * b) |
| 118 | // |
| 119 | // This doubles the number of correct binary digits in the approximation |
| 120 | // with each iteration. |
| 121 | var correction64: u64 = undefined; |
| 122 | correction64 = @truncate(~(@as(u128, recip64) *% q63b >> 64) +% 1); |
| 123 | recip64 = @truncate(@as(u128, recip64) *% correction64 >> 63); |
| 124 | correction64 = @truncate(~(@as(u128, recip64) *% q63b >> 64) +% 1); |
| 125 | recip64 = @truncate(@as(u128, recip64) *% correction64 >> 63); |
| 126 | correction64 = @truncate(~(@as(u128, recip64) *% q63b >> 64) +% 1); |
| 127 | recip64 = @truncate(@as(u128, recip64) *% correction64 >> 63); |
| 128 | correction64 = @truncate(~(@as(u128, recip64) *% q63b >> 64) +% 1); |
| 129 | recip64 = @truncate(@as(u128, recip64) *% correction64 >> 63); |
| 130 | correction64 = @truncate(~(@as(u128, recip64) *% q63b >> 64) +% 1); |
| 131 | recip64 = @truncate(@as(u128, recip64) *% correction64 >> 63); |
| 132 | |
| 133 | // The reciprocal may have overflowed to zero if the upper half of b is |
| 134 | // exactly 1.0. This would sabatoge the full-width final stage of the |
| 135 | // computation that follows, so we adjust the reciprocal down by one bit. |
| 136 | recip64 -%= 1; |
| 137 | |
| 138 | // We need to perform one more iteration to get us to 112 binary digits; |
| 139 | // The last iteration needs to happen with extra precision. |
| 140 | const q127blo: u64 = @truncate(bSignificand << 15); |
| 141 | var correction: u128 = undefined; |
| 142 | var reciprocal: u128 = undefined; |
| 143 | |
| 144 | // NOTE: This operation is equivalent to __multi3, which is not implemented |
| 145 | // in some architecture |
| 146 | var r64q63: u128 = undefined; |
| 147 | var r64q127: u128 = undefined; |
| 148 | var r64cH: u128 = undefined; |
| 149 | var r64cL: u128 = undefined; |
| 150 | var dummy: u128 = undefined; |
| 151 | wideMultiply(u128, recip64, q63b, &dummy, &r64q63); |
| 152 | wideMultiply(u128, recip64, q127blo, &dummy, &r64q127); |
| 153 | |
| 154 | correction = -%(r64q63 + (r64q127 >> 64)); |
| 155 | |
| 156 | const cHi: u64 = @truncate(correction >> 64); |
| 157 | const cLo: u64 = @truncate(correction); |
| 158 | |
| 159 | wideMultiply(u128, recip64, cHi, &dummy, &r64cH); |
| 160 | wideMultiply(u128, recip64, cLo, &dummy, &r64cL); |
| 161 | |
| 162 | reciprocal = r64cH + (r64cL >> 64); |
| 163 | |
| 164 | // Adjust the final 128-bit reciprocal estimate downward to ensure that it |
| 165 | // is strictly smaller than the infinitely precise exact reciprocal. Because |
| 166 | // the computation of the Newton-Raphson step is truncating at every step, |
| 167 | // this adjustment is small; most of the work is already done. |
| 168 | reciprocal -%= 2; |
| 169 | |
| 170 | // The numerical reciprocal is accurate to within 2^-112, lies in the |
| 171 | // interval [0.5, 1.0), and is strictly smaller than the true reciprocal |
| 172 | // of b. Multiplying a by this reciprocal thus gives a numerical q = a/b |
| 173 | // in Q127 with the following properties: |
| 174 | // |
| 175 | // 1. q < a/b |
| 176 | // 2. q is in the interval [0.5, 2.0) |
| 177 | // 3. The error in q is bounded away from 2^-113 (actually, we have a |
| 178 | // couple of bits to spare, but this is all we need). |
| 179 | |
| 180 | // We need a 128 x 128 multiply high to compute q. |
| 181 | var quotient: u128 = undefined; |
| 182 | var quotientLo: u128 = undefined; |
| 183 | wideMultiply(u128, aSignificand << 2, reciprocal, &quotient, &quotientLo); |
| 184 | |
| 185 | // Two cases: quotient is in [0.5, 1.0) or quotient is in [1.0, 2.0). |
| 186 | // In either case, we are going to compute a residual of the form |
| 187 | // |
| 188 | // r = a - q*b |
| 189 | // |
| 190 | // We know from the construction of q that r satisfies: |
| 191 | // |
| 192 | // 0 <= r < ulp(q)*b |
| 193 | // |
| 194 | // If r is greater than 1/2 ulp(q)*b, then q rounds up. Otherwise, we |
| 195 | // already have the correct result. The exact halfway case cannot occur. |
| 196 | // We also take this time to right shift quotient if it falls in the [1,2) |
| 197 | // range and adjust the exponent accordingly. |
| 198 | var residual: u128 = undefined; |
| 199 | var qb: u128 = undefined; |
| 200 | |
| 201 | if (quotient < (implicitBit << 1)) { |
| 202 | wideMultiply(u128, quotient, bSignificand, &dummy, &qb); |
| 203 | residual = (aSignificand << 113) -% qb; |
| 204 | quotientExponent -%= 1; |
| 205 | } else { |
| 206 | quotient >>= 1; |
| 207 | wideMultiply(u128, quotient, bSignificand, &dummy, &qb); |
| 208 | residual = (aSignificand << 112) -% qb; |
| 209 | } |
| 210 | |
| 211 | const writtenExponent = quotientExponent +% exponentBias; |
| 212 | |
| 213 | if (writtenExponent >= maxExponent) { |
| 214 | // If we have overflowed the exponent, return infinity. |
| 215 | return @bitCast(infRep | quotientSign); |
| 216 | } else if (writtenExponent < 1) { |
| 217 | if (writtenExponent == 0) { |
| 218 | // Check whether the rounded result is normal. |
| 219 | const round = @intFromBool((residual << 1) > bSignificand); |
| 220 | // Clear the implicit bit. |
| 221 | var absResult = quotient & significandMask; |
| 222 | // Round. |
| 223 | absResult += round; |
| 224 | if ((absResult & ~significandMask) > 0) { |
| 225 | // The rounded result is normal; return it. |
| 226 | return @bitCast(absResult | quotientSign); |
| 227 | } |
| 228 | // Result is denormal with exponent 0 |
| 229 | return @bitCast(absResult | quotientSign); |
| 230 | } else { |
| 231 | // For denormals with writtenExponent < 0, |
| 232 | // the implicit bit must be shifted into the mantissa (IEEE 754) |
| 233 | const shiftAmount = @as(u7, @intCast(1 - writtenExponent)); |
| 234 | |
| 235 | // Check for underflow |
| 236 | if (shiftAmount > significandBits) { |
| 237 | return @bitCast(quotientSign); |
| 238 | } |
| 239 | |
| 240 | // Round the quotient before pushing |
| 241 | const shouldRound = (residual << 1) > bSignificand; |
| 242 | const roundedQuotient = quotient +% @as(u113, @intFromBool(shouldRound)); |
| 243 | |
| 244 | // Move to the denormal range and apply the mask |
| 245 | const denormQuotient = roundedQuotient >> shiftAmount; |
| 246 | const absResult = denormQuotient & significandMask; |
| 247 | |
| 248 | // Add sign to denormal mantissa and return |
| 249 | return @bitCast(absResult | quotientSign); |
| 250 | } |
| 251 | } else { |
| 252 | const round = @intFromBool((residual << 1) >= bSignificand); |
| 253 | // Clear the implicit bit |
| 254 | var absResult = quotient & significandMask; |
| 255 | // Insert the exponent |
| 256 | absResult |= @as(Z, @intCast(writtenExponent)) << significandBits; |
| 257 | // Round |
| 258 | absResult +%= round; |
| 259 | // Insert the sign and return |
| 260 | return @bitCast(absResult | quotientSign); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | test { |
| 265 | _ = @import("divtf3_test.zig"); |
| 266 | } |