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