Skip to content

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

Open
arjav1528 wants to merge 4 commits intoapache:masterfrom
arjav1528:dev-issue-18311
Open

libc: Add C23 stdbit.h with optional Kconfig and generic implementation#18347
arjav1528 wants to merge 4 commits 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.

@github-actions github-actions bot added Area: Documentation Improvements or additions to documentation Area: Tooling Area: Build system and removed Arch: arm64 Issues related to ARM64 (64-bit) architecture Area: OS Components OS Components issues labels Feb 5, 2026
Signed-off-by: Arjav Patel <arjav1528@gmail.com>
@arjav1528
Copy link
Author

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.

yep, that was a rebase issue, nvm I have reapplied the changes, you can check now

Copy link
Contributor

Choose a reason for hiding this comment

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

Are there any test-suites freely available we could bring to NuttX to make sure this implementation works? I don't think we have any way to test it right now (not really a way around that though).

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we could write our own tests in apps directory. They should be pretty straightforward based on stdbit specification https://cppreference.com/w/c/header/stdbit.html. The only complicated part is to test it for every possible int size.

@linguini1 linguini1 dismissed their stale review February 5, 2026 19:47

Not familiar enough with a method to test this implementation. Thank you for adding docs though!

Please add the Impact and Testing sections to your PR description (even if there's no way to test, just mention that).

Signed-off-by: Arjav Patel <arjav1528@gmail.com>
Signed-off-by: Arjav Patel <arjav1528@gmail.com>
(unsigned long)__builtin_clzl(x))
# define stdc_leading_zeros_ull(x) \
((x) == 0 ? (unsigned long long)(8*sizeof(unsigned long long)) : \
(unsigned long long)__builtin_clzll(x))
Copy link
Contributor

Choose a reason for hiding this comment

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

Just realized we should add common stdc_leading_zeros. It can be implemented as

#define stdc_leading_zeros(x) (stdc_leading_zeros_ull(x) - (unsigned int)(8 * (sizeof(0ULL) - sizeof(x)))

and similarly for other stdc defines.

Copy link
Author

Choose a reason for hiding this comment

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

Me too!, I have commited the change, kindly review the PR

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 Area: Documentation Improvements or additions to documentation Area: Tooling Size: M The size of the change in this PR is medium Size: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Missing stdbit.h header

3 participants