mirror of
https://github.com/AuxXxilium/kmod.git
synced 2024-12-28 06:15:20 +07:00
libkmod-hash: use generic function for unaligned access
This commit is contained in:
parent
a6b67f90c3
commit
af9080d9f9
@ -21,6 +21,7 @@
|
||||
#include "libkmod.h"
|
||||
#include "libkmod-hash.h"
|
||||
|
||||
#include "libkmod-util.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
@ -83,15 +84,6 @@ void hash_free(struct hash *hash)
|
||||
free(hash);
|
||||
}
|
||||
|
||||
struct unaligned_short {
|
||||
unsigned short v;
|
||||
} __attribute__((packed));
|
||||
|
||||
static inline unsigned short get16bits(const char *ptr)
|
||||
{
|
||||
return ((struct unaligned_short *)ptr)->v;
|
||||
}
|
||||
|
||||
static inline unsigned int hash_superfast(const char *key, unsigned int len)
|
||||
{
|
||||
/* Paul Hsieh (http://www.azillionmonkeys.com/qed/hash.html)
|
||||
@ -104,8 +96,8 @@ static inline unsigned int hash_superfast(const char *key, unsigned int len)
|
||||
|
||||
/* Main loop */
|
||||
for (; len > 0; len--) {
|
||||
hash += get16bits(key);
|
||||
tmp = (get16bits(key + 2) << 11) ^ hash;
|
||||
hash += get_unaligned((uint16_t *) key);
|
||||
tmp = (get_unaligned((uint16_t *)(key + 2)) << 11) ^ hash;
|
||||
hash = (hash << 16) ^ tmp;
|
||||
key += 4;
|
||||
hash += hash >> 11;
|
||||
@ -114,14 +106,14 @@ static inline unsigned int hash_superfast(const char *key, unsigned int len)
|
||||
/* Handle end cases */
|
||||
switch (rem) {
|
||||
case 3:
|
||||
hash += get16bits(key);
|
||||
hash += get_unaligned((uint16_t *) key);
|
||||
hash ^= hash << 16;
|
||||
hash ^= key[2] << 18;
|
||||
hash += hash >> 11;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
hash += get16bits(key);
|
||||
hash += get_unaligned((uint16_t *) key);
|
||||
hash ^= hash << 11;
|
||||
hash += hash >> 17;
|
||||
break;
|
||||
|
@ -33,7 +33,7 @@ unsigned long long stat_mstamp(const struct stat *st);
|
||||
__p->__v; \
|
||||
})
|
||||
|
||||
#define bt_put_unaligned(val, ptr) \
|
||||
#define put_unaligned(val, ptr) \
|
||||
do { \
|
||||
struct __attribute__((packed)) { \
|
||||
typeof(*(ptr)) __v; \
|
||||
|
Loading…
Reference in New Issue
Block a user