mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-20 08: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
@@ -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