a2cf7d
From 90e84ac5ac8e774ce0cfd3abc5f7d8834efd2c9b Mon Sep 17 00:00:00 2001
a2cf7d
From: Stefan Liebler <stli@linux.ibm.com>
a2cf7d
Date: Wed, 11 Dec 2019 15:09:29 +0100
a2cf7d
Subject: [PATCH 20/28] S390: Use convert-to-fixed instruction for lrint
a2cf7d
 functions.
a2cf7d
a2cf7d
If compiled with z196 zarch support, the convert-to-fixed instruction
a2cf7d
is used to implement lrint, lrintf, lrintl.
a2cf7d
Otherwise the common-code implementation is used.
a2cf7d
a2cf7d
(cherry picked from commit e3f07622209c1b4436ef364b134dfd2cd4ca9976)
a2cf7d
---
a2cf7d
 sysdeps/s390/fpu/s_lrint.c  | 55 ++++++++++++++++++++++++++++++++++++
a2cf7d
 sysdeps/s390/fpu/s_lrintf.c | 55 ++++++++++++++++++++++++++++++++++++
a2cf7d
 sysdeps/s390/fpu/s_lrintl.c | 56 +++++++++++++++++++++++++++++++++++++
a2cf7d
 3 files changed, 166 insertions(+)
a2cf7d
 create mode 100644 sysdeps/s390/fpu/s_lrint.c
a2cf7d
 create mode 100644 sysdeps/s390/fpu/s_lrintf.c
a2cf7d
 create mode 100644 sysdeps/s390/fpu/s_lrintl.c
a2cf7d
a2cf7d
diff --git a/sysdeps/s390/fpu/s_lrint.c b/sysdeps/s390/fpu/s_lrint.c
a2cf7d
new file mode 100644
a2cf7d
index 0000000000..7be60665b5
a2cf7d
--- /dev/null
a2cf7d
+++ b/sysdeps/s390/fpu/s_lrint.c
a2cf7d
@@ -0,0 +1,55 @@
a2cf7d
+/* lrint() - S390 version.
a2cf7d
+   Copyright (C) 2019 Free Software Foundation, Inc.
a2cf7d
+
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 License as
a2cf7d
+   published by the Free Software Foundation; either version 2.1 of the
a2cf7d
+   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
+#ifdef HAVE_S390_MIN_Z196_ZARCH_ASM_SUPPORT
a2cf7d
+# include <math.h>
a2cf7d
+# include <libm-alias-double.h>
a2cf7d
+
a2cf7d
+/* The sizeof (long int) differs between s390x (8byte) and s390 (4byte).
a2cf7d
+   Thus we need different instructions as the target size is encoded there.
a2cf7d
+   Note: On s390 this instruction is only used if build with -mzarch.  */
a2cf7d
+# ifdef __s390x__
a2cf7d
+#  define INSN "cgdbra"
a2cf7d
+# else
a2cf7d
+#  define INSN "cfdbra"
a2cf7d
+# endif
a2cf7d
+
a2cf7d
+long int
a2cf7d
+__lrint (double x)
a2cf7d
+{
a2cf7d
+  long int y;
a2cf7d
+  /* The z196 zarch "convert to fixed" (cgdbra) instruction is rounding
a2cf7d
+     according to current rounding mode (M3-field: 0).
a2cf7d
+     First convert x with suppressed inexact exception and check if the
a2cf7d
+     resulting value is beyond the target limits (indicated by cc=3;
a2cf7d
+     Note: a nan is also indicated by cc=3).
a2cf7d
+     If the resulting value is within the target limits, redo
a2cf7d
+     without suppressing the inexact exception.  */
a2cf7d
+  __asm__ (INSN " %0,0,%1,4 \n\t"
a2cf7d
+	   "jo 1f \n\t"
a2cf7d
+	   INSN " %0,0,%1,0 \n\t"
a2cf7d
+	   "1:"
a2cf7d
+	   : "=&d" (y) : "f" (x) : "cc");
a2cf7d
+  return y;
a2cf7d
+}
a2cf7d
+libm_alias_double (__lrint, lrint)
a2cf7d
+
a2cf7d
+#else
a2cf7d
+# include <sysdeps/ieee754/dbl-64/s_lrint.c>
a2cf7d
+#endif
a2cf7d
diff --git a/sysdeps/s390/fpu/s_lrintf.c b/sysdeps/s390/fpu/s_lrintf.c
a2cf7d
new file mode 100644
a2cf7d
index 0000000000..d6a2a4081a
a2cf7d
--- /dev/null
a2cf7d
+++ b/sysdeps/s390/fpu/s_lrintf.c
a2cf7d
@@ -0,0 +1,55 @@
a2cf7d
+/* lrintf() - S390 version.
a2cf7d
+   Copyright (C) 2019 Free Software Foundation, Inc.
a2cf7d
+
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 License as
a2cf7d
+   published by the Free Software Foundation; either version 2.1 of the
a2cf7d
+   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
+#ifdef HAVE_S390_MIN_Z196_ZARCH_ASM_SUPPORT
a2cf7d
+# include <math.h>
a2cf7d
+# include <libm-alias-float.h>
a2cf7d
+
a2cf7d
+/* The sizeof (long int) differs between s390x (8byte) and s390 (4byte).
a2cf7d
+   Thus we need different instructions as the target size is encoded there.
a2cf7d
+   Note: On s390 this instruction is only used if build with -mzarch.  */
a2cf7d
+# ifdef __s390x__
a2cf7d
+#  define INSN "cgebra"
a2cf7d
+# else
a2cf7d
+#  define INSN "cfebra"
a2cf7d
+# endif
a2cf7d
+
a2cf7d
+long int
a2cf7d
+__lrintf (float x)
a2cf7d
+{
a2cf7d
+  long int y;
a2cf7d
+  /* The z196 zarch "convert to fixed" (cgebra) instruction is rounding
a2cf7d
+     according to current rounding mode (M3-field: 0).
a2cf7d
+     First convert x with suppressed inexact exception and check if the
a2cf7d
+     resulting value is beyond the target limits (indicated by cc=3;
a2cf7d
+     Note: a nan is also indicated by cc=3).
a2cf7d
+     If the resulting value is within the target limits, redo
a2cf7d
+     without suppressing the inexact exception.  */
a2cf7d
+  __asm__ (INSN " %0,0,%1,4 \n\t"
a2cf7d
+	   "jo 1f \n\t"
a2cf7d
+	   INSN " %0,0,%1,0 \n\t"
a2cf7d
+	   "1:"
a2cf7d
+	   : "=&d" (y) : "f" (x) : "cc");
a2cf7d
+  return y;
a2cf7d
+}
a2cf7d
+libm_alias_float (__lrint, lrint)
a2cf7d
+
a2cf7d
+#else
a2cf7d
+# include <sysdeps/ieee754/flt-32/s_lrintf.c>
a2cf7d
+#endif
a2cf7d
diff --git a/sysdeps/s390/fpu/s_lrintl.c b/sysdeps/s390/fpu/s_lrintl.c
a2cf7d
new file mode 100644
a2cf7d
index 0000000000..2d386ecff9
a2cf7d
--- /dev/null
a2cf7d
+++ b/sysdeps/s390/fpu/s_lrintl.c
a2cf7d
@@ -0,0 +1,56 @@
a2cf7d
+/* lrintl() - S390 version.
a2cf7d
+   Copyright (C) 2019 Free Software Foundation, Inc.
a2cf7d
+
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 License as
a2cf7d
+   published by the Free Software Foundation; either version 2.1 of the
a2cf7d
+   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
+#ifdef HAVE_S390_MIN_Z196_ZARCH_ASM_SUPPORT
a2cf7d
+# include <math.h>
a2cf7d
+# include <math_private.h>
a2cf7d
+# include <libm-alias-ldouble.h>
a2cf7d
+
a2cf7d
+/* The sizeof (long int) differs between s390x (8byte) and s390 (4byte).
a2cf7d
+   Thus we need different instructions as the target size is encoded there.
a2cf7d
+   Note: On s390 this instruction is only used if build with -mzarch.  */
a2cf7d
+# ifdef __s390x__
a2cf7d
+#  define INSN "cgxbra"
a2cf7d
+# else
a2cf7d
+#  define INSN "cfxbra"
a2cf7d
+# endif
a2cf7d
+
a2cf7d
+long int
a2cf7d
+__lrintl (_Float128 x)
a2cf7d
+{
a2cf7d
+  long int y;
a2cf7d
+  /* The z196 zarch "convert to fixed" (cgxbra) instruction is rounding
a2cf7d
+     according to current rounding mode (M3-field: 0).
a2cf7d
+     First convert x with suppressed inexact exception and check if the
a2cf7d
+     resulting value is beyond the target limits (indicated by cc=3;
a2cf7d
+     Note: a nan is also indicated by cc=3).
a2cf7d
+     If the resulting value is within the target limits, redo
a2cf7d
+     without suppressing the inexact exception.  */
a2cf7d
+  __asm__ (INSN " %0,0,%1,4 \n\t"
a2cf7d
+	   "jo 1f \n\t"
a2cf7d
+	   INSN " %0,0,%1,0 \n\t"
a2cf7d
+	   "1:"
a2cf7d
+	   : "=&d" (y) : "f" (x) : "cc");
a2cf7d
+  return y;
a2cf7d
+}
a2cf7d
+libm_alias_ldouble (__lrint, lrint)
a2cf7d
+
a2cf7d
+#else
a2cf7d
+# include <sysdeps/ieee754/ldbl-128/s_lrintl.c>
a2cf7d
+#endif
a2cf7d
-- 
a2cf7d
2.18.2
a2cf7d