[PATCH] c: Add __builtin_stdc_bit_{width,floor,ceil} builtins

Jakub Jelinek jakub@redhat.com
Tue Nov 21 08:24:36 GMT 2023


On Mon, Nov 20, 2023 at 04:29:47PM +0100, Jakub Jelinek wrote:
> On Mon, Nov 20, 2023 at 04:03:07PM +0100, Jakub Jelinek wrote:
> > > Note that stdc_bit_ceil now has defined behavior (return 0) on overflow: 
> > > CD2 comment FR-135 was accepted for the DIS at the June WG14 meeting.  
> > > This affects both the documentation and the implementation, as they need 
> > > to avoid an undefined shift by the width of the type.  That's why my 
> > > stdbit.h implementations have two shifts (not claiming that's necessarily 
> > > the optimal way of ensuring the correct result in the overflow case).
> > > 
> > >   return __x <= 1 ? 1 : ((uint64_t) 1) << (__bw64_inline (__x - 1) - 1) << 1;
> > 
> > Given the feedback from Richi I've in the meantime reworked the patch to
> > add all 14 builtins (but because the enum rid is very close to 256 values
> > and with 14 new ones was already 7 too many, used one RID value for all 14
> > builtins (different spellings)).
> > 
> > Will need to rework it for CD2 FR-135 then...
> 
> Here it is updated to use that
> x <= 1 ? 1 : ((type) 2) << (prec - 1 - __builtin_clzg ((type) (x - 1)))
> I've mentioned.

Now successfully bootstrapped/regtested on x86_64-linux and i686-linux
on top of the https://gcc.gnu.org/pipermail/gcc-patches/2023-November/637540.html
patch (the bug was discovered while working on this patch).

> 2023-11-20  Jakub Jelinek  <jakub@redhat.com>
> 
> gcc/
> 	* doc/extend.texi (__builtin_stdc_bit_ceil, __builtin_stdc_bit_floor,
> 	__builtin_stdc_bit_width, __builtin_stdc_count_ones,
> 	__builtin_stdc_count_zeros, __builtin_stdc_first_leading_one,
> 	__builtin_stdc_first_leading_zero, __builtin_stdc_first_trailing_one,
> 	__builtin_stdc_first_trailing_zero, __builtin_stdc_has_single_bit,
> 	__builtin_stdc_leading_ones, __builtin_stdc_leading_zeros,
> 	__builtin_stdc_trailing_ones, __builtin_stdc_trailing_zeros): Document.
> gcc/c-family/
> 	* c-common.h (enum rid): Add RID_BUILTIN_STDC: New.
> 	* c-common.cc (c_common_reswords): Add __builtin_stdc_bit_ceil,
> 	__builtin_stdc_bit_floor, __builtin_stdc_bit_width,
> 	__builtin_stdc_count_ones, __builtin_stdc_count_zeros,
> 	__builtin_stdc_first_leading_one, __builtin_stdc_first_leading_zero,
> 	__builtin_stdc_first_trailing_one, __builtin_stdc_first_trailing_zero,
> 	__builtin_stdc_has_single_bit, __builtin_stdc_leading_ones,
> 	__builtin_stdc_leading_zeros, __builtin_stdc_trailing_ones and
> 	__builtin_stdc_trailing_zeros.  Move __builtin_assoc_barrier
> 	alphabetically earlier.
> gcc/c/
> 	* c-parser.cc (c_parser_postfix_expression): Handle RID_BUILTIN_STDC.
> 	* c-decl.cc (names_builtin_p): Likewise.
> gcc/testsuite/
> 	* gcc.dg/builtin-stdc-bit-1.c: New test.
> 	* gcc.dg/builtin-stdc-bit-2.c: New test.

	Jakub



More information about the Gcc-patches mailing list