Repository navigation
✨ (minor) v6: Replace hal::map() with a template hal::rescale() - #143
Merged
Merged
Conversation
libhal names a value on a scale hal::scale, so the float function that moves a value from one range to another is now hal::rescale(), in its own hal.util:rescale partition. util v5 keeps hal::map().
kammce
commented
Nov 29, 2026
kammce
left a comment
Member
Author
There was a problem hiding this comment.
Should rescale be a template so its not locked to floating point numbers?
…range
hal::rescale(hal::scale{ adc, 0, 4095 }, { 1000, 2000 }) moves a value from
its scale onto a range, for integers as well as floats. Integers compute in a
type twice as wide and round to the nearest; a range whose last end is below
its first reverses the direction. hal::range is new. Requires libhal's
hal::scale (5.0.0-alpha.26).
The integer and float paths each convert the clamped value, the scale's bounds and the range's ends once, into named locals of the type the math uses, so the arithmetic itself has no casts.
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.
Replaces util v6's float-only
hal::map()with a templatehal::rescale(). It moves a value from ahal::scale(libhal/libhal#280) onto a newhal::range, for integers as well as floating point. The partition, file and test are renamed too:hal.util:mapbecomeshal.util:rescale. It requires libhal 5.0.0-alpha.26, which has libhal#280.Design
Input is a
hal::scale: the value and the scale it's on, the same type libhal's percentages take. libhal's percentage conversions are one fixed case of this: a scale moved onto 0 tofull.hal::range<T>{ first, last }is new: where the bottom and the top of the scale go.lastmay be belowfirst, which reverses the direction, for example to flip a joystick axis. It has a constructor rather than being an aggregate, so{ 1000, 2000 }passes tidy'smodernize-use-designated-initializers. The constructor keepsbugprone-easily-swappable-parametersoff with aNOLINT, likehal::scale: the order is how a range reads. It lives in util for now and may move to libhal later.The result type defaults to the scale's, and another can be named:
hal::rescale<hal::i16>(...). It may be an integer of 32 bits or fewer, or a floating point type.Integers compute in a type twice as wide: 32-bit when the scale and the range are 16 bits or narrower, 64-bit otherwise. They round to the nearest value. A reversed range uses the magnitude of its span and subtracts, so neither direction overflows the wide type.
Floating point uses the old function's operation order: the ratio of the spans first, then the offset. That ratio is exactly 1 when the scale and the range match, so such a value comes back unchanged. A float result rounded to an integer type is clamped to the range first, so the cast can't overflow. It is then rounded half away from zero by hand, because
std::round()isn'tconstexprin libc++.Edge cases:
first.first.Behavior changes from
hal::map():hal::map()sorted both ranges withstd::minmax, so it couldn't reverse. A reversed output range now reverses.firstinstead of being sorted, sincehal::scaledefineslowerandupper.hal::map()tookfloats;rescale()keeps the type it's given, sodoubles staydouble.Compatibility
hal::map()from util v5, which is unchanged.Size
Cortex-M3, LLVM 23,
-Os, soft float. Each case is compiled alone, linked with only the helpers it needs, and writes through an external function, against a 16 B baseline:rescale(scale{ adc, 0, 4095 }, { 1000, 2000 })rescale<i16>(scale{ adc, 0, 4095 }, { -90, 90 })rescale(scale<u32>{ ... }, { 0U, 1000U })(64-bit division)rescale<u16>(scale<float>{ v, 0.0f, 3.3f }, { 0, 4095 })(soft float)Tested
Host tests with clang-tidy, built against libhal#280 locally:
i16,u8)i16scales whose bounds wrap when castu32up to its maximum, andi32All 18 host tests pass.