Menu

#1000 Modern bit utilities - stdbit.h

None
closed-fixed
None
5
2025-09-03
2025-09-01
No

ISO C23 introduced the header stdbit.h, providing various utilities IMO quite useful to SDCC users.
C2Y will expand upon this, providing functions for bit rotation, memory reversal, and endian-aware loads and stores, including some functionality requested by SDCC users in the past.
It looks like the interface and semantics have stabilized by now; though standardization of parameter forward declarations might still subsequent trigger changes to the interface if it happens sufficiently before C2Y deadlines.

I've started work on this, and the first batch should be ready to commit to trunk soon.

Related

Wiki: NGI0-Commons-SDCC

Discussion

  • Philipp Klaus Krause

    In [r15635], I implemented all the new functions, except for the bit rotation functions, including tests for them, and some additional tests for functions already added in C23. Since I expect users of these functions to care for speed, I've used inline to make them efficient.

    Remaining known issues:

    • Still missing rotation functions
    • [bugs:#3874] affects the endian-aware load and store functions; they are thus disabled unless --stack-auto is used (either explicitly or implicitly).
    • I see some calls to __memcpy being emittted in the generated code, even for ports where there should be builtin memcpy for better performance.
     

    Related

    Bugs: #3874
    Commit: [r15635]

  • Philipp Klaus Krause

    In [r15636], the missing functions have been implemented.

    Remaining known issues:

    • [bugs:#3874] affects the endian-aware load and store functions and the rotation functions; they are thus disabled unless --stack-auto is used (either explicitly or implicitly).
    • I see some calls to __memcpy being emittted in the generated code, even for ports where there should be builtin memcpy for better performance.
    • Some rotation functions do not get optimized into the ROT iCode.
     

    Related

    Bugs: #3874
    Commit: [r15636]


    Last edit: Philipp Klaus Krause 2025-09-01
    • Philipp Klaus Krause

      In [r15638], the old AST-based rotation optimization was replaced by an iCode-based one, which works much better with function inlining. We do get ROT iCodes for the stdbit.h rotation functions now.

       

      Related

      Commit: [r15638]

    • Philipp Klaus Krause

      In [r15639], built-in memcpy is used, where available.

       

      Related

      Commit: [r15639]

    • Philipp Klaus Krause

      In [r15645], there is a partial fix for the inlining issue. This fix works for rotation functions, but:

      • endian-aware and store are still affected by further aspects of [bugs:#3874].
      • The test for the rotation functions fails for mos6502 and mos65c02.
       

      Related

      Bugs: #3874
      Commit: [r15645]

      • Philipp Klaus Krause

        In [r15646], the rest of that bug is fixed, so all functions are now enabled for all ports. The test failures on mos6502 and mos65c02 are the only currently known remaining problem.

         

        Related

        Commit: [r15646]

        • Philipp Klaus Krause

          Only the right rotation test actually fails for mos65(c)02, so I enabled the tests for left rotation in [r15647].

           

          Related

          Commit: [r15647]

  • Philipp Klaus Krause

    When committing this, I thought I had tested it on a representative selection of ports. Turns out it fails tests for multiple ports that I did not test.

     
    • Philipp Klaus Krause

      The r2ka failures were due to two preexisting Rabbit codegen issues triggered by a new test. Those are fixed in [r15641].

       

      Related

      Commit: [r15641]

    • Philipp Klaus Krause

      The pdk15-stack-auto failure was due to the new iCode rotation optimization handling volatile better than the old one, which in turn resulted in a preexisting pdk codegen bug for reads from volatile variables being triggered. Fixed in [r15642].

       

      Related

      Commit: [r15642]

    • Philipp Klaus Krause

      The ds390 and mos65(c)02 ports ocdegen can't handle _BitInt(N) rotations, unless N is a multiple of 8. But those ports did announced support for it in their hasExtBitOp functions. Fixed in [r15643].

       

      Related

      Commit: [r15643]

  • Philipp Klaus Krause

    • status: open --> closed-fixed
    • Group: -->
     

Log in to post a comment.

MongoDB Logo MongoDB