Re: [PATCH] newlib/libc/stdbit: Fix static assertions for 16-bit int

Jonathan Wakely <[email protected]> Mon, 20 Apr 2026 21:32:18 +0100
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
On Mon, 20 Apr 2026 at 14:47 -0500, Joel Sherrill wrote:
>Is there a conditional defined when building newlib to know you are inside
>newlib? I'd like to guard changes from the original FreeBSD code if that's
>possible. We do that inside RTEMS and it makes updates easier.

You could add:

--- a/newlib/libc/stdbit/stdbit_internal.h
+++ b/newlib/libc/stdbit/stdbit_internal.h
@@ -44,4 +44,6 @@
  #define ULLONG_WIDTH __LONG_LONG_WIDTH__
  #endif
  
+#define newlib_stdbit_h 1
+
  #endif

And then test for it in stdc_bit_ceil.c etc. because that macro would
not be defined by the FreeBSD version of stdbit_internal.h

Or propose the fixes upstream to FreeBSD, but they might not be
interested in supporting 16-bit targets.


>--joel
>
>On Mon, Apr 20, 2026, 2:32 PM Corinna Vinschen <[email protected]> wrote:
>
>>
>> [CC Joel Sherrill, please review]
>>
>> On Apr 20 16:24, Jonathan Wakely wrote:
>> > All the USHRT_WIDTH < UINT_WIDTH assertions fail for 16-bit int targets,
>> > such as msp430-elf. Remove those static assertions and instead fix the
>> > stdc_xxx_us functions to work correctly when USHRT_WIDTH == UINT_WIDTH.
>> >
>> > Signed-off-by: Jonathan Wakely <[email protected]>
>> > ---
>> >
>> > This is untested, except to check that it now compiles on msp430-elf and
>> > still compiles on arm-eabi.
>> >
>> >  newlib/libc/stdbit/stdc_bit_ceil.c       |  9 +++++----
>> >  newlib/libc/stdbit/stdc_leading_ones.c   | 10 ++++++----
>> >  newlib/libc/stdbit/stdc_leading_zeros.c  | 10 ++++++----
>> >  newlib/libc/stdbit/stdc_trailing_ones.c  | 10 ++++++----
>> >  newlib/libc/stdbit/stdc_trailing_zeros.c | 11 +++++++----
>> >  5 files changed, 30 insertions(+), 20 deletions(-)
>> > [...]
>> >  unsigned int
>> >  stdc_leading_ones_us(unsigned short x)
>> >  {
>> > +#if USHRT_WIDTH == UINT_WIDTH
>> > +     /* Avoid triggering undefined behavior if x == 0. */
>> > +     if (x == ~0U)
>>
>> ~0 is not 0...
>>
>>
>> Thanks,
>> Corinna
>>