std/softfloat: return early for zero operands, merge the f64 add/sub fast paths and add a faster f32_from_u32

f64_add and f64_sub now share one fast path for normal operands
(f64_add_normal) and both return early when one operand is zero. The f32
add and sub do the same, and f32_from_u32 converts values below 2^24
directly. All results are unchanged: the sweep, the old-vs-new
differential test and the ops harness report zero mismatches.
This commit is contained in:
Claude committed 2026-10-27 10:18:57 +00:00
1 parent 49356e06e4
commit ab5930ffbb
4 files changed
+195 -269

No files matched your search

+75 -128
View File
@@ -367,13 +367,16 @@ pub func arith.f64_add(a: base.u64, b: base.u64) 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)
z = this.f64_add_normal(a: args.a, b: args.b)
if z <> 0xFFFF_FFFF_FFFF_FFFF {
return z
}
} else if ((args.b & F64_ABS) == 0) and (((args.a & F64_ABS) ~mod- 1) < (F64_INF - 1)) {
// b is a zero and a is finite and non-zero, so (a + b) is a.
return args.a
} else if ((args.a & F64_ABS) == 0) and (((args.b & F64_ABS) ~mod- 1) < (F64_INF - 1)) {
// a is a zero and b is finite and non-zero, so (a + b) is b.
return args.b
}
if ((args.a ^ args.b) >> 63) == 0 {
return this.f64_add_mags(a: args.a, b: args.b, sign: args.a & F64_SIGN)
@@ -386,14 +389,19 @@ 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)
// Both operands are normal: take the fast path. For non-NaN
// operands, (a - b) is exactly (a + -b), including the sign of a
// zero result.
z = this.f64_add_normal(a: args.a, b: args.b ^ F64_SIGN)
if z <> 0xFFFF_FFFF_FFFF_FFFF {
return z
}
} else if ((args.b & F64_ABS) == 0) and (((args.a & F64_ABS) ~mod- 1) < (F64_INF - 1)) {
// b is a zero and a is finite and non-zero, so (a - b) is a.
return args.a
} else if ((args.a & F64_ABS) == 0) and (((args.b & F64_ABS) ~mod- 1) < (F64_INF - 1)) {
// a is a zero and b is finite and non-zero, so (a - b) is -b.
return args.b ^ F64_SIGN
}
if ((args.a ^ args.b) >> 63) == 0 {
return this.f64_sub_mags(a: args.a, b: args.b, sign: args.a & F64_SIGN)
@@ -401,141 +409,80 @@ pub func arith.f64_sub(a: base.u64, b: base.u64) base.u64 {
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]
// f64_add_normal returns (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 (but less general) version of f64_add_mags and f64_sub_mags.
pri func arith.f64_add_normal(a: base.u64, b: base.u64) base.u64 {
var x : base.u64
var y : base.u64
var ex : base.u32[..= 0x800]
var d : base.u32
var mx : base.u64[..= 0x7FFF_FFFF_FFFF_FFFF]
var my : base.u64[..= 0x7FFF_FFFF_FFFF_FFFF]
var z : base.u64
var s : base.u32[..= 63]
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
// Order the operands so that |x| >= |y|. The result has x's sign.
x = args.a
y = args.b
if (y & F64_ABS) > (x & F64_ABS) {
x = args.b
y = args.a
}
ex = ((x >> 52) & 0x7FF) as base.u32
d = ex ~mod- (((y >> 52) & 0x7FF) as base.u32)
if d > 63 {
d = 63
}
// The significands, with their implicit bit at bit 62 and 10 extra bits
// of precision at the bottom. Align my with mx, keeping a sticky bit.
mx = ((x & F64_FRAC) | F64_HIDDEN) << 10
my = ((y & F64_FRAC) | F64_HIDDEN) << 10
if (my & (((1 as base.u64) << (d & 63)) ~mod- 1)) <> 0 {
my = (my >> (d & 63)) | 1
} else {
d = (ea ~mod- eb) & 0x7FF
my = my >> (d & 63)
}
if d >= 63 {
mb = 1
} else if d > 0 {
if (mb ~mod<< (64 - d)) <> 0 {
mb = (mb >> d) | 1
} else {
mb = mb >> d
if ((x ^ y) >> 63) == 0 {
z = mx + my
if z >= 0x8000_0000_0000_0000 {
ex += 1
z = (z >> 1) | (z & 1)
}
}
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 {
} else {
// mx >= my, since |x| >= |y|.
z = mx ~mod- my
if z == 0 {
// 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
} else if z < 0x4000_0000_0000_0000 {
// If (d >= 2) then z >= (1 << 61) and the normalization shift
// is 1. Otherwise, no bits were shifted out of my.
s = (this.clz64(a: z) ~mod- 1) & 63
if s >= ex {
return 0xFFFF_FFFF_FFFF_FFFF
}
ex = (ex ~mod- s) & 0x7FF
z = z ~mod<< s
}
// 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
// Round to nearest, ties to even. z is now in [1 << 52 ..= 1 << 53] and
// ex in [1 ..= 0x7FF]. Adding z (whose bit 52 is the implicit bit) to the
// shifted exponent carries any rounding overflow into the exponent field.
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)
z = (((ex ~mod- 1) as base.u64) ~mod<< 52) ~mod+ z
if z >= F64_INF {
z = F64_INF
}
return (x & F64_SIGN) | z
}
// f64_add_mags returns (sign * (|a| + |b|)).