Skip to content

wbN: A suffix for specifying the width of a bit-precise integer literal #129256

@alejandro-colomar

Description

@alejandro-colomar

Currently, we have no way of specifying the width of a bit-precise integer literal. We only have the wb suffix, which say "pick the smaller width possible".

That wb suffix is insufficient, as sometimes we need a type wider than the value we're using, if we're going to use for example shifts.

As an example, there's no good way (and by good, I mean something that doesn't involve casts) to create a value of width N (let's use 3 as a small example) where all bits are 1 except for the leftmost (or rightmost) one.

alx@debian:~/tmp$ cat bw.c 
int
main(void)
{
	unsigned _BitInt(3) i = ~0wb >> 1;

	return i;
}
alx@debian:~/tmp$ gcc -Wall -Wextra bw.c 
alx@debian:~/tmp$ ./a.out; echo $?
7
alx@debian:~/tmp$ clang -Weverything -Wno-c23-extensions -Wno-bit-int-extension bw.c 
bw.c:4:31: warning: implicit conversion changes signedness: '_BitInt(2)' to 'unsigned _BitInt(3)' [-Wsign-conversion]
    4 |         unsigned _BitInt(3) i = ~0wb >> 1;
      |                             ~   ~~~~~^~~~
1 warning generated.
alx@debian:~/tmp$ ./a.out; echo $?
7

It would make sense to be able to write it like this (IMO):

    unsigned _BitInt(3) i = ~0wb3u >> 1;  // stores the value 5 (0b011).

GCC bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119058
Cc: @AaronBallman

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions