Menu ▾ ▴

#4147 Fix stdbit.h issues and add PIC16 stdbit runtime support

open
nobody
pic16 (2)
PIC16
5
1 day ago
2026-10-14
No

This patch fixes several issues in stdbit.h and makes the C23 bit utility helpers usable from the PIC16 library build.

Main fixes:

  • repair malformed typed stdc_* macros and wrong helper references;
  • fix incorrect inversions in stdc_first_leading_ / stdc_first_trailing_;
  • add missing _BitInt entries in __typewidth() and fix the _BitInt(56) width typo;
  • guard _BitInt-specific generic selections when the target does not support them;
  • add __stdc_bit_floorull() to avoid the previous shift-by-width implementation;
  • introduce __SDCC_ULLONG_WIDTH, using ULLONG_WIDTH where available and a C11-compatible fallback otherwise;
  • add the existing __stdc_* helper sources to the PIC16 libc18f.a build.

Validation:

  • all 8 __stdc_* helpers compile successfully for PIC16 with the normal --std-c11 library flags;
  • the rebuilt libc18f.a contains all helper objects;
  • an end-to-end PIC18F452 link using stdc_count_ones_ull() succeeds and produces a HEX file;
  • stdbit_all.c parses successfully for PIC16 in C23 mode;
  • __typewidth() was checked for missing and mismatched _BitInt(N) : N entries.
1 Attachments

Discussion

  • Maarten Brock

    Maarten Brock - 6 days ago

    Apart from the copyright in __stdc_bit_floorull.c this looks good to me.
    I doubt @spth Philipp sent you that file for you to attach it here.
    Though I do understand the code was practically copied from stdbit.h.
    Can the two of you please resolve this?

     
  • zeynelsagcan

    zeynelsagcan - 5 days ago

    Thanks, Maarten. You're right. Philipp did not send me this file. I created __stdc_bit_floorull.c using one of the existing _stdc* helper files as a template and accidentally left its copyright line in the new file. I'll correct that. I have attached the corrected file.

     

    Last edit: zeynelsagcan 5 days ago
    • Philipp Klaus Krause

      • I don't see the purpose of __SDCC_ULLONG_WIDTH - what's wrong with using ULLONG_WIDTH in the .c files? After all, those .c files are part of our own library and compiled as C11 or later. Also, I don't think we should #include <limits.h> in stdbit.h, since that would #define macros that the C standard does not list as defined for stdbit.h.

      • You mentioned some inversion being incorrect. Could you add regression test for them in sdcc/support/regression/tests? Ideally also including some tests that use the types that were affected by the __typewidth issues, so the tests cover both.

      • I've picked the _BitInt entry fixes in [r17062], and also added an entry for signed _BitInt(1).

       

      Related

      Commit: [r17062]

      • Maarten Brock

        Maarten Brock - 5 days ago
        • Note that ULLONG_WIDTH is only defined in limits.h for C23+, not for C11. But without including it this is no longer relevant. I have now added checks if ULLONG_WIDTH is defined and define it if it wasn't.
        • You also might have picked the macros that are missing a closing parenthesis. I have done so now.
        • I have removed signed _BitInt(1) again because it triggers warning 315. Either prevent the warning, make it pedantic or drop it.

        All in [r17065].

        I have not yet added __stdc_bit_floorull.c.

         

        Related

        Commit: [r17065]

        • Philipp Klaus Krause

          I also had noticed the warning, though unfortunately only after the commit. I'll make signed _BitInt(1) conditional on the standard version - I think we need it for correct results of these functions for signed _BitInt(1), which would be allowed in C2y.

           
          • Philipp Klaus Krause

            Done in [r17083].

             

            Related

            Commit: [r17083]

  • Maarten Brock

    Maarten Brock - 1 day ago

    @spth & @janet-chien will you come to an agreement on what to do with __stdc_bit_floorull.c, resolve it and close this one?

     

Log in to post a comment.