Hi Michael,
On Wed, Oct 15, 2025 at 04:40:08PM +0200, Michael Matz wrote:
> > > T
> > > bit_ceil(T x)
> > > {
> > > return 1 + (_Maxof(T) >> leading_zeros(x));
> > > }
>
> FWIW, this converts to T on return, ...
>
> > And I don't think we want to support bool in <stdbit.h>. It doesn't
> > behave like all the other unsigned integer types:
> >
> > alx@devuan:~/tmp$ cat bc.c
> > #include <stdbool.h>
> > #include <stdio.h>
> >
> > #define bit_ceil_b(x) ((bool)1 + ((bool)1 >> (x == 0 ? 1 : 0)))
>
> ... while this doesn't. So it's not equivalent, add an outer cast to bool
> to make it so, and then you'd get:
>
> $ ./a.out
> 1
> 0
> 1
> 1True. Which makes it even worse: 2 at least is a valid power response for bit_ceil(), just as 0 is also a valid response, if we consider wrapping arithmetics, but 1 is not. That is, 1 is not the smallest power of two larger than 1. > which is exactly the difference between wrapping (uint1) vs saturating > (bool) plus. As the original expression contains an obvious wrap around > for x==maxof(T), T's behaviour on overflow matters, and I don't find that > surprising or sufficiently different to exclude bool from anything where > it naturally can be assigned meaning. signed integer types also have > different behaviour on wrap-around, so to C programmers it shouldn't come > as a surprise that arithmetic expressions need to be formulated carefully. Signed integers are a different category than unsigned integers, right. And as we're seeing, bool belongs in a third class of integer types, in which it is the only member. It doesn't belong in the class of unsigned integer types (which is BTW something under discussion in the C Committee at the moment). Within the signed and unsigned classes of integers, it makes sense to have an operator that gives the maximum and minimum values, to be able to overload macros to work with arbitrary integers within each class. But within the boolean class of integers, it makes no sense to use a generic operator to get the limits, as there's only one type: bool. Thus, you just hardcode true and false. And I have yet to see a macro that would make sense for all three classes of integer types (signed, unsigned, and boolean). Have a lovely day! Alex -- <https://www.alejandro-colomar.es> Use port 80 (that is, <...:80/>).
signature.asc
Description: PGP signature
