Zephyr API 3.6.99
Loading...
Searching...
No Matches
hash_function.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Meta
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_SYS_HASH_FUNCTION_H_
8#define ZEPHYR_INCLUDE_SYS_HASH_FUNCTION_H_
9
10#include <stddef.h>
11#include <stdint.h>
12
13#include <zephyr/sys/__assert.h>
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
41typedef uint32_t (*sys_hash_func32_t)(const void *str, size_t n);
42
58static inline uint32_t sys_hash32_identity(const void *str, size_t n)
59{
60 switch (n) {
61 case sizeof(uint8_t):
62 return *(uint8_t *)str;
63 case sizeof(uint16_t):
64 return *(uint16_t *)str;
65 case sizeof(uint32_t):
66 return *(uint32_t *)str;
67 case sizeof(uint64_t):
68 return (uint32_t)(*(uint64_t *)str);
69 default:
70 break;
71 }
72
73 __ASSERT(false, "invalid str length %zu", n);
74
75 return 0;
76}
77
95uint32_t sys_hash32_djb2(const void *str, size_t n);
96
109uint32_t sys_hash32_murmur3(const void *str, size_t n);
110
119static inline uint32_t sys_hash32(const void *str, size_t n)
120{
121 if (IS_ENABLED(CONFIG_SYS_HASH_FUNC32_CHOICE_IDENTITY)) {
122 return sys_hash32_identity(str, n);
123 }
124
125 if (IS_ENABLED(CONFIG_SYS_HASH_FUNC32_CHOICE_DJB2)) {
126 return sys_hash32_djb2(str, n);
127 }
128
129 if (IS_ENABLED(CONFIG_SYS_HASH_FUNC32_CHOICE_MURMUR3)) {
130 return sys_hash32_murmur3(str, n);
131 }
132
133 __ASSERT(0, "No default 32-bit hash. See CONFIG_SYS_HASH_FUNC32_CHOICE");
134
135 return 0;
136}
137
142#ifdef __cplusplus
143}
144#endif
145
146#endif /* ZEPHYR_INCLUDE_SYS_HASH_FUNCTION_H_ */
uint32_t(* sys_hash_func32_t)(const void *str, size_t n)
32-bit Hash function interface
Definition hash_function.h:41
static uint32_t sys_hash32_identity(const void *str, size_t n)
The naive identity hash function.
Definition hash_function.h:58
uint32_t sys_hash32_djb2(const void *str, size_t n)
Daniel J. Bernstein's hash function.
uint32_t sys_hash32_murmur3(const void *str, size_t n)
Murmur3 hash function.
static uint32_t sys_hash32(const void *str, size_t n)
System default 32-bit hash function.
Definition hash_function.h:119
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition util_macro.h:124
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Macro utilities.