math: Fix UB in ldbl-128ibm lrintl

This commit is contained in:
Adhemerval Zanella 2025-05-08 12:20:56 +00:00
parent 673776c7be
commit 623bd741fb
2 changed files with 18 additions and 10 deletions

View File

@ -28,7 +28,7 @@
static inline bool static inline bool
check_sign (unsigned long int v1, unsigned long int v2) check_sign (unsigned long int v1, unsigned long int v2)
{ {
enum { sign_shift = sizeof (unsigned long long int) * CHAR_BIT - 1 }; enum { sign_shift = sizeof (unsigned long int) * CHAR_BIT - 1 };
return (v1 & (1UL << sign_shift)) == (v2 & (1UL << sign_shift)); return (v1 & (1UL << sign_shift)) == (v2 & (1UL << sign_shift));
} }

View File

@ -23,13 +23,20 @@
#include <math_ldbl_opt.h> #include <math_ldbl_opt.h>
#include <float.h> #include <float.h>
#include <ieee754.h> #include <ieee754.h>
#include <intprops.h>
static inline bool
check_sign (unsigned long int v1, unsigned long int v2)
{
enum { sign_shift = sizeof (unsigned long int) * CHAR_BIT - 1 };
return (v1 & (1UL << sign_shift)) == (v2 & (1UL << sign_shift));
}
long long
__lrintl (long double x) __lrintl (long double x)
{ {
double xh, xl; double xh, xl;
long res, hi, lo; long res, hi, lo, tmp;
int save_round; int save_round;
ldbl_unpack (x, &xh, &xl); ldbl_unpack (x, &xh, &xl);
@ -88,7 +95,7 @@ __lrintl (long double x)
res = (long int) ((unsigned long int) hi + (unsigned long int) lo); res = (long int) ((unsigned long int) hi + (unsigned long int) lo);
/* This is just sign(hi) == sign(lo) && sign(res) != sign(hi). */ /* This is just sign(hi) == sign(lo) && sign(res) != sign(hi). */
if (__glibc_unlikely (((~(hi ^ lo) & (res ^ hi)) < 0))) if (__glibc_unlikely (check_sign (hi, lo) && !check_sign (res, hi)))
goto overflow; goto overflow;
xh -= lo; xh -= lo;
@ -106,31 +113,32 @@ __lrintl (long double x)
return res; return res;
if (xh < 0.0) if (xh < 0.0)
res -= 1UL; INT_SUBTRACT_WRAPV (res, 1UL, &res);
else else
res += 1UL; INT_ADD_WRAPV (res, 1, &res);
break; break;
case FE_TOWARDZERO: case FE_TOWARDZERO:
if (res > 0 && (xh < 0.0 || (xh == 0.0 && xl < 0.0))) if (res > 0 && (xh < 0.0 || (xh == 0.0 && xl < 0.0)))
res -= 1UL; INT_SUBTRACT_WRAPV (res, 1UL, &res);
else if (res < 0 && (xh > 0.0 || (xh == 0.0 && xl > 0.0))) else if (res < 0 && (xh > 0.0 || (xh == 0.0 && xl > 0.0)))
res += 1UL; INT_ADD_WRAPV (res, 1, &res);
return res; return res;
break; break;
case FE_UPWARD: case FE_UPWARD:
if (xh > 0.0 || (xh == 0.0 && xl > 0.0)) if (xh > 0.0 || (xh == 0.0 && xl > 0.0))
res += 1UL; INT_ADD_WRAPV (res, 1, &res);
break; break;
case FE_DOWNWARD: case FE_DOWNWARD:
if (xh < 0.0 || (xh == 0.0 && xl < 0.0)) if (xh < 0.0 || (xh == 0.0 && xl < 0.0))
res -= 1UL; INT_SUBTRACT_WRAPV (res, 1UL, &res);
break; break;
} }
if (__glibc_unlikely (((~(hi ^ (res - hi)) & (res ^ hi)) < 0))) INT_SUBTRACT_WRAPV (res, hi, &tmp);
if (__glibc_unlikely (check_sign (hi, tmp) && !check_sign (res, hi)))
goto overflow; goto overflow;
return res; return res;