Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Date: Sat, 7 Jan 2023 02:51:34 +0100
From: Gabriel Ravier <gabravier@...il.com>
To: musl@...ts.openwall.com, Markus Wichmann <nullplan@....net>,
 Rich Felker <dalias@...c.org>
Subject: Re: [PATCH] fix return value of wcs{,n}cmp for near-limits
 signed wchar_t values

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.

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.