From b26c1af4bdf3f531bc1a57b3cc3695c603fd3be0 Mon Sep 17 00:00:00 2001 From: Adhemerval Zanella Date: Mon, 21 Apr 2025 14:43:49 -0300 Subject: [PATCH] math: Remove UB from float128 ilogbf 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 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sysdeps/ieee754/ldbl-128/e_ilogbl.c b/sysdeps/ieee754/ldbl-128/e_ilogbl.c index 1d81a26fb8..624a0a9c48 100644 --- a/sysdeps/ieee754/ldbl-128/e_ilogbl.c +++ b/sysdeps/ieee754/ldbl-128/e_ilogbl.c @@ -25,11 +25,12 @@ static char rcsid[] = "$NetBSD: $"; #include #include +#include #include 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; } -- 2.47.2