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