public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Vineet Gupta <vineetg@rivosinc.com>
To: Palmer Dabbelt <palmer@rivosinc.com>, libc-alpha@sourceware.org
Cc: gnu-toolchain <gnu-toolchain@rivosinc.com>,
	Joseph Myers <joseph@codesourcery.com>,
	Szabolcs Nagy <szabolcs.nagy@arm.com>,
	Andrew Waterman <andrew@sifive.com>
Subject: Re: [RFC] RISC-V: Implement {convert,round}toint()
Date: Tue, 24 Jan 2023 16:53:54 -0800	[thread overview]
Message-ID: <97fc2219-d9ff-d808-352c-5e9f97087d66@rivosinc.com> (raw)
In-Reply-To: <20220803174258.4235-1-palmer@rivosinc.com>



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

      reply	other threads:[~2023-01-25  0:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-03 17:42 Palmer Dabbelt
2023-01-25  0:53 ` Vineet Gupta [this message]

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=97fc2219-d9ff-d808-352c-5e9f97087d66@rivosinc.com \
    --to=vineetg@rivosinc.com \
    --cc=andrew@sifive.com \
    --cc=gnu-toolchain@rivosinc.com \
    --cc=joseph@codesourcery.com \
    --cc=libc-alpha@sourceware.org \
    --cc=palmer@rivosinc.com \
    --cc=szabolcs.nagy@arm.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).