authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-19 14:42:29-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-22 17:46:49-04:00
logd83836825f1d386e0872fd87bacfaa6f545783a9
treede87c9e9fc732e475eb03436be3ff6c2e2a173ad
parent324cbb9864c5dd1e25cf46dba6b1fa863815d399
signaturelock-open Commit is signed but in an unrecognized format.

add mulXf3 to compiler-rt

this adds the following functions to compiler-rt: * `__mulsf3` * `__muldf3` * `__multf3` See #1290

5 files changed, 388 insertions(+), 1 deletions(-)

CMakeLists.txt+1
......@@ -662,6 +662,7 @@ set(ZIG_STD_FILES
662662 "special/compiler_rt/floatuntisf.zig"
663663 "special/compiler_rt/floatuntitf.zig"
664664 "special/compiler_rt/muloti4.zig"
665 "special/compiler_rt/mulXf3.zig"
665666 "special/compiler_rt/multi3.zig"
666667 "special/compiler_rt/popcountdi2.zig"
667668 "special/compiler_rt/truncXfYf2.zig"
std/math.zig+12-1
......@@ -593,7 +593,16 @@ fn testRem() void {
593593
594594/// Returns the absolute value of the integer parameter.
595595/// Result is an unsigned integer.
596pub fn absCast(x: var) @IntType(false, @typeOf(x).bit_count) {
596pub fn absCast(x: var) t: {
597 if (@typeOf(x) == comptime_int) {
598 break :t comptime_int;
599 } else {
600 break :t @IntType(false, @typeOf(x).bit_count);
601 }
602} {
603 if (@typeOf(x) == comptime_int) {
604 return if (x < 0) -x else x;
605 }
597606 const uint = @IntType(false, @typeOf(x).bit_count);
598607 if (x >= 0) return @intCast(uint, x);
599608
......@@ -609,6 +618,8 @@ test "math.absCast" {
609618
610619 testing.expect(absCast(i32(minInt(i32))) == -minInt(i32));
611620 testing.expect(@typeOf(absCast(i32(minInt(i32)))) == u32);
621
622 testing.expect(absCast(-999) == 999);
612623}
613624
614625/// Returns the negation of the integer parameter.
std/special/compiler_rt.zig+4
......@@ -24,6 +24,10 @@ comptime {
2424 @export("__addtf3", @import("compiler_rt/addXf3.zig").__addtf3, linkage);
2525 @export("__subtf3", @import("compiler_rt/addXf3.zig").__subtf3, linkage);
2626
27 @export("__mulsf3", @import("compiler_rt/mulXf3.zig").__mulsf3, linkage);
28 @export("__muldf3", @import("compiler_rt/mulXf3.zig").__muldf3, linkage);
29 @export("__multf3", @import("compiler_rt/mulXf3.zig").__multf3, linkage);
30
2731 @export("__floattitf", @import("compiler_rt/floattitf.zig").__floattitf, linkage);
2832 @export("__floattidf", @import("compiler_rt/floattidf.zig").__floattidf, linkage);
2933 @export("__floattisf", @import("compiler_rt/floattisf.zig").__floattisf, linkage);
std/special/compiler_rt/mulXf3.zig created+285
......@@ -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
5const std = @import("std");
6const builtin = @import("builtin");
7const compiler_rt = @import("../compiler_rt.zig");
8
9pub extern fn __multf3(a: f128, b: f128) f128 {
10 return mulXf3(f128, a, b);
11}
12pub extern fn __muldf3(a: f64, b: f64) f64 {
13 return mulXf3(f64, a, b);
14}
15pub extern fn __mulsf3(a: f32, b: f32) f32 {
16 return mulXf3(f32, a, b);
17}
18
19fn 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
147fn 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
255fn 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
265fn 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
283test "import mulXf3" {
284 _ = @import("mulXf3_test.zig");
285}
std/special/compiler_rt/mulXf3_test.zig created+86
......@@ -0,0 +1,86 @@
1// Ported from:
2//
3// https://github.com/llvm/llvm-project/blob/2ffb1b0413efa9a24eb3c49e710e36f92e2cb50b/compiler-rt/test/builtins/Unit/multf3_test.c
4
5const qnan128 = @bitCast(f128, u128(0x7fff800000000000) << 64);
6const inf128 = @bitCast(f128, u128(0x7fff000000000000) << 64);
7
8const __multf3 = @import("mulXf3.zig").__multf3;
9
10// return true if equal
11// use two 64-bit integers intead of one 128-bit integer
12// because 128-bit integer constant can't be assigned directly
13fn compareResultLD(result: f128, expectedHi: u64, expectedLo: u64) bool {
14 const rep = @bitCast(u128, result);
15 const hi = @intCast(u64, rep >> 64);
16 const lo = @truncate(u64, rep);
17
18 if (hi == expectedHi and lo == expectedLo) {
19 return true;
20 }
21 // test other possible NaN representation(signal NaN)
22 if (expectedHi == 0x7fff800000000000 and expectedLo == 0x0) {
23 if ((hi & 0x7fff000000000000) == 0x7fff000000000000 and
24 ((hi & 0xffffffffffff) > 0 or lo > 0))
25 {
26 return true;
27 }
28 }
29 return false;
30}
31
32fn test__multf3(a: f128, b: f128, expected_hi: u64, expected_lo: u64) void {
33 const x = __multf3(a, b);
34
35 if (compareResultLD(x, expected_hi, expected_lo))
36 return;
37
38 @panic("__multf3 test failure");
39}
40
41fn makeNaN128(rand: u64) f128 {
42 const int_result = u128(0x7fff000000000000 | (rand & 0xffffffffffff)) << 64;
43 const float_result = @bitCast(f128, int_result);
44 return float_result;
45}
46test "multf3" {
47 // qNaN * any = qNaN
48 test__multf3(qnan128, 0x1.23456789abcdefp+5, 0x7fff800000000000, 0x0);
49
50 // NaN * any = NaN
51 const a = makeNaN128(0x800030000000);
52 test__multf3(a, 0x1.23456789abcdefp+5, 0x7fff800000000000, 0x0);
53 // inf * any = inf
54 test__multf3(inf128, 0x1.23456789abcdefp+5, 0x7fff000000000000, 0x0);
55
56 // any * any
57 test__multf3(
58 @bitCast(f128, u128(0x40042eab345678439abcdefea5678234)),
59 @bitCast(f128, u128(0x3ffeedcb34a235253948765432134675)),
60 0x400423e7f9e3c9fc,
61 0xd906c2c2a85777c4,
62 );
63
64 test__multf3(
65 @bitCast(f128, u128(0x3fcd353e45674d89abacc3a2ebf3ff50)),
66 @bitCast(f128, u128(0x3ff6ed8764648369535adf4be3214568)),
67 0x3fc52a163c6223fc,
68 0xc94c4bf0430768b4,
69 );
70
71 test__multf3(
72 0x1.234425696abcad34a35eeffefdcbap+456,
73 0x451.ed98d76e5d46e5f24323dff21ffp+600,
74 0x44293a91de5e0e94,
75 0xe8ed17cc2cdf64ac,
76 );
77
78 test__multf3(
79 @bitCast(f128, u128(0x3f154356473c82a9fabf2d22ace345df)),
80 @bitCast(f128, u128(0x3e38eda98765476743ab21da23d45679)),
81 0x3d4f37c1a3137cae,
82 0xfc6807048bc2836a,
83 );
84
85 test__multf3(0x1.23456734245345p-10000, 0x1.edcba524498724p-6497, 0x0, 0x0);
86}