From f466b9678b4b68e56d7374dea51218348d628b06 Mon Sep 17 00:00:00 2001 From: Grigori Goronzy Date: Wed, 29 Jan 2014 05:25:40 +0100 Subject: [PATCH] Unroll FNV-1A hash function Unroll the hash function with Duff's device for improved performance. --- libass/ass_utils.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/libass/ass_utils.h b/libass/ass_utils.h index 6d795f0..4e2ba6c 100644 --- a/libass/ass_utils.h +++ b/libass/ass_utils.h @@ -132,11 +132,16 @@ static inline int rot_key(double a) static inline unsigned fnv_32a_buf(void *buf, size_t len, unsigned hval) { unsigned char *bp = buf; - unsigned char *be = bp + len; - while (bp < be) { - hval ^= (unsigned) *bp++; - hval *= FNV1_32A_PRIME; + size_t n = (len + 3) / 4; + + switch (len % 4) { + case 0: do { hval ^= (unsigned) *bp++; hval *= FNV1_32A_PRIME; + case 3: hval ^= (unsigned) *bp++; hval *= FNV1_32A_PRIME; + case 2: hval ^= (unsigned) *bp++; hval *= FNV1_32A_PRIME; + case 1: hval ^= (unsigned) *bp++; hval *= FNV1_32A_PRIME; + } while (--n > 0); } + return hval; } static inline unsigned fnv_32a_str(char *str, unsigned hval) -- 2.40.0