From d350cb833a607cbfbd34c092fed458d507d9ff94 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 23 Sep 2025 19:47:01 -0600 Subject: [PATCH] Speed up ValueFormat len() calc --- src/OT/Layout/GPOS/ValueFormat.hh | 2 +- src/hb-algs.hh | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/OT/Layout/GPOS/ValueFormat.hh b/src/OT/Layout/GPOS/ValueFormat.hh index 89a5493f4..8db39f9eb 100644 --- a/src/OT/Layout/GPOS/ValueFormat.hh +++ b/src/OT/Layout/GPOS/ValueFormat.hh @@ -58,7 +58,7 @@ struct ValueFormat : HBUINT16 NumType& operator = (uint16_t i) { v = i; return *this; } - unsigned int get_len () const { return hb_popcount ((unsigned int) *this); } + unsigned int get_len () const { return hb_popcount8 ((unsigned int) *this); } unsigned int get_size () const { return get_len () * Value::static_size; } hb_vector_t get_device_table_indices () const { diff --git a/src/hb-algs.hh b/src/hb-algs.hh index d87daa1d3..e881df29f 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -854,6 +854,17 @@ HB_FUNCOBJ (hb_clamp); * Bithacks. */ +/* Return the number of 1 bits in a uint8_t; faster than hb_popcount() */ +static inline unsigned +hb_popcount8 (unsigned v) +{ + static const uint8_t popcount4[16] = { + 0, 1, 1, 2, 1, 2, 2, 3, + 1, 2, 2, 3, 2, 3, 3, 4 + }; + return popcount4[v & 0xF] + popcount4[v >> 4]; +} + /* Return the number of 1 bits in v. */ template static inline unsigned int