[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
This commit is contained in:
Behdad Esfahbod committed 2026-10-03 20:51:48 -06:00
1 parent 64259b67cd
commit bc8e5d8760
1 file changed
+8 -1
+8 -1
View File
@@ -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<i64> {
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,
);
},