Repository navigation
🐛 (patch) v5: Fix hal::bit_limits for signed fields - #130
Merged
Merged
Conversation
max() of a signed field the width of its type gave -1, and min() of a signed field gave 1 << BitWidth, which doesn't fit the type for a field as wide as the type, or wider than 32 bits. Both now give the range a field of that width and signedness holds, the same as v6.
kammce
commented
Nov 14, 2026
kammce
left a comment
Member
Author
There was a problem hiding this comment.
This PR was written by Claude sonnet 5.5 and I just wanted to note that in this case I read the tests first and ensure that the test were correct and then more lightly skimmed the actual code to make sure that it looked fine. This is one of the cases where I applied test driven design to ensure that the code works since the amount of co-changes in that large. That seems to be reasonably effective for small PR's like this, especially when they're just numerical.
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.
Fixes the range
hal::bit_limitsreports for signed fields in v5, as libhal-util#124 did for v6.The problem
max()of a signed field the width of its type was -1:bit_limits<32, std::int32_t>::max()shifted a field of ones right by one, and a full-width field of ones is -1.min()of a signed field was1 << BitWidth, which doesn't fit the type for a field as wide as the type, and is positive for any other width.bit_limits<8, std::int32_t>::min()gave 256, andbit_limits<8, std::int8_t>::min()doesn't compile with-Werror.intshift while building the field of ones.Every current user in the org is unsigned and narrow (libhal-arm-mcu v1 CAN and ADC), so nothing is affected today.
Changes
bit_limits::max()isstd::numeric_limits<int_t>::max()for a field as wide as its type. Otherwise it is computed in the unsigned type:(1 << (BitWidth - 1)) - 1for a signed field,(1 << BitWidth) - 1for an unsigned one.bit_limits::min()is-max() - 1for a signed field and 0 for an unsigned one.tests/bit_limits.test.cppcovers narrow fields, fields as wide as their type, and a 40-bit field. They are the same cases as v6's tests.generate_field_of_ones()is unchanged.overflow_counterstill uses it.Testing
The new tests pass with Clang 22 (
-Wall -Wextra -Wshadow -Werror). Against the old header they don't compile, because of themin()conversion above.