Skip to content

Commit 8951d94

Browse files
committed
FEAT: Using Knuth's multiplicative hash for color tuples
1 parent 97496e7 commit 8951d94

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/core/s-crc.c

+15-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
** REBOL [R3] Language Interpreter and Run-time Environment
44
**
55
** Copyright 2012 REBOL Technologies
6-
** Copyright 2012-2021 Rebol Open Source Contributors
6+
** Copyright 2012-2022 Rebol Open Source Contributors
77
** REBOL is a trademark of REBOL Technologies
88
**
99
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -242,7 +242,20 @@ static REBCNT *CRC32_Table = 0;
242242
break;
243243

244244
case REB_TUPLE:
245-
ret = Hash_String(VAL_TUPLE(val), VAL_TUPLE_LEN(val));
245+
if (VAL_TUPLE_LEN(val) <= 4) {
246+
// For colors (tuple of size 4) there may be used Knuth's multiplicative hash.
247+
// Knuth's multiplication constant is 2654435761L, but it may be different as long
248+
// as it is:
249+
// 1. Odd number (Otherwise we may lose the highest bit.)
250+
// 2. Has no long streaks of ones or zeros.
251+
ret = ((REBCNT)VAL_INT32(val) * 2654435761L) >> 26;
252+
// The higher bits contain more mixture from the multiplication,
253+
// so the shift is not 32, but just 26.
254+
255+
// In Brotli compressor the constant is 506832829L and the shift is 18.
256+
} else {
257+
ret = Hash_String(VAL_TUPLE(val), VAL_TUPLE_LEN(val));
258+
}
246259
break;
247260

248261
case REB_PAIR:

0 commit comments

Comments
 (0)