master
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 builtin = @import("builtin");
7const arch = builtin.cpu.arch;
8const common = @import("common.zig");
9
10const normalize = common.normalize;
11const wideMultiply = common.wideMultiply;
12
13pub const panic = common.panic;
14
15comptime {
16 if (common.want_aeabi) {
17 @export(&__aeabi_ddiv, .{ .name = "__aeabi_ddiv", .linkage = common.linkage, .visibility = common.visibility });
18 } else {
19 @export(&__divdf3, .{ .name = "__divdf3", .linkage = common.linkage, .visibility = common.visibility });
20 }
21}
22
23pub fn __divdf3(a: f64, b: f64) callconv(.c) f64 {
24 return div(a, b);
25}
26
27fn __aeabi_ddiv(a: f64, b: f64) callconv(.{ .arm_aapcs = .{} }) f64 {
28 return div(a, b);
29}
30
31inline fn div(a: f64, b: f64) f64 {
32 const Z = std.meta.Int(.unsigned, 64);
33 const SignedZ = std.meta.Int(.signed, 64);
34
35 const significandBits = std.math.floatMantissaBits(f64);
36 const exponentBits = std.math.floatExponentBits(f64);
37
38 const signBit = (@as(Z, 1) << (significandBits + exponentBits));
39 const maxExponent = ((1 << exponentBits) - 1);
40 const exponentBias = (maxExponent >> 1);
41
42 const implicitBit = (@as(Z, 1) << significandBits);
43 const quietBit = implicitBit >> 1;
44 const significandMask = implicitBit - 1;
45
46 const absMask = signBit - 1;
47 const exponentMask = absMask ^ significandMask;
48 const qnanRep = exponentMask | quietBit;
49 const infRep = @as(Z, @bitCast(std.math.inf(f64)));
50
51 const aExponent: u32 = @truncate((@as(Z, @bitCast(a)) >> significandBits) & maxExponent);
52 const bExponent: u32 = @truncate((@as(Z, @bitCast(b)) >> significandBits) & maxExponent);
53 const quotientSign: Z = (@as(Z, @bitCast(a)) ^ @as(Z, @bitCast(b))) & signBit;
54
55 var aSignificand: Z = @as(Z, @bitCast(a)) & significandMask;
56 var bSignificand: Z = @as(Z, @bitCast(b)) & significandMask;
57 var scale: i32 = 0;
58
59 // Detect if a or b is zero, denormal, infinity, or NaN.
60 if (aExponent -% 1 >= maxExponent - 1 or bExponent -% 1 >= maxExponent - 1) {
61 const aAbs: Z = @as(Z, @bitCast(a)) & absMask;
62 const bAbs: Z = @as(Z, @bitCast(b)) & absMask;
63
64 // NaN / anything = qNaN
65 if (aAbs > infRep) return @bitCast(@as(Z, @bitCast(a)) | quietBit);
66 // anything / NaN = qNaN
67 if (bAbs > infRep) return @bitCast(@as(Z, @bitCast(b)) | quietBit);
68
69 if (aAbs == infRep) {
70 // infinity / infinity = NaN
71 if (bAbs == infRep) {
72 return @bitCast(qnanRep);
73 }
74 // infinity / anything else = +/- infinity
75 else {
76 return @bitCast(aAbs | quotientSign);
77 }
78 }
79
80 // anything else / infinity = +/- 0
81 if (bAbs == infRep) return @bitCast(quotientSign);
82
83 if (aAbs == 0) {
84 // zero / zero = NaN
85 if (bAbs == 0) {
86 return @bitCast(qnanRep);
87 }
88 // zero / anything else = +/- zero
89 else {
90 return @bitCast(quotientSign);
91 }
92 }
93 // anything else / zero = +/- infinity
94 if (bAbs == 0) return @bitCast(infRep | quotientSign);
95
96 // one or both of a or b is denormal, the other (if applicable) is a
97 // normal number. Renormalize one or both of a and b, and set scale to
98 // include the necessary exponent adjustment.
99 if (aAbs < implicitBit) scale +%= normalize(f64, &aSignificand);
100 if (bAbs < implicitBit) scale -%= normalize(f64, &bSignificand);
101 }
102
103 // Or in the implicit significand bit. (If we fell through from the
104 // denormal path it was already set by normalize( ), but setting it twice
105 // won't hurt anything.)
106 aSignificand |= implicitBit;
107 bSignificand |= implicitBit;
108 var quotientExponent: i32 = @as(i32, @bitCast(aExponent -% bExponent)) +% scale;
109
110 // Align the significand of b as a Q31 fixed-point number in the range
111 // [1, 2.0) and get a Q32 approximate reciprocal using a small minimax
112 // polynomial approximation: reciprocal = 3/4 + 1/sqrt(2) - b/2. This
113 // is accurate to about 3.5 binary digits.
114 const q31b: u32 = @truncate(bSignificand >> 21);
115 var recip32 = @as(u32, 0x7504f333) -% q31b;
116
117 // Now refine the reciprocal estimate using a Newton-Raphson iteration:
118 //
119 // x1 = x0 * (2 - x0 * b)
120 //
121 // This doubles the number of correct binary digits in the approximation
122 // with each iteration, so after three iterations, we have about 28 binary
123 // digits of accuracy.
124 var correction32: u32 = undefined;
125 correction32 = @truncate(~(@as(u64, recip32) *% q31b >> 32) +% 1);
126 recip32 = @truncate(@as(u64, recip32) *% correction32 >> 31);
127 correction32 = @truncate(~(@as(u64, recip32) *% q31b >> 32) +% 1);
128 recip32 = @truncate(@as(u64, recip32) *% correction32 >> 31);
129 correction32 = @truncate(~(@as(u64, recip32) *% q31b >> 32) +% 1);
130 recip32 = @truncate(@as(u64, recip32) *% correction32 >> 31);
131
132 // recip32 might have overflowed to exactly zero in the preceding
133 // computation if the high word of b is exactly 1.0. This would sabotage
134 // the full-width final stage of the computation that follows, so we adjust
135 // recip32 downward by one bit.
136 recip32 -%= 1;
137
138 // We need to perform one more iteration to get us to 56 binary digits;
139 // The last iteration needs to happen with extra precision.
140 const q63blo: u32 = @truncate(bSignificand << 11);
141 var correction: u64 = undefined;
142 var reciprocal: u64 = undefined;
143 correction = ~(@as(u64, recip32) *% q31b +% (@as(u64, recip32) *% q63blo >> 32)) +% 1;
144 const cHi: u32 = @truncate(correction >> 32);
145 const cLo: u32 = @truncate(correction);
146 reciprocal = @as(u64, recip32) *% cHi +% (@as(u64, recip32) *% cLo >> 32);
147
148 // We already adjusted the 32-bit estimate, now we need to adjust the final
149 // 64-bit reciprocal estimate downward to ensure that it is strictly smaller
150 // than the infinitely precise exact reciprocal. Because the computation
151 // of the Newton-Raphson step is truncating at every step, this adjustment
152 // is small; most of the work is already done.
153 reciprocal -%= 2;
154
155 // The numerical reciprocal is accurate to within 2^-56, lies in the
156 // interval [0.5, 1.0), and is strictly smaller than the true reciprocal
157 // of b. Multiplying a by this reciprocal thus gives a numerical q = a/b
158 // in Q53 with the following properties:
159 //
160 // 1. q < a/b
161 // 2. q is in the interval [0.5, 2.0)
162 // 3. the error in q is bounded away from 2^-53 (actually, we have a
163 // couple of bits to spare, but this is all we need).
164
165 // We need a 64 x 64 multiply high to compute q, which isn't a basic
166 // operation in C, so we need to be a little bit fussy.
167 var quotient: Z = undefined;
168 var quotientLo: Z = undefined;
169 wideMultiply(Z, aSignificand << 2, reciprocal, "ient, "ientLo);
170
171 // Two cases: quotient is in [0.5, 1.0) or quotient is in [1.0, 2.0).
172 // In either case, we are going to compute a residual of the form
173 //
174 // r = a - q*b
175 //
176 // We know from the construction of q that r satisfies:
177 //
178 // 0 <= r < ulp(q)*b
179 //
180 // if r is greater than 1/2 ulp(q)*b, then q rounds up. Otherwise, we
181 // already have the correct result. The exact halfway case cannot occur.
182 // We also take this time to right shift quotient if it falls in the [1,2)
183 // range and adjust the exponent accordingly.
184 var residual: Z = undefined;
185 if (quotient < (implicitBit << 1)) {
186 residual = (aSignificand << 53) -% quotient *% bSignificand;
187 quotientExponent -%= 1;
188 } else {
189 quotient >>= 1;
190 residual = (aSignificand << 52) -% quotient *% bSignificand;
191 }
192
193 const writtenExponent = quotientExponent +% exponentBias;
194
195 if (writtenExponent >= maxExponent) {
196 // If we have overflowed the exponent, return infinity.
197 return @bitCast(infRep | quotientSign);
198 } else if (writtenExponent < 1) {
199 if (writtenExponent == 0) {
200 // Check whether the rounded result is normal.
201 const round = @intFromBool((residual << 1) > bSignificand);
202 // Clear the implicit bit.
203 var absResult = quotient & significandMask;
204 // Round.
205 absResult += round;
206 if ((absResult & ~significandMask) != 0) {
207 // The rounded result is normal; return it.
208 return @bitCast(absResult | quotientSign);
209 }
210 }
211 // Flush denormals to zero. In the future, it would be nice to add
212 // code to round them correctly.
213 return @bitCast(quotientSign);
214 } else {
215 const round = @intFromBool((residual << 1) > bSignificand);
216 // Clear the implicit bit
217 var absResult = quotient & significandMask;
218 // Insert the exponent
219 absResult |= @as(Z, @bitCast(@as(SignedZ, writtenExponent))) << significandBits;
220 // Round
221 absResult +%= round;
222 // Insert the sign and return
223 return @bitCast(absResult | quotientSign);
224 }
225}
226
227test {
228 _ = @import("divdf3_test.zig");
229}