Repository navigation
✨ (minor) v6: Add hal::bit_is_set() - #129
Merged
Merged
Conversation
hal::bit_is_set<mask>(value) returns whether the one bit a single-bit mask selects is 1, as a bool. It reads as the question it asks, where comparing hal::bit_extract() with 0 or 1, or using it as a condition, leaves the reader to work out that the field is one bit wide. A wider mask fails to compile, since "set" would be ambiguous for it.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds
hal::bit_is_set<mask>(value): whether the one bit that a single-bit mask selects is 1, as abool.Single-bit checks are written today as
hal::bit_extract<mask>(value) != 0, as== 1, or as the bare result in a condition. All three leave the reader to work out that the field is one bit wide, andbit_extract()returns the value's type, notbool.bit_is_set()reads as the question it asks.hal::bit_extract<mask>().static_assert), since "set" would be ambiguous: any bit, or all of them?hal::bit_extract()covers wider fields.bit_is_clear():not hal::bit_is_set<mask>(value)reads as well.[[nodiscard]], andconstexpr.-Os,((v >> 9) & 1) == 1,!= 0and(v & (1 << 9)) != 0all compile to the same singleubfx, and the samelslswith a conditional move when they decide a branch.bit_is_set()is the first of these.Testing
tests/bit.test.cpp: the lowest, a middle and the highest bit of au32, the top bits of au8and au64, and use at compile time.tests/compile_checks/bit_rejects.cpp: a two-bit mask must fail with thestatic_assertmessage. Changing it to a one-bit mask makes the check fail, as it should.conan build v6with LLVM 23 on macOS: all 17 test suites pass, clang-tidy is clean, and the compile checks pass.