[algs] Add a signed integer clamp helper

Provide an always-inline int64_t overload of hb_clamp_to for cold
position formulas that need wider intermediate arithmetic while
preserving integer division and rounding semantics.

Add boundary tests. This supports the complete position-overflow fix
for issue #6151.

Testing: meson test -C build --print-errorlogs

Assisted-by: Codex <codex@openai.com>
This commit is contained in:
Behdad Esfahbod committed 2026-08-12 17:38:49 -06:00
1 parent 3c5af5f416
commit 8ff823adce
2 files changed
+14

No files matched your search

+11
View File
@@ -958,6 +958,17 @@ hb_clamp_to (double v)
(double) hb_int_max (T));
}
template <typename T>
static HB_ALWAYS_INLINE T
hb_clamp_to (int64_t v)
{
static_assert (std::is_integral<T>::value && std::is_signed<T>::value, "");
return (T) hb_clamp (v,
(int64_t) hb_int_min (T),
(int64_t) hb_int_max (T));
}
/*
* Bithacks.
*/
+3
View File
@@ -102,6 +102,9 @@ main (int argc, char **argv)
hb_always_assert (hb_clamp_to<int> (1.) == 1);
hb_always_assert (hb_clamp_to<int> ((double) INT_MAX + 1.) == INT_MAX);
hb_always_assert (hb_clamp_to<int> ((double) INT_MIN - 1.) == INT_MIN);
hb_always_assert (hb_clamp_to<int> ((int64_t) 1) == 1);
hb_always_assert (hb_clamp_to<int> ((int64_t) INT_MAX + 1) == INT_MAX);
hb_always_assert (hb_clamp_to<int> ((int64_t) INT_MIN - 1) == INT_MIN);
x = 1;
hb_always_assert (++hb_inc (x) == 3);