From bc8e5d876081f37b200220dbf253aff76cabe091 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 3 Oct 2026 20:51:48 -0600 Subject: [PATCH] [rust] Round Fontations advances without a libm call Float rounding accounts for about a third of static horizontal advance lookup time. Bias in f64 before the saturating integer conversion to preserve ties away from zero without calling roundf for each glyph. Using f64 also preserves values immediately below half-integers. An exhaustive comparison of all f32 bit patterns produces the same integer results. All 144 font/scale/location metric snapshots match. The full Meson suite passes (281 tests, one skip); release Clippy uses opt-level=0 to work around a nightly compiler hang. Library and benchmark builds remain optimized. Assisted-by: OpenAI Codex --- src/rust/font.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/rust/font.rs b/src/rust/font.rs index 42730169f..f635b1e53 100644 --- a/src/rust/font.rs +++ b/src/rust/font.rs @@ -234,6 +234,13 @@ fn hb_position(value: i64) -> hb_position_t { value.clamp(hb_position_t::MIN as i64, hb_position_t::MAX as i64) as hb_position_t } +#[inline] +fn round_to_position(value: f32) -> hb_position_t { + // Bias in f64 so values just below a half-integer do not round up. + let value = f64::from(value); + (value + 0.5f64.copysign(value)) as hb_position_t +} + fn font_line_extents(font: *mut hb_font_t) -> LineExtents { let mut extents: hb_font_extents_t = unsafe { std::mem::zeroed() }; unsafe { @@ -508,7 +515,7 @@ extern "C" fn _hb_fontations_get_glyph_h_advances( |glyphs| { data.instance.glyph_metrics().h_advance_batched( // Skrifa rounded advances to design units before applying our scale. - |advance| (advance.to_i32() as f32 * data.x_mult).round() as hb_position_t, + |advance| round_to_position(advance.to_i32() as f32 * data.x_mult), glyphs, ); },