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
@@ -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
|
||||
|
||||
Reference in new issue
Block a user