libc: Prevent FCSEL instruction from being used to avoid raising an unintended exception

If you write an if expression in mem.doNotOptimizeAway like
doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);,
FCSEL instruction is used on AArch64.
FCSEL instruction selects one of the two registers according to
the condition and copies its value.
In this example, `x / 0x1p120` and `x + 0x1p120` are expressions
that raise different floating-point exceptions.
However, since both are actually evaluated before the FCSEL
instruction, the exception not intended by the programmer may
also be raised.

To prevent FCSEL instruction from being used here, this commit
splits doNotOptimizeAway in two.
This commit is contained in:
Koki Ueha 2025-06-15 01:05:58 +00:00 committed by Jacob Young
parent 3ce8d19f76
commit 878b7b80c1
3 changed files with 42 additions and 6 deletions

View file

@ -49,7 +49,13 @@ pub fn sinf(x: f32) callconv(.c) f32 {
if (ix <= 0x3f490fda) { // |x| ~<= pi/4
if (ix < 0x39800000) { // |x| < 2**-12
// raise inexact if x!=0 and underflow if subnormal
if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00800000) x / 0x1p120 else x + 0x1p120);
if (common.want_float_exceptions) {
if (ix < 0x00800000) {
mem.doNotOptimizeAway(x / 0x1p120);
} else {
mem.doNotOptimizeAway(x + 0x1p120);
}
}
return x;
}
return trig.__sindf(x);
@ -98,7 +104,13 @@ pub fn sin(x: f64) callconv(.c) f64 {
if (ix <= 0x3fe921fb) {
if (ix < 0x3e500000) { // |x| < 2**-26
// raise inexact if x != 0 and underflow if subnormal
if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
if (common.want_float_exceptions) {
if (ix < 0x00100000) {
mem.doNotOptimizeAway(x / 0x1p120);
} else {
mem.doNotOptimizeAway(x + 0x1p120);
}
}
return x;
}
return trig.__sin(x, 0.0, 0);

View file

@ -46,7 +46,13 @@ pub fn sincosf(x: f32, r_sin: *f32, r_cos: *f32) callconv(.c) void {
// |x| < 2**-12
if (ix < 0x39800000) {
// raise inexact if x!=0 and underflow if subnormal
if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
if (common.want_float_exceptions) {
if (ix < 0x00100000) {
mem.doNotOptimizeAway(x / 0x1p120);
} else {
mem.doNotOptimizeAway(x + 0x1p120);
}
}
r_sin.* = x;
r_cos.* = 1.0;
return;
@ -134,7 +140,13 @@ pub fn sincos(x: f64, r_sin: *f64, r_cos: *f64) callconv(.c) void {
// if |x| < 2**-27 * sqrt(2)
if (ix < 0x3e46a09e) {
// raise inexact if x != 0 and underflow if subnormal
if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
if (common.want_float_exceptions) {
if (ix < 0x00100000) {
mem.doNotOptimizeAway(x / 0x1p120);
} else {
mem.doNotOptimizeAway(x + 0x1p120);
}
}
r_sin.* = x;
r_cos.* = 1.0;
return;

View file

@ -51,7 +51,13 @@ pub fn tanf(x: f32) callconv(.c) f32 {
if (ix <= 0x3f490fda) { // |x| ~<= pi/4
if (ix < 0x39800000) { // |x| < 2**-12
// raise inexact if x!=0 and underflow if subnormal
if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00800000) x / 0x1p120 else x + 0x1p120);
if (common.want_float_exceptions) {
if (ix < 0x00800000) {
mem.doNotOptimizeAway(x / 0x1p120);
} else {
mem.doNotOptimizeAway(x + 0x1p120);
}
}
return x;
}
return kernel.__tandf(x, false);
@ -89,7 +95,13 @@ pub fn tan(x: f64) callconv(.c) f64 {
if (ix <= 0x3fe921fb) {
if (ix < 0x3e400000) { // |x| < 2**-27
// raise inexact if x!=0 and underflow if subnormal
if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
if (common.want_float_exceptions) {
if (ix < 0x00100000) {
mem.doNotOptimizeAway(x / 0x1p120);
} else {
mem.doNotOptimizeAway(x + 0x1p120);
}
}
return x;
}
return kernel.__tan(x, 0.0, false);