Simplify hb_in_range()

It's both faster and produces smaller code.  Now I feel stupid for
not writing it this way before.
pull/46/head
Behdad Esfahbod 10 years ago
parent db8934faa1
commit a8b89a09f6
  1. 16
      src/hb-private.hh

@ -786,20 +786,16 @@ struct hb_auto_trace_t<0, ret_t> {
/* Misc */
template <typename T> class hb_assert_unsigned_t;
template <> class hb_assert_unsigned_t<unsigned char> {};
template <> class hb_assert_unsigned_t<unsigned int> {};
template <> class hb_assert_unsigned_t<unsigned long> {};
/* 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 <typename T> 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<T> error_hb_in_range_called_with_signed_type HB_UNUSED;
return (u - lo) <= (hi - lo);
}
template <typename T> static inline bool

Loading…
Cancel
Save