From 7d9a8408ec9741caacdea3fb30cee877a836e69a Mon Sep 17 00:00:00 2001 From: Adhemerval Zanella Date: Wed, 17 Dec 2025 14:51:51 -0300 Subject: [PATCH] math: Fix potential underflow on ldbl-128 erfl The multiplication operation is required only if the branch is taken, and the compiler might not optimize it away. It fixes the following issues when the testcase is built with clang: FAIL: math/test-ldouble-erf Failure: erf (-0x4p-16384): Exception "Underflow" set Failure: erf (0x1p-10000): Exception "Underflow" set Failure: erf (0x3.8b7f12369ded54f38760a41abb84p-16384): Exception "Underflow" set Failure: erf (0x3.8b7f12369ded54f38760a41abb88p-16384): Exception "Underflow" set Failure: erf (0x3.8b7f12369ded54f4p-16384): Exception "Underflow" set Failure: erf (0x3.8b7f12369ded54f8p-16384): Exception "Underflow" set Failure: erf (0x3.8b7f12369ded5518p-16384): Exception "Underflow" set Failure: erf (0x3.8b7f12369ded551cp-16384): Exception "Underflow" set Failure: erf (0x3.8b7f12369ded552p-16384): Exception "Underflow" set Failure: erf (0x4p-16384): Exception "Underflow" set Failure: erf_downward (-0x4p-16384): Exception "Underflow" set Failure: erf_downward (0x1p-10000): Exception "Underflow" set Failure: erf_downward (0x3.8b7f12369ded54f38760a41abb84p-16384): Exception "Underflow" set Failure: erf_downward (0x3.8b7f12369ded54f38760a41abb88p-16384): Exception "Underflow" set Failure: erf_downward (0x3.8b7f12369ded54f4p-16384): Exception "Underflow" set Failure: erf_downward (0x3.8b7f12369ded54f8p-16384): Exception "Underflow" set Failure: erf_downward (0x3.8b7f12369ded5518p-16384): Exception "Underflow" set Failure: erf_downward (0x3.8b7f12369ded551cp-16384): Exception "Underflow" set Failure: erf_downward (0x3.8b7f12369ded552p-16384): Exception "Underflow" set Failure: erf_downward (0x4p-16384): Exception "Underflow" set Failure: erf_towardzero (-0x4p-16384): Exception "Underflow" set Failure: erf_towardzero (0x1p-10000): Exception "Underflow" set Failure: erf_towardzero (0x3.8b7f12369ded54f38760a41abb84p-16384): Exception "Underflow" set Failure: erf_towardzero (0x3.8b7f12369ded54f38760a41abb88p-16384): Exception "Underflow" set Failure: erf_towardzero (0x3.8b7f12369ded54f4p-16384): Exception "Underflow" set Failure: erf_towardzero (0x3.8b7f12369ded54f8p-16384): Exception "Underflow" set Failure: erf_towardzero (0x3.8b7f12369ded5518p-16384): Exception "Underflow" set Failure: erf_towardzero (0x3.8b7f12369ded551cp-16384): Exception "Underflow" set Failure: erf_towardzero (0x3.8b7f12369ded552p-16384): Exception "Underflow" set Failure: erf_towardzero (0x4p-16384): Exception "Underflow" set Failure: erf_upward (-0x4p-16384): Exception "Underflow" set Failure: erf_upward (0x1p-10000): Exception "Underflow" set Failure: erf_upward (0x3.8b7f12369ded54f38760a41abb84p-16384): Exception "Underflow" set Failure: erf_upward (0x3.8b7f12369ded54f38760a41abb88p-16384): Exception "Underflow" set Failure: erf_upward (0x3.8b7f12369ded54f4p-16384): Exception "Underflow" set Failure: erf_upward (0x3.8b7f12369ded54f8p-16384): Exception "Underflow" set Failure: erf_upward (0x3.8b7f12369ded5518p-16384): Exception "Underflow" set Failure: erf_upward (0x3.8b7f12369ded551cp-16384): Exception "Underflow" set Failure: erf_upward (0x3.8b7f12369ded552p-16384): Exception "Underflow" set Failure: erf_upward (0x4p-16384): Exception "Underflow" set Checked on x86_64-linux-gnu and aarch64-linux-gnu with gcc-15 and clang-18. Reviewed-by: H.J. Lu --- sysdeps/ieee754/ldbl-128/s_erfl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sysdeps/ieee754/ldbl-128/s_erfl.c b/sysdeps/ieee754/ldbl-128/s_erfl.c index 1cbedc249d..e52d88bde3 100644 --- a/sysdeps/ieee754/ldbl-128/s_erfl.c +++ b/sysdeps/ieee754/ldbl-128/s_erfl.c @@ -778,7 +778,6 @@ __erfl (_Float128 x) } u.parts32.w0 = ix; a = u.value; - z = x * x; if (ix < 0x3ffec000) /* a < 0.875 */ { if (ix < 0x3fc60000) /* |x|<2**-57 */ @@ -792,6 +791,7 @@ __erfl (_Float128 x) } return x + efx * x; } + z = x * x; y = a + a * neval (z, TN1, NTN1) / deval (z, TD1, NTD1); } else