Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
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
12
13#ifndef ZEPHYR_INCLUDE_SYS_HASH_FUNCTION_H_
14#define ZEPHYR_INCLUDE_SYS_HASH_FUNCTION_H_
15
16#include <stddef.h>
17#include <stdint.h>
18
19#include <zephyr/sys/__assert.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
31
47typedef uint32_t (*sys_hash_func32_t)(const void *str, size_t n);
48
64static inline uint32_t sys_hash32_identity(const void *str, size_t n)
65{
66 switch (n) {
67 case sizeof(uint8_t):
68 return *(uint8_t *)str;
69 case sizeof(uint16_t):
70 return *(uint16_t *)str;
71 case sizeof(uint32_t):
72 return *(uint32_t *)str;
73 case sizeof(uint64_t):
74 return (uint32_t)(*(uint64_t *)str);
75 default:
76 break;
77 }
78
79 __ASSERT(false, "invalid str length %zu", n);
80
81 return 0;
82}
83
101uint32_t sys_hash32_djb2(const void *str, size_t n);
102
115uint32_t sys_hash32_murmur3(const void *str, size_t n);
116
125static inline uint32_t sys_hash32(const void *str, size_t n)
126{
127 if (IS_ENABLED(CONFIG_SYS_HASH_FUNC32_CHOICE_IDENTITY)) {
128 return sys_hash32_identity(str, n);
129 }
130
131 if (IS_ENABLED(CONFIG_SYS_HASH_FUNC32_CHOICE_DJB2)) {
132 return sys_hash32_djb2(str, n);
133 }
134
135 if (IS_ENABLED(CONFIG_SYS_HASH_FUNC32_CHOICE_MURMUR3)) {
136 return sys_hash32_murmur3(str, n);
137 }
138
139 __ASSERT(0, "No default 32-bit hash. See CONFIG_SYS_HASH_FUNC32_CHOICE");
140
141 return 0;
142}
143
147
148#ifdef __cplusplus
149}
150#endif
151
152#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:47
static uint32_t sys_hash32_identity(const void *str, size_t n)
The naive identity hash function.
Definition hash_function.h:64
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:125
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition util_macro.h:154
__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.