[PATCH v3 7/8] x86_64: Add cosh with FMA

Adhemerval Zanella adhemerval.zanella@linaro.org
Thu Mar 12 14:21:11 GMT 2026


The cosh shows an improvement of about ~35% when building for
x86_64-v3.

Reviewed-by: DJ Delorie <dj@redhat.com>
---
 sysdeps/ieee754/dbl-64/e_cosh.c           | 13 +++++--
 sysdeps/x86_64/fpu/multiarch/Makefile     |  2 ++
 sysdeps/x86_64/fpu/multiarch/e_cosh-fma.c |  5 +++
 sysdeps/x86_64/fpu/multiarch/e_cosh.c     | 44 +++++++++++++++++++++++
 4 files changed, 61 insertions(+), 3 deletions(-)
 create mode 100644 sysdeps/x86_64/fpu/multiarch/e_cosh-fma.c
 create mode 100644 sysdeps/x86_64/fpu/multiarch/e_cosh.c

diff --git a/sysdeps/ieee754/dbl-64/e_cosh.c b/sysdeps/ieee754/dbl-64/e_cosh.c
index f9008e6e5f..4cfaf9ea59 100644
--- a/sysdeps/ieee754/dbl-64/e_cosh.c
+++ b/sysdeps/ieee754/dbl-64/e_cosh.c
@@ -34,6 +34,10 @@ SOFTWARE.
 #include "e_coshsinh_data.h"
 #include "math_config.h"
 
+#ifndef SECTION
+#  define SECTION
+#endif
+
 static __attribute__ ((noinline)) double as_cosh_database (double, double);
 
 static inline double
@@ -125,6 +129,7 @@ as_cosh_database (double x, double f)
   return f;
 }
 
+SECTION
 double
 __cosh (double x)
 {
@@ -293,11 +298,13 @@ __cosh (double x)
     return as_cosh_database (x, rh);
   return rh;
 }
+#ifndef __cosh
 strong_alias (__cosh, __ieee754_cosh)
-#if LIBM_SVID_COMPAT
+# if LIBM_SVID_COMPAT
 versioned_symbol (libm, __cosh, cosh, GLIBC_2_44);
 libm_alias_double_other (__cosh, cosh)
-#else
+# else
 libm_alias_double (__cosh, cosh)
-#endif
+# endif
 libm_alias_finite (__ieee754_cosh, __cosh)
+#endif
diff --git a/sysdeps/x86_64/fpu/multiarch/Makefile b/sysdeps/x86_64/fpu/multiarch/Makefile
index 145ad08207..28e5123810 100644
--- a/sysdeps/x86_64/fpu/multiarch/Makefile
+++ b/sysdeps/x86_64/fpu/multiarch/Makefile
@@ -6,6 +6,7 @@ CFLAGS-e_exp-fma.c = -mfma -mavx2
 CFLAGS-e_log-fma.c = -mfma -mavx2
 CFLAGS-e_log2-fma.c = -mfma -mavx2
 CFLAGS-e_pow-fma.c = -mfma -mavx2
+CFLAGS-e_cosh-fma.c = -mfma -mavx2
 CFLAGS-e_sinh-fma.c = -mfma -mavx2
 CFLAGS-s_atan-fma.c = -mfma -mavx2
 CFLAGS-s_expm1-fma.c = -mfma -mavx2
@@ -73,6 +74,7 @@ libm-sysdep_routines += \
   e_atan2-avx \
   e_atan2-fma \
   e_atanh-fma \
+  e_cosh-fma \
   e_exp-avx \
   e_exp-fma \
   e_exp2f-fma \
diff --git a/sysdeps/x86_64/fpu/multiarch/e_cosh-fma.c b/sysdeps/x86_64/fpu/multiarch/e_cosh-fma.c
new file mode 100644
index 0000000000..3f84b68fe8
--- /dev/null
+++ b/sysdeps/x86_64/fpu/multiarch/e_cosh-fma.c
@@ -0,0 +1,5 @@
+#define __cosh __cosh_fma
+
+#define SECTION __attribute__ ((section (".text.fma")))
+
+#include <sysdeps/ieee754/dbl-64/e_cosh.c>
diff --git a/sysdeps/x86_64/fpu/multiarch/e_cosh.c b/sysdeps/x86_64/fpu/multiarch/e_cosh.c
new file mode 100644
index 0000000000..16185d0569
--- /dev/null
+++ b/sysdeps/x86_64/fpu/multiarch/e_cosh.c
@@ -0,0 +1,44 @@
+/* Multiple versions of cosh.
+   Copyright (C) 2025 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sysdeps/x86/isa-level.h>
+#if MINIMUM_X86_ISA_LEVEL < AVX2_X86_ISA_LEVEL
+# include <libm-alias-finite.h>
+# include <libm-alias-double.h>
+# include <math-svid-compat.h>
+
+extern double __redirect_cosh (double);
+
+# define SYMBOL_NAME cosh
+# include "ifunc-fma.h"
+
+libc_ifunc_redirected (__redirect_cosh, __cosh,
+		       IFUNC_SELECTOR ());
+
+strong_alias (__cosh, __ieee754_cosh)
+# if LIBM_SVID_COMPAT
+versioned_symbol (libm, __cosh, cosh, GLIBC_2_44);
+libm_alias_double_other (__cosh, cosh)
+# else
+libm_alias_double (__cosh, cosh)
+# endif
+libm_alias_finite (__ieee754_cosh, __cosh)
+
+# define __cosh __cosh_sse2
+#endif
+#include <sysdeps/ieee754/dbl-64/e_cosh.c>
-- 
2.43.0



More information about the Libc-alpha mailing list