[PATCH v2] math: Relax the fast-math gate for vector math declarations

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Thu Nov 5 11:00:08 GMT 2026



On 16/10/26 13:39, Philipp Tomsich wrote:
> The aarch64 and x86_64 <bits/math-vector.h> headers declare the libmvec
> variants only under __FAST_MATH__. GCC does not define that macro for
> -ffast-math -fno-finite-math-only, so code built that way to keep NaN
> and Inf semantics gets no vectorized math calls. For example, GCC 16
> on AArch64 vectorizes this loop with _ZGVnN4v_expf at -O3 -ffast-math,
> but not once -fno-finite-math-only is added:
> 
>   #include <math.h>
>   void f (float *restrict y, const float *restrict x, int n)
>   {
>     for (int i = 0; i < n; i++)
>       y[i] = expf (x[i]);
>   }
> 
> The vector routines handle NaN and Inf. They do not set errno or
> exception flags, do not handle signed zeros or directed rounding, and
> can be less accurate than the scalar functions. So also enable the
> declarations when GCC defines __NO_MATH_ERRNO__, __NO_TRAPPING_MATH__,
> __NO_SIGNED_ZEROS__ and __UNSAFE_MATH_OPTIMIZATIONS__, but not
> __ROUNDING_MATH__. __UNSAFE_MATH_OPTIMIZATIONS__ means the user accepts
> less accurate results, which GCC already requires for external vector
> math libraries (-mveclibabi). The rounding check is needed because the
> AArch64 tan and tanf vector routines can give wrong results in directed
> rounding modes.
> 
> __UNSAFE_MATH_OPTIMIZATIONS__ is a new GCC predefined macro
> (commit 459d3cb9b8b96555a9a815f2306bfc8e5d2085f8).
> 
> The __FAST_MATH__ condition is unchanged, so the patch only enables the
> declarations in more cases. Strict modes, glibc's own build and compilers
> without the new macro are unaffected.
> 
> The motivating case is 736.ocio_r from SPEC CPU 2026, which relies on
> NaN handling and so cannot use -ffinite-math-only.
> 
> Tested on aarch64-linux-gnu and x86_64-linux-gnu.
> 
> Suggested-by: Joe Ramsay <joe.ramsay@arm.com>
> Co-authored-by: Konstantinos Eleftheriou <konstantinos.eleftheriou@vrull.eu>
> Signed-off-by: Philipp Tomsich <philipp.tomsich@vrull.eu>

LGTM, I will install this shortly.

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

> ---
> 
> Notes:
>     Changes in v2:
>     - Gate on the new GCC macro __UNSAFE_MATH_OPTIMIZATIONS__ (plus
>       __NO_SIGNED_ZEROS__) instead of __ASSOCIATIVE_MATH__, as suggested
>       by Joe Ramsay, so lower-accuracy routines are only used when the
>       user opted into unsafe math optimizations.
>     - The GCC side adding __UNSAFE_MATH_OPTIMIZATIONS__ has been merged:
>       https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=459d3cb9b8b96555a9a815f2306bfc8e5d2085f8
>     
>     v1 and review: https://inbox.sourceware.org/libc-alpha/1de0db8a-c6a1-4be7-9b81-30fbf50251f0@arm.com/T/
> 
>  sysdeps/aarch64/fpu/bits/math-vector.h | 13 ++++++++++++-
>  sysdeps/x86/fpu/bits/math-vector.h     | 13 ++++++++++++-
>  2 files changed, 24 insertions(+), 2 deletions(-)
> 
> diff --git a/sysdeps/aarch64/fpu/bits/math-vector.h b/sysdeps/aarch64/fpu/bits/math-vector.h
> index db218eedf9..5d79bf2bda 100644
> --- a/sysdeps/aarch64/fpu/bits/math-vector.h
> +++ b/sysdeps/aarch64/fpu/bits/math-vector.h
> @@ -25,7 +25,18 @@
>  /* Get default empty definitions for simd declarations.  */
>  #include <bits/libm-simd-decl-stubs.h>
>  
> -#if defined __aarch64__ && defined __FAST_MATH__ && __GNUC_PREREQ (6, 0)
> +/* The vector routines handle NaN and Inf but not errno, exception
> +   flags, signed zeros or directed rounding, and can be less accurate
> +   than the scalar functions.  Besides -ffast-math, enable the
> +   declarations when GCC's predefined macros show that the code does
> +   not rely on any of these and allows unsafe math optimizations, as
> +   with -ffast-math -fno-finite-math-only.  */
> +#if defined __aarch64__ && __GNUC_PREREQ (6, 0) \
> +    && (defined __FAST_MATH__ \
> +	|| (defined __NO_MATH_ERRNO__ && defined __NO_TRAPPING_MATH__ \
> +	    && defined __NO_SIGNED_ZEROS__ \
> +	    && defined __UNSAFE_MATH_OPTIMIZATIONS__ \
> +	    && !defined __ROUNDING_MATH__))
>  /* Requires GCC >= 6 for __attribute__ ((__simd__)).  */
>  # define __DECL_SIMD_aarch64 __attribute__ ((__simd__ ("notinbranch"), const))
>  
> diff --git a/sysdeps/x86/fpu/bits/math-vector.h b/sysdeps/x86/fpu/bits/math-vector.h
> index f72bab1609..d73d5f0d28 100644
> --- a/sysdeps/x86/fpu/bits/math-vector.h
> +++ b/sysdeps/x86/fpu/bits/math-vector.h
> @@ -24,7 +24,18 @@
>  /* Get default empty definitions for simd declarations.  */
>  #include <bits/libm-simd-decl-stubs.h>
>  
> -#if defined __x86_64__ && defined __FAST_MATH__
> +/* The vector routines handle NaN and Inf but not errno, exception
> +   flags, signed zeros or directed rounding, and can be less accurate
> +   than the scalar functions.  Besides -ffast-math, enable the
> +   declarations when GCC's predefined macros show that the code does
> +   not rely on any of these and allows unsafe math optimizations, as
> +   with -ffast-math -fno-finite-math-only.  */
> +#if defined __x86_64__ \
> +    && (defined __FAST_MATH__ \
> +	|| (defined __NO_MATH_ERRNO__ && defined __NO_TRAPPING_MATH__ \
> +	    && defined __NO_SIGNED_ZEROS__ \
> +	    && defined __UNSAFE_MATH_OPTIMIZATIONS__ \
> +	    && !defined __ROUNDING_MATH__))
>  # if defined _OPENMP && _OPENMP >= 201307
>  /* OpenMP case.  */
>  #  define __DECL_SIMD_x86_64 _Pragma ("omp declare simd notinbranch")



More information about the Libc-alpha mailing list