Skip to content

🐛 (patch) v5: Fix hal::bit_limits for signed fields - #130

Merged
kammce merged 1 commit into
mainfrom
v5-bit-limits
Nov 14, 2026
Merged

kammce merged 1 commit into
mainfrom
v5-bit-limits

Conversation

@kammce

@kammce kammce commented Nov 14, 2026

Copy link
Copy Markdown
Member

Fixes the range hal::bit_limits reports 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 was 1 << 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, and bit_limits<8, std::int8_t>::min() doesn't compile with -Werror.
  • Fields wider than 32 bits overflowed an int shift 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() is std::numeric_limits<int_t>::max() for a field as wide as its type. Otherwise it is computed in the unsigned type: (1 << (BitWidth - 1)) - 1 for a signed field, (1 << BitWidth) - 1 for an unsigned one.
  • bit_limits::min() is -max() - 1 for a signed field and 0 for an unsigned one.
  • tests/bit_limits.test.cpp covers 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_counter still uses it.

Testing

The new tests pass with Clang 22 (-Wall -Wextra -Wshadow -Werror). Against the old header they don't compile, because of the min() conversion above.

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 kammce left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@kammce
kammce merged commit c2de5e4 into main Nov 14, 2026
13 checks passed
@kammce
kammce deleted the v5-bit-limits branch November 14, 2026 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant