master
1const std = @import("std");
2const math = std.math;
3const builtin = @import("builtin");
4const common = @import("./common.zig");
5
6/// Ported from:
7/// https://github.com/llvm/llvm-project/blob/2ffb1b0413efa9a24eb3c49e710e36f92e2cb50b/compiler-rt/lib/builtins/fp_mul_impl.inc
8pub inline fn mulf3(comptime T: type, a: T, b: T) T {
9 @setRuntimeSafety(common.test_safety);
10 const typeWidth = @typeInfo(T).float.bits;
11 const significandBits = math.floatMantissaBits(T);
12 const fractionalBits = math.floatFractionalBits(T);
13 const exponentBits = math.floatExponentBits(T);
14
15 const Z = std.meta.Int(.unsigned, typeWidth);
16
17 // ZSignificand is large enough to contain the significand, including an explicit integer bit
18 const ZSignificand = PowerOfTwoSignificandZ(T);
19 const ZSignificandBits = @typeInfo(ZSignificand).int.bits;
20
21 const roundBit = (1 << (ZSignificandBits - 1));
22 const signBit = (@as(Z, 1) << (significandBits + exponentBits));
23 const maxExponent = ((1 << exponentBits) - 1);
24 const exponentBias = (maxExponent >> 1);
25
26 const integerBit = (@as(ZSignificand, 1) << fractionalBits);
27 const quietBit = integerBit >> 1;
28 const significandMask = (@as(Z, 1) << significandBits) - 1;
29
30 const absMask = signBit - 1;
31 const qnanRep = @as(Z, @bitCast(math.nan(T))) | quietBit;
32 const infRep: Z = @bitCast(math.inf(T));
33 const minNormalRep: Z = @bitCast(math.floatMin(T));
34
35 const ZExp = if (typeWidth >= 32) u32 else Z;
36 const aExponent: ZExp = @truncate((@as(Z, @bitCast(a)) >> significandBits) & maxExponent);
37 const bExponent: ZExp = @truncate((@as(Z, @bitCast(b)) >> significandBits) & maxExponent);
38 const productSign: Z = (@as(Z, @bitCast(a)) ^ @as(Z, @bitCast(b))) & signBit;
39
40 var aSignificand: ZSignificand = @intCast(@as(Z, @bitCast(a)) & significandMask);
41 var bSignificand: ZSignificand = @intCast(@as(Z, @bitCast(b)) & significandMask);
42 var scale: i32 = 0;
43
44 // Detect if a or b is zero, denormal, infinity, or NaN.
45 if (aExponent -% 1 >= maxExponent - 1 or bExponent -% 1 >= maxExponent - 1) {
46 const aAbs: Z = @as(Z, @bitCast(a)) & absMask;
47 const bAbs: Z = @as(Z, @bitCast(b)) & absMask;
48
49 // NaN * anything = qNaN
50 if (aAbs > infRep) return @bitCast(@as(Z, @bitCast(a)) | quietBit);
51 // anything * NaN = qNaN
52 if (bAbs > infRep) return @bitCast(@as(Z, @bitCast(b)) | quietBit);
53
54 if (aAbs == infRep) {
55 // infinity * non-zero = +/- infinity
56 if (bAbs != 0) {
57 return @bitCast(aAbs | productSign);
58 } else {
59 // infinity * zero = NaN
60 return @bitCast(qnanRep);
61 }
62 }
63
64 if (bAbs == infRep) {
65 //? non-zero * infinity = +/- infinity
66 if (aAbs != 0) {
67 return @bitCast(bAbs | productSign);
68 } else {
69 // zero * infinity = NaN
70 return @bitCast(qnanRep);
71 }
72 }
73
74 // zero * anything = +/- zero
75 if (aAbs == 0) return @bitCast(productSign);
76 // anything * zero = +/- zero
77 if (bAbs == 0) return @bitCast(productSign);
78
79 // one or both of a or b is denormal, the other (if applicable) is a
80 // normal number. Renormalize one or both of a and b, and set scale to
81 // include the necessary exponent adjustment.
82 if (aAbs < minNormalRep) scale += normalize(T, &aSignificand);
83 if (bAbs < minNormalRep) scale += normalize(T, &bSignificand);
84 }
85
86 // Or in the implicit significand bit. (If we fell through from the
87 // denormal path it was already set by normalize( ), but setting it twice
88 // won't hurt anything.)
89 aSignificand |= integerBit;
90 bSignificand |= integerBit;
91
92 // Get the significand of a*b. Before multiplying the significands, shift
93 // one of them left to left-align it in the field. Thus, the product will
94 // have (exponentBits + 2) integral digits, all but two of which must be
95 // zero. Normalizing this result is just a conditional left-shift by one
96 // and bumping the exponent accordingly.
97 var productHi: ZSignificand = undefined;
98 var productLo: ZSignificand = undefined;
99 const left_align_shift = ZSignificandBits - fractionalBits - 1;
100 common.wideMultiply(ZSignificand, aSignificand, bSignificand << left_align_shift, &productHi, &productLo);
101
102 var productExponent: i32 = @as(i32, @intCast(aExponent + bExponent)) - exponentBias + scale;
103
104 // Normalize the significand, adjust exponent if needed.
105 if ((productHi & integerBit) != 0) {
106 productExponent +%= 1;
107 } else {
108 productHi = (productHi << 1) | (productLo >> (ZSignificandBits - 1));
109 productLo = productLo << 1;
110 }
111
112 // If we have overflowed the type, return +/- infinity.
113 if (productExponent >= maxExponent) return @bitCast(infRep | productSign);
114
115 var result: Z = undefined;
116 if (productExponent <= 0) {
117 // Result is denormal before rounding
118 //
119 // If the result is so small that it just underflows to zero, return
120 // a zero of the appropriate sign. Mathematically there is no need to
121 // handle this case separately, but we make it a special case to
122 // simplify the shift logic.
123 const shift: u32 = @truncate(@as(Z, 1) -% @as(u32, @bitCast(productExponent)));
124 if (shift >= ZSignificandBits) return @bitCast(productSign);
125
126 // Otherwise, shift the significand of the result so that the round
127 // bit is the high bit of productLo.
128 const sticky = wideShrWithTruncation(ZSignificand, &productHi, &productLo, shift);
129 productLo |= @intFromBool(sticky);
130 result = productHi;
131
132 // We include the integer bit so that rounding will carry to the exponent,
133 // but it will be removed later if the result is still denormal
134 if (significandBits != fractionalBits) result |= integerBit;
135 } else {
136 // Result is normal before rounding; insert the exponent.
137 result = productHi & significandMask;
138 result |= @as(Z, @intCast(productExponent)) << significandBits;
139 }
140
141 // Final rounding. The final result may overflow to infinity, or underflow
142 // to zero, but those are the correct results in those cases. We use the
143 // default IEEE-754 round-to-nearest, ties-to-even rounding mode.
144 if (productLo > roundBit) result +%= 1;
145 if (productLo == roundBit) result +%= result & 1;
146
147 // Restore any explicit integer bit, if it was rounded off
148 if (significandBits != fractionalBits) {
149 if ((result >> significandBits) != 0) {
150 result |= integerBit;
151 } else {
152 result &= ~integerBit;
153 }
154 }
155
156 // Insert the sign of the result:
157 result |= productSign;
158
159 return @bitCast(result);
160}
161
162/// Returns `true` if the right shift is inexact (i.e. any bit shifted out is non-zero)
163///
164/// This is analogous to an shr version of `@shlWithOverflow`
165fn wideShrWithTruncation(comptime Z: type, hi: *Z, lo: *Z, count: u32) bool {
166 @setRuntimeSafety(common.test_safety);
167 const typeWidth = @typeInfo(Z).int.bits;
168 var inexact = false;
169 if (count < typeWidth) {
170 inexact = (lo.* << @intCast(typeWidth -% count)) != 0;
171 lo.* = (hi.* << @intCast(typeWidth -% count)) | (lo.* >> @intCast(count));
172 hi.* = hi.* >> @intCast(count);
173 } else if (count < 2 * typeWidth) {
174 inexact = (hi.* << @intCast(2 * typeWidth -% count) | lo.*) != 0;
175 lo.* = hi.* >> @intCast(count -% typeWidth);
176 hi.* = 0;
177 } else {
178 inexact = (hi.* | lo.*) != 0;
179 lo.* = 0;
180 hi.* = 0;
181 }
182 return inexact;
183}
184
185fn normalize(comptime T: type, significand: *PowerOfTwoSignificandZ(T)) i32 {
186 const Z = PowerOfTwoSignificandZ(T);
187 const integerBit = @as(Z, 1) << math.floatFractionalBits(T);
188
189 const shift = @clz(significand.*) - @clz(integerBit);
190 significand.* <<= @intCast(shift);
191 return @as(i32, 1) - shift;
192}
193
194/// Returns a power-of-two integer type that is large enough to contain
195/// the significand of T, including an explicit integer bit
196fn PowerOfTwoSignificandZ(comptime T: type) type {
197 const bits = math.ceilPowerOfTwoAssert(u16, math.floatFractionalBits(T) + 1);
198 return std.meta.Int(.unsigned, bits);
199}
200
201test {
202 _ = @import("mulf3_test.zig");
203}