mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: [PATCH] math: optimize lrint on 32bit targets
Date: Mon, 23 Sep 2019 16:42:05 -0400	[thread overview]
Message-ID: <20190923204205.GO9017@brightrain.aerifal.cx> (raw)
In-Reply-To: <20190923183818.GE22009@port70.net>

On Mon, Sep 23, 2019 at 08:38:19PM +0200, Szabolcs Nagy wrote:
> * Rich Felker <dalias@libc.org> [2019-09-23 13:40:29 -0400]:
> > On Sun, Sep 22, 2019 at 10:43:35PM +0200, Szabolcs Nagy wrote:
> > > +long lrint(double x)
> > > +{
> > > +	uint32_t abstop = asuint64(x)>>32 & 0x7fffffff;
> > > +	uint64_t sign = asuint64(x) & (1ULL << 63);
> > > +
> > > +	if (abstop < 0x41dfffff) {
> > > +		/* |x| < 0x7ffffc00, no overflow */
> > > +		double_t toint = asdouble(asuint64(1/EPS) | sign);
> > > +		double_t y = x + toint - toint;
> > > +		return (long)y;
> > > +	}
> > > +	return lrint_slow(x);
> > > +}
> > >  #else
> > >  long lrint(double x)
> > >  {
> > 
> > This code should be considerably faster than calling rint on 64-bit
> > archs too, no? I wonder if it should be something like (untested,
> > written inline here):
> 
> yeah i'd expect it to be a bit faster, but e.g. a
> target may prefer sign?-1/EPS:1/EPS to 1/EPS|sign,

Yes, perhaps. But this will be a lot less of a difference than an
extra call frame I think.

> and you need a threshold check even if there is no
> inexact overflow issue:

Ah yes.

> > long lrint(double x)
> > {
> > 	uint32_t abstop = asuint64(x)>>32 & 0x7fffffff;
> > 	uint64_t sign = asuint64(x) & (1ULL << 63);
> > 
> > #if LONG_MAX < 1U<<53 && defined(FE_INEXACT)
> > 	if (abstop >= 0x41dfffff) return lrint_slow(x);
> 
> #else
> 	if (abstop >= 0x43300000) return (long)x;
> 	/* |x| < 2^52 <= 1/EPS */
> 
> > #endif
> > 	/* |x| < 0x7ffffc00, no overflow */
> > 	double_t toint = asdouble(asuint64(1/EPS) | sign);
> > 	double_t y = x + toint - toint;
> > 	return (long)y;
> > }
> 
> i can try to benchmark this (although on x86_64 and
> aarch64 there is single instruction lrint so i can
> only benchmark machines where this is not relevant).

Yeah I think it's mainly riscv64 and s390x that might benefit right
now. Everything else 64-bit seems to have an optimized one.

Rich


      reply	other threads:[~2019-09-23 20:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-21 15:52 Szabolcs Nagy
2019-09-22 20:43 ` Szabolcs Nagy
2019-09-23 14:24   ` Rich Felker
2019-09-23 14:54     ` Szabolcs Nagy
2019-09-23 16:08       ` Rich Felker
2019-09-23 17:40   ` Rich Felker
2019-09-23 18:38     ` Szabolcs Nagy
2019-09-23 20:42       ` Rich Felker [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=20190923204205.GO9017@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).