diff mbox series

math: Fix exp10 undefined left shift

Message ID 20240531092543.2694952-1-szabolcs.nagy@arm.com
State New
Headers show
Series math: Fix exp10 undefined left shift | expand

Commit Message

Szabolcs Nagy May 31, 2024, 9:25 a.m. UTC
Left shift of ki is undefined when ki<0, copy the logic from exp,
which uses unsigned arithmetics, to fix it.
---
 sysdeps/ieee754/dbl-64/e_exp10.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Adhemerval Zanella Netto June 4, 2024, 2:28 p.m. UTC | #1
On 31/05/24 06:25, Szabolcs Nagy wrote:
> Left shift of ki is undefined when ki<0, copy the logic from exp,
> which uses unsigned arithmetics, to fix it.

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  sysdeps/ieee754/dbl-64/e_exp10.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/sysdeps/ieee754/dbl-64/e_exp10.c b/sysdeps/ieee754/dbl-64/e_exp10.c
> index 225fc74c4c..7ea8270063 100644
> --- a/sysdeps/ieee754/dbl-64/e_exp10.c
> +++ b/sysdeps/ieee754/dbl-64/e_exp10.c
> @@ -38,7 +38,7 @@ special_case (uint64_t sbits, double_t tmp, uint64_t ki)
>  {
>    double_t scale, y;
>  
> -  if (ki - (1ull << 16) < 0x80000000)
> +  if ((ki & 0x80000000) == 0)
>      {
>        /* The exponent of scale might have overflowed by 1.  */
>        sbits -= 1ull << 52;
> @@ -100,14 +100,14 @@ __exp10 (double x)
>    /* Reduce x: z = x * N / log10(2), k = round(z).  */
>    double_t z = __exp_data.invlog10_2N * x;
>    double_t kd;
> -  int64_t ki;
> +  uint64_t ki;
>  #if TOINT_INTRINSICS
>    kd = roundtoint (z);
>    ki = converttoint (z);
>  #else
>    kd = math_narrow_eval (z + Shift);
> +  ki = asuint64 (kd);
>    kd -= Shift;
> -  ki = kd;
>  #endif
>  
>    /* r = x - k * log10(2), r in [-0.5, 0.5].  */
diff mbox series

Patch

diff --git a/sysdeps/ieee754/dbl-64/e_exp10.c b/sysdeps/ieee754/dbl-64/e_exp10.c
index 225fc74c4c..7ea8270063 100644
--- a/sysdeps/ieee754/dbl-64/e_exp10.c
+++ b/sysdeps/ieee754/dbl-64/e_exp10.c
@@ -38,7 +38,7 @@  special_case (uint64_t sbits, double_t tmp, uint64_t ki)
 {
   double_t scale, y;
 
-  if (ki - (1ull << 16) < 0x80000000)
+  if ((ki & 0x80000000) == 0)
     {
       /* The exponent of scale might have overflowed by 1.  */
       sbits -= 1ull << 52;
@@ -100,14 +100,14 @@  __exp10 (double x)
   /* Reduce x: z = x * N / log10(2), k = round(z).  */
   double_t z = __exp_data.invlog10_2N * x;
   double_t kd;
-  int64_t ki;
+  uint64_t ki;
 #if TOINT_INTRINSICS
   kd = roundtoint (z);
   ki = converttoint (z);
 #else
   kd = math_narrow_eval (z + Shift);
+  ki = asuint64 (kd);
   kd -= Shift;
-  ki = kd;
 #endif
 
   /* r = x - k * log10(2), r in [-0.5, 0.5].  */