Skip to content

libc: Add C23 stdbit.h with optional Kconfig and generic implementation#18347

Open
arjav1528 wants to merge 1 commit intoapache:masterfrom
arjav1528:dev-issue-18311
Open

libc: Add C23 stdbit.h with optional Kconfig and generic implementation#18347
arjav1528 wants to merge 1 commit intoapache:masterfrom
arjav1528:dev-issue-18311

Conversation

@arjav1528
Copy link

@arjav1528 arjav1528 commented Feb 3, 2026

Fixes: #18311

Summary

Add optional C23 stdbit.h support so builds can provide <stdbit.h> when the toolchain does not. Follows the same pattern as stdarg.h / math.h: redirect at include/nuttx/lib/stdbit.h, copied to include/stdbit.h when enabled.

  • Kconfig: ARCH_HAVE_STDBIT_H, ARCH_STDBIT_H (arch-specific), LIBC_STDBIT_GENERIC (generic on any arch).
  • Header: Generic implementation using __builtin_clz/__builtin_ctz/__builtin_popcount for all C23 stdbit macros; archs may provide arch/<arch>/include/stdbit.h.
  • Build: Unix.mk, Win.mk, CMake copy the header when either option is set.
  • Libc: Optional libs/libc/stdbit/stdbit_verify.c (compile-time check when stdbit enabled).
  • Docs: Documentation/legacy_README.md and Documentation/components/libs/libc/index.rst.

Impact

  • Users: Optional; no change unless ARCH_STDBIT_H or LIBC_STDBIT_GENERIC is enabled. Code can then use C23 <stdbit.h> macros when the toolchain lacks them.
  • Build: One header is copied to include/stdbit.h when stdbit support is enabled; build system (Unix.mk, Win.mk, CMake) is extended only for this copy.
  • Compatibility: Improves portability for C23 stdbit usage on toolchains that don’t yet ship stdbit.h. No impact on existing configs that don’t enable it.

Testing

There is no freely available, upstream test suite for C23 stdbit.h that we could add to NuttX to validate this implementation. Verification is limited to:

  • Enabling LIBC_STDBIT_GENERIC (or arch-specific ARCH_STDBIT_H where implemented) and confirming the build completes and stdbit_verify.c compiles (compile-time checks that the macros expand as expected).
  • Manual inspection that the generic implementation matches C23 semantics and uses the intended builtins.

@github-actions github-actions bot added Area: Documentation Improvements or additions to documentation Area: Tooling Area: Build system Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Feb 3, 2026
@linguini1 linguini1 requested a review from michallenc February 3, 2026 17:47
Copy link
Contributor

@michallenc michallenc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was fast, nice work! Please squash the commits ideally into two - one implementing stdbit and other updating the documentation. Also fix long lines in stdbit.h causing CI to fail.

We definitely need proper tests before merging this. These should go to app repository. I can write some, but won't get to it until next week probably.

@arjav1528 arjav1528 force-pushed the dev-issue-18311 branch 2 times, most recently from ad2e28a to 277c277 Compare February 4, 2026 12:43
@arjav1528 arjav1528 requested a review from michallenc February 4, 2026 12:44
@arjav1528 arjav1528 force-pushed the dev-issue-18311 branch 2 times, most recently from 8abb829 to 8e473c0 Compare February 4, 2026 16:46
@github-actions github-actions bot added Arch: arm64 Issues related to ARM64 (64-bit) architecture Size: XS The size of the change in this PR is very small and removed Area: Documentation Improvements or additions to documentation Area: Tooling Area: Build system labels Feb 4, 2026
@michallenc
Copy link
Contributor

Seems the merge request now contains completely different commits unrelated to the title and previous changes.

Copy link
Contributor

@linguini1 linguini1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check that your PR follows the contributing guidelines. Also, a mistake might have happened during rebase/squashing that has caused your commits to disappear, as Michal pointed out.

@arjav1528
Copy link
Author

@michallenc @linguini1 could you please help me with the failing test cases

Sure, the commit message is missing. It should be in format

title - short info what is changed

description - more detailed text describing what you did and why

sign-off

You only have title and sign of in your commits.

understood, and about the other test failing?

right, sorry, I missed that one. Seems like esp only problem when downloading MCUboot and not caused by this MR I think, @tmedicci could you take a look please?

is it possible that the testcase might have stopped bcoz of a timeout error or something similar??, coz in the logs I cant see anything wrong, it is getting built, then cleaned up, and repeat

Yes, it may happen. You can try force pushing the branch to restart the CI.

done, could you please review the PR and approve the workflow

@arjav1528
Copy link
Author

arjav1528 commented Feb 14, 2026

@michallenc could you please approve the workflows again :(

@arjav1528
Copy link
Author

@michallenc could you please approve the workflows again as I have force pushed new commits

@arjav1528
Copy link
Author

@michallenc is this same timeout issue, or is anything wrong??

@michallenc
Copy link
Contributor

Not sure here honestly. @simbit18 @acassis do you have some ideas what's wrong with CI here?

@simbit18
Copy link
Contributor

@michallenc restarted the workflow

@arjav1528
Copy link
Author

@michallenc restarted the workflow

same issue, ig it might be due to multiple PR workflows in the repo, so the CPU load increases which results in the slower builds and hence timeouts

@simbit18
Copy link
Contributor

simbit18 commented Feb 23, 2026

@arjav1528 please rebase
you need to update your branch

CONFIG_INTERPRETER_LUA_32BIT has been renamed with this PR #18398
CONFIG_INTERPRETER_LUA_32BIT -> CONFIG_INTERPRETERS_LUA_32BIT

====================================================================================
Configuration/Tool: sim/lua
2026-02-23 17:09:58
------------------------------------------------------------------------------------
  Cleaning...
  Configuring...
  Building NuttX...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0      %  0T o t a l0        %  0R e c e i v e0d   %   X f e0r d- - :A-v-e:r-a-g e- -S:p-e-e:d- -   T-i-m:e- - : - -T im e      0  Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

100 30374    0 30374    0     0  84594      0 --:--:-- --:--:-- --:--:-- 84594

100  6564    0  6564    0     0  17357      0 --:--:-- --:--:-- --:--:-- 17357

100  425k    0  425k    0     0  1022k      0 --:--:-- --:--:-- --:--:-- 1022k

100 78624    0 78624    0     0   180k      0 --:--:-- --:--:-- --:--:--  180k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

100 1431k  100 1431k    0     0  4163k      0 --:--:-- --:--:-- --:--:-- 4163k

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

 32 1646k   32  538k    0     0  1096k      0  0:00:01 --:--:--  0:00:01 1096k
100 1646k  100 1646k    0     0  3102k      0 --:--:-- --:--:-- --:--:-- 27.7M
  [1/1] Normalize sim/lua
8d7
< # CONFIG_INTERPRETER_LUA_32BIT is not set
Saving the new configuration file
HEAD detached at pull/18347/merge
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   boards/sim/sim/sim/configs/lua/defconfig

no changes added to commit (use "git add" and/or "git commit -a")
====================================================================================

@arjav1528
Copy link
Author

@simbit18 done, could you approve the workflows..

@github-actions github-actions bot removed Area: Documentation Improvements or additions to documentation Area: Tooling Size: XS The size of the change in this PR is very small labels Feb 23, 2026
* C23 endianness macros (reuse LITTLE_ENDIAN/BIG_ENDIAN from sys/endian.h)
****************************************************************************/

# include <sys/endian.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move after line 37

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not fix yet

*
****************************************************************************/

#ifndef __INCLUDE_NUTTX_LIB_STDBIT_H
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

squash your change into one patch

@arjav1528 arjav1528 force-pushed the dev-issue-18311 branch 3 times, most recently from 74e2c99 to 5e1c7a4 Compare February 24, 2026 14:00
@arjav1528
Copy link
Author

done @xiaoxiang781216, could you please approve the workflows

@github-actions github-actions bot added Size: L The size of the change in this PR is large and removed Size: M The size of the change in this PR is medium labels Feb 24, 2026
@xiaoxiang781216
Copy link
Contributor

done @xiaoxiang781216, could you please approve the workflows

please fix the ci issue before asking review:
https://github.com/apache/nuttx/actions/runs/22354112984/job/64759945293?pr=18347

Error: /home/runner/work/nuttx/nuttx/nuttx/include/nuttx/lib/stdbit.h:85:78: error: Long line found
Error: /home/runner/work/nuttx/nuttx/nuttx/include/nuttx/lib/stdbit.h:90:80: error: Long line found
Error: /home/runner/work/nuttx/nuttx/nuttx/include/nuttx/lib/stdbit.h:113:78: error: Long line found
Error: /home/runner/work/nuttx/nuttx/nuttx/include/nuttx/lib/stdbit.h:118:80: error: Long line found
Error: /home/runner/work/nuttx/nuttx/nuttx/include/nuttx/lib/stdbit.h:149:45: error: Upper case hex constant found
Error: /home/runner/work/nuttx/nuttx/nuttx/include/nuttx/lib/stdbit.h:149:84: error: Long line found
Error: /home/runner/work/nuttx/nuttx/nuttx/include/nuttx/lib/stdbit.h:154:46: error: Upper case hex constant found
Error: /home/runner/work/nuttx/nuttx/nuttx/include/nuttx/lib/stdbit.h:204:45: error: Upper case hex constant found
Error: /home/runner/work/nuttx/nuttx/nuttx/include/nuttx/lib/stdbit.h:210:46: error: Upper case hex constant found

- Introduced Kconfig options for stdbit.h, allowing architecture-specific and generic implementations.
- Added new documentation for stdbit.h, detailing its usage and configuration.
- Updated CMake and Makefile to handle the inclusion of stdbit.h based on configuration settings.

This enhances the NuttX library with optional C23 bit manipulation utilities, improving functionality for architectures that support it.

Signed-off-by: Arjav Patel <arjav1528@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Build system Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Missing stdbit.h header

5 participants