mbox series

[v2,0/4] Remove UB and optimize ilogbf/ilogb

Message ID 20250428170430.2030400-1-adhemerval.zanella@linaro.org
Headers show
Series Remove UB and optimize ilogbf/ilogb | expand

Message

Adhemerval Zanella Netto April 28, 2025, 5:03 p.m. UTC
From a recent experiment to build glibc with -fsanitize=undefined [1],
I found that both the float and double ilogb triggered UB due to the
use of shift with a signed integer.

This patchset fixes the UB issues on both implementations by
reimplementing with a slightly better code.

[1] https://sourceware.org/git/?p=glibc.git;a=shortlog;h=refs/heads/azanella/ubsan-undef

Adhemerval Zanella (4):
  math: Remove UB and optimize double ilogb
  math: Optimize double ilogb/llogb
  math: Remove UB and optimize double ilogbf
  math: Optimize float ilogb/llogb

 sysdeps/i386/fpu/math_err.c          |  1 -
 sysdeps/ieee754/dbl-64/e_ilogb.c     | 64 +--------------------------
 sysdeps/ieee754/dbl-64/math_config.h |  2 +
 sysdeps/ieee754/dbl-64/math_err.c    | 32 ++++++++++++++
 sysdeps/ieee754/dbl-64/w_ilogb.c     | 63 ++++++++++++++++++++++++++
 sysdeps/ieee754/dbl-64/w_llogb.c     |  2 +
 sysdeps/ieee754/flt-32/e_ilogbf.c    | 44 +------------------
 sysdeps/ieee754/flt-32/math_config.h |  2 +
 sysdeps/ieee754/flt-32/math_errf.c   | 33 ++++++++++++++
 sysdeps/ieee754/flt-32/w_ilogbf.c    | 66 ++++++++++++++++++++++++++++
 sysdeps/ieee754/flt-32/w_llogbf.c    |  2 +
 11 files changed, 204 insertions(+), 107 deletions(-)
 delete mode 100644 sysdeps/i386/fpu/math_err.c
 create mode 100644 sysdeps/ieee754/dbl-64/w_ilogb.c
 create mode 100644 sysdeps/ieee754/dbl-64/w_llogb.c
 create mode 100644 sysdeps/ieee754/flt-32/w_ilogbf.c
 create mode 100644 sysdeps/ieee754/flt-32/w_llogbf.c

Comments

Adhemerval Zanella Netto April 28, 2025, 5:35 p.m. UTC | #1
On 28/04/25 14:03, Adhemerval Zanella wrote:
> From a recent experiment to build glibc with -fsanitize=undefined [1],
> I found that both the float and double ilogb triggered UB due to the
> use of shift with a signed integer.
> 
> This patchset fixes the UB issues on both implementations by
> reimplementing with a slightly better code.
> 
> [1] https://sourceware.org/git/?p=glibc.git;a=shortlog;h=refs/heads/azanella/ubsan-undef

This version triggers some issues on loongarch and fails to build
on m68k.  Both architecture will need to some adjustment, I will
send a v3.

> 
> Adhemerval Zanella (4):
>   math: Remove UB and optimize double ilogb
>   math: Optimize double ilogb/llogb
>   math: Remove UB and optimize double ilogbf
>   math: Optimize float ilogb/llogb
> 
>  sysdeps/i386/fpu/math_err.c          |  1 -
>  sysdeps/ieee754/dbl-64/e_ilogb.c     | 64 +--------------------------
>  sysdeps/ieee754/dbl-64/math_config.h |  2 +
>  sysdeps/ieee754/dbl-64/math_err.c    | 32 ++++++++++++++
>  sysdeps/ieee754/dbl-64/w_ilogb.c     | 63 ++++++++++++++++++++++++++
>  sysdeps/ieee754/dbl-64/w_llogb.c     |  2 +
>  sysdeps/ieee754/flt-32/e_ilogbf.c    | 44 +------------------
>  sysdeps/ieee754/flt-32/math_config.h |  2 +
>  sysdeps/ieee754/flt-32/math_errf.c   | 33 ++++++++++++++
>  sysdeps/ieee754/flt-32/w_ilogbf.c    | 66 ++++++++++++++++++++++++++++
>  sysdeps/ieee754/flt-32/w_llogbf.c    |  2 +
>  11 files changed, 204 insertions(+), 107 deletions(-)
>  delete mode 100644 sysdeps/i386/fpu/math_err.c
>  create mode 100644 sysdeps/ieee754/dbl-64/w_ilogb.c
>  create mode 100644 sysdeps/ieee754/dbl-64/w_llogb.c
>  create mode 100644 sysdeps/ieee754/flt-32/w_ilogbf.c
>  create mode 100644 sysdeps/ieee754/flt-32/w_llogbf.c
>