1//! Ported from:
2//!
3//! https://github.com/llvm/llvm-project/commit/d674d96bc56c0f377879d01c9d8dfdaaa7859cdb/compiler-rt/lib/builtins/divdf3.c
4
5const std = @import("std");
6const compiler_rt = @import("../compiler_rt.zig");
7const symbol = compiler_rt.symbol;
8
9const normalize = compiler_rt.normalize;
10const wideMultiply = compiler_rt.wideMultiply;
11
12comptime {
13 if (compiler_rt.want_aeabi) {
14 symbol(&__aeabi_ddiv, "__aeabi_ddiv");
15 } else {
16 symbol(&__divdf3, "__divdf3");
17 }
18}
19
20fn __divdf3(a: compiler_rt.f64.Abi, b: compiler_rt.f64.Abi) callconv(.c) compiler_rt.f64.Abi {
21 return compiler_rt.f64.toAbi(div_f64(compiler_rt.f64.fromAbi(a), compiler_rt.f64.fromAbi(b)));
22}
23
24fn __aeabi_ddiv(a: f64, b: f64) callconv(.{ .arm_aapcs = .{} }) f64 {
25 return div_f64(a, b);
26}
27
28pub fn div_f64(a: f64, b: f64) f64 {
29 const Z = @Int(.unsigned, 64);
30 const SignedZ = @Int(.signed, 64);
31
32 const significandBits = std.math.floatMantissaBits(f64);
33 const exponentBits = std.math.floatExponentBits(f64);
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 = @as(Z, @bitCast(std.math.inf(f64)));
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(f64, &aSignificand);
97 if (bAbs < implicitBit) scale -%= normalize(f64, &bSignificand);
98 }
99
100 // Or in 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 Q31 fixed-point number in the range
108 // [1, 2.0) and get a Q32 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 q31b: u32 = @truncate(bSignificand >> 21);
112 var recip32 = @as(u32, 0x7504f333) -% q31b;
113
114 // Now refine the reciprocal estimate using a Newton-Raphson iteration:
115 //
116 // x1 = x0 * (2 - x0 * b)
117 //
118 // This doubles the number of correct binary digits in the approximation
119 // with each iteration, so after three iterations, we have about 28 binary
120 // digits of accuracy.
121 var correction32: u32 = undefined;
122 correction32 = @truncate(~(@as(u64, recip32) *% q31b >> 32) +% 1);
123 recip32 = @truncate(@as(u64, recip32) *% correction32 >> 31);
124 correction32 = @truncate(~(@as(u64, recip32) *% q31b >> 32) +% 1);
125 recip32 = @truncate(@as(u64, recip32) *% correction32 >> 31);
126 correction32 = @truncate(~(@as(u64, recip32) *% q31b >> 32) +% 1);
127 recip32 = @truncate(@as(u64, recip32) *% correction32 >> 31);
128
129 // recip32 might have overflowed to exactly zero in the preceding
130 // computation if the high word of b is exactly 1.0. This would sabotage
131 // the full-width final stage of the computation that follows, so we adjust
132 // recip32 downward by one bit.
133 recip32 -%= 1;
134
135 // We need to perform one more iteration to get us to 56 binary digits;
136 // The last iteration needs to happen with extra precision.
137 const q63blo: u32 = @truncate(bSignificand << 11);
138 var correction: u64 = undefined;
139 var reciprocal: u64 = undefined;
140 correction = ~(@as(u64, recip32) *% q31b +% (@as(u64, recip32) *% q63blo >> 32)) +% 1;
141 const cHi: u32 = @truncate(correction >> 32);
142 const cLo: u32 = @truncate(correction);
143 reciprocal = @as(u64, recip32) *% cHi +% (@as(u64, recip32) *% cLo >> 32);
144
145 // We already adjusted the 32-bit estimate, now we need to adjust the final
146 // 64-bit reciprocal estimate downward to ensure that it is strictly smaller
147 // than the infinitely precise exact reciprocal. Because the computation
148 // of the Newton-Raphson step is truncating at every step, this adjustment
149 // is small; most of the work is already done.
150 reciprocal -%= 2;
151
152 // The numerical reciprocal is accurate to within 2^-56, lies in the
153 // interval [0.5, 1.0), and is strictly smaller than the true reciprocal
154 // of b. Multiplying a by this reciprocal thus gives a numerical q = a/b
155 // in Q53 with the following properties:
156 //
157 // 1. q < a/b
158 // 2. q is in the interval [0.5, 2.0)
159 // 3. the error in q is bounded away from 2^-53 (actually, we have a
160 // couple of bits to spare, but this is all we need).
161
162 // We need a 64 x 64 multiply high to compute q, which isn't a basic
163 // operation in C, so we need to be a little bit fussy.
164 var quotient: Z = undefined;
165 var quotientLo: Z = undefined;
166 wideMultiply(Z, aSignificand << 2, reciprocal, &quotient, &quotientLo);
167
168 // Two cases: quotient is in [0.5, 1.0) or quotient is in [1.0, 2.0).
169 // In either case, we are going to compute a residual of the form
170 //
171 // r = a - q*b
172 //
173 // We know from the construction of q that r satisfies:
174 //
175 // 0 <= r < ulp(q)*b
176 //
177 // if r is greater than 1/2 ulp(q)*b, then q rounds up. Otherwise, we
178 // already have the correct result. The exact halfway case cannot occur.
179 // We also take this time to right shift quotient if it falls in the [1,2)
180 // range and adjust the exponent accordingly.
181 var residual: Z = undefined;
182 if (quotient < (implicitBit << 1)) {
183 residual = (aSignificand << 53) -% quotient *% bSignificand;
184 quotientExponent -%= 1;
185 } else {
186 quotient >>= 1;
187 residual = (aSignificand << 52) -% quotient *% bSignificand;
188 }
189
190 const writtenExponent = quotientExponent +% exponentBias;
191
192 const round = @intFromBool((residual << 1) >= bSignificand);
193
194 if (writtenExponent >= maxExponent) {
195 // If we have overflowed the exponent, return infinity.
196 return @bitCast(infRep | quotientSign);
197 } else if (writtenExponent < 1) {
198 if (writtenExponent == 0) {
199 var absResult = quotient & significandMask;
200 // Round.
201 absResult += round;
202 if ((absResult & ~significandMask) != 0) {
203 // The rounded result is normal; return it.
204 return @bitCast(absResult | quotientSign);
205 }
206 }
207
208 const roundedQuotient = quotient +% round;
209 const shiftAmount: u32 = @intCast(1 - writtenExponent);
210 if (shiftAmount > significandBits + 1) {
211 return @bitCast(quotientSign);
212 }
213
214 const denormQuotient = roundedQuotient >> @as(std.math.Log2Int(Z), @intCast(shiftAmount));
215 return @bitCast((denormQuotient & significandMask) | quotientSign);
216 } else {
217 // Clear the implicit bit
218 var absResult = quotient & significandMask;
219 // Insert the exponent
220 absResult |= @as(Z, @bitCast(@as(SignedZ, writtenExponent))) << significandBits;
221 // Round
222 absResult +%= round;
223 // Insert the sign and return
224 return @bitCast(absResult | quotientSign);
225 }
226}
227
228test {
229 _ = @import("divdf3_test.zig");
230}