authorgravatar for justin.b.alexander1@gmail.comvegecode <justin.b.alexander1@gmail.com> 2019-04-03 13:12:31-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-04 15:38:09-04:00
log12c4ab39275553e8bdd8e451141b60149b314aa7
treea84b64dc428da0e0316be65d6f8473784947392c
parent7dd1e0fc2bc15cedde0944441757c59ead93830c

Add divsf3 to compiler rt


4 files changed, 239 insertions(+), 0 deletions(-)

CMakeLists.txt+1
...@@ -631,6 +631,7 @@ set(ZIG_STD_FILES...@@ -631,6 +631,7 @@ set(ZIG_STD_FILES
631 "special/compiler_rt/aulldiv.zig"631 "special/compiler_rt/aulldiv.zig"
632 "special/compiler_rt/aullrem.zig"632 "special/compiler_rt/aullrem.zig"
633 "special/compiler_rt/comparetf2.zig"633 "special/compiler_rt/comparetf2.zig"
634 "special/compiler_rt/divsf3.zig"
634 "special/compiler_rt/divti3.zig"635 "special/compiler_rt/divti3.zig"
635 "special/compiler_rt/extendXfYf2.zig"636 "special/compiler_rt/extendXfYf2.zig"
636 "special/compiler_rt/fixdfdi.zig"637 "special/compiler_rt/fixdfdi.zig"
std/special/compiler_rt.zig+4
...@@ -32,6 +32,8 @@ comptime {...@@ -32,6 +32,8 @@ comptime {
32 @export("__muldf3", @import("compiler_rt/mulXf3.zig").__muldf3, linkage);32 @export("__muldf3", @import("compiler_rt/mulXf3.zig").__muldf3, linkage);
33 @export("__multf3", @import("compiler_rt/mulXf3.zig").__multf3, linkage);33 @export("__multf3", @import("compiler_rt/mulXf3.zig").__multf3, linkage);
3434
35 @export("__divsf3", @import("compiler_rt/divsf3.zig").__divsf3, linkage);
36
35 @export("__floattitf", @import("compiler_rt/floattitf.zig").__floattitf, linkage);37 @export("__floattitf", @import("compiler_rt/floattitf.zig").__floattitf, linkage);
36 @export("__floattidf", @import("compiler_rt/floattidf.zig").__floattidf, linkage);38 @export("__floattidf", @import("compiler_rt/floattidf.zig").__floattidf, linkage);
37 @export("__floattisf", @import("compiler_rt/floattisf.zig").__floattisf, linkage);39 @export("__floattisf", @import("compiler_rt/floattisf.zig").__floattisf, linkage);
...@@ -138,6 +140,8 @@ comptime {...@@ -138,6 +140,8 @@ comptime {
138140
139 @export("__aeabi_f2iz", @import("compiler_rt/fixsfsi.zig").__fixsfsi, linkage);141 @export("__aeabi_f2iz", @import("compiler_rt/fixsfsi.zig").__fixsfsi, linkage);
140 @export("__aeabi_d2iz", @import("compiler_rt/fixdfsi.zig").__fixdfsi, linkage);142 @export("__aeabi_d2iz", @import("compiler_rt/fixdfsi.zig").__fixdfsi, linkage);
143
144 @export("__aeabi_fdiv", @import("compiler_rt/divsf3.zig").__divsf3, linkage);
141 }145 }
142 if (builtin.os == builtin.Os.windows) {146 if (builtin.os == builtin.Os.windows) {
143 switch (builtin.arch) {147 switch (builtin.arch) {
std/special/compiler_rt/divsf3.zig created+200
...@@ -0,0 +1,200 @@
1// Ported from:
2//
3// https://github.com/llvm/llvm-project/commit/d674d96bc56c0f377879d01c9d8dfdaaa7859cdb/compiler-rt/lib/builtins/divsf3.c
4
5const std = @import("std");
6
7pub extern fn __divsf3(a: f32, b: f32) f32 {
8 const Z = @IntType(false, f32.bit_count);
9
10 const typeWidth = f32.bit_count;
11 const significandBits = std.math.floatMantissaBits(f32);
12 const exponentBits = std.math.floatExponentBits(f32);
13
14 const signBit = (Z(1) << (significandBits + exponentBits));
15 const maxExponent = ((1 << exponentBits) - 1);
16 const exponentBias = (maxExponent >> 1);
17
18 const implicitBit = (Z(1) << significandBits);
19 const quietBit = implicitBit >> 1;
20 const significandMask = implicitBit - 1;
21
22 const absMask = signBit - 1;
23 const exponentMask = absMask ^ significandMask;
24 const qnanRep = exponentMask | quietBit;
25 const infRep = @bitCast(Z, std.math.inf(f32));
26
27 const aExponent = @truncate(u32, (@bitCast(Z, a) >> significandBits) & maxExponent);
28 const bExponent = @truncate(u32, (@bitCast(Z, b) >> significandBits) & maxExponent);
29 const quotientSign: Z = (@bitCast(Z, a) ^ @bitCast(Z, b)) & signBit;
30
31 var aSignificand: Z = @bitCast(Z, a) & significandMask;
32 var bSignificand: Z = @bitCast(Z, b) & significandMask;
33 var scale: i32 = 0;
34
35 // Detect if a or b is zero, denormal, infinity, or NaN.
36 if (aExponent -% 1 >= maxExponent -% 1 or bExponent -% 1 >= maxExponent -% 1) {
37 const aAbs: Z = @bitCast(Z, a) & absMask;
38 const bAbs: Z = @bitCast(Z, b) & absMask;
39
40 // NaN * anything = qNaN
41 if (aAbs > infRep) return @bitCast(f32, @bitCast(Z, a) | quietBit);
42 // anything * NaN = qNaN
43 if (bAbs > infRep) return @bitCast(f32, @bitCast(Z, b) | quietBit);
44
45 if (aAbs == infRep) {
46 // infinity * non-zero = +/- infinity
47 if (bAbs != 0) {
48 return @bitCast(f32, aAbs | quotientSign);
49 } else {
50 // infinity * zero = NaN
51 return @bitCast(f32, qnanRep);
52 }
53 }
54
55 if (bAbs == infRep) {
56 //? non-zero * infinity = +/- infinity
57 if (aAbs != 0) {
58 return @bitCast(f32, bAbs | quotientSign);
59 } else {
60 // zero * infinity = NaN
61 return @bitCast(f32, qnanRep);
62 }
63 }
64
65 // zero * anything = +/- zero
66 if (aAbs == 0) return @bitCast(f32, quotientSign);
67 // anything * zero = +/- zero
68 if (bAbs == 0) return @bitCast(f32, quotientSign);
69
70 // one or both of a or b is denormal, the other (if applicable) is a
71 // normal number. Renormalize one or both of a and b, and set scale to
72 // include the necessary exponent adjustment.
73 if (aAbs < implicitBit) scale +%= normalize(f32, &aSignificand);
74 if (bAbs < implicitBit) scale +%= normalize(f32, &bSignificand);
75 }
76
77 // Or in the implicit significand bit. (If we fell through from the
78 // denormal path it was already set by normalize( ), but setting it twice
79 // won't hurt anything.)
80 aSignificand |= implicitBit;
81 bSignificand |= implicitBit;
82 var quotientExponent: i32 = @bitCast(i32, aExponent -% bExponent) +% scale;
83
84 // Align the significand of b as a Q31 fixed-point number in the range
85 // [1, 2.0) and get a Q32 approximate reciprocal using a small minimax
86 // polynomial approximation: reciprocal = 3/4 + 1/sqrt(2) - b/2. This
87 // is accurate to about 3.5 binary digits.
88 const q31b = switch (f32) {
89 f32 => bSignificand << 8,
90 f64 => bSignificand >> 21,
91 else => @compileError("Type not implemented."),
92 };
93 var reciprocal = u32(0x7504f333) -% q31b;
94
95 // Now refine the reciprocal estimate using a Newton-Raphson iteration:
96 //
97 // x1 = x0 * (2 - x0 * b)
98 //
99 // This doubles the number of correct binary digits in the approximation
100 // with each iteration, so after three iterations, we have about 28 binary
101 // digits of accuracy.
102 var correction: u32 = undefined;
103 correction = @truncate(u32, ~(u64(reciprocal) *% q31b >> 32) +% 1);
104 reciprocal = @truncate(u32, u64(reciprocal) *% correction >> 31);
105 correction = @truncate(u32, ~(u64(reciprocal) *% q31b >> 32) +% 1);
106 reciprocal = @truncate(u32, u64(reciprocal) *% correction >> 31);
107 correction = @truncate(u32, ~(u64(reciprocal) *% q31b >> 32) +% 1);
108 reciprocal = @truncate(u32, u64(reciprocal) *% correction >> 31);
109
110 // Exhaustive testing shows that the error in reciprocal after three steps
111 // is in the interval [-0x1.f58108p-31, 0x1.d0e48cp-29], in line with our
112 // expectations. We bump the reciprocal by a tiny value to force the error
113 // to be strictly positive (in the range [0x1.4fdfp-37,0x1.287246p-29], to
114 // be specific). This also causes 1/1 to give a sensible approximation
115 // instead of zero (due to overflow).
116 reciprocal -%= 2;
117
118 // The numerical reciprocal is accurate to within 2^-28, lies in the
119 // interval [0x1.000000eep-1, 0x1.fffffffcp-1], and is strictly smaller
120 // than the true reciprocal of b. Multiplying a by this reciprocal thus
121 // gives a numerical q = a/b in Q24 with the following properties:
122 //
123 // 1. q < a/b
124 // 2. q is in the interval [0x1.000000eep-1, 0x1.fffffffcp0)
125 // 3. the error in q is at most 2^-24 + 2^-27 -- the 2^24 term comes
126 // from the fact that we truncate the product, and the 2^27 term
127 // is the error in the reciprocal of b scaled by the maximum
128 // possible value of a. As a consequence of this error bound,
129 // either q or nextafter(q) is the correctly rounded
130 var quotient: Z = @truncate(u32, u64(reciprocal) *% (aSignificand << 1) >> 32);
131
132 // Two cases: quotient is in [0.5, 1.0) or quotient is in [1.0, 2.0).
133 // In either case, we are going to compute a residual of the form
134 //
135 // r = a - q*b
136 //
137 // We know from the construction of q that r satisfies:
138 //
139 // 0 <= r < ulp(q)*b
140 //
141 // if r is greater than 1/2 ulp(q)*b, then q rounds up. Otherwise, we
142 // already have the correct result. The exact halfway case cannot occur.
143 // We also take this time to right shift quotient if it falls in the [1,2)
144 // range and adjust the exponent accordingly.
145 var residual: Z = undefined;
146 if (quotient < (implicitBit << 1)) {
147 residual = (aSignificand << 24) -% quotient *% bSignificand;
148 quotientExponent -%= 1;
149 } else {
150 quotient >>= 1;
151 residual = (aSignificand << 23) -% quotient *% bSignificand;
152 }
153
154 const writtenExponent = quotientExponent +% exponentBias;
155
156 if (writtenExponent >= maxExponent) {
157 // If we have overflowed the exponent, return infinity.
158 return @bitCast(f32, infRep | quotientSign);
159 } else if (writtenExponent < 1) {
160 if (writtenExponent == 0) {
161 // Check whether the rounded result is normal.
162 const round = @boolToInt((residual << 1) > bSignificand);
163 // Clear the implicit bit.
164 var absResult = quotient & significandMask;
165 // Round.
166 absResult += round;
167 if ((absResult & ~significandMask) > 0) {
168 // The rounded result is normal; return it.
169 return @bitCast(f32, absResult | quotientSign);
170 }
171 }
172 // Flush denormals to zero. In the future, it would be nice to add
173 // code to round them correctly.
174 return @bitCast(f32, quotientSign);
175 } else {
176 const round = @boolToInt((residual << 1) > bSignificand);
177 // Clear the implicit bit
178 var absResult = quotient & significandMask;
179 // Insert the exponent
180 absResult |= @bitCast(Z, writtenExponent) << significandBits;
181 // Round
182 absResult +%= round;
183 // Insert the sign and return
184 return @bitCast(f32, absResult | quotientSign);
185 }
186}
187
188fn normalize(comptime T: type, significand: *@IntType(false, T.bit_count)) i32 {
189 const Z = @IntType(false, T.bit_count);
190 const significandBits = std.math.floatMantissaBits(T);
191 const implicitBit = Z(1) << significandBits;
192
193 const shift = @clz(significand.*) - @clz(implicitBit);
194 significand.* <<= @intCast(std.math.Log2Int(Z), shift);
195 return 1 - shift;
196}
197
198test "import divsf3" {
199 _ = @import("divsf3_test.zig");
200}
std/special/compiler_rt/divsf3_test.zig created+34
...@@ -0,0 +1,34 @@
1// Ported from:
2//
3// https://github.com/llvm/llvm-project/commit/d674d96bc56c0f377879d01c9d8dfdaaa7859cdb/compiler-rt/test/builtins/Unit/divsf3_test.c
4
5const __divsf3 = @import("divsf3.zig").__divsf3;
6const testing = @import("std").testing;
7
8fn compareResultF(result: f32, expected: u32) bool {
9 const rep = @bitCast(u32, result);
10
11 if (rep == expected) {
12 return true;
13 }
14 // test other possible NaN representation(signal NaN)
15 else if (expected == 0x7fc00000) {
16 if ((rep & 0x7f800000) == 0x7f800000 and
17 (rep & 0x7fffff) > 0)
18 {
19 return true;
20 }
21 }
22 return false;
23}
24
25fn test__divsf3(a: f32, b: f32, expected: u32) void {
26 const x = __divsf3(a, b);
27 const ret = compareResultF(x, expected);
28 testing.expect(ret == true);
29}
30
31test "divsf3" {
32 test__divsf3(1.0, 3.0, 0x3EAAAAAB);
33 test__divsf3(2.3509887e-38, 2.0, 0x00800000);
34}