Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Thu, 12 Dec 2019 18:10:31 +0300 (MSK)
From: Alexander Monakov <amonakov@...ras.ru>
To: musl@...ts.openwall.com
Subject: Re: [PATCH] a_ctz_32: Instead of a subtraction by 31, use an
 xor

On Thu, 7 Nov 2019, Rosen Penev wrote:

> This reduces produced assembly from a mov and sub to a single xor.
> 
> Tested with godbolt: https://godbolt.org/z/Qz-Qr5
> 
> Signed-off-by: Rosen Penev <rosenp@...il.com>
> ---
>  src/internal/atomic.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/src/internal/atomic.h b/src/internal/atomic.h
> index f938879b..196b3fb1 100644
> --- a/src/internal/atomic.h
> +++ b/src/internal/atomic.h
> @@ -256,7 +256,7 @@ static inline void a_crash()
>  static inline int a_ctz_32(uint32_t x)
>  {
>  #ifdef a_clz_32
> -	return 31-a_clz_32(x&-x);
> +	return a_clz_32(x&-x)^31;

(since there was no other response...)

While certainly this is a nice improvement when considered in isolation,
looking at how this is used in the rest of the library (in ffs, ffsl, qsort)
reveals that returned value is used in further arithmetics.

So e.g. ffs/ffsl do a_ctz_l(i)+1, and +1 may be folded together with 31-...,
but not with the xor.  Thus the original is preferable.

Alexander

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.