dfa500
From 735a36828f349419379f15e942bfdf0c532d58eb Mon Sep 17 00:00:00 2001
dfa500
From: Stefan Liebler <stli@linux.ibm.com>
dfa500
Date: Wed, 11 Dec 2019 15:09:18 +0100
dfa500
Subject: [PATCH 07/28] Use GCC builtins for nearbyint functions if desired.
dfa500
dfa500
This patch is using the corresponding GCC builtin for nearbyintf, nearbyint,
dfa500
nearbintl and nearbyintf128 if the USE_FUNCTION_BUILTIN macros are defined to one
dfa500
in math-use-builtins.h.
dfa500
dfa500
This is the case for s390 if build with at least --march=z196 --mzarch.
dfa500
Otherwise the generic implementation is used.  The code of the generic
dfa500
implementation is not changed.
dfa500
dfa500
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
dfa500
(cherry picked from commit ae3577f607b50bf3ce9b0877e43ad2508c9da61b)
dfa500
Note: The TWO52 constants are now located in # ! USE_NEARBYINT_BUILTIN
dfa500
---
dfa500
 sysdeps/generic/math-use-builtins.h         | 29 ++++++++++++
dfa500
 sysdeps/ieee754/dbl-64/s_nearbyint.c        | 17 ++++---
dfa500
 sysdeps/ieee754/float128/float128_private.h |  4 ++
dfa500
 sysdeps/ieee754/flt-32/s_nearbyintf.c       | 17 ++++---
dfa500
 sysdeps/ieee754/ldbl-128/s_nearbyintl.c     | 17 ++++---
dfa500
 sysdeps/s390/fpu/math-use-builtins.h        | 49 +++++++++++++++++++++
dfa500
 6 files changed, 115 insertions(+), 18 deletions(-)
dfa500
 create mode 100644 sysdeps/generic/math-use-builtins.h
dfa500
 create mode 100644 sysdeps/s390/fpu/math-use-builtins.h
dfa500
dfa500
diff --git a/sysdeps/generic/math-use-builtins.h b/sysdeps/generic/math-use-builtins.h
dfa500
new file mode 100644
dfa500
index 0000000000..e12490ed41
dfa500
--- /dev/null
dfa500
+++ b/sysdeps/generic/math-use-builtins.h
dfa500
@@ -0,0 +1,29 @@
dfa500
+/* Using math gcc builtins instead of generic implementation.  Generic version.
dfa500
+   Copyright (C) 2019 Free Software Foundation, Inc.
dfa500
+   This file is part of the GNU C Library.
dfa500
+
dfa500
+   The GNU C Library is free software; you can redistribute it and/or
dfa500
+   modify it under the terms of the GNU Lesser General Public
dfa500
+   License as published by the Free Software Foundation; either
dfa500
+   version 2.1 of the License, or (at your option) any later version.
dfa500
+
dfa500
+   The GNU C Library is distributed in the hope that it will be useful,
dfa500
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
dfa500
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
dfa500
+   Lesser General Public License for more details.
dfa500
+
dfa500
+   You should have received a copy of the GNU Lesser General Public
dfa500
+   License along with the GNU C Library; if not, see
dfa500
+   <https://www.gnu.org/licenses/>.  */
dfa500
+
dfa500
+#ifndef MATH_USE_BUILTINS_H
dfa500
+#define MATH_USE_BUILTINS_H	1
dfa500
+
dfa500
+/* Define these macros to 1 to use __builtin_xyz instead of the
dfa500
+   generic implementation.  */
dfa500
+#define USE_NEARBYINT_BUILTIN 0
dfa500
+#define USE_NEARBYINTF_BUILTIN 0
dfa500
+#define USE_NEARBYINTL_BUILTIN 0
dfa500
+#define USE_NEARBYINTF128_BUILTIN 0
dfa500
+
dfa500
+#endif /* math-use-builtins.h */
dfa500
diff --git a/sysdeps/ieee754/dbl-64/s_nearbyint.c b/sysdeps/ieee754/dbl-64/s_nearbyint.c
dfa500
index 903121d456..6b9f44dc8d 100644
dfa500
--- a/sysdeps/ieee754/dbl-64/s_nearbyint.c
dfa500
+++ b/sysdeps/ieee754/dbl-64/s_nearbyint.c
dfa500
@@ -29,16 +29,20 @@ static char rcsid[] = "$NetBSD: s_rint.c,v 1.8 1995/05/10 20:48:04 jtc Exp $";
dfa500
 #include <math-barriers.h>
dfa500
 #include <math_private.h>
dfa500
 #include <libm-alias-double.h>
dfa500
-
dfa500
-static const double
dfa500
-  TWO52[2] = {
dfa500
-  4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
dfa500
- -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */
dfa500
-};
dfa500
+#include <math-use-builtins.h>
dfa500
 
dfa500
 double
dfa500
 __nearbyint (double x)
dfa500
 {
dfa500
+#if USE_NEARBYINT_BUILTIN
dfa500
+  return __builtin_nearbyint (x);
dfa500
+#else
dfa500
+  /* Use generic implementation.  */
dfa500
+  static const double
dfa500
+    TWO52[2] = {
dfa500
+		4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
dfa500
+		-4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */
dfa500
+  };
dfa500
   fenv_t env;
dfa500
   int32_t i0, j0, sx;
dfa500
   double w, t;
dfa500
@@ -72,5 +76,6 @@ __nearbyint (double x)
dfa500
   math_force_eval (t);
dfa500
   libc_fesetenv (&env;;
dfa500
   return t;
dfa500
+#endif /* ! USE_NEARBYINT_BUILTIN  */
dfa500
 }
dfa500
 libm_alias_double (__nearbyint, nearbyint)
dfa500
diff --git a/sysdeps/ieee754/float128/float128_private.h b/sysdeps/ieee754/float128/float128_private.h
dfa500
index 9dd15601e6..0bf6e8dee2 100644
dfa500
--- a/sysdeps/ieee754/float128/float128_private.h
dfa500
+++ b/sysdeps/ieee754/float128/float128_private.h
dfa500
@@ -138,6 +138,9 @@
dfa500
 #undef libm_alias_double_ldouble
dfa500
 #define libm_alias_double_ldouble(func) libm_alias_float64_float128 (func)
dfa500
 
dfa500
+#include <math-use-builtins.h>
dfa500
+#undef USE_NEARBYINTL_BUILTIN
dfa500
+#define USE_NEARBYINTL_BUILTIN USE_NEARBYINTF128_BUILTIN
dfa500
 
dfa500
 /* IEEE function renames.  */
dfa500
 #define __ieee754_acoshl __ieee754_acoshf128
dfa500
@@ -339,6 +342,7 @@
dfa500
 /* Builtin renames.  */
dfa500
 #define __builtin_copysignl __builtin_copysignf128
dfa500
 #define __builtin_signbitl __builtin_signbit
dfa500
+#define __builtin_nearbyintl __builtin_nearbyintf128
dfa500
 
dfa500
 /* Get the constant suffix from bits/floatn-compat.h.  */
dfa500
 #define L(x) __f128 (x)
dfa500
diff --git a/sysdeps/ieee754/flt-32/s_nearbyintf.c b/sysdeps/ieee754/flt-32/s_nearbyintf.c
dfa500
index 4dfe491f27..438dcae8cc 100644
dfa500
--- a/sysdeps/ieee754/flt-32/s_nearbyintf.c
dfa500
+++ b/sysdeps/ieee754/flt-32/s_nearbyintf.c
dfa500
@@ -20,16 +20,20 @@
dfa500
 #include <math-barriers.h>
dfa500
 #include <math_private.h>
dfa500
 #include <libm-alias-float.h>
dfa500
-
dfa500
-static const float
dfa500
-TWO23[2]={
dfa500
-  8.3886080000e+06, /* 0x4b000000 */
dfa500
- -8.3886080000e+06, /* 0xcb000000 */
dfa500
-};
dfa500
+#include <math-use-builtins.h>
dfa500
 
dfa500
 float
dfa500
 __nearbyintf(float x)
dfa500
 {
dfa500
+#if USE_NEARBYINTF_BUILTIN
dfa500
+  return __builtin_nearbyintf (x);
dfa500
+#else
dfa500
+  /* Use generic implementation.  */
dfa500
+  static const float
dfa500
+    TWO23[2] = {
dfa500
+		8.3886080000e+06, /* 0x4b000000 */
dfa500
+		-8.3886080000e+06, /* 0xcb000000 */
dfa500
+  };
dfa500
 	fenv_t env;
dfa500
 	int32_t i0,j0,sx;
dfa500
 	float w,t;
dfa500
@@ -57,5 +61,6 @@ __nearbyintf(float x)
dfa500
 	math_force_eval (t);
dfa500
 	libc_fesetenvf (&env;;
dfa500
 	return t;
dfa500
+#endif /* ! USE_NEARBYINT_BUILTIN  */
dfa500
 }
dfa500
 libm_alias_float (__nearbyint, nearbyint)
dfa500
diff --git a/sysdeps/ieee754/ldbl-128/s_nearbyintl.c b/sysdeps/ieee754/ldbl-128/s_nearbyintl.c
dfa500
index f044cb4334..a4ad8e82e5 100644
dfa500
--- a/sysdeps/ieee754/ldbl-128/s_nearbyintl.c
dfa500
+++ b/sysdeps/ieee754/ldbl-128/s_nearbyintl.c
dfa500
@@ -28,15 +28,19 @@
dfa500
 #include <math-barriers.h>
dfa500
 #include <math_private.h>
dfa500
 #include <libm-alias-ldouble.h>
dfa500
-
dfa500
-static const _Float128
dfa500
-TWO112[2]={
dfa500
-  L(5.19229685853482762853049632922009600E+33), /* 0x406F000000000000, 0 */
dfa500
- L(-5.19229685853482762853049632922009600E+33)  /* 0xC06F000000000000, 0 */
dfa500
-};
dfa500
+#include <math-use-builtins.h>
dfa500
 
dfa500
 _Float128 __nearbyintl(_Float128 x)
dfa500
 {
dfa500
+#if USE_NEARBYINTL_BUILTIN
dfa500
+  return __builtin_nearbyintl (x);
dfa500
+#else
dfa500
+  /* Use generic implementation.  */
dfa500
+  static const _Float128
dfa500
+    TWO112[2] = {
dfa500
+		 L(5.19229685853482762853049632922009600E+33), /* 0x406F000000000000, 0 */
dfa500
+		 L(-5.19229685853482762853049632922009600E+33)  /* 0xC06F000000000000, 0 */
dfa500
+  };
dfa500
 	fenv_t env;
dfa500
 	int64_t i0,j0,sx;
dfa500
 	uint64_t i1 __attribute__ ((unused));
dfa500
@@ -65,5 +69,6 @@ _Float128 __nearbyintl(_Float128 x)
dfa500
 	math_force_eval (t);
dfa500
 	fesetenv (&env;;
dfa500
 	return t;
dfa500
+#endif /* ! USE_NEARBYINTL_BUILTIN  */
dfa500
 }
dfa500
 libm_alias_ldouble (__nearbyint, nearbyint)
dfa500
diff --git a/sysdeps/s390/fpu/math-use-builtins.h b/sysdeps/s390/fpu/math-use-builtins.h
dfa500
new file mode 100644
dfa500
index 0000000000..7abbfb3b50
dfa500
--- /dev/null
dfa500
+++ b/sysdeps/s390/fpu/math-use-builtins.h
dfa500
@@ -0,0 +1,49 @@
dfa500
+/* Using math gcc builtins instead of generic implementation.  s390/s390x version.
dfa500
+   Copyright (C) 2019 Free Software Foundation, Inc.
dfa500
+   This file is part of the GNU C Library.
dfa500
+
dfa500
+   The GNU C Library is free software; you can redistribute it and/or
dfa500
+   modify it under the terms of the GNU Lesser General Public
dfa500
+   License as published by the Free Software Foundation; either
dfa500
+   version 2.1 of the License, or (at your option) any later version.
dfa500
+
dfa500
+   The GNU C Library is distributed in the hope that it will be useful,
dfa500
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
dfa500
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
dfa500
+   Lesser General Public License for more details.
dfa500
+
dfa500
+   You should have received a copy of the GNU Lesser General Public
dfa500
+   License along with the GNU C Library; if not, see
dfa500
+   <https://www.gnu.org/licenses/>.  */
dfa500
+
dfa500
+#ifndef MATH_USE_BUILTINS_S390_H
dfa500
+#define MATH_USE_BUILTINS_S390_H	1
dfa500
+
dfa500
+#ifdef HAVE_S390_MIN_Z196_ZARCH_ASM_SUPPORT
dfa500
+
dfa500
+# include <features.h> /* For __GNUC_PREREQ.  */
dfa500
+
dfa500
+/* GCC emits the z196 zarch "load fp integer" instructions for these
dfa500
+   builtins if build with at least --march=z196 -mzarch.  Otherwise a
dfa500
+   function call to libc is emitted.  */
dfa500
+# define USE_NEARBYINT_BUILTIN 1
dfa500
+# define USE_NEARBYINTF_BUILTIN 1
dfa500
+# define USE_NEARBYINTL_BUILTIN 1
dfa500
+
dfa500
+# if __GNUC_PREREQ (8, 0)
dfa500
+#  define USE_NEARBYINTF128_BUILTIN 1
dfa500
+# else
dfa500
+#  define USE_NEARBYINTF128_BUILTIN 0
dfa500
+# endif
dfa500
+
dfa500
+#else
dfa500
+
dfa500
+/* Disable the builtins if we do not have the z196 zarch instructions.  */
dfa500
+# define USE_NEARBYINT_BUILTIN 0
dfa500
+# define USE_NEARBYINTF_BUILTIN 0
dfa500
+# define USE_NEARBYINTL_BUILTIN 0
dfa500
+# define USE_NEARBYINTF128_BUILTIN 0
dfa500
+
dfa500
+#endif /* ! HAVE_S390_MIN_Z196_ZARCH_ASM_SUPPORT  */
dfa500
+
dfa500
+#endif /* math-use-builtins.h */
dfa500
-- 
dfa500
2.18.2
dfa500