[array] Speed up hash() for byte arrays
This commit is contained in:
parent
d7b492e3f5
commit
bca569ae53
|
@ -424,17 +424,41 @@ inline bool hb_array_t<const unsigned char>::operator == (const hb_array_t<const
|
||||||
return 0 == hb_memcmp (arrayZ, o.arrayZ, length);
|
return 0 == hb_memcmp (arrayZ, o.arrayZ, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Specialize hash() for byte arrays. */
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
inline uint32_t hb_array_t<const char>::hash () const {
|
inline uint32_t hb_array_t<const char>::hash () const
|
||||||
|
{
|
||||||
uint32_t current = 0;
|
uint32_t current = 0;
|
||||||
for (unsigned int i = 0; i < this->length; i++)
|
unsigned i = 0;
|
||||||
|
|
||||||
|
#if defined(__OPTIMIZE__) && !defined(HB_NO_PACKED) && \
|
||||||
|
((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__))
|
||||||
|
struct __attribute__((packed)) packed_uint32_t { uint32_t v; };
|
||||||
|
for (; i + 4 <= this->length; i += 4)
|
||||||
|
current = current * 31 + (uint32_t) ((((packed_uint32_t *) &this->arrayZ[i])->v) * 2654435761u);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
for (; i < this->length; i++)
|
||||||
current = current * 31 + (uint32_t) (this->arrayZ[i] * 2654435761u);
|
current = current * 31 + (uint32_t) (this->arrayZ[i] * 2654435761u);
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
inline uint32_t hb_array_t<const unsigned char>::hash () const {
|
inline uint32_t hb_array_t<const unsigned char>::hash () const
|
||||||
|
{
|
||||||
uint32_t current = 0;
|
uint32_t current = 0;
|
||||||
for (unsigned int i = 0; i < this->length; i++)
|
unsigned i = 0;
|
||||||
|
|
||||||
|
#if defined(__OPTIMIZE__) && !defined(HB_NO_PACKED) && \
|
||||||
|
((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__))
|
||||||
|
struct __attribute__((packed)) packed_uint32_t { uint32_t v; };
|
||||||
|
for (; i + 4 <= this->length; i += 4)
|
||||||
|
current = current * 31 + (uint32_t) ((((packed_uint32_t *) &this->arrayZ[i])->v) * 2654435761u);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
for (; i < this->length; i++)
|
||||||
current = current * 31 + (uint32_t) (this->arrayZ[i] * 2654435761u);
|
current = current * 31 + (uint32_t) (this->arrayZ[i] * 2654435761u);
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue