From a8b89a09f6d3a3466282aae07fd65e143f9f8f83 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 11 Jul 2014 14:18:01 -0400 Subject: [PATCH] Simplify hb_in_range() It's both faster and produces smaller code. Now I feel stupid for not writing it this way before. --- src/hb-private.hh | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/hb-private.hh b/src/hb-private.hh index f2f580a54..fb07e2451 100644 --- a/src/hb-private.hh +++ b/src/hb-private.hh @@ -786,20 +786,16 @@ struct hb_auto_trace_t<0, ret_t> { /* Misc */ +template class hb_assert_unsigned_t; +template <> class hb_assert_unsigned_t {}; +template <> class hb_assert_unsigned_t {}; +template <> class hb_assert_unsigned_t {}; -/* Pre-mature optimization: - * Checks for lo <= u <= hi but with an optimization if lo and hi - * are only different in a contiguous set of lower-most bits. - */ template static inline bool hb_in_range (T u, T lo, T hi) { - if ( ((lo^hi) & lo) == 0 && - ((lo^hi) & hi) == (lo^hi) && - ((lo^hi) & ((lo^hi) + 1)) == 0 ) - return (u & ~(lo^hi)) == lo; - else - return lo <= u && u <= hi; + hb_assert_unsigned_t error_hb_in_range_called_with_signed_type HB_UNUSED; + return (u - lo) <= (hi - lo); } template static inline bool