public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Florian Weimer <fweimer@redhat.com>
Cc: Joseph Myers <joseph@codesourcery.com>, libc-alpha@sourceware.org
Subject: Re: [RFC] stdbit.h implementation
Date: Sat, 18 Nov 2023 12:09:36 +0100	[thread overview]
Message-ID: <ZVibcNjrfAqdq/SA@tucnak> (raw)
In-Reply-To: <87cyw7nxl5.fsf@oldenburg.str.redhat.com>

On Sat, Nov 18, 2023 at 11:51:34AM +0100, Florian Weimer wrote:
> * Jakub Jelinek:
> 
> > On Sat, Nov 18, 2023 at 11:10:56AM +0100, Florian Weimer wrote:
> >> * Jakub Jelinek:
> >> 
> >> > +# define __stdc_type_width(x)				\
> >> > +  __builtin_popcountg ((__typeof (x)) ~(__typeof (x)) 0)
> >> 
> >> > +# define stdc_leading_zeros(x)				\
> >> > +  ((unsigned int)					\
> >> > +   __builtin_clzg (x, __stdc_type_width (x)))
> >> 
> >> These macros will not be very useful if they expand their arguments
> >> multiple times because the preprocessor output becomes too big if
> >> expressions are nested.  I think we learned that with the type-generic
> >> math facilities.
> >
> > That is the case of the previously posted macros (some of them) as well:
> > #define stdc_first_leading_one(x)                       \
> >   (sizeof (x) == 8 ? stdc_first_leading_one_ull (x)     \
> >    : sizeof (x) == 4 ? stdc_first_leading_one_ui (x)    \
> >    : sizeof (x) == 2 ? stdc_first_leading_one_us (x)    \
> >    : stdc_first_leading_one_uc (x))
> 
> > Do we really need to add compiler builtins for all those 14 type-generic
> > macros because of that?
> 
> Can we use auto?

Maybe __auto_type, I think while the header is C23 only, we want to make
it usable even in older C modes.

>   #define stdc_first_leading_one(x)                            \
>     ({auto __x = (x);                                          \
>       sizeof (__x) == 8 ? stdc_first_leading_one_ull (__x)     \
>       : sizeof (__x) == 4 ? stdc_first_leading_one_ui (__x)    \
>       : sizeof (__x) == 2 ? stdc_first_leading_one_us (__x)    \
>       : stdc_first_leading_one_uc (__x))
> 
> That would avoid multiple expansions.

I think Joseph wanted to avoid ({ }) as it isn't usable outside of functions
(nor in C++ in template argument lists).  Something like
int a = sizeof (stdc_first_leading_one (0ULL));
is valid C23 I believe, though any of the type-generic macro implementations
which use inline functions for something actually isn't usable in C constant
expressions.

If we wanted to build at least most of the stdc type-generic macros that
can't be done right now without preprocessing arguments more than once
through some smaller building blocks, I think we could have some
__builtin_clzg/__builtin_ctzg variants which would use as second argument
implicitly the bitwidth of the first argument and have some type-generic
builtin which would invert bits of argument (__builtin_invertg) -
~(x) is good enough for bitint types and int and larger, but not for
unsigned char/unsigned short.
Perhaps if we don't want to extend the two former builtins two 3 arguments
or add 2 new builtins for that, we could document that if the second
argument has some special type (not int), it would mean the bit width of
the first argument.  Something like (void *) 0, or 0.0, or
__builtin_type_width (), ...
I think with such extensions we could cover all but stdc_bit_width
(where we want to subtract clzg from the type's bit width) and
stdc_bit_floor/stdc_bit_ceil (which are just complicated).

	Jakub


  reply	other threads:[~2023-11-18 11:09 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-14 17:00 Jakub Jelinek
2023-11-14 18:02 ` Joseph Myers
2023-11-14 19:14   ` Jakub Jelinek
2023-11-14 21:52     ` Joseph Myers
2023-11-15 19:18       ` Andrew Pinski
2023-11-16 15:59       ` Jakub Jelinek
2023-11-16 16:01         ` Jakub Jelinek
2023-11-18 10:10         ` Florian Weimer
2023-11-18 10:22           ` Jakub Jelinek
2023-11-18 10:51             ` Florian Weimer
2023-11-18 11:09               ` Jakub Jelinek [this message]
2023-11-15 19:11     ` tavianator
2023-11-16 13:18       ` Jakub Jelinek
2023-11-14 19:58   ` Jakub Jelinek

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=ZVibcNjrfAqdq/SA@tucnak \
    --to=jakub@redhat.com \
    --cc=fweimer@redhat.com \
    --cc=joseph@codesourcery.com \
    --cc=libc-alpha@sourceware.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 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).