libc/stdbit/stdc_bit_ceil.c doesn't compile on msp430-elf
Jonathan Wakely <[email protected]> Mon, 20 Apr 2026 15:27:26 +0100
| Newsgroups | gmane.comp.lib.newlib |
|---|---|
| Message-ID | <CAH6eHdS+hMXufNCtDqBBfe1NuQ8hM=rx1NB1s8QegGidN1XE0A@mail.gmail.com> |
make[3]: Entering directory
'/home/jwakely/src/gcc/build-msp430/gcc-obj/msp430-elf/newlib'
CC libc/stdbit/libc_a-stdc_bit_ceil.o
/home/jwakely/src/gcc/gcc/newlib/libc/stdbit/stdc_bit_ceil.c:27:1:
error: static assertion failed: "stdc_bit_ceil_us needs USHRT_WIDTH <
UINT_WIDTH"
27 | _Static_assert(USHRT_WIDTH < UINT_WIDTH,
| ^~~~~~~~~~~~~~
This fails because both values are 16. Would this be a better implementation?
--- a/newlib/libc/stdbit/stdc_bit_ceil.c
+++ b/newlib/libc/stdbit/stdc_bit_ceil.c
@@ -23,16 +23,17 @@ stdc_bit_ceil_uc(unsigned char x)
return (1U << (UINT_WIDTH - __builtin_clz(x - 1)));
}
-/* Ensure we don't shift 1U out of range. */
-_Static_assert(USHRT_WIDTH < UINT_WIDTH,
- "stdc_bit_ceil_us needs USHRT_WIDTH < UINT_WIDTH");
-
unsigned short
stdc_bit_ceil_us(unsigned short x)
{
if (x <= 1)
return (1);
+#if USHRT_WIDTH == UINT_WIDTH
+ if (x > USHRT_MAX/2 + 1)
+ return (0);
+#endif
+
return (1U << (UINT_WIDTH - __builtin_clz(x - 1)));
}