[algs] Complete signed saturation helpers

Add an always-inline saturating negation for position values that can
reach INT_MIN. Also require the floating-point clamp helper to inline,
matching the add and subtract helpers used throughout shaping.

Extend the boundary tests for both helpers. 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:23:05 -06:00
1 parent f57cc31a02
commit 291b7ef1e0
2 files changed
+16 -1

No files matched your search

+10 -1
View File
@@ -937,11 +937,20 @@ hb_saturate_sub (T a, T b)
return b < 0 ? hb_int_max (T) : hb_int_min (T);
}
template <typename T>
static HB_ALWAYS_INLINE T
hb_saturate_neg (T a)
{
static_assert (std::is_integral<T>::value && std::is_signed<T>::value, "");
return hb_saturate_sub ((T) 0, a);
}
/* Convert a floating-point value to integer type T, saturating to T's
* range instead of relying on the undefined behavior of an out-of-range
* float-to-int conversion. NaN saturates to the minimum of T. */
template <typename T>
static inline T
static HB_ALWAYS_INLINE T
hb_clamp_to (double v)
{
return (T) hb_clamp (v,
+6
View File
@@ -96,6 +96,12 @@ main (int argc, char **argv)
hb_always_assert (hb_saturate_sub (INT_MAX, -1) == INT_MAX);
hb_always_assert (hb_saturate_sub (INT_MIN, 1) == INT_MIN);
hb_always_assert (hb_saturate_sub (INT_MIN, INT_MIN) == 0);
hb_always_assert (hb_saturate_neg (2) == -2);
hb_always_assert (hb_saturate_neg (-2) == 2);
hb_always_assert (hb_saturate_neg (INT_MIN) == INT_MAX);
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);
x = 1;
hb_always_assert (++hb_inc (x) == 3);