]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
math: Remove UB from float128 ilogbf
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Mon, 21 Apr 2025 17:43:49 +0000 (14:43 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 7 May 2025 17:21:21 +0000 (14:21 -0300)
The subnormal exponent calculation invokes UB by left shifting the
high or lower work.  Use unsigned values and stdc_leading_zeros
instead.

sysdeps/ieee754/ldbl-128/e_ilogbl.c

index 1d81a26fb81e9e107dcc1bf9982e859edc6d2dd5..624a0a9c48368d11bad9d9d16ff8091bf615d5ce 100644 (file)
@@ -25,11 +25,12 @@ static char rcsid[] = "$NetBSD: $";
 
 #include <limits.h>
 #include <math.h>
+#include <stdbit.h>
 #include <math_private.h>
 
 int __ieee754_ilogbl (_Float128 x)
 {
-       int64_t hx,lx;
+       uint64_t hx,lx;
        int ix;
 
        GET_LDOUBLE_WORDS64(hx,lx,x);
@@ -39,9 +40,9 @@ int __ieee754_ilogbl (_Float128 x)
                return FP_ILOGB0;       /* ilogbl(0) = FP_ILOGB0 */
            else                        /* subnormal x */
                if(hx==0) {
-                   for (ix = -16431; lx>0; lx<<=1) ix -=1;
+                   return -16431 - stdc_leading_zeros (lx);
                } else {
-                   for (ix = -16382, hx<<=15; hx>0; hx<<=1) ix -=1;
+                   return -16382 - stdc_leading_zeros (hx << 15);
                }
            return ix;
        }