diff --git a/src/hb-algs.hh b/src/hb-algs.hh index 71f28434e..52e64aed6 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -35,7 +35,9 @@ #include "hb-number.hh" -/* Flags */ +/* + * Flags + */ /* Enable bitwise ops on enums marked as flags_t */ /* To my surprise, looks like the function resolver is happy to silently cast @@ -71,6 +73,83 @@ #define FLAG64_UNSAFE(x) ((unsigned)(x) < 64 ? (((uint64_t) 1ULL) << (unsigned)(x)) : 0) +/* + * Big-endian integers. + */ + +/* Endian swap, used in Windows related backends */ +static inline constexpr uint16_t hb_uint16_swap (uint16_t v) +{ return (v >> 8) | (v << 8); } +static inline constexpr uint32_t hb_uint32_swap (uint32_t v) +{ return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16); } + +template struct BEInt; +template +struct BEInt +{ + public: + BEInt () = default; + constexpr BEInt (Type V) : v {V} {} + constexpr operator Type () const { return v; } + private: uint8_t v; +}; +template +struct BEInt +{ + public: + BEInt () = default; + constexpr BEInt (Type V) : v {(V >> 8) & 0xFF, + (V ) & 0xFF} {} + constexpr operator Type () const + { +#if ((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__)) && \ + defined(__BYTE_ORDER) && \ + (__BYTE_ORDER == __LITTLE_ENDIAN || __BYTE_ORDER == __BIG_ENDIAN) + /* Spoon-feed the compiler a big-endian integer with alignment 1. + * https://github.com/harfbuzz/harfbuzz/pull/1398 */ + struct __attribute__((packed)) packed_uint16_t { uint16_t v; }; +#if __BYTE_ORDER == __LITTLE_ENDIAN + return __builtin_bswap16 (((packed_uint16_t *) this)->v); +#else /* __BYTE_ORDER == __BIG_ENDIAN */ + return ((packed_uint16_t *) this)->v; +#endif +#endif + return (v[0] << 8) + + (v[1] ); + } + private: uint8_t v[2]; +}; +template +struct BEInt +{ + public: + BEInt () = default; + constexpr BEInt (Type V) : v {(V >> 16) & 0xFF, + (V >> 8) & 0xFF, + (V ) & 0xFF} {} + constexpr operator Type () const { return (v[0] << 16) + + (v[1] << 8) + + (v[2] ); } + private: uint8_t v[3]; +}; +template +struct BEInt +{ + public: + BEInt () = default; + constexpr BEInt (Type V) : v {(V >> 24) & 0xFF, + (V >> 16) & 0xFF, + (V >> 8) & 0xFF, + (V ) & 0xFF} {} + constexpr operator Type () const { return (v[0] << 24) + + (v[1] << 16) + + (v[2] << 8) + + (v[3] ); } + private: uint8_t v[4]; +}; + + + /* Encodes three unsigned integers in one 64-bit number. If the inputs have more than 21 bits, * values will be truncated / overlap, and might not decode exactly. */ #define HB_CODEPOINT_ENCODE3(x,y,z) (((uint64_t) (x) << 42) | ((uint64_t) (y) << 21) | (uint64_t) (z)) diff --git a/src/hb.hh b/src/hb.hh index fe6ffc873..54d0d9823 100644 --- a/src/hb.hh +++ b/src/hb.hh @@ -457,82 +457,6 @@ static inline float _hb_roundf (float x) { return floorf (x + .5f); } #define roundf(x) _hb_roundf(x) -/* Endian swap, used in Windows related backends */ -static inline constexpr uint16_t hb_uint16_swap (uint16_t v) -{ return (v >> 8) | (v << 8); } -static inline constexpr uint32_t hb_uint32_swap (uint32_t v) -{ return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16); } - -/* - * Big-endian integers. - */ - -template struct BEInt; - -template -struct BEInt -{ - public: - BEInt () = default; - constexpr BEInt (Type V) : v {V} {} - constexpr operator Type () const { return v; } - private: uint8_t v; -}; -template -struct BEInt -{ - public: - BEInt () = default; - constexpr BEInt (Type V) : v {(V >> 8) & 0xFF, - (V ) & 0xFF} {} - constexpr operator Type () const - { -#if ((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__)) && \ - defined(__BYTE_ORDER) && \ - (__BYTE_ORDER == __LITTLE_ENDIAN || __BYTE_ORDER == __BIG_ENDIAN) - /* Spoon-feed the compiler a big-endian integer with alignment 1. - * https://github.com/harfbuzz/harfbuzz/pull/1398 */ - struct __attribute__((packed)) packed_uint16_t { uint16_t v; }; -#if __BYTE_ORDER == __LITTLE_ENDIAN - return __builtin_bswap16 (((packed_uint16_t *) this)->v); -#else /* __BYTE_ORDER == __BIG_ENDIAN */ - return ((packed_uint16_t *) this)->v; -#endif -#endif - return (v[0] << 8) - + (v[1] ); - } - private: uint8_t v[2]; -}; -template -struct BEInt -{ - public: - BEInt () = default; - constexpr BEInt (Type V) : v {(V >> 16) & 0xFF, - (V >> 8) & 0xFF, - (V ) & 0xFF} {} - constexpr operator Type () const { return (v[0] << 16) - + (v[1] << 8) - + (v[2] ); } - private: uint8_t v[3]; -}; -template -struct BEInt -{ - public: - BEInt () = default; - constexpr BEInt (Type V) : v {(V >> 24) & 0xFF, - (V >> 16) & 0xFF, - (V >> 8) & 0xFF, - (V ) & 0xFF} {} - constexpr operator Type () const { return (v[0] << 24) - + (v[1] << 16) - + (v[2] << 8) - + (v[3] ); } - private: uint8_t v[4]; -}; - /* * For lack of a better place, put Zawgyi script hack here.