From 8ff823adcedf07faa8e5fb539f77d3e43d04be70 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 12 Aug 2026 17:38:49 -0600 Subject: [PATCH] [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 --- src/hb-algs.hh | 11 +++++++++++ src/test-algs.cc | 3 +++ 2 files changed, 14 insertions(+) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index 514ce71b8..319b65cc1 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -958,6 +958,17 @@ hb_clamp_to (double v) (double) hb_int_max (T)); } +template +static HB_ALWAYS_INLINE T +hb_clamp_to (int64_t v) +{ + static_assert (std::is_integral::value && std::is_signed::value, ""); + + return (T) hb_clamp (v, + (int64_t) hb_int_min (T), + (int64_t) hb_int_max (T)); +} + /* * Bithacks. */ diff --git a/src/test-algs.cc b/src/test-algs.cc index 66cd1c9d5..3689d9f6f 100644 --- a/src/test-algs.cc +++ b/src/test-algs.cc @@ -102,6 +102,9 @@ main (int argc, char **argv) hb_always_assert (hb_clamp_to (1.) == 1); hb_always_assert (hb_clamp_to ((double) INT_MAX + 1.) == INT_MAX); hb_always_assert (hb_clamp_to ((double) INT_MIN - 1.) == INT_MIN); + hb_always_assert (hb_clamp_to ((int64_t) 1) == 1); + hb_always_assert (hb_clamp_to ((int64_t) INT_MAX + 1) == INT_MAX); + hb_always_assert (hb_clamp_to ((int64_t) INT_MIN - 1) == INT_MIN); x = 1; hb_always_assert (++hb_inc (x) == 3);