[array] Implement FNV-1a hash function

pull/4225/head
Behdad Esfahbod 2 years ago
parent ebdeab8baa
commit 6d7de2f8dd
  1. 34
      src/hb-array.hh

@ -122,9 +122,13 @@ struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
uint32_t hash () const
{
uint32_t current = 7;
// FNV-1a hash function
uint32_t current = /*cbf29ce4*/0x84222325;
for (auto &v : *this)
current = current * 31 + hb_hash (v);
{
current = current ^ hb_hash (v);
current = current * 16777619;
}
return current;
}
@ -452,36 +456,50 @@ inline bool hb_array_t<const unsigned char>::operator == (const hb_array_t<const
template <>
inline uint32_t hb_array_t<const char>::hash () const
{
uint32_t current = 7;
// FNV-1a hash function
uint32_t current = /*cbf29ce4*/0x84222325;
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 + hb_hash ((uint32_t) ((packed_uint32_t *) &this->arrayZ[i])->v);
{
current = current ^ hb_hash ((uint32_t) ((packed_uint32_t *) &this->arrayZ[i])->v);
current = current * 16777619;
}
#endif
for (; i < this->length; i++)
current = current * 31 + hb_hash (this->arrayZ[i]);
{
current = current ^ hb_hash (this->arrayZ[i]);
current = current * 16777619;
}
return current;
}
template <>
inline uint32_t hb_array_t<const unsigned char>::hash () const
{
uint32_t current = 7;
// FNV-1a hash function
uint32_t current = /*cbf29ce4*/0x84222325;
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 + hb_hash ((uint32_t) ((packed_uint32_t *) &this->arrayZ[i])->v);
{
current = current ^ hb_hash ((uint32_t) ((packed_uint32_t *) &this->arrayZ[i])->v);
current = current * 16777619;
}
#endif
for (; i < this->length; i++)
current = current * 31 + hb_hash (this->arrayZ[i]);
{
current = current ^ hb_hash (this->arrayZ[i]);
current = current * 16777619;
}
return current;
}

Loading…
Cancel
Save