All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: linux-toolchains@vger.kernel.org
Subject: Re: ilog2 vs. GCC inlining heuristics
Date: Wed, 21 Oct 2020 15:45:13 +0200	[thread overview]
Message-ID: <20201021134513.GD2176@tucnak> (raw)
In-Reply-To: <14215e71-cd43-81aa-1965-0b0a6fb667fa@csgroup.eu>

On Wed, Oct 21, 2020 at 03:36:14PM +0200, Christophe Leroy wrote:
> > --- a/include/linux/log2.h
> > +++ b/include/linux/log2.h
> > @@ -156,7 +156,8 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
> >   #define ilog2(n) \
> >   ( \
> >   	__builtin_constant_p(n) ?	\
> > -	const_ilog2(n) :		\
> > +	((n) < 2 ? 0 :			\
> > +	 63 - __builtin_clzll (n)) :	\
> 
> That is likely to work only on 64 bits targets.

No, it should work on all.  __builtin_clzll argument is long long, which
should be 64-bit on all architectures Linux kernel supports (and all
architectures GCC supports - ok, avr with -mint8 doesn't, but it isn't C
requirements compatible in that case).
const_ilog2 also uses long long and will work with any long long or
unsigned long long value (but e.g. not with __uint128_t values on 64-bit
architectures, guess the kernel doesn't need that though).
And similarly, for the non-constant operands the sizeof below ensures
that __ilog2_u64 is used (even on 32-bit arches) and will handle that.

> On 32 bits targets I guess it needs to be  31 - __builtin_clz (n), doesn't it ?
> 
> >   	(sizeof(n) <= 4) ?		\
> >   	__ilog2_u32(n) :		\
> >   	__ilog2_u64(n)			\
> > 
> > 	Jakub
> > 

	Jakub


  reply	other threads:[~2020-10-21 13:45 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-21 13:27 ilog2 vs. GCC inlining heuristics Jakub Jelinek
2020-10-21 13:36 ` Christophe Leroy
2020-10-21 13:45   ` Jakub Jelinek [this message]
2020-10-21 15:19 ` Peter Zijlstra
2020-10-21 18:40   ` Christophe Leroy
2020-10-22  4:01     ` Randy Dunlap
2020-10-22  7:12       ` Jakub Jelinek
2020-11-20 12:33 ` [tip: core/core] " tip-bot2 for Jakub Jelinek
2020-11-20 12:41   ` Peter Zijlstra

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=20201021134513.GD2176@tucnak \
    --to=jakub@redhat.com \
    --cc=christophe.leroy@csgroup.eu \
    --cc=linux-toolchains@vger.kernel.org \
    /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.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.