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

Message ID 20220803174258.4235-1-palmer@rivosinc.com
State New
Headers
Series [RFC] RISC-V: Implement {convert,round}toint() |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Palmer Dabbelt Aug. 3, 2022, 5:42 p.m. UTC
  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
  

Comments

Vineet Gupta Jan. 25, 2023, 12:53 a.m. UTC | #1
On 8/3/22 10:42, Palmer Dabbelt wrote:
> 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

I gave this a spin (applied to glibc 2.36) but this causes a bunch of 
additional failures (similar results on both Qemu and Hifive Unmatched).

FAIL: math/test-float-ccos
FAIL: math/test-float-ccosh
FAIL: math/test-float-cexp
FAIL: math/test-float-cos
FAIL: math/test-float-cpow
FAIL: math/test-float-csin
FAIL: math/test-float-csinh
FAIL: math/test-float-ctan
FAIL: math/test-float-ctanh
FAIL: math/test-float-sin
FAIL: math/test-float-sincos
FAIL: math/test-float-tan
FAIL: math/test-float32-ccos
FAIL: math/test-float32-ccosh
FAIL: math/test-float32-cexp
FAIL: math/test-float32-cos
FAIL: math/test-float32-cpow
FAIL: math/test-float32-csin
FAIL: math/test-float32-csinh
FAIL: math/test-float32-ctan
FAIL: math/test-float32-ctanh
FAIL: math/test-float32-sin
FAIL: math/test-float32-sincos
FAIL: math/test-float32-tan

regen-ulps doesn't help since the loss of precision if way too high in 
some cases:

     cat workspace/glibc-exp-build/math/test-float-sin.out
     ...

     Failure: Test: sin_upward (0x8p+0)
     Result:
     is: 9.89324987e-01 0x1.fa88cep-1
     should be: 9.89358306e-01 0x1.fa8d2cp-1
     difference: 3.33189965e-05 0x1.178000p-15
     ulp : 559.0000
     max.ulp : 1.0000

So this will have to wait for the new instructions after all.

-Vineet
  

Patch

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