Skip to content

Commit 4aec463

Browse files
committed
Improving djbHash for MIPS.
1 parent 7ae9afe commit 4aec463

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/mips/common/util/djbhash.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,32 @@ SOFTWARE.
2828

2929
#include <stdint.h>
3030

31+
#ifdef __cplusplus
32+
#include <concepts>
33+
34+
namespace djb {
35+
36+
template <std::integral T = uint32_t>
37+
static inline constexpr T process(T hash, const char str[], unsigned n) {
38+
return n ? process(((hash << 5) + hash) ^ static_cast<uint8_t>(str[0]), str + 1, n - 1) : hash;
39+
}
40+
41+
template <std::integral T = uint32_t>
42+
static inline T constexpr hash(const char* str, unsigned n) {
43+
return process(T(5381), str, n);
44+
}
45+
46+
template <std::integral T = uint32_t, unsigned S>
47+
static inline T constexpr hash(const char (&str)[S]) {
48+
return process(T(5381), str, S - 1);
49+
}
50+
51+
} // namespace djb
52+
53+
#endif
54+
3155
static inline uint32_t djbProcess(uint32_t hash, const char str[], unsigned n) {
32-
return n ? djbProcess(((hash << 5) + hash) ^ str[0], str + 1, n - 1) : hash;
56+
return n ? djbProcess(((hash << 5) + hash) ^ ((uint8_t)str[0]), str + 1, n - 1) : hash;
3357
}
3458

3559
static inline uint32_t djbHash(const char* str, unsigned n) { return djbProcess(5381, str, n); }

0 commit comments

Comments
 (0)