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