From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/14709 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH] math: optimize lrint on 32bit targets Date: Mon, 23 Sep 2019 12:08:18 -0400 Message-ID: <20190923160818.GM9017@brightrain.aerifal.cx> References: <20190921155234.GA22009@port70.net> <20190922204335.GC22009@port70.net> <20190923142436.GL9017@brightrain.aerifal.cx> <20190923145423.GD22009@port70.net> Reply-To: musl@lists.openwall.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="137798"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mutt/1.5.21 (2010-09-15) To: musl@lists.openwall.com Original-X-From: musl-return-14725-gllmg-musl=m.gmane.org@lists.openwall.com Mon Sep 23 18:08:34 2019 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.89) (envelope-from ) id 1iCQt8-000Zlz-7R for gllmg-musl@m.gmane.org; Mon, 23 Sep 2019 18:08:34 +0200 Original-Received: (qmail 26470 invoked by uid 550); 23 Sep 2019 16:08:31 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 26452 invoked from network); 23 Sep 2019 16:08:31 -0000 Content-Disposition: inline In-Reply-To: <20190923145423.GD22009@port70.net> Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:14709 Archived-At: On Mon, Sep 23, 2019 at 04:54:23PM +0200, Szabolcs Nagy wrote: > * Rich Felker [2019-09-23 10:24:36 -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) > > > { > > > -- > > > > Looks good! Thanks for working on this. > > > > Does asuint64(1/EPS) compile to an integer constant rather than > > needing to load a floating point operand? I would assume so but just > > want to check, since otherwise it might make more sense to write this > > as an expression involving [L]DBL_MANT_DIG and integer bitshifts. > > i think if 1/EPS was rounding mode dependent then it would > be computed at runtime, but since it's an exact power-of-two > gcc const folds it (on arm there is no load, the value is put > together by bitops with immediates) Nice. I'm checking and yes it looks fine. Looks similar (modulo bad codegen in general) on sh4 too, chosen as another arch with hardfloat but no optimized lrint, and where there's not really any good way to write one -- the only conversion insn is a truncating one. Rich