* Re: master 2fd38e5c496: Simplify and speed up numeric comparisons
[not found] ` <20240727100529.7278BC41F04@vcs2.savannah.gnu.org>
@ 2024-07-27 10:39 ` Pip Cet
2024-07-27 12:07 ` Mattias Engdegård
0 siblings, 1 reply; 2+ messages in thread
From: Pip Cet @ 2024-07-27 10:39 UTC (permalink / raw)
To: Mattias Engdegård; +Cc: Emacs Devel
On Saturday, July 27th, 2024 at 10:05, Mattias Engdegård via Mailing list for Emacs changes <emacs-diffs@gnu.org> wrote:
> branch: master
> commit 2fd38e5c496a2351a25e95df37a7900f6f80f22f
> Author: Mattias Engdegård mattiase@acm.org
>
> Commit: Mattias Engdegård mattiase@acm.org
>
> Simplify and speed up numeric comparisons
>
> This makes comparison functions (=, /=, <, <=, >, >=, min, max) quite
> a bit faster (10-20 %). Bytecode ops on fixnums are not affected,
> nor is `value<`.
How was this measured? I would expect GCC to perform similar optimizations with -flto, at least. IIUC, the "if" branches are currently optimized in order for byte-compiled code, though the fixnum <=> fixnum case should of course be moved after the fixnum <=> bignum case.
> else
> - i2 = mpz_sgn (*xbignum_val (num2));
> + {
> + int sgn = mpz_sgn (*xbignum_val (num2));
> + eq = sgn == 0;
> + lt = sgn > 0;
> + gt = sgn < 0;
> + }
As we use a fixnum for 0, mpz_sgn (*xbignum_val (num2)) can never be 0, IIUC, here and in a few other places. This would simplify the code further (but probably not be much of a speed up since we use bignums rarely), though "eassume (!eq)" might be a better way to write it.
I would have preferred arithcompare() to return 1.0, 0.0, -1.0, or NaN (or another set of four values) for the four valid orderings, rather than using three mutually-exclusive bits, but maybe I missed something about NaNs there. Are they mutually exclusive? If so, why do we test with "&" rather than "==" when we're testing a single bit?
I have a very hypothetical objection, too: I have code somewhere around here that replaces floats by mpq_t big rationals, and for those, equality comparisons are much cheaper than ordering them is :-)
Pip
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: master 2fd38e5c496: Simplify and speed up numeric comparisons
2024-07-27 10:39 ` master 2fd38e5c496: Simplify and speed up numeric comparisons Pip Cet
@ 2024-07-27 12:07 ` Mattias Engdegård
0 siblings, 0 replies; 2+ messages in thread
From: Mattias Engdegård @ 2024-07-27 12:07 UTC (permalink / raw)
To: Pip Cet; +Cc: Emacs Devel
27 juli 2024 kl. 12.39 skrev Pip Cet <pipcet@protonmail.com>:
> How was this measured?
Calling each function from Lisp with a bunch of floats chosen so that it won't return early. The actual gain in practice will inevitably be disappointing since my measurements are specifically designed to reduce overhead.
> I would expect GCC to perform similar optimizations with -flto, at least.
Well I wouldn't, and in any case I never build with LTO so there.
> the "if" branches are currently optimized in order for byte-compiled code, though the fixnum <=> fixnum case should of course be moved after the fixnum <=> bignum case.
Yes, probably, but it's not completely obvious. I'll see what can be done.
> As we use a fixnum for 0, mpz_sgn (*xbignum_val (num2)) can never be 0
Good point, I missed that one. Will amend.
> I would have preferred arithcompare() to return 1.0, 0.0, -1.0, or NaN (or another set of four values) for the four valid orderings, rather than using three mutually-exclusive bits, but maybe I missed something about NaNs there. Are they mutually exclusive? If so, why do we test with "&" rather than "==" when we're testing a single bit?
There is indeed more than one way to do it; using bits is the standard way when we want to test more than one at the same time, as for `<=` and `>=`. I used the bitwise AND for individual bits just for symmetry; it shouldn't matter.
(Loosely inspired by PowerPC, actually.)
NaN is of course terrible and we should be at least a little ashamed to use the 'standard' NaN semantics for comparisons (min and max are fine), but I didn't want to change the behaviour.
> I have a very hypothetical objection, too: I have code somewhere around here that replaces floats by mpq_t big rationals, and for those, equality comparisons are much cheaper than ordering them is
Pip, I'm so sorry. Had I known!
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-07-27 12:07 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <172207472641.7015.10274857436137393822@vcs2.savannah.gnu.org>
[not found] ` <20240727100529.7278BC41F04@vcs2.savannah.gnu.org>
2024-07-27 10:39 ` master 2fd38e5c496: Simplify and speed up numeric comparisons Pip Cet
2024-07-27 12:07 ` Mattias Engdegård
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
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).