Re: libc/stdbit/stdc_bit_ceil.c doesn't compile on msp430-elf
Jonathan Wakely <[email protected]> Mon, 20 Apr 2026 15:59:56 +0100
| Newsgroups | gmane.comp.lib.newlib |
|---|---|
| Message-ID | <CAH6eHdRheB4uCJQCvQLQ3rC5xGgLgA+9H_V60meVR3xrMikzxQ@mail.gmail.com> |
stdc_leading_ones.c fails similarly
CC libc/stdbit/libc_a-stdc_leading_ones.o
/home/jwakely/src/gcc/gcc/newlib/libc/stdbit/stdc_leading_ones.c:26:1:
error: static assertion failed: "stdc_leading_ones_us needs
USHRT_WIDTH < UINT_WIDTH"
26 | _Static_assert(USHRT_WIDTH < UINT_WIDTH,
| ^~~~~~~~~~~~~~
Which could be fixed by:
--- a/newlib/libc/stdbit/stdc_leading_ones.c
+++ b/newlib/libc/stdbit/stdc_leading_ones.c
@@ -22,13 +22,15 @@ stdc_leading_ones_uc(unsigned char x)
return (__builtin_clz(~(x << offset)));
}
-/* Avoid triggering undefined behavior if x == 0. */
-_Static_assert(USHRT_WIDTH < UINT_WIDTH,
- "stdc_leading_ones_us needs USHRT_WIDTH < UINT_WIDTH");
-
unsigned int
stdc_leading_ones_us(unsigned short x)
{
+#if USHRT_WIDTH == UINT_WIDTH
+ /* Avoid triggering undefined behavior if x == 0. */
+ if (x == ~0U)
+ return (USHRT_WIDTH);
+#endif
+
const int offset = UINT_WIDTH - USHRT_WIDTH;
return (__builtin_clz(~(x << offset)));
The comment is wrong though, isn't it? And the one on line 13. The UB
happens when x == ~0 not when x == 0.