public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Adhemerval Zanella <azanella@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc/azanella/mips-hw-fp-round] mips: Implement ceil with hardware floating-point rounding instruction
Date: Wed, 27 Dec 2023 13:23:38 +0000 (GMT)	[thread overview]
Message-ID: <20231227132338.63A7D3858C62@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=96af11c2077c95860317169303c3ce25bcc0f43f

commit 96af11c2077c95860317169303c3ce25bcc0f43f
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Tue Dec 26 16:42:04 2023 -0300

    mips: Implement ceil with hardware floating-point rounding instruction

Diff:
---
 sysdeps/mips/fpu/round_to_integer.h | 68 +++++++++++++++++++++++++++++++++++++
 sysdeps/mips/fpu/s_ceil.c           | 36 ++++++++++++++++++++
 sysdeps/mips/mips32/Implies         |  1 +
 sysdeps/mips/mips64/Implies         |  1 +
 4 files changed, 106 insertions(+)

diff --git a/sysdeps/mips/fpu/round_to_integer.h b/sysdeps/mips/fpu/round_to_integer.h
new file mode 100644
index 0000000000..7ac6783e54
--- /dev/null
+++ b/sysdeps/mips/fpu/round_to_integer.h
@@ -0,0 +1,68 @@
+/* Round to integer generic implementation.
+   Copyright (C) 2023 Free Software .
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library.  If not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _ROUND_TO_INTEGER_H
+#define _ROUND_TO_INTEGER_H
+
+#include <fenv_private.h>
+#include <sysdeps/ieee754/dbl-64/math_config.h>
+
+enum round_mode
+{
+  CEIL,
+};
+
+static inline double
+round_to_integer_double (enum round_mode mode, double x)
+{
+  uint64_t hx = asuint64 (x);
+  int ex = (hx & ~SIGN_MASK) >> MANTISSA_WIDTH;
+
+  double r = x;
+
+  fenv_t fe;
+  libc_feholdexcept (&fe);
+  switch (mode)
+  {
+  case CEIL:
+    asm ("ceil.l.d %0, %0" : "+f" (r));
+    break;
+  }
+  libc_fesetenv (&fe);
+
+  /* Inf or NaN.  */
+  if (__glibc_unlikely (ex == 0x7ff))
+    return x + x;
+
+  /* Number input and no fraction, return the number itself.  */
+  if (ex >= 1024 + 53)
+    return x;
+
+  asm ("cvt.d.l %0, %1\n" : "+f" (r));
+  /* The copysign seems to generate better code.  */
+#if 1
+  return copysign (r, x);
+#else
+  bool sign = hx & SIGN_MASK;
+  if (ex >= 1023)
+    return r;
+  return sign ? -r : r;
+#endif
+}
+
+#endif
diff --git a/sysdeps/mips/fpu/s_ceil.c b/sysdeps/mips/fpu/s_ceil.c
new file mode 100644
index 0000000000..ed79cb6dd9
--- /dev/null
+++ b/sysdeps/mips/fpu/s_ceil.c
@@ -0,0 +1,36 @@
+/* Round to nearest integer, away from zero.  MIPS version.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library.  If not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#if ((__mips_fpr == 64) \
+      && (__mips_hard_float == 1) \
+      && ((__mips == 32 && __mips_isa_rev > 1) || __mips == 64))
+
+#define NO_MATH_REDIRECT
+#include <math.h>
+#include <round_to_integer.h>
+#include <libm-alias-double.h>
+
+double
+__ceil (double x)
+{
+  return round_to_integer_double (CEIL, x);
+}
+libm_alias_double (__ceil, ceil)
+#else
+# include <sysdeps/ieee754/dbl-64/s_ceil.c>
+#endif
diff --git a/sysdeps/mips/mips32/Implies b/sysdeps/mips/mips32/Implies
index 6473f2517c..71b3678c6c 100644
--- a/sysdeps/mips/mips32/Implies
+++ b/sysdeps/mips/mips32/Implies
@@ -1,3 +1,4 @@
+mips/fpu
 mips/ieee754
 mips
 wordsize-32
diff --git a/sysdeps/mips/mips64/Implies b/sysdeps/mips/mips64/Implies
index 826ff1541f..8885ebd564 100644
--- a/sysdeps/mips/mips64/Implies
+++ b/sysdeps/mips/mips64/Implies
@@ -1,4 +1,5 @@
 # MIPS uses IEEE 754 floating point.
+mips/fpu
 mips/ieee754
 ieee754/flt-32
 ieee754/dbl-64

             reply	other threads:[~2023-12-27 13:23 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-27 13:23 Adhemerval Zanella [this message]
  -- strict thread matches above, loose matches on Subject: below --
2023-12-26 20:06 Adhemerval Zanella

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231227132338.63A7D3858C62@sourceware.org \
    --to=azanella@sourceware.org \
    --cc=glibc-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).