-
Notifications
You must be signed in to change notification settings - Fork 1.5k
libc: Add C23 stdbit.h with optional Kconfig and generic implementation #18347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arjav1528
wants to merge
4
commits into
apache:master
Choose a base branch
from
arjav1528:dev-issue-18311
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+345
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c200f2f
libc: add C23 stdbit.h implementation
arjav1528 27a3b8e
libc: add optional C23 stdbit.h header and documentation
arjav1528 4bbd833
cmake: fix formatting in nuttx_generate_headers.cmake
arjav1528 1151f5d
libc: enhance stdbit.h with improved leading zero/one macros
arjav1528 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| ======== | ||
| stdbit.h | ||
| ======== | ||
|
|
||
| The optional C23 ``stdbit.h`` header provides bit manipulation macros | ||
| (endianness, leading/trailing zeros and ones, count, single-bit test, | ||
| bit width, bit floor, bit ceil). NuttX provides this header only when | ||
| explicitly enabled via Kconfig. | ||
|
|
||
| Configuration | ||
| ============= | ||
|
|
||
| - **CONFIG_ARCH_HAVE_STDBIT_H** (bool, selected by arch) | ||
| Architecture indicates it provides ``arch/<arch>/include/stdbit.h``. | ||
|
|
||
| - **CONFIG_ARCH_STDBIT_H** (bool "stdbit.h", depends on ARCH_HAVE_STDBIT_H) | ||
| Use the redirecting header. The build copies | ||
| ``include/nuttx/lib/stdbit.h`` to ``include/stdbit.h``; that header | ||
| then includes ``<arch/stdbit.h>`` when this option is set. | ||
|
|
||
| - **CONFIG_LIBC_STDBIT_GENERIC** (bool "stdbit.h (generic C23)") | ||
| Use the generic C23 implementation. The same redirecting file | ||
| ``include/nuttx/lib/stdbit.h`` is copied to ``include/stdbit.h``, | ||
| and the generic implementation is used (no arch header). Requires | ||
| compiler builtins (e.g. ``__builtin_clz``, ``__builtin_ctz``, | ||
| ``__builtin_popcount``); see ``CONFIG_HAVE_BUILTIN_*`` in | ||
| ``nuttx/compiler.h``. | ||
|
|
||
| Either **CONFIG_ARCH_STDBIT_H** or **CONFIG_LIBC_STDBIT_GENERIC** may be | ||
| enabled so that ``#include <stdbit.h>`` is available. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| /**************************************************************************** | ||
| * include/nuttx/lib/stdbit.h | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to you under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| ****************************************************************************/ | ||
|
|
||
| #ifndef __INCLUDE_NUTTX_LIB_STDBIT_H | ||
| #define __INCLUDE_NUTTX_LIB_STDBIT_H | ||
|
|
||
| /**************************************************************************** | ||
| * Included Files | ||
| ****************************************************************************/ | ||
|
|
||
| #include <nuttx/config.h> | ||
| #include <nuttx/compiler.h> | ||
| #include <stdint.h> | ||
|
|
||
| #ifdef CONFIG_ARCH_STDBIT_H | ||
| # include <arch/stdbit.h> | ||
| #else | ||
|
|
||
| /**************************************************************************** | ||
| * Generic C23 stdbit implementation (CONFIG_LIBC_STDBIT_GENERIC). | ||
| * Requires __builtin_clz, __builtin_ctz, __builtin_popcount (and ll | ||
| * variants). If the toolchain does not provide them, enable the | ||
| * corresponding CONFIG_HAVE_BUILTIN_* in nuttx/compiler.h or do not | ||
| * use LIBC_STDBIT_GENERIC. | ||
| ****************************************************************************/ | ||
|
|
||
| # if !defined(CONFIG_HAVE_BUILTIN_CLZ) || \ | ||
| !defined(CONFIG_HAVE_BUILTIN_CTZ) || \ | ||
| !defined(CONFIG_HAVE_BUILTIN_POPCOUNT) | ||
| # error "Generic stdbit requires CONFIG_HAVE_BUILTIN_CLZ, CTZ, POPCOUNT" | ||
| # endif | ||
|
|
||
| # if defined(CONFIG_HAVE_LONG_LONG) && \ | ||
| !defined(CONFIG_HAVE_BUILTIN_POPCOUNTLL) | ||
| # error "Generic stdbit 64-bit requires CONFIG_HAVE_BUILTIN_POPCOUNTLL" | ||
| # endif | ||
|
|
||
| /**************************************************************************** | ||
| * C23 endianness macros | ||
| ****************************************************************************/ | ||
|
|
||
| # ifdef CONFIG_ENDIAN_BIG | ||
| # define __STDC_ENDIAN_LITTLE__ 1234 | ||
| # define __STDC_ENDIAN_BIG__ 4321 | ||
| # define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_BIG__ | ||
| # else | ||
| # define __STDC_ENDIAN_LITTLE__ 1234 | ||
| # define __STDC_ENDIAN_BIG__ 4321 | ||
| # define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_LITTLE__ | ||
| # endif | ||
|
|
||
| /**************************************************************************** | ||
| * Leading / trailing zeros (C23: zero input returns bit width) | ||
| ****************************************************************************/ | ||
|
|
||
| # define stdc_leading_zeros_uc(x) \ | ||
| ((unsigned char)(x) == 0 ? 8 : \ | ||
| (unsigned char)__builtin_clz((unsigned)(x) << 24)) | ||
| # define stdc_leading_zeros_us(x) \ | ||
| ((unsigned short)(x) == 0 ? 16 : \ | ||
| (unsigned short)__builtin_clz((unsigned)(x) << 16)) | ||
| # define stdc_leading_zeros_ui(x) \ | ||
| ((x) == 0 ? 32 : (unsigned)__builtin_clz(x)) | ||
| # define stdc_leading_zeros_ul(x) \ | ||
| ((x) == 0 ? (unsigned long)(8*sizeof(unsigned long)) : \ | ||
| (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)) | ||
arjav1528 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # define stdc_trailing_zeros_uc(x) \ | ||
| ((unsigned char)(x) == 0 ? 8 : (unsigned char)__builtin_ctz((unsigned)(x))) | ||
| # define stdc_trailing_zeros_us(x) \ | ||
| ((unsigned short)(x) == 0 ? 16 : \ | ||
| (unsigned short)__builtin_ctz((unsigned)(x))) | ||
| # define stdc_trailing_zeros_ui(x) \ | ||
| ((x) == 0 ? 32 : (unsigned)__builtin_ctz(x)) | ||
| # define stdc_trailing_zeros_ul(x) \ | ||
| ((x) == 0 ? (unsigned long)(8*sizeof(unsigned long)) : \ | ||
| (unsigned long)__builtin_ctzl(x)) | ||
| # define stdc_trailing_zeros_ull(x) \ | ||
| ((x) == 0 ? (unsigned long long)(8*sizeof(unsigned long long)) : \ | ||
| (unsigned long long)__builtin_ctzll(x)) | ||
|
|
||
| /**************************************************************************** | ||
| * Leading / trailing ones (C23: zero input returns 0) | ||
| ****************************************************************************/ | ||
|
|
||
| # define stdc_leading_ones_uc(x) stdc_leading_zeros_uc((unsigned char)~(x)) | ||
| # define stdc_leading_ones_us(x) stdc_leading_zeros_us((unsigned short)~(x)) | ||
| # define stdc_leading_ones_ui(x) stdc_leading_zeros_ui(~(x)) | ||
| # define stdc_leading_ones_ul(x) stdc_leading_zeros_ul(~(x)) | ||
| # define stdc_leading_ones_ull(x) stdc_leading_zeros_ull(~(x)) | ||
|
|
||
| # define stdc_trailing_ones_uc(x) stdc_trailing_zeros_uc((unsigned char)~(x)) | ||
| # define stdc_trailing_ones_us(x) stdc_trailing_zeros_us((unsigned short)~(x)) | ||
| # define stdc_trailing_ones_ui(x) stdc_trailing_zeros_ui(~(x)) | ||
| # define stdc_trailing_ones_ul(x) stdc_trailing_zeros_ul(~(x)) | ||
| # define stdc_trailing_ones_ull(x) stdc_trailing_zeros_ull(~(x)) | ||
|
|
||
| /**************************************************************************** | ||
| * First leading zero/one (bit index from MSB; C23: 0 returns bit width) | ||
| ****************************************************************************/ | ||
|
|
||
| # define stdc_first_leading_zero_uc(x) \ | ||
| ((unsigned char)(x) == 0xFF ? 0 : stdc_leading_ones_uc(x) + 1) | ||
| # define stdc_first_leading_zero_us(x) \ | ||
| ((unsigned short)(x) == 0xFFFF ? 0 : stdc_leading_ones_us(x) + 1) | ||
| # define stdc_first_leading_zero_ui(x) \ | ||
| ((x) == ~0u ? 0 : stdc_leading_ones_ui(x) + 1) | ||
| # define stdc_first_leading_zero_ul(x) \ | ||
| ((x) == ~0ul ? 0 : stdc_leading_ones_ul(x) + 1) | ||
| # define stdc_first_leading_zero_ull(x) \ | ||
| ((x) == ~0ull ? 0 : stdc_leading_ones_ull(x) + 1) | ||
|
|
||
| # define stdc_first_leading_one_uc(x) \ | ||
| ((unsigned char)(x) == 0 ? 0 : stdc_leading_zeros_uc(x) + 1) | ||
| # define stdc_first_leading_one_us(x) \ | ||
| ((unsigned short)(x) == 0 ? 0 : stdc_leading_zeros_us(x) + 1) | ||
| # define stdc_first_leading_one_ui(x) \ | ||
| ((x) == 0 ? 0 : stdc_leading_zeros_ui(x) + 1) | ||
| # define stdc_first_leading_one_ul(x) \ | ||
| ((x) == 0 ? 0 : stdc_leading_zeros_ul(x) + 1) | ||
| # define stdc_first_leading_one_ull(x) \ | ||
| ((x) == 0 ? 0 : stdc_leading_zeros_ull(x) + 1) | ||
|
|
||
| /**************************************************************************** | ||
| * First trailing zero/one (bit index from LSB; C23: 0 returns bit width) | ||
| ****************************************************************************/ | ||
|
|
||
| # define stdc_first_trailing_zero_uc(x) \ | ||
| ((unsigned char)(x) == 0xFF ? 0 : stdc_trailing_ones_uc(x) + 1) | ||
| # define stdc_first_trailing_zero_us(x) \ | ||
| ((unsigned short)(x) == 0xFFFF ? 0 : stdc_trailing_ones_us(x) + 1) | ||
| # define stdc_first_trailing_zero_ui(x) \ | ||
| ((x) == ~0u ? 0 : stdc_trailing_ones_ui(x) + 1) | ||
| # define stdc_first_trailing_zero_ul(x) \ | ||
| ((x) == ~0ul ? 0 : stdc_trailing_ones_ul(x) + 1) | ||
| # define stdc_first_trailing_zero_ull(x) \ | ||
| ((x) == ~0ull ? 0 : stdc_trailing_ones_ull(x) + 1) | ||
|
|
||
| # define stdc_first_trailing_one_uc(x) \ | ||
| ((unsigned char)(x) == 0 ? 0 : stdc_trailing_zeros_uc(x) + 1) | ||
| # define stdc_first_trailing_one_us(x) \ | ||
| ((unsigned short)(x) == 0 ? 0 : stdc_trailing_zeros_us(x) + 1) | ||
| # define stdc_first_trailing_one_ui(x) \ | ||
| ((x) == 0 ? 0 : stdc_trailing_zeros_ui(x) + 1) | ||
| # define stdc_first_trailing_one_ul(x) \ | ||
| ((x) == 0 ? 0 : stdc_trailing_zeros_ul(x) + 1) | ||
| # define stdc_first_trailing_one_ull(x) \ | ||
| ((x) == 0 ? 0 : stdc_trailing_zeros_ull(x) + 1) | ||
|
|
||
| /**************************************************************************** | ||
| * Count zeros / ones | ||
| ****************************************************************************/ | ||
|
|
||
| # define stdc_count_ones_uc(x) \ | ||
| ((unsigned char)__builtin_popcount((unsigned)(x))) | ||
| # define stdc_count_ones_us(x) \ | ||
| ((unsigned short)__builtin_popcount((unsigned)(x))) | ||
| # define stdc_count_ones_ui(x) ((unsigned)__builtin_popcount(x)) | ||
| # define stdc_count_ones_ul(x) ((unsigned long)__builtin_popcountl(x)) | ||
| # define stdc_count_ones_ull(x) ((unsigned long long)__builtin_popcountll(x)) | ||
|
|
||
| # define stdc_count_zeros_uc(x) ((unsigned char)8 - stdc_count_ones_uc(x)) | ||
| # define stdc_count_zeros_us(x) ((unsigned short)16 - stdc_count_ones_us(x)) | ||
| # define stdc_count_zeros_ui(x) (32 - stdc_count_ones_ui(x)) | ||
| # define stdc_count_zeros_ul(x) \ | ||
| ((unsigned long)(8*sizeof(unsigned long)) - stdc_count_ones_ul(x)) | ||
| # define stdc_count_zeros_ull(x) \ | ||
| ((unsigned long long)(8*sizeof(unsigned long long)) - \ | ||
| stdc_count_ones_ull(x)) | ||
|
|
||
| /**************************************************************************** | ||
| * Single-bit test, bit width, bit floor, bit ceil | ||
| ****************************************************************************/ | ||
|
|
||
| # define stdc_has_single_bit_uc(x) (stdc_count_ones_uc(x) == 1) | ||
| # define stdc_has_single_bit_us(x) (stdc_count_ones_us(x) == 1) | ||
| # define stdc_has_single_bit_ui(x) (stdc_count_ones_ui(x) == 1) | ||
| # define stdc_has_single_bit_ul(x) (stdc_count_ones_ul(x) == 1) | ||
| # define stdc_has_single_bit_ull(x) (stdc_count_ones_ull(x) == 1) | ||
|
|
||
| # define stdc_bit_width_uc(x) \ | ||
| ((unsigned char)(x) == 0 ? 0 : 8 - stdc_leading_zeros_uc(x)) | ||
| # define stdc_bit_width_us(x) \ | ||
| ((unsigned short)(x) == 0 ? 0 : 16 - stdc_leading_zeros_us(x)) | ||
| # define stdc_bit_width_ui(x) ((x) == 0 ? 0 : 32 - stdc_leading_zeros_ui(x)) | ||
| # define stdc_bit_width_ul(x) \ | ||
| ((x) == 0 ? 0 : (int)(8*sizeof(unsigned long) - \ | ||
| stdc_leading_zeros_ul(x))) | ||
| # define stdc_bit_width_ull(x) \ | ||
| ((x) == 0 ? 0 : (int)(8*sizeof(unsigned long long) - \ | ||
| stdc_leading_zeros_ull(x))) | ||
|
|
||
| # define stdc_bit_floor_uc(x) \ | ||
| ((unsigned char)((x) != 0 ? 1 << (7 - stdc_leading_zeros_uc(x)) : 0)) | ||
| # define stdc_bit_floor_us(x) \ | ||
| ((unsigned short)((x) != 0 ? 1 << (15 - stdc_leading_zeros_us(x)) : 0)) | ||
| # define stdc_bit_floor_ui(x) \ | ||
| ((x) != 0 ? 1u << (31 - stdc_leading_zeros_ui(x)) : 0u) | ||
| # define stdc_bit_floor_ul(x) \ | ||
| ((x) != 0 ? (unsigned long)1 << (8*sizeof(unsigned long)-1 - \ | ||
| stdc_leading_zeros_ul(x)) : 0ul) | ||
| # define stdc_bit_floor_ull(x) \ | ||
| ((x) != 0 ? (unsigned long long)1 << (8*sizeof(unsigned long long)-1 - \ | ||
| stdc_leading_zeros_ull(x)) : 0ull) | ||
|
|
||
| # define stdc_bit_ceil_uc(x) \ | ||
| ((unsigned char)((x) <= 1 ? 1 : (unsigned char)1 << (8 - \ | ||
| stdc_leading_zeros_uc((unsigned char)(x)-1)))) | ||
| # define stdc_bit_ceil_us(x) \ | ||
| ((unsigned short)((x) <= 1 ? 1 : (unsigned short)1 << (16 - \ | ||
| stdc_leading_zeros_us((unsigned short)(x)-1)))) | ||
| # define stdc_bit_ceil_ui(x) \ | ||
| ((x) <= 1 ? 1u : 1u << (32 - stdc_leading_zeros_ui((x)-1))) | ||
| # define stdc_bit_ceil_ul(x) \ | ||
| ((x) <= 1 ? 1ul : 1ul << (8*sizeof(unsigned long) - \ | ||
| stdc_leading_zeros_ul((x)-1))) | ||
| # define stdc_bit_ceil_ull(x) \ | ||
| ((x) <= 1 ? 1ull : 1ull << (8*sizeof(unsigned long long) - \ | ||
| stdc_leading_zeros_ull((x)-1))) | ||
|
|
||
| #endif /* CONFIG_ARCH_STDBIT_H */ | ||
|
|
||
| #endif /* __INCLUDE_NUTTX_LIB_STDBIT_H */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.