[RFC] math/powl: Workaround clang build

YunQiang Su yunqiang@isrc.iscas.ac.cn
Wed Dec 11 01:03:46 GMT 2024


In condtional operator, only one operand of the result operands is allowed
to be evaluated, while in some cases clang has a bug on it.
So in some cases both underflow and overflow exceptions are raised.

See
https://github.com/llvm/llvm-project/issues/119341
---
 sysdeps/ieee754/ldbl-128/e_powl.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/sysdeps/ieee754/ldbl-128/e_powl.c b/sysdeps/ieee754/ldbl-128/e_powl.c
index 4e20616705..2954548525 100644
--- a/sysdeps/ieee754/ldbl-128/e_powl.c
+++ b/sysdeps/ieee754/ldbl-128/e_powl.c
@@ -146,6 +146,19 @@ static const _Float128
   cp_h = L(9.6179669392597555432899980587535537779331E-1),
   cp_l = L(5.0577616648125906047157785230014751039424E-17);
 
+/* Workaround clang bug of return x?y*y:z*z.
+   https://github.com/llvm/llvm-project/issues/119341 */
+static inline _Float128
+calc_pow2 (_Float128 x)
+{
+  return x * x;
+}
+static inline _Float128
+calc_pow2_sign (_Float128 x, _Float128 sgn)
+{
+  return sgn * x * x;
+}
+
 _Float128
 __ieee754_powl (_Float128 x, _Float128 y)
 {
@@ -279,15 +292,15 @@ __ieee754_powl (_Float128 x, _Float128 y)
       if (iy > 0x407d654b)
 	{
 	  if (ix <= 0x3ffeffff)
-	    return (hy < 0) ? huge * huge : tiny * tiny;
+	    return (hy < 0) ? calc_pow2 (huge) : calc_pow2 (tiny);
 	  if (ix >= 0x3fff0000)
-	    return (hy > 0) ? huge * huge : tiny * tiny;
+	    return (hy > 0) ? calc_pow2 (huge) : calc_pow2 (tiny);
 	}
       /* over/underflow if x is not close to one */
       if (ix < 0x3ffeffff)
-	return (hy < 0) ? sgn * huge * huge : sgn * tiny * tiny;
+	return (hy < 0) ? calc_pow2_sign (huge, sgn) : calc_pow2_sign (tiny, sgn);
       if (ix > 0x3fff0000)
-	return (hy > 0) ? sgn * huge * huge : sgn * tiny * tiny;
+	return (hy > 0) ? calc_pow2_sign (huge, sgn) : calc_pow2_sign (tiny, sgn);
     }
 
   ay = y > 0 ? y : -y;
-- 
2.39.5 (Apple Git-154)



More information about the Libc-alpha mailing list