diff mbox series

[2/8] math: Fix UB on coshf

Message ID 20250425205618.360232-3-adhemerval.zanella@linaro.org
State Superseded
Headers show
Series Fix UB in math implementations | expand

Commit Message

Adhemerval Zanella Netto April 25, 2025, 8:54 p.m. UTC
The left shift overflows for 'int', use uint64_t instead.  It syncs
with CORE-MATH commit 4d6192d2.

Checked on aarch64-linux-gnu, x86_64-linux-gnu, and i686-linux-gnu.
---
 sysdeps/ieee754/flt-32/e_coshf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Carlos O'Donell April 29, 2025, 12:43 p.m. UTC | #1
On 4/25/25 4:54 PM, Adhemerval Zanella wrote:
> The left shift overflows for 'int', use uint64_t instead.  It syncs
> with CORE-MATH commit 4d6192d2.
> 
> 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_coshf.c | 6 +++---
>   1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/sysdeps/ieee754/flt-32/e_coshf.c b/sysdeps/ieee754/flt-32/e_coshf.c
> index 5f6ff8c29f..382cd55cdf 100644
> --- a/sysdeps/ieee754/flt-32/e_coshf.c
> +++ b/sysdeps/ieee754/flt-32/e_coshf.c
> @@ -3,7 +3,7 @@
>   Copyright (c) 2022-2024 Alexei Sibidanov.
>   
>   The original version of this file was copied from the CORE-MATH
> -project (file src/binary32/cosh/coshf.c, revision 5c58ea1).
> +project (file src/binary32/cosh/coshf.c, revision de59ecfb).

OK.

>   
>   Permission is hereby granted, free of charge, to any person obtaining a copy
>   of this software and associated documentation files (the "Software"), to deal
> @@ -69,8 +69,8 @@ __ieee754_coshf (float x)
>     double h2 = h * h;
>     int64_t jp = asuint64 (ia + 0x1.8p52);
>     int64_t jm = -jp;
> -  double sp = asdouble (TB[jp & 31] + ((jp >> 5) << 52));
> -  double sm = asdouble (TB[jm & 31] + ((jm >> 5) << 52));
> +  double sp = asdouble (TB[jp & 31] + ((uint64_t)(jp >> 5) << 52));
> +  double sm = asdouble (TB[jm & 31] + ((uint64_t)(jm >> 5) << 52));
>     double te = C[0] + h2 * C[2];
>     double to = (C[1] + h2 * C[3]);
>     double rp = sp * (te + h * to);

OK, fixes with (uint64_t) cast.

commit de59ecfbe35b4e2dddbad2849f3cdde7835d5a48 (HEAD)
Author: Paul Zimmermann <Paul.Zimmermann@inria.fr>
Date:   Thu Mar 13 17:06:24 2025 +0100

     [coshf] fixed undefined behavior
diff mbox series

Patch

diff --git a/sysdeps/ieee754/flt-32/e_coshf.c b/sysdeps/ieee754/flt-32/e_coshf.c
index 5f6ff8c29f..382cd55cdf 100644
--- a/sysdeps/ieee754/flt-32/e_coshf.c
+++ b/sysdeps/ieee754/flt-32/e_coshf.c
@@ -3,7 +3,7 @@ 
 Copyright (c) 2022-2024 Alexei Sibidanov.
 
 The original version of this file was copied from the CORE-MATH
-project (file src/binary32/cosh/coshf.c, revision 5c58ea1).
+project (file src/binary32/cosh/coshf.c, revision de59ecfb).
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
@@ -69,8 +69,8 @@  __ieee754_coshf (float x)
   double h2 = h * h;
   int64_t jp = asuint64 (ia + 0x1.8p52);
   int64_t jm = -jp;
-  double sp = asdouble (TB[jp & 31] + ((jp >> 5) << 52));
-  double sm = asdouble (TB[jm & 31] + ((jm >> 5) << 52));
+  double sp = asdouble (TB[jp & 31] + ((uint64_t)(jp >> 5) << 52));
+  double sm = asdouble (TB[jm & 31] + ((uint64_t)(jm >> 5) << 52));
   double te = C[0] + h2 * C[2];
   double to = (C[1] + h2 * C[3]);
   double rp = sp * (te + h * to);