mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 16:47:28 +01:00
std/softfloat: add fast paths for f32 and f64 add, sub and mul
f32_add, f32_sub and f32_mul now compute the result directly in binary32 when both operands are normal and the result is normal (or overflows), instead of always going through f64. f64_add and f64_sub get a similar fast path for normal operands. Every other case takes the previous code path, so results (including NaN payloads) are unchanged. Add script/manual-test-softfloat-sweep.c, which checks the f32 and f64 add, sub, mul and div operations over every pair of operand exponents with many mantissa patterns, and the f32 conversions, against the hardware and (bit-for-bit, NaNs included) the via-f64 definitions.
This commit is contained in:
5 files changed
+1474
-18
No files matched your search
+47
-6
@@ -96,12 +96,24 @@ integer long division, 11 quotient bits per (64-bit integer) division step,
|
||||
with the final remainder as the sticky bit. Leading zero counts use a 256-entry
|
||||
table.
|
||||
|
||||
The `f32` add, sub, mul and div operations convert their operands exactly to
|
||||
`f64`, perform the `f64` operation and then round to `f32`. This double
|
||||
rounding is innocuous: binary64's 53 bits of precision are more than `(2 * 24)
|
||||
+ 2`, which suffices for `+`, `-`, `*` and `/` (S. A. Figueroa, "When is double
|
||||
rounding innocuous?", ACM SIGNUM Newsletter, 1995). The tests confirm this
|
||||
against hardware, including for subnormal `f32` results.
|
||||
`f64_add` and `f64_sub` have a fast path for the common case where both
|
||||
operands are normal (`f64_add_normal_mags` and `f64_sub_normal_mags`): it
|
||||
skips the special-case handling, aligns the significands with a sticky bit,
|
||||
adds or subtracts them and rounds inline. Only a subtraction whose result is
|
||||
subnormal falls back to the general code (`f64_add_mags`, `f64_sub_mags`).
|
||||
|
||||
Likewise, `f32_add`, `f32_sub` and `f32_mul` compute the result directly in
|
||||
binary32 (with 32-bit significands for add and sub and an exact 48-bit
|
||||
product for mul) when both operands are normal and the result is normal (or,
|
||||
for add and mul, overflows to infinity). Otherwise (zeroes, subnormal
|
||||
operands or results, infinities and NaNs), they, and `f32_div` always,
|
||||
convert their operands exactly to `f64`, perform the `f64` operation and then
|
||||
round to `f32`. This double rounding is innocuous: binary64's 53 bits of
|
||||
precision are more than `(2 * 24) + 2`, which suffices for `+`, `-`, `*` and
|
||||
`/` (S. A. Figueroa, "When is double rounding innocuous?", ACM SIGNUM
|
||||
Newsletter, 1995). The tests confirm this against hardware, including for
|
||||
subnormal `f32` results. The fast paths never produce a NaN, so NaN results
|
||||
(and their payloads) are exactly those of the "via `f64`" computation.
|
||||
|
||||
|
||||
## log2
|
||||
@@ -187,3 +199,32 @@ threads):
|
||||
(6 minutes 20 seconds wall clock in total)
|
||||
|
||||
No exception table is needed.
|
||||
|
||||
`script/manual-test-softfloat-sweep.c` systematically checks the `f32` add,
|
||||
sub, mul and div operations over every pair of operand exponents (`[0 ..=
|
||||
255]` squared), both signs and 40 x 40 mantissa patterns (exact results, ties,
|
||||
carries, cancellation, subnormal results, overflow, NaN payloads): each result
|
||||
must equal the hardware's (any NaN matching any NaN) and, bit-for-bit
|
||||
including NaNs, the "via `f64`" composition `f64_to_f32(f64_op(f32_to_f64(a),
|
||||
f32_to_f64(b)))`. It does the same for the `f64` add, sub, mul and div
|
||||
operations (every pair of exponents in `[0 ..= 2047]`, 9 x 9 mantissa
|
||||
patterns, NaN results checked bit-for-bit against the NaN rule above), and
|
||||
checks `f64_to_f32` (326 exponents times 2²⁰ fraction patterns times both
|
||||
signs), `f32_to_f64` (every `f32`), `f32_from_u32` and `f32_from_i32` (every
|
||||
32-bit integer) and `f32_from_u64` (2³² structured values). Result (x86-64,
|
||||
gcc 13 `-O2`, 4 threads):
|
||||
|
||||
f32_add 419430400 operand pairs, 0 mismatches
|
||||
f32_sub 419430400 operand pairs, 0 mismatches
|
||||
f32_mul 419430400 operand pairs, 0 mismatches
|
||||
f32_div 419430400 operand pairs, 0 mismatches
|
||||
f64_add 1358954496 operand pairs, 0 mismatches
|
||||
f64_sub 1358954496 operand pairs, 0 mismatches
|
||||
f64_mul 1358954496 operand pairs, 0 mismatches
|
||||
f64_div 1358954496 operand pairs, 0 mismatches
|
||||
f64_to_f32 341835776 operands, 0 mismatches
|
||||
f32_to_f64 4294967296 operands, 0 mismatches
|
||||
f32_from_u32 4294967296 operands, 0 mismatches
|
||||
f32_from_i32 4294967296 operands, 0 mismatches
|
||||
f32_from_u64 4294967296 operands, 0 mismatches
|
||||
PASS: 0 mismatches in total
|
||||
@@ -61,6 +61,24 @@ pri func arith.clz64(a: base.u64) base.u32[..= 63] {
|
||||
return (n32 + n16 + n8 + ((CLZ8[x >> 56] as base.u32) & 7)) & 63
|
||||
}
|
||||
|
||||
// clz32 returns the number of leading zero bits of a, which must be non-zero.
|
||||
pri func arith.clz32(a: base.u32) base.u32[..= 31] {
|
||||
var x : base.u32
|
||||
var n16 : base.u32[..= 16]
|
||||
var n8 : base.u32[..= 8]
|
||||
|
||||
x = args.a
|
||||
if (x >> 16) == 0 {
|
||||
x = x ~mod<< 16
|
||||
n16 = 16
|
||||
}
|
||||
if (x >> 24) == 0 {
|
||||
x = x ~mod<< 8
|
||||
n8 = 8
|
||||
}
|
||||
return (n16 + n8 + ((CLZ8[x >> 24] as base.u32) & 7)) & 31
|
||||
}
|
||||
|
||||
// shift_right_jam64 returns (a >> dist), except that if any non-zero bits
|
||||
// were shifted out then the least significant bit of the result is set. This
|
||||
// "sticky bit" is needed to round correctly.
|
||||
@@ -345,6 +363,18 @@ pub func arith.f64_max(a: base.u64, b: base.u64) base.u64 {
|
||||
|
||||
// f64_add returns (a + b).
|
||||
pub func arith.f64_add(a: base.u64, b: base.u64) base.u64 {
|
||||
var z : base.u64
|
||||
|
||||
if ((((args.a >> 52) & 0x7FF) ~mod- 1) < 0x7FE) and ((((args.b >> 52) & 0x7FF) ~mod- 1) < 0x7FE) {
|
||||
// Both operands are normal: take the fast path.
|
||||
if ((args.a ^ args.b) >> 63) == 0 {
|
||||
return this.f64_add_normal_mags(a: args.a, b: args.b)
|
||||
}
|
||||
z = this.f64_sub_normal_mags(a: args.a, b: args.b)
|
||||
if z <> 0xFFFF_FFFF_FFFF_FFFF {
|
||||
return z
|
||||
}
|
||||
}
|
||||
if ((args.a ^ args.b) >> 63) == 0 {
|
||||
return this.f64_add_mags(a: args.a, b: args.b, sign: args.a & F64_SIGN)
|
||||
}
|
||||
@@ -353,12 +383,161 @@ pub func arith.f64_add(a: base.u64, b: base.u64) base.u64 {
|
||||
|
||||
// f64_sub returns (a - b).
|
||||
pub func arith.f64_sub(a: base.u64, b: base.u64) base.u64 {
|
||||
var z : base.u64
|
||||
|
||||
if ((((args.a >> 52) & 0x7FF) ~mod- 1) < 0x7FE) and ((((args.b >> 52) & 0x7FF) ~mod- 1) < 0x7FE) {
|
||||
// Both operands are normal: take the fast path.
|
||||
if ((args.a ^ args.b) >> 63) <> 0 {
|
||||
return this.f64_add_normal_mags(a: args.a, b: args.b)
|
||||
}
|
||||
z = this.f64_sub_normal_mags(a: args.a, b: args.b)
|
||||
if z <> 0xFFFF_FFFF_FFFF_FFFF {
|
||||
return z
|
||||
}
|
||||
}
|
||||
if ((args.a ^ args.b) >> 63) == 0 {
|
||||
return this.f64_sub_mags(a: args.a, b: args.b, sign: args.a & F64_SIGN)
|
||||
}
|
||||
return this.f64_add_mags(a: args.a, b: args.b, sign: args.a & F64_SIGN)
|
||||
}
|
||||
|
||||
// f64_add_normal_mags returns (sign(a) * (|a| + |b|)), for normal a and b.
|
||||
// It is a faster version of f64_add_mags.
|
||||
pri func arith.f64_add_normal_mags(a: base.u64, b: base.u64) base.u64 {
|
||||
var ea : base.u32[..= 0x800]
|
||||
var eb : base.u32[..= 0x7FF]
|
||||
var ma : base.u64[..= 0x3FFF_FFFF_FFFF_FE00]
|
||||
var mb : base.u64[..= 0x3FFF_FFFF_FFFF_FE00]
|
||||
var d : base.u32[..= 0x7FF]
|
||||
var z : base.u64
|
||||
var rb : base.u64
|
||||
|
||||
ea = ((args.a >> 52) & 0x7FF) as base.u32
|
||||
eb = ((args.b >> 52) & 0x7FF) as base.u32
|
||||
// The significands, with their implicit bit at bit 61 and 9 extra bits
|
||||
// of precision at the bottom.
|
||||
ma = ((args.a & F64_FRAC) | F64_HIDDEN) << 9
|
||||
mb = ((args.b & F64_FRAC) | F64_HIDDEN) << 9
|
||||
if ea < eb {
|
||||
d = (eb ~mod- ea) & 0x7FF
|
||||
ea = eb
|
||||
z = ma
|
||||
ma = mb
|
||||
mb = z
|
||||
} else {
|
||||
d = (ea ~mod- eb) & 0x7FF
|
||||
}
|
||||
if d >= 63 {
|
||||
mb = 1
|
||||
} else if d > 0 {
|
||||
if (mb ~mod<< (64 - d)) <> 0 {
|
||||
mb = (mb >> d) | 1
|
||||
} else {
|
||||
mb = mb >> d
|
||||
}
|
||||
}
|
||||
z = ma + mb
|
||||
if z >= 0x4000_0000_0000_0000 {
|
||||
ea += 1
|
||||
z = (z >> 1) | (z & 1)
|
||||
}
|
||||
rb = z & 0x1FF
|
||||
z = (z ~mod+ 0x100) >> 9
|
||||
if rb == 0x100 {
|
||||
z &= 0xFFFF_FFFF_FFFF_FFFE
|
||||
}
|
||||
// ea is in [1 ..= 0x7FF] and z is in [1 << 52 ..= 1 << 53]. Adding z
|
||||
// (whose bit 52 is the implicit bit) carries any rounding overflow into
|
||||
// the exponent field.
|
||||
z = (((ea ~mod- 1) as base.u64) ~mod<< 52) ~mod+ z
|
||||
if z >= F64_INF {
|
||||
z = F64_INF
|
||||
}
|
||||
return (args.a & F64_SIGN) | z
|
||||
}
|
||||
|
||||
// f64_sub_normal_mags returns (sign(a) * (|a| - |b|)), for normal a and b,
|
||||
// unless the result is subnormal, in which case it returns
|
||||
// 0xFFFF_FFFF_FFFF_FFFF (a NaN, which it never otherwise returns) and the
|
||||
// caller must take its slow path. It is a faster version of f64_sub_mags.
|
||||
pri func arith.f64_sub_normal_mags(a: base.u64, b: base.u64) base.u64 {
|
||||
var sign : base.u64
|
||||
var ea : base.u32[..= 0x7FF]
|
||||
var eb : base.u32[..= 0x7FF]
|
||||
var fa : base.u64[..= 0x000F_FFFF_FFFF_FFFF]
|
||||
var fb : base.u64[..= 0x000F_FFFF_FFFF_FFFF]
|
||||
var ma : base.u64
|
||||
var mb : base.u64
|
||||
var d : base.u32[..= 0x7FF]
|
||||
var s : base.u32[..= 63]
|
||||
var z : base.u64
|
||||
var rb : base.u64
|
||||
|
||||
sign = args.a & F64_SIGN
|
||||
ea = ((args.a >> 52) & 0x7FF) as base.u32
|
||||
eb = ((args.b >> 52) & 0x7FF) as base.u32
|
||||
fa = args.a & F64_FRAC
|
||||
fb = args.b & F64_FRAC
|
||||
|
||||
if ea == eb {
|
||||
// The difference is exact.
|
||||
if fa == fb {
|
||||
// Exact cancellation gives +0 when rounding to nearest.
|
||||
return 0
|
||||
} else if fa < fb {
|
||||
sign ^= F64_SIGN
|
||||
z = fb ~mod- fa
|
||||
} else {
|
||||
z = fa ~mod- fb
|
||||
}
|
||||
// z is in [1 ..= F64_FRAC]. Shift its top bit to bit 52.
|
||||
s = (this.clz64(a: z) ~mod- 11) & 63
|
||||
if s >= ea {
|
||||
return 0xFFFF_FFFF_FFFF_FFFF
|
||||
}
|
||||
return sign | (((((ea ~mod- s) ~mod- 1) as base.u64) ~mod<< 52) ~mod+ (z ~mod<< s))
|
||||
}
|
||||
|
||||
// The significands, with their implicit bit at bit 62 and 10 extra bits
|
||||
// of precision at the bottom.
|
||||
ma = (fa | F64_HIDDEN) << 10
|
||||
mb = (fb | F64_HIDDEN) << 10
|
||||
if ea < eb {
|
||||
sign ^= F64_SIGN
|
||||
d = (eb ~mod- ea) & 0x7FF
|
||||
ea = eb
|
||||
z = ma
|
||||
ma = mb
|
||||
mb = z
|
||||
} else {
|
||||
d = (ea ~mod- eb) & 0x7FF
|
||||
}
|
||||
if d >= 63 {
|
||||
mb = 1
|
||||
} else if (mb ~mod<< ((64 - d) & 63)) <> 0 {
|
||||
mb = (mb >> d) | 1
|
||||
} else {
|
||||
mb = mb >> d
|
||||
}
|
||||
// ma > mb, so z is non-zero. If (d >= 2) then z >= (1 << 61) and the
|
||||
// normalization shift is at most 1. Otherwise, no bits were shifted out.
|
||||
z = ma ~mod- mb
|
||||
s = (this.clz64(a: z) ~mod- 1) & 63
|
||||
if s >= ea {
|
||||
return 0xFFFF_FFFF_FFFF_FFFF
|
||||
}
|
||||
ea = (ea ~mod- s) & 0x7FF
|
||||
z = z ~mod<< s
|
||||
rb = z & 0x3FF
|
||||
z = (z ~mod+ 0x200) >> 10
|
||||
if rb == 0x200 {
|
||||
z &= 0xFFFF_FFFF_FFFF_FFFE
|
||||
}
|
||||
// ea is in [1 ..= 0x7FE] and z is in [1 << 52 ..= 1 << 53], so this does
|
||||
// not overflow.
|
||||
return sign | ((((ea ~mod- 1) as base.u64) ~mod<< 52) ~mod+ z)
|
||||
}
|
||||
|
||||
// f64_add_mags returns (sign * (|a| + |b|)).
|
||||
pri func arith.f64_add_mags(a: base.u64, b: base.u64, sign: base.u64) base.u64 {
|
||||
var exp_a : base.u32
|
||||
|
||||
@@ -10,12 +10,18 @@
|
||||
|
||||
// -------- f32 (binary32) operations.
|
||||
//
|
||||
// The f32 add, sub, mul and div functions convert their (exactly
|
||||
// representable) operands to f64, perform the f64 operation and round the
|
||||
// result to f32. Double rounding is innocuous here: binary64 has more than
|
||||
// (2 * 24) + 2 bits of precision, which suffices for +, -, * and / (Figueroa,
|
||||
// "When is double rounding innocuous?", 1995). This is also exhaustively
|
||||
// cross-checked against hardware by the tests.
|
||||
// When both operands are normal (and, for sub_mags and mul, when the result
|
||||
// is normal too), the f32 add, sub and mul functions compute the result
|
||||
// directly in binary32 (with 32-bit or 64-bit integer significands). This is
|
||||
// the common case. Otherwise (zeroes, subnormals, infinities, NaNs and
|
||||
// subnormal results), they and f32_div convert their (exactly representable)
|
||||
// operands to f64, perform the f64 operation and round the result to f32
|
||||
// (the slow_etc functions). Double rounding is innocuous here: binary64 has
|
||||
// more than (2 * 24) + 2 bits of precision, which suffices for +, -, * and /
|
||||
// (Figueroa, "When is double rounding innocuous?", 1995). This is also
|
||||
// exhaustively cross-checked against hardware by the tests. The fast paths
|
||||
// never return a NaN, so NaN results (and their payloads) always come from
|
||||
// the slow paths.
|
||||
|
||||
pri const F32_SIGN : base.u32 = 0x8000_0000
|
||||
pri const F32_ABS : base.u32 = 0x7FFF_FFFF
|
||||
@@ -74,20 +80,232 @@ pub func arith.f32_abs(a: base.u32) base.u32 {
|
||||
|
||||
// f32_add returns (a + b).
|
||||
pub func arith.f32_add(a: base.u32, b: base.u32) base.u32 {
|
||||
var z : base.u32
|
||||
|
||||
if ((((args.a >> 23) & 0xFF) ~mod- 1) < 0xFE) and ((((args.b >> 23) & 0xFF) ~mod- 1) < 0xFE) {
|
||||
if ((args.a ^ args.b) >> 31) == 0 {
|
||||
return this.f32_add_mags(a: args.a, b: args.b)
|
||||
}
|
||||
z = this.f32_sub_mags(a: args.a, b: args.b)
|
||||
if z <> 0xFFFF_FFFF {
|
||||
return z
|
||||
}
|
||||
}
|
||||
return this.slow_f32_add(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_sub returns (a - b).
|
||||
pub func arith.f32_sub(a: base.u32, b: base.u32) base.u32 {
|
||||
var z : base.u32
|
||||
|
||||
if ((((args.a >> 23) & 0xFF) ~mod- 1) < 0xFE) and ((((args.b >> 23) & 0xFF) ~mod- 1) < 0xFE) {
|
||||
if ((args.a ^ args.b) >> 31) <> 0 {
|
||||
return this.f32_add_mags(a: args.a, b: args.b)
|
||||
}
|
||||
z = this.f32_sub_mags(a: args.a, b: args.b)
|
||||
if z <> 0xFFFF_FFFF {
|
||||
return z
|
||||
}
|
||||
}
|
||||
return this.slow_f32_sub(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_mul returns (a * b).
|
||||
pub func arith.f32_mul(a: base.u32, b: base.u32) base.u32 {
|
||||
var ea : base.u32[..= 0xFF]
|
||||
var eb : base.u32[..= 0xFF]
|
||||
var e : base.u32[..= 0x1FF]
|
||||
var p : base.u64
|
||||
var m : base.u32
|
||||
var r : base.u32
|
||||
var z : base.u32
|
||||
|
||||
ea = (args.a >> 23) & 0xFF
|
||||
eb = (args.b >> 23) & 0xFF
|
||||
if ((ea ~mod- 1) < 0xFE) and ((eb ~mod- 1) < 0xFE) {
|
||||
// Both operands are normal. The exact product of the two 24-bit
|
||||
// significands, p, is in [1 << 46, 1 << 48). The biased exponent of
|
||||
// the result, e, is (ea + eb - 127) (plus 1 if p >= (1 << 47)).
|
||||
p = (((args.a & 0x7F_FFFF) | 0x80_0000) as base.u64) *
|
||||
(((args.b & 0x7F_FFFF) | 0x80_0000) as base.u64)
|
||||
e = ea + eb
|
||||
if p >= 0x8000_0000_0000 {
|
||||
e += 1
|
||||
m = ((p >> 24) & 0xFF_FFFF) as base.u32
|
||||
r = ((p & 0xFF_FFFF) as base.u32) ~mod- 0x80_0000
|
||||
} else {
|
||||
m = ((p >> 23) & 0xFF_FFFF) as base.u32
|
||||
r = ((p & 0x7F_FFFF) as base.u32) ~mod- 0x40_0000
|
||||
}
|
||||
// r is the remainder minus half an ULP, as a two's complement
|
||||
// integer. Round to nearest, ties to even.
|
||||
if (r <> 0) and (r < 0x8000_0000) {
|
||||
m ~mod+= 1
|
||||
} else if r == 0 {
|
||||
m ~mod+= m & 1
|
||||
}
|
||||
if (e >= 128) and (e <= 381) {
|
||||
// Adding m (whose bit 23 is the implicit bit) carries any
|
||||
// rounding overflow into the exponent field.
|
||||
z = ((e - 128) << 23) ~mod+ m
|
||||
if z >= F32_INF {
|
||||
z = F32_INF
|
||||
}
|
||||
return ((args.a ^ args.b) & F32_SIGN) | z
|
||||
}
|
||||
}
|
||||
return this.slow_f32_mul(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_add_mags returns (sign(a) * (|a| + |b|)), for normal a and b.
|
||||
pri func arith.f32_add_mags(a: base.u32, b: base.u32) base.u32 {
|
||||
var ea : base.u32[..= 0x100]
|
||||
var eb : base.u32[..= 0xFF]
|
||||
var ma : base.u32[..= 0x3FFF_FFC0]
|
||||
var mb : base.u32[..= 0x3FFF_FFC0]
|
||||
var d : base.u32[..= 0xFF]
|
||||
var z : base.u32
|
||||
var rb : base.u32
|
||||
|
||||
ea = (args.a >> 23) & 0xFF
|
||||
eb = (args.b >> 23) & 0xFF
|
||||
// The significands, with their implicit bit at bit 29 and 6 extra bits
|
||||
// of precision at the bottom.
|
||||
ma = ((args.a & 0x7F_FFFF) | 0x80_0000) << 6
|
||||
mb = ((args.b & 0x7F_FFFF) | 0x80_0000) << 6
|
||||
if ea < eb {
|
||||
d = (eb ~mod- ea) & 0xFF
|
||||
ea = eb
|
||||
z = ma
|
||||
ma = mb
|
||||
mb = z
|
||||
} else {
|
||||
d = (ea ~mod- eb) & 0xFF
|
||||
}
|
||||
if d >= 31 {
|
||||
mb = 1
|
||||
} else if d > 0 {
|
||||
if (mb ~mod<< (32 - d)) <> 0 {
|
||||
mb = (mb >> d) | 1
|
||||
} else {
|
||||
mb = mb >> d
|
||||
}
|
||||
}
|
||||
z = (ma & 0x3FFF_FFFF) + (mb & 0x3FFF_FFFF)
|
||||
if z >= 0x4000_0000 {
|
||||
ea += 1
|
||||
z = (z >> 1) | (z & 1)
|
||||
}
|
||||
rb = z & 0x3F
|
||||
z = (z ~mod+ 0x20) >> 6
|
||||
if rb == 0x20 {
|
||||
z &= 0xFFFF_FFFE
|
||||
}
|
||||
// ea is in [1 ..= 255] and z is in [1 << 23 ..= 1 << 24].
|
||||
z = ((ea ~mod- 1) ~mod<< 23) ~mod+ z
|
||||
if z >= F32_INF {
|
||||
z = F32_INF
|
||||
}
|
||||
return (args.a & F32_SIGN) | z
|
||||
}
|
||||
|
||||
// f32_sub_mags returns (sign(a) * (|a| - |b|)), for normal a and b, unless
|
||||
// the result is subnormal, in which case it returns 0xFFFF_FFFF (a NaN, which
|
||||
// it never otherwise returns) and the caller must take its slow path.
|
||||
pri func arith.f32_sub_mags(a: base.u32, b: base.u32) base.u32 {
|
||||
var sign : base.u32
|
||||
var ea : base.u32[..= 0xFF]
|
||||
var eb : base.u32[..= 0xFF]
|
||||
var fa : base.u32[..= 0x7F_FFFF]
|
||||
var fb : base.u32[..= 0x7F_FFFF]
|
||||
var ma : base.u32
|
||||
var mb : base.u32
|
||||
var d : base.u32[..= 0xFF]
|
||||
var s : base.u32[..= 31]
|
||||
var z : base.u32
|
||||
var rb : base.u32
|
||||
|
||||
sign = args.a & F32_SIGN
|
||||
ea = (args.a >> 23) & 0xFF
|
||||
eb = (args.b >> 23) & 0xFF
|
||||
fa = args.a & 0x7F_FFFF
|
||||
fb = args.b & 0x7F_FFFF
|
||||
|
||||
if ea == eb {
|
||||
// The difference is exact.
|
||||
if fa == fb {
|
||||
// Exact cancellation gives +0 when rounding to nearest.
|
||||
return 0
|
||||
} else if fa < fb {
|
||||
sign ^= F32_SIGN
|
||||
z = fb ~mod- fa
|
||||
} else {
|
||||
z = fa ~mod- fb
|
||||
}
|
||||
// z is in [1 ..= 0x7F_FFFF]. Shift its top bit to bit 23.
|
||||
s = (this.clz32(a: z) ~mod- 8) & 31
|
||||
if s >= ea {
|
||||
return 0xFFFF_FFFF
|
||||
}
|
||||
return sign | ((((ea ~mod- s) ~mod- 1) ~mod<< 23) ~mod+ (z ~mod<< s))
|
||||
}
|
||||
|
||||
// The significands, with their implicit bit at bit 30 and 7 extra bits
|
||||
// of precision at the bottom.
|
||||
ma = (fa | 0x80_0000) << 7
|
||||
mb = (fb | 0x80_0000) << 7
|
||||
if ea < eb {
|
||||
sign ^= F32_SIGN
|
||||
d = (eb ~mod- ea) & 0xFF
|
||||
ea = eb
|
||||
z = ma
|
||||
ma = mb
|
||||
mb = z
|
||||
} else {
|
||||
d = (ea ~mod- eb) & 0xFF
|
||||
}
|
||||
if d >= 31 {
|
||||
mb = 1
|
||||
} else if (mb ~mod<< ((32 - d) & 31)) <> 0 {
|
||||
mb = (mb >> d) | 1
|
||||
} else {
|
||||
mb = mb >> d
|
||||
}
|
||||
// ma > mb, so z is non-zero. If (d >= 2) then z >= (1 << 29) and the
|
||||
// normalization shift is at most 1. Otherwise, no bits were shifted out.
|
||||
z = ma ~mod- mb
|
||||
s = (this.clz32(a: z) ~mod- 1) & 31
|
||||
if s >= ea {
|
||||
return 0xFFFF_FFFF
|
||||
}
|
||||
ea = (ea ~mod- s) & 0xFF
|
||||
z = z ~mod<< s
|
||||
rb = z & 0x7F
|
||||
z = (z ~mod+ 0x40) >> 7
|
||||
if rb == 0x40 {
|
||||
z &= 0xFFFF_FFFE
|
||||
}
|
||||
// ea is in [1 ..= 254] and z is in [1 << 23 ..= 1 << 24], so this does
|
||||
// not overflow.
|
||||
return sign | (((ea ~mod- 1) ~mod<< 23) ~mod+ z)
|
||||
}
|
||||
|
||||
// slow_f32_add returns (a + b), computed via f64.
|
||||
pri func arith.slow_f32_add(a: base.u32, b: base.u32) base.u32 {
|
||||
return this.f64_to_f32(a: this.f64_add(
|
||||
a: this.f32_to_f64(a: args.a),
|
||||
b: this.f32_to_f64(a: args.b)))
|
||||
}
|
||||
|
||||
// f32_sub returns (a - b).
|
||||
pub func arith.f32_sub(a: base.u32, b: base.u32) base.u32 {
|
||||
// slow_f32_sub returns (a - b), computed via f64.
|
||||
pri func arith.slow_f32_sub(a: base.u32, b: base.u32) base.u32 {
|
||||
return this.f64_to_f32(a: this.f64_sub(
|
||||
a: this.f32_to_f64(a: args.a),
|
||||
b: this.f32_to_f64(a: args.b)))
|
||||
}
|
||||
|
||||
// f32_mul returns (a * b).
|
||||
pub func arith.f32_mul(a: base.u32, b: base.u32) base.u32 {
|
||||
// slow_f32_mul returns (a * b), computed via f64.
|
||||
pri func arith.slow_f32_mul(a: base.u32, b: base.u32) base.u32 {
|
||||
return this.f64_to_f32(a: this.f64_mul(
|
||||
a: this.f32_to_f64(a: args.a),
|
||||
b: this.f32_to_f64(a: args.b)))
|
||||
|
||||
Reference in new issue
Block a user