Skip to content

✨ (minor) v6: Replace hal::map() with a template hal::rescale() - #143

Merged
kammce merged 3 commits into
mainfrom
v6-rescale
Nov 29, 2026
Merged

kammce merged 3 commits into
mainfrom
v6-rescale

Conversation

@kammce

@kammce kammce commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Replaces util v6's float-only hal::map() with a template hal::rescale(). It moves a value from a hal::scale (libhal/libhal#280) onto a new hal::range, for integers as well as floating point. The partition, file and test are renamed too: hal.util:map becomes hal.util:rescale. It requires libhal 5.0.0-alpha.26, which has libhal#280.

// An ADC reading onto a servo pulse, in microseconds
auto const pulse = hal::rescale(hal::scale{ adc, 0, 4095 }, { 1000, 2000 });
// Into another type: name it
auto const angle = hal::rescale<hal::i16>(hal::scale{ adc, 0, 4095 }, { -90, 90 });
// Reversed: the bottom of the scale goes to 2000
auto const flipped = hal::rescale(hal::scale{ adc, 0, 4095 }, { 2000, 1000 });
// Floating point, as before
auto const y = hal::rescale(hal::scale<float>{ x, 0.0f, 10.0f }, { 100.0f, 200.0f });

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 to full.

hal::range<T>{ first, last } is new: where the bottom and the top of the scale go. last may be below first, 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's modernize-use-designated-initializers. The constructor keeps bugprone-easily-swappable-parameters off with a NOLINT, like hal::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't constexpr in libc++.

Edge cases:

  • A value outside its scale is clamped, so the result is always within the range.
  • A scale whose upper bound isn't above its lower bound gives first.
  • A NaN value gives first.

Behavior changes from hal::map():

  • hal::map() sorted both ranges with std::minmax, so it couldn't reverse. A reversed output range now reverses.
  • A reversed input scale gives first instead of being sorted, since hal::scale defines lower and upper.
  • hal::map() took floats; rescale() keeps the type it's given, so doubles stay double.

Compatibility

  • Breaking for util v6, which has no users outside its own test.
  • libhal-actuator and libhal-sensor use 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:

Call Linked size
rescale(scale{ adc, 0, 4095 }, { 1000, 2000 }) 108 B
the same, reversed 108 B
rescale<i16>(scale{ adc, 0, 4095 }, { -90, 90 }) 108 B
scale and range all known only at run time 100 B
rescale(scale<u32>{ ... }, { 0U, 1000U }) (64-bit division) 580 B
rescale<u16>(scale<float>{ v, 0.0f, 3.3f }, { 0, 4095 }) (soft float) 2,432 B

Tested

Host tests with clang-tidy, built against libhal#280 locally:

  • Compile-time checks:
    • end points, the middle, and rounding (1 and 2 out of 3)
    • clamping on both sides
    • reversed ranges at both ends, the middle and past the scale
    • named result types (i16, u8)
    • i16 scales whose bounds wrap when cast
    • u32 up to its maximum, and i32
    • float scales onto integers, rounding a true half away from zero both ways
    • integer scales onto floats
    • degenerate scales and single-point ranges
    • the default result type
  • Run-time tests: the old float cases, reversed floats, the old boundary cases, NaN for float and integer results, and integers at run time in both directions.

All 18 host tests pass.

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 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.

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).
@kammce kammce changed the title ♻️ (minor) v6: Rename hal::map() to hal::rescale() ✨ (minor) v6: Replace hal::map() with a template hal::rescale() Nov 29, 2026

@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.

✅

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.
@kammce
kammce merged commit 13f3fd2 into main Nov 29, 2026
10 checks passed
@kammce
kammce deleted the v6-rescale branch November 29, 2026 13:51
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