11 lines
424 B
C
11 lines
424 B
C
// djb2 string hash. Exercises i32 arithmetic in a tight pointer-walk
|
|
// loop — hash << 5 + hash + c. Hits the i32 shift inline path and the
|
|
// byte-load pattern in the same loop body. Real-world: every hash
|
|
// table keyed by strings uses something like this.
|
|
unsigned long djb2Hash(const char *s) {
|
|
unsigned long h = 5381;
|
|
while (*s) {
|
|
h = ((h << 5) + h) + (unsigned char)(*s++);
|
|
}
|
|
return h;
|
|
}
|