dfa500
From 8e5e36d7ba097b8e110ab45794659156af182f54 Mon Sep 17 00:00:00 2001
dfa500
From: Stefan Liebler <stli@linux.ibm.com>
dfa500
Date: Wed, 11 Dec 2019 15:09:31 +0100
dfa500
Subject: [PATCH 27/28] S390: Implement libc_fe* macros.
dfa500
dfa500
This patch provides the s390 specific implementation for
dfa500
libc_feholdexcept, libc_fesetround, libc_feholdexcept_setround,
dfa500
libc_fetestexcept, libc_fesetenv, libc_feupdateenv_test,
dfa500
libc_feupdateenv, libc_feholdsetround_ctx, libc_feresetround_ctx,
dfa500
libc_feholdsetround_noex_ctx and libc_feresetround_noex_ctx.
dfa500
dfa500
(cherry picked from commit 7c94d036c17dfd352d11e9bf98e5d84122c1f95e)
dfa500
Note: glibc-2.28 does not have a generic fenv_private.h.
dfa500
Therefore include_next does not work. Instead fenv_private.h needs to
dfa500
be included in the s390 specific mathp_private.h just before including
dfa500
the generic math_private.h.
dfa500
As the s390 specific math_private.h is introduced with the backport of
dfa500
commit "S390: Implement roundtoint and converttoint and define TOINT_INTRINSICS.",
dfa500
the order of cherry-picking was changed compared to upstream!
dfa500
---
dfa500
 sysdeps/s390/fpu/fenv_private.h | 248 ++++++++++++++++++++++++++++++++
dfa500
 sysdeps/s390/fpu/math_private.h |   1 +
dfa500
 2 files changed, 249 insertions(+)
dfa500
 create mode 100644 sysdeps/s390/fpu/fenv_private.h
dfa500
dfa500
diff --git a/sysdeps/s390/fpu/fenv_private.h b/sysdeps/s390/fpu/fenv_private.h
dfa500
new file mode 100644
dfa500
index 0000000000..8899f8f434
dfa500
--- /dev/null
dfa500
+++ b/sysdeps/s390/fpu/fenv_private.h
dfa500
@@ -0,0 +1,248 @@
dfa500
+/* Private floating point rounding and exceptions handling.  390/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 S390_FENV_PRIVATE_H
dfa500
+#define S390_FENV_PRIVATE_H 1
dfa500
+
dfa500
+#include <fenv.h>
dfa500
+#include <fenv_libc.h>
dfa500
+#include <fpu_control.h>
dfa500
+
dfa500
+static __always_inline void
dfa500
+libc_feholdexcept_s390 (fenv_t *envp)
dfa500
+{
dfa500
+  fpu_control_t fpc, fpc_new;
dfa500
+
dfa500
+  /* Store the environment.  */
dfa500
+  _FPU_GETCW (fpc);
dfa500
+  envp->__fpc = fpc;
dfa500
+
dfa500
+  /* Clear the current exception flags and dxc field.
dfa500
+     Hold from generating fpu exceptions temporarily.  */
dfa500
+  fpc_new = fpc & ~(FPC_FLAGS_MASK | FPC_DXC_MASK | FPC_EXCEPTION_MASK);
dfa500
+
dfa500
+  /* Only set new environment if it has changed.  */
dfa500
+  if (fpc_new != fpc)
dfa500
+    _FPU_SETCW (fpc_new);
dfa500
+}
dfa500
+
dfa500
+#define libc_feholdexcept  libc_feholdexcept_s390
dfa500
+#define libc_feholdexceptf libc_feholdexcept_s390
dfa500
+#define libc_feholdexceptl libc_feholdexcept_s390
dfa500
+
dfa500
+static __always_inline void
dfa500
+libc_fesetround_s390 (int round)
dfa500
+{
dfa500
+  __asm__ __volatile__ ("srnm 0(%0)" : : "a" (round));
dfa500
+}
dfa500
+
dfa500
+#define libc_fesetround  libc_fesetround_s390
dfa500
+#define libc_fesetroundf libc_fesetround_s390
dfa500
+#define libc_fesetroundl libc_fesetround_s390
dfa500
+
dfa500
+static __always_inline void
dfa500
+libc_feholdexcept_setround_s390 (fenv_t *envp, int r)
dfa500
+{
dfa500
+  fpu_control_t fpc, fpc_new;
dfa500
+
dfa500
+  _FPU_GETCW (fpc);
dfa500
+  envp->__fpc = fpc;
dfa500
+
dfa500
+  /* Clear the current exception flags and dxc field.
dfa500
+     Hold from generating fpu exceptions temporarily.
dfa500
+     Reset rounding mode bits.  */
dfa500
+  fpc_new = fpc & ~(FPC_FLAGS_MASK | FPC_DXC_MASK | FPC_EXCEPTION_MASK
dfa500
+		    | FPC_RM_MASK);
dfa500
+
dfa500
+  /* Set new rounding mode.  */
dfa500
+  fpc_new |= (r & FPC_RM_MASK);
dfa500
+
dfa500
+  /* Only set new environment if it has changed.  */
dfa500
+  if (fpc_new != fpc)
dfa500
+    _FPU_SETCW (fpc_new);
dfa500
+}
dfa500
+
dfa500
+#define libc_feholdexcept_setround  libc_feholdexcept_setround_s390
dfa500
+#define libc_feholdexcept_setroundf libc_feholdexcept_setround_s390
dfa500
+#define libc_feholdexcept_setroundl libc_feholdexcept_setround_s390
dfa500
+
dfa500
+static __always_inline int
dfa500
+libc_fetestexcept_s390 (int excepts)
dfa500
+{
dfa500
+  int res;
dfa500
+  fexcept_t fpc;
dfa500
+
dfa500
+  _FPU_GETCW (fpc);
dfa500
+
dfa500
+  /* Get current exceptions.  */
dfa500
+  res = (fpc >> FPC_FLAGS_SHIFT) & FE_ALL_EXCEPT;
dfa500
+  if ((fpc & FPC_NOT_FPU_EXCEPTION) == 0)
dfa500
+    /* Bits 6, 7 of dxc-byte are zero,
dfa500
+       thus bits 0-5 of dxc-byte correspond to the flag-bits.
dfa500
+       Evaluate flags and last dxc-exception-code.  */
dfa500
+    res |= (fpc >> FPC_DXC_SHIFT) & FE_ALL_EXCEPT;
dfa500
+
dfa500
+  return res & excepts;
dfa500
+}
dfa500
+
dfa500
+#define libc_fetestexcept  libc_fetestexcept_s390
dfa500
+#define libc_fetestexceptf libc_fetestexcept_s390
dfa500
+#define libc_fetestexceptl libc_fetestexcept_s390
dfa500
+
dfa500
+static __always_inline void
dfa500
+libc_fesetenv_s390 (const fenv_t *envp)
dfa500
+{
dfa500
+  _FPU_SETCW (envp->__fpc);
dfa500
+}
dfa500
+
dfa500
+#define libc_fesetenv  libc_fesetenv_s390
dfa500
+#define libc_fesetenvf libc_fesetenv_s390
dfa500
+#define libc_fesetenvl libc_fesetenv_s390
dfa500
+
dfa500
+static __always_inline int
dfa500
+libc_feupdateenv_test_s390 (const fenv_t *envp, int ex)
dfa500
+{
dfa500
+  /* Get the currently raised exceptions.  */
dfa500
+  int excepts;
dfa500
+  fexcept_t fpc_old;
dfa500
+
dfa500
+  _FPU_GETCW (fpc_old);
dfa500
+
dfa500
+  /* Get current exceptions.  */
dfa500
+  excepts = (fpc_old >> FPC_FLAGS_SHIFT) & FE_ALL_EXCEPT;
dfa500
+  if ((fpc_old & FPC_NOT_FPU_EXCEPTION) == 0)
dfa500
+    /* Bits 6, 7 of dxc-byte are zero,
dfa500
+       thus bits 0-5 of dxc-byte correspond to the flag-bits.
dfa500
+       Evaluate flags and last dxc-exception-code.  */
dfa500
+    excepts |= (fpc_old >> FPC_DXC_SHIFT) & FE_ALL_EXCEPT;
dfa500
+
dfa500
+  /* Merge the currently raised exceptions with those in envp.  */
dfa500
+  fpu_control_t fpc_new = envp->__fpc;
dfa500
+  fpc_new |= excepts << FPC_FLAGS_SHIFT;
dfa500
+
dfa500
+  /* Install the new fpc from envp.  */
dfa500
+  if (fpc_new != fpc_old)
dfa500
+    _FPU_SETCW (fpc_new);
dfa500
+
dfa500
+  /* Raise the exceptions if enabled in new fpc.  */
dfa500
+  if (__glibc_unlikely ((fpc_new >> FPC_EXCEPTION_MASK_SHIFT) & excepts))
dfa500
+    __feraiseexcept (excepts);
dfa500
+
dfa500
+  return excepts & ex;
dfa500
+}
dfa500
+
dfa500
+#define libc_feupdateenv_test  libc_feupdateenv_test_s390
dfa500
+#define libc_feupdateenv_testf libc_feupdateenv_test_s390
dfa500
+#define libc_feupdateenv_testl libc_feupdateenv_test_s390
dfa500
+
dfa500
+static __always_inline void
dfa500
+libc_feupdateenv_s390 (const fenv_t *envp)
dfa500
+{
dfa500
+  libc_feupdateenv_test_s390 (envp, 0);
dfa500
+}
dfa500
+
dfa500
+#define libc_feupdateenv  libc_feupdateenv_s390
dfa500
+#define libc_feupdateenvf libc_feupdateenv_s390
dfa500
+#define libc_feupdateenvl libc_feupdateenv_s390
dfa500
+
dfa500
+static __always_inline fenv_t
dfa500
+libc_handle_user_fenv_s390 (const fenv_t *envp)
dfa500
+{
dfa500
+  fenv_t env;
dfa500
+  if (envp == FE_DFL_ENV)
dfa500
+    {
dfa500
+      env.__fpc = _FPU_DEFAULT;
dfa500
+    }
dfa500
+  else if (envp == FE_NOMASK_ENV)
dfa500
+    {
dfa500
+      env.__fpc = FPC_EXCEPTION_MASK;
dfa500
+    }
dfa500
+  else
dfa500
+    env = (*envp);
dfa500
+
dfa500
+  return env;
dfa500
+}
dfa500
+
dfa500
+/* We have support for rounding mode context.  */
dfa500
+#define HAVE_RM_CTX 1
dfa500
+
dfa500
+static __always_inline void
dfa500
+libc_feholdsetround_s390_ctx (struct rm_ctx *ctx, int r)
dfa500
+{
dfa500
+  fpu_control_t fpc;
dfa500
+  int round;
dfa500
+
dfa500
+  _FPU_GETCW (fpc);
dfa500
+  ctx->env.__fpc = fpc;
dfa500
+
dfa500
+  /* Check whether rounding modes are different.  */
dfa500
+  round = fpc & FPC_RM_MASK;
dfa500
+
dfa500
+  /* Set the rounding mode if changed.  */
dfa500
+  if (__glibc_unlikely (round != r))
dfa500
+    {
dfa500
+      ctx->updated_status = true;
dfa500
+      libc_fesetround_s390 (r);
dfa500
+    }
dfa500
+  else
dfa500
+    ctx->updated_status = false;
dfa500
+}
dfa500
+
dfa500
+#define libc_feholdsetround_ctx		libc_feholdsetround_s390_ctx
dfa500
+#define libc_feholdsetroundf_ctx	libc_feholdsetround_s390_ctx
dfa500
+#define libc_feholdsetroundl_ctx	libc_feholdsetround_s390_ctx
dfa500
+
dfa500
+static __always_inline void
dfa500
+libc_feresetround_s390_ctx (struct rm_ctx *ctx)
dfa500
+{
dfa500
+  /* Restore the rounding mode if updated.  */
dfa500
+  if (__glibc_unlikely (ctx->updated_status))
dfa500
+    {
dfa500
+      fpu_control_t fpc;
dfa500
+      _FPU_GETCW (fpc);
dfa500
+      fpc = ctx->env.__fpc | (fpc & FPC_FLAGS_MASK);
dfa500
+      _FPU_SETCW (fpc);
dfa500
+    }
dfa500
+}
dfa500
+
dfa500
+#define libc_feresetround_ctx		libc_feresetround_s390_ctx
dfa500
+#define libc_feresetroundf_ctx		libc_feresetround_s390_ctx
dfa500
+#define libc_feresetroundl_ctx		libc_feresetround_s390_ctx
dfa500
+
dfa500
+static __always_inline void
dfa500
+libc_feholdsetround_noex_s390_ctx (struct rm_ctx *ctx, int r)
dfa500
+{
dfa500
+  libc_feholdexcept_setround_s390 (&ctx->env, r);
dfa500
+}
dfa500
+
dfa500
+#define libc_feholdsetround_noex_ctx	libc_feholdsetround_noex_s390_ctx
dfa500
+#define libc_feholdsetround_noexf_ctx	libc_feholdsetround_noex_s390_ctx
dfa500
+#define libc_feholdsetround_noexl_ctx	libc_feholdsetround_noex_s390_ctx
dfa500
+
dfa500
+static __always_inline void
dfa500
+libc_feresetround_noex_s390_ctx (struct rm_ctx *ctx)
dfa500
+{
dfa500
+  /* Restore exception flags and rounding mode.  */
dfa500
+  libc_fesetenv_s390 (&ctx->env);
dfa500
+}
dfa500
+
dfa500
+#define libc_feresetround_noex_ctx	libc_feresetround_noex_s390_ctx
dfa500
+#define libc_feresetround_noexf_ctx	libc_feresetround_noex_s390_ctx
dfa500
+#define libc_feresetround_noexl_ctx	libc_feresetround_noex_s390_ctx
dfa500
+
dfa500
+#endif
dfa500
diff --git a/sysdeps/s390/fpu/math_private.h b/sysdeps/s390/fpu/math_private.h
dfa500
index a1ae91a87c..f3c770d59a 100644
dfa500
--- a/sysdeps/s390/fpu/math_private.h
dfa500
+++ b/sysdeps/s390/fpu/math_private.h
dfa500
@@ -48,6 +48,7 @@ converttoint (double_t x)
dfa500
 }
dfa500
 #endif
dfa500
 
dfa500
+#include <fenv_private.h>
dfa500
 #include_next <math_private.h>
dfa500
 
dfa500
 #endif
dfa500
-- 
dfa500
2.18.2
dfa500