[RFC] RISC-V: Implement {convert,round}toint()

Palmer Dabbelt palmer@rivosinc.com
Wed Aug 3 17:42:59 GMT 2022


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



More information about the Libc-alpha mailing list