From: Palmer Dabbelt <palmer@rivosinc.com>
To: libc-alpha@sourceware.org, Vineet Gupta <vineetg@rivosinc.com>
Subject: [RFC] RISC-V: Implement {convert,round}toint()
Date: Wed, 3 Aug 2022 10:42:59 -0700 [thread overview]
Message-ID: <20220803174258.4235-1-palmer@rivosinc.com> (raw)
We currently have fairly inefficient rounding sequences on RISC-V
because we lack direct float->float round instructions. This results in
a bunch of unnecessary handling of FP exceptions in the exp() and pow().
Luckily TOINT_INTRINSICS seems to exist in order to handle exactly these
problems.
---
Thanks to Vineet for finding this one. I haven't had a chance to test
it yet, but I figured it'd be best to send this out as an RFC so it
doesn't get lost.
---
sysdeps/riscv/rvd/math_private.h | 54 ++++++++++++++++++++++++++++++++
1 file changed, 54 insertions(+)
create mode 100644 sysdeps/riscv/rvd/math_private.h
diff --git a/sysdeps/riscv/rvd/math_private.h b/sysdeps/riscv/rvd/math_private.h
new file mode 100644
index 0000000000..74a5aef07c
--- /dev/null
+++ b/sysdeps/riscv/rvd/math_private.h
@@ -0,0 +1,54 @@
+/* Configure optimized libm functions. RISC-V version.
+ Copyright (C) 2017-2022 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/>. */
+
+#ifndef RISCV_MATH_PRIVATE_H
+#define RISCV_MATH_PRIVATE_H 1
+
+#include <stdint.h>
+#include <math.h>
+
+/* Use inline round and lround instructions. */
+#define TOINT_INTRINSICS 1
+
+/* The results of these two functions only need to be specified if they can be
+ representable in an int32_t. The rounding just has to be consistent with
+ each other, here we're using the dynamic rounding mode under the assumption
+ that callers avoid changing it. */
+static inline int32_t
+converttoint (double_t x)
+{
+ int32_t o;
+ /* This returns a poorly-formed int32_t when the input exceeds its range.
+ That's a pretty hefty use of the unspecified behavior, as it also breaks
+ the ABI, but it's slightly faster. */
+ __asm__ ("fcvt.w.d %0, %1" : "=r"(o) : "f"(x));
+ return o;
+}
+
+static inline double_t
+roundtoint (double_t x)
+{
+ double o;
+ int32_t i = converttoint(x);
+ __asm__ ("fcvt.d.w %0, %1" : "=f"(o) : "r"(i));
+ return o;
+}
+
+#include_next <math_private.h>
+
+#endif
--
2.34.1
next reply other threads:[~2022-08-03 17:43 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-03 17:42 Palmer Dabbelt [this message]
2023-01-25 0:53 ` Vineet Gupta
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=20220803174258.4235-1-palmer@rivosinc.com \
--to=palmer@rivosinc.com \
--cc=libc-alpha@sourceware.org \
--cc=vineetg@rivosinc.com \
/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).