mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Gabriel Ravier <gabravier@gmail.com>
To: musl@lists.openwall.com, Markus Wichmann <nullplan@gmx.net>,
	Rich Felker <dalias@libc.org>
Subject: Re: [musl] [PATCH] fix return value of wcs{,n}cmp for near-limits signed wchar_t values
Date: Sat, 7 Jan 2023 02:51:34 +0100	[thread overview]
Message-ID: <21898826-8aa6-855c-9f85-eec59884631c@gmail.com> (raw)
In-Reply-To: <20230106165708.GC2032@voyager>

On 1/6/23 17:57, Markus Wichmann wrote:
> Hi all,
>
> I thought a bit more about it. It would be possible to compress the
> information we need somewhat like this:
>
> int64_t d = (int64_t)*l - *r;
> return (d >> 1) | (d & INT_MAX);
>
> The idea is that I need bit 31 of the output to equal bit 32 of the
> difference, and bits 0-30 of the output need to be nonzero exactly if
> bits 0-31 of the difference were nonzero. So it's one big disjunction.
>
> So I managed to find a branchless function. Not sure if it is actually
> worth it to implement it. The branching version is easier to understand
> and apparently gives better machine code on i386 and x86_64 (from just
> eyeballing it). It is not even implemented with branches on those
> architectures. And it is a micro-optimization, anyway.
>
> Ciao,
> Markus

If you really want branchless machine code I think that could be 
obtained from just doing something like `return (a > b) - (a < b)` (on a 
good compiler) but I didn't use that because I didn't know if it would 
even be faster and it seemed a little confusing.

Now I've looked into it a bit more and after a quick benchmark at 
(https://quick-bench.com/q/0D4dWdFpDnMGu-BJNYmLQfBLVeo) it appears like 
all the options are roughly equal: the biggest difference I could find 
between all the different methods was that on Clang my second method was 
~30% faster than my first one... but it then becomes slightly slower 
than the others when compiled on GCC. So personally I would stay on the 
first version as it seems the simplest and the other alternatives aren't 
particularly faster.


      reply	other threads:[~2023-01-07  1:51 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-04 15:07 Gabriel Ravier
2023-01-06  9:20 ` Markus Wichmann
2023-01-06  9:31   ` Markus Wichmann
2023-01-06 11:17   ` Rich Felker
2023-01-06 16:57     ` Markus Wichmann
2023-01-07  1:51       ` Gabriel Ravier [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=21898826-8aa6-855c-9f85-eec59884631c@gmail.com \
    --to=gabravier@gmail.com \
    --cc=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    --cc=nullplan@gmx.net \
    /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).