Decoupling Architectural-specific Code (#35)
[lunaix-os.git] / lunaix-os / libs / hash.c
1 #include <klibc/hash.h>
2 #include <lunaix/compiler.h>
3
4 /**
5  * @brief Simple string hash function
6  *
7  * ref: https://stackoverflow.com/a/7666577
8  *
9  * @param str
10  * @return unsigned int
11  */
12 u32_t weak
13 strhash_32(const char* str, u32_t truncate_to)
14 {
15     if (!str)
16         return 0;
17
18     u32_t hash = 5381;
19     int c;
20
21     while ((c = *str++))
22         hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
23
24     return hash >> (HASH_SIZE_BITS - truncate_to);
25 }