diff mbox series

[3/8] math: Fix UB on logf

Message ID 20250425205618.360232-4-adhemerval.zanella@linaro.org
State Accepted
Commit de0c4adf94a379873c0167f792519e91df28c3ed
Headers show
Series Fix UB in math implementations | expand

Commit Message

Adhemerval Zanella April 25, 2025, 8:54 p.m. UTC
The left shift overflows for 'int', use a literal instead.  It syncs
with OPTIMIZED-ROUTINES commit 0f87f607b976820ef41fe64d004fe67dc7af8236.

Checked on aarch64-linux-gnu, x86_64-linux-gnu, and i686-linux-gnu.
---
 sysdeps/ieee754/flt-32/e_logf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Carlos O'Donell April 29, 2025, 12:29 p.m. UTC | #1
On 4/25/25 4:54 PM, Adhemerval Zanella wrote:
> The left shift overflows for 'int', use a literal instead.  It syncs
> with OPTIMIZED-ROUTINES commit 0f87f607b976820ef41fe64d004fe67dc7af8236.
> 
> Checked on aarch64-linux-gnu, x86_64-linux-gnu, and i686-linux-gnu.

LGTM.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>

> ---
>   sysdeps/ieee754/flt-32/e_logf.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/sysdeps/ieee754/flt-32/e_logf.c b/sysdeps/ieee754/flt-32/e_logf.c
> index 6a595cf7f0..207151c76a 100644
> --- a/sysdeps/ieee754/flt-32/e_logf.c
> +++ b/sysdeps/ieee754/flt-32/e_logf.c
> @@ -70,7 +70,7 @@ __logf (float x)
>     tmp = ix - OFF;
>     i = (tmp >> (23 - LOGF_TABLE_BITS)) % N;
>     k = (int32_t) tmp >> 23; /* arithmetic shift */
> -  iz = ix - (tmp & 0x1ff << 23);
> +  iz = ix - (tmp & 0xff800000);
>     invc = T[i].invc;
>     logc = T[i].logc;
>     z = (double_t) asfloat (iz);

OK, confirmed from git@github.com:ARM-software/optimized-routines.git

commit 0f87f607b976820ef41fe64d004fe67dc7af8236
Author: Joe Ramsay <Joe.Ramsay@arm.com>
Date:   Thu Jan 5 11:56:20 2023 +0000

     Rewrite two abs masks as literals
     
     These were technically undefined behaviour - they have been rewritten
     without the shift so that their type is unsigned int by default.
diff mbox series

Patch

diff --git a/sysdeps/ieee754/flt-32/e_logf.c b/sysdeps/ieee754/flt-32/e_logf.c
index 6a595cf7f0..207151c76a 100644
--- a/sysdeps/ieee754/flt-32/e_logf.c
+++ b/sysdeps/ieee754/flt-32/e_logf.c
@@ -70,7 +70,7 @@  __logf (float x)
   tmp = ix - OFF;
   i = (tmp >> (23 - LOGF_TABLE_BITS)) % N;
   k = (int32_t) tmp >> 23; /* arithmetic shift */
-  iz = ix - (tmp & 0x1ff << 23);
+  iz = ix - (tmp & 0xff800000);
   invc = T[i].invc;
   logc = T[i].logc;
   z = (double_t) asfloat (iz);