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 -124
View File
@@ -41206,14 +41206,7 @@ wuffs_softfloat__arith__f64_propagate_nan(
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__f64_add_normal_mags(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__f64_sub_normal_mags(
wuffs_softfloat__arith__f64_add_normal(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
@@ -41897,13 +41890,14 @@ wuffs_softfloat__arith__f64_add(
uint64_t v_z = 0;
if ((((uint64_t)(((a_a >> 52u) & 2047u) - 1u)) < 2046u) && (((uint64_t)(((a_b >> 52u) & 2047u) - 1u)) < 2046u)) {
if (((a_a ^ a_b) >> 63u) == 0u) {
return wuffs_softfloat__arith__f64_add_normal_mags(self, a_a, a_b);
}
v_z = wuffs_softfloat__arith__f64_sub_normal_mags(self, a_a, a_b);
v_z = wuffs_softfloat__arith__f64_add_normal(self, a_a, a_b);
if (v_z != 18446744073709551615u) {
return v_z;
}
} else if (((a_b & 9223372036854775807u) == 0u) && (((uint64_t)((a_a & 9223372036854775807u) - 1u)) < 9218868437227405311u)) {
return a_a;
} else if (((a_a & 9223372036854775807u) == 0u) && (((uint64_t)((a_b & 9223372036854775807u) - 1u)) < 9218868437227405311u)) {
return a_b;
}
if (((a_a ^ a_b) >> 63u) == 0u) {
return wuffs_softfloat__arith__f64_add_mags(self, a_a, a_b, (a_a & 9223372036854775808u));
@@ -41930,13 +41924,14 @@ wuffs_softfloat__arith__f64_sub(
uint64_t v_z = 0;
if ((((uint64_t)(((a_a >> 52u) & 2047u) - 1u)) < 2046u) && (((uint64_t)(((a_b >> 52u) & 2047u) - 1u)) < 2046u)) {
if (((a_a ^ a_b) >> 63u) != 0u) {
return wuffs_softfloat__arith__f64_add_normal_mags(self, a_a, a_b);
}
v_z = wuffs_softfloat__arith__f64_sub_normal_mags(self, a_a, a_b);
v_z = wuffs_softfloat__arith__f64_add_normal(self, a_a, (a_b ^ 9223372036854775808u));
if (v_z != 18446744073709551615u) {
return v_z;
}
} else if (((a_b & 9223372036854775807u) == 0u) && (((uint64_t)((a_a & 9223372036854775807u) - 1u)) < 9218868437227405311u)) {
return a_a;
} else if (((a_a & 9223372036854775807u) == 0u) && (((uint64_t)((a_b & 9223372036854775807u) - 1u)) < 9218868437227405311u)) {
return (a_b ^ 9223372036854775808u);
}
if (((a_a ^ a_b) >> 63u) == 0u) {
return wuffs_softfloat__arith__f64_sub_mags(self, a_a, a_b, (a_a & 9223372036854775808u));
@@ -41944,133 +41939,71 @@ wuffs_softfloat__arith__f64_sub(
return wuffs_softfloat__arith__f64_add_mags(self, a_a, a_b, (a_a & 9223372036854775808u));
}
// -------- func softfloat.arith.f64_add_normal_mags
// -------- func softfloat.arith.f64_add_normal
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__f64_add_normal_mags(
wuffs_softfloat__arith__f64_add_normal(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
uint32_t v_ea = 0;
uint32_t v_eb = 0;
uint64_t v_ma = 0;
uint64_t v_mb = 0;
uint64_t v_x = 0;
uint64_t v_y = 0;
uint32_t v_ex = 0;
uint32_t v_d = 0;
uint64_t v_mx = 0;
uint64_t v_my = 0;
uint64_t v_z = 0;
uint64_t v_rb = 0;
v_ea = ((uint32_t)(((a_a >> 52u) & 2047u)));
v_eb = ((uint32_t)(((a_b >> 52u) & 2047u)));
v_ma = (((a_a & 4503599627370495u) | 4503599627370496u) << 9u);
v_mb = (((a_b & 4503599627370495u) | 4503599627370496u) << 9u);
if (v_ea < v_eb) {
v_d = (((uint32_t)(v_eb - v_ea)) & 2047u);
v_ea = v_eb;
v_z = v_ma;
v_ma = v_mb;
v_mb = v_z;
} else {
v_d = (((uint32_t)(v_ea - v_eb)) & 2047u);
}
if (v_d >= 63u) {
v_mb = 1u;
} else if (v_d > 0u) {
if (((uint64_t)(v_mb << (64u - v_d))) != 0u) {
v_mb = ((v_mb >> v_d) | 1u);
} else {
v_mb = (v_mb >> v_d);
}
}
v_z = (v_ma + v_mb);
if (v_z >= 4611686018427387904u) {
v_ea += 1u;
v_z = ((v_z >> 1u) | (v_z & 1u));
}
v_rb = (v_z & 511u);
v_z = (((uint64_t)(v_z + 256u)) >> 9u);
if (v_rb == 256u) {
v_z &= 18446744073709551614u;
}
v_z = ((uint64_t)(((uint64_t)(((uint64_t)(((uint32_t)(v_ea - 1u)))) << 52u)) + v_z));
if (v_z >= 9218868437227405312u) {
v_z = 9218868437227405312u;
}
return ((a_a & 9223372036854775808u) | v_z);
}
// -------- func softfloat.arith.f64_sub_normal_mags
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__f64_sub_normal_mags(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
uint64_t v_sign = 0;
uint32_t v_ea = 0;
uint32_t v_eb = 0;
uint64_t v_fa = 0;
uint64_t v_fb = 0;
uint64_t v_ma = 0;
uint64_t v_mb = 0;
uint32_t v_d = 0;
uint32_t v_s = 0;
uint64_t v_z = 0;
uint64_t v_rb = 0;
v_sign = (a_a & 9223372036854775808u);
v_ea = ((uint32_t)(((a_a >> 52u) & 2047u)));
v_eb = ((uint32_t)(((a_b >> 52u) & 2047u)));
v_fa = (a_a & 4503599627370495u);
v_fb = (a_b & 4503599627370495u);
if (v_ea == v_eb) {
if (v_fa == v_fb) {
v_x = a_a;
v_y = a_b;
if ((v_y & 9223372036854775807u) > (v_x & 9223372036854775807u)) {
v_x = a_b;
v_y = a_a;
}
v_ex = ((uint32_t)(((v_x >> 52u) & 2047u)));
v_d = ((uint32_t)(v_ex - ((uint32_t)(((v_y >> 52u) & 2047u)))));
if (v_d > 63u) {
v_d = 63u;
}
v_mx = (((v_x & 4503599627370495u) | 4503599627370496u) << 10u);
v_my = (((v_y & 4503599627370495u) | 4503599627370496u) << 10u);
if ((v_my & ((uint64_t)((((uint64_t)(1u)) << (v_d & 63u)) - 1u))) != 0u) {
v_my = ((v_my >> (v_d & 63u)) | 1u);
} else {
v_my = (v_my >> (v_d & 63u));
}
if (((v_x ^ v_y) >> 63u) == 0u) {
v_z = (v_mx + v_my);
if (v_z >= 9223372036854775808u) {
v_ex += 1u;
v_z = ((v_z >> 1u) | (v_z & 1u));
}
} else {
v_z = ((uint64_t)(v_mx - v_my));
if (v_z == 0u) {
return 0u;
} else if (v_fa < v_fb) {
v_sign ^= 9223372036854775808u;
v_z = ((uint64_t)(v_fb - v_fa));
} else {
v_z = ((uint64_t)(v_fa - v_fb));
} else if (v_z < 4611686018427387904u) {
v_s = (((uint32_t)(wuffs_softfloat__arith__clz64(self, v_z) - 1u)) & 63u);
if (v_s >= v_ex) {
return 18446744073709551615u;
}
v_ex = (((uint32_t)(v_ex - v_s)) & 2047u);
v_z = ((uint64_t)(v_z << v_s));
}
v_s = (((uint32_t)(wuffs_softfloat__arith__clz64(self, v_z) - 11u)) & 63u);
if (v_s >= v_ea) {
return 18446744073709551615u;
}
return (v_sign | ((uint64_t)(((uint64_t)(((uint64_t)(((uint32_t)(((uint32_t)(v_ea - v_s)) - 1u)))) << 52u)) + ((uint64_t)(v_z << v_s)))));
}
v_ma = ((v_fa | 4503599627370496u) << 10u);
v_mb = ((v_fb | 4503599627370496u) << 10u);
if (v_ea < v_eb) {
v_sign ^= 9223372036854775808u;
v_d = (((uint32_t)(v_eb - v_ea)) & 2047u);
v_ea = v_eb;
v_z = v_ma;
v_ma = v_mb;
v_mb = v_z;
} else {
v_d = (((uint32_t)(v_ea - v_eb)) & 2047u);
}
if (v_d >= 63u) {
v_mb = 1u;
} else if (((uint64_t)(v_mb << ((64u - v_d) & 63u))) != 0u) {
v_mb = ((v_mb >> v_d) | 1u);
} else {
v_mb = (v_mb >> v_d);
}
v_z = ((uint64_t)(v_ma - v_mb));
v_s = (((uint32_t)(wuffs_softfloat__arith__clz64(self, v_z) - 1u)) & 63u);
if (v_s >= v_ea) {
return 18446744073709551615u;
}
v_ea = (((uint32_t)(v_ea - v_s)) & 2047u);
v_z = ((uint64_t)(v_z << v_s));
v_rb = (v_z & 1023u);
v_z = (((uint64_t)(v_z + 512u)) >> 10u);
if (v_rb == 512u) {
v_z &= 18446744073709551614u;
}
return (v_sign | ((uint64_t)(((uint64_t)(((uint64_t)(((uint32_t)(v_ea - 1u)))) << 52u)) + v_z)));
v_z = ((uint64_t)(((uint64_t)(((uint64_t)(((uint32_t)(v_ex - 1u)))) << 52u)) + v_z));
if (v_z >= 9218868437227405312u) {
v_z = 9218868437227405312u;
}
return ((v_x & 9223372036854775808u) | v_z);
}
// -------- func softfloat.arith.f64_add_mags
@@ -42832,6 +42765,10 @@ wuffs_softfloat__arith__f32_add(
if (v_z != 4294967295u) {
return v_z;
}
} else if (((a_b & 2147483647u) == 0u) && (((uint32_t)((a_a & 2147483647u) - 1u)) < 2139095039u)) {
return a_a;
} else if (((a_a & 2147483647u) == 0u) && (((uint32_t)((a_b & 2147483647u) - 1u)) < 2139095039u)) {
return a_b;
}
return wuffs_softfloat__arith__slow_f32_add(self, a_a, a_b);
}
@@ -42862,6 +42799,10 @@ wuffs_softfloat__arith__f32_sub(
if (v_z != 4294967295u) {
return v_z;
}
} else if (((a_b & 2147483647u) == 0u) && (((uint32_t)((a_a & 2147483647u) - 1u)) < 2139095039u)) {
return a_a;
} else if (((a_a & 2147483647u) == 0u) && (((uint32_t)((a_b & 2147483647u) - 1u)) < 2139095039u)) {
return (a_b ^ 2147483648u);
}
return wuffs_softfloat__arith__slow_f32_sub(self, a_a, a_b);
}
@@ -43334,6 +43275,16 @@ wuffs_softfloat__arith__f32_from_u32(
return 0;
}
uint32_t v_c = 0;
if (a_a == 0u) {
return 0u;
} else if (a_a < 16777216u) {
v_c = wuffs_softfloat__arith__clz32(self, a_a);
if (v_c >= 8u) {
return ((uint32_t)(((uint32_t)(((uint32_t)(157u - v_c)) << 23u)) + ((uint32_t)(a_a << (v_c - 8u)))));
}
}
return wuffs_softfloat__arith__f64_to_f32(self, wuffs_softfloat__arith__f64_from_u32(self, a_a));
}
+8 -5
View File
@@ -97,15 +97,18 @@ with the final remainder as the sticky bit. Leading zero counts use a 256-entry
table.
`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`).
operands are normal (`f64_add_normal`): it orders the operands by magnitude,
aligns the significands with a sticky bit, adds or subtracts them and rounds
inline. Only a result that is subnormal falls back to the general code
(`f64_add_mags`, `f64_sub_mags`). Adding (or subtracting) a zero and a finite
non-zero value also returns early.
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
for add and mul, overflows to infinity), and add and sub return early for a
zero plus a finite non-zero value. `f32_from_u32` converts integers below
`1 << 24` (which are exact) directly. 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
+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|)).
+37 -12
View File
@@ -10,18 +10,19 @@
// -------- f32 (binary32) operations.
//
// 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.
// When both operands are normal (and, unless it overflows to infinity, 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. Adding or subtracting a zero and a finite
// non-zero operand is also handled directly. 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
@@ -90,6 +91,12 @@ pub func arith.f32_add(a: base.u32, b: base.u32) base.u32 {
if z <> 0xFFFF_FFFF {
return z
}
} else if ((args.b & F32_ABS) == 0) and (((args.a & F32_ABS) ~mod- 1) < (F32_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 & F32_ABS) == 0) and (((args.b & F32_ABS) ~mod- 1) < (F32_INF - 1)) {
// a is a zero and b is finite and non-zero, so (a + b) is b.
return args.b
}
return this.slow_f32_add(a: args.a, b: args.b)
}
@@ -106,6 +113,12 @@ pub func arith.f32_sub(a: base.u32, b: base.u32) base.u32 {
if z <> 0xFFFF_FFFF {
return z
}
} else if ((args.b & F32_ABS) == 0) and (((args.a & F32_ABS) ~mod- 1) < (F32_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 & F32_ABS) == 0) and (((args.b & F32_ABS) ~mod- 1) < (F32_INF - 1)) {
// a is a zero and b is finite and non-zero, so (a - b) is -b.
return args.b ^ F32_SIGN
}
return this.slow_f32_sub(a: args.a, b: args.b)
}
@@ -415,6 +428,18 @@ pub func arith.f32_max(a: base.u32, b: base.u32) base.u32 {
// f32_from_u32 returns the f32 nearest to the integer a (C's (float)a).
pub func arith.f32_from_u32(a: base.u32) base.u32 {
var c : base.u32[..= 31]
if args.a == 0 {
return 0
} else if args.a < 0x100_0000 {
// a is exactly representable. Shift its top bit to bit 23 (the
// implicit bit, which is then added to the exponent field).
c = this.clz32(a: args.a)
if c >= 8 {
return ((157 ~mod- c) ~mod<< 23) ~mod+ (args.a ~mod<< (c - 8))
}
}
// The u32 to f64 conversion is exact, so there is only one rounding.
return this.f64_to_f32(a: this.f64_from_u32(a: args.a))
}