xref: /OK3568_Linux_fs/external/mpp/osal/inc/mpp_hash.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2021 Rockchip Electronics Co. LTD
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __MPP_HASH_H__
18 #define __MPP_HASH_H__
19 
20 #include <stdbool.h>
21 
22 #include "rk_type.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #define GOLDEN_RATIO_32 0x61C88647
29 #define GOLDEN_RATIO_64 0x61C8864680B583EBull
30 
31 #if __SIZEOF_POINTER__ == 4
32 #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_32
33 #define hash_long(val, bits) hash_32(val, bits)
34 #elif __SIZEOF_POINTER__ == 8
35 #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_64
36 #define hash_long(val, bits) hash_64(val, bits)
37 #else
38 #error __SIZEOF_POINTER__ not 4 or 8
39 #endif
40 
41 struct hlist_node {
42     struct hlist_node *next, **pprev;
43 };
44 
45 struct hlist_head {
46     struct hlist_node *first;
47 };
48 
49 #define HLIST_HEAD_INIT { .first = NULL }
50 #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
51 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
52 
53 #define LIST_POISON1  ((void *) 0x100)
54 #define LIST_POISON2  ((void *) 0x200)
55 
56 #define WRITE_ONCE(var, val) \
57     (*((volatile typeof(val) *)(&(var))) = (val))
58 
59 #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
60 
INIT_HLIST_NODE(struct hlist_node * h)61 static inline void INIT_HLIST_NODE(struct hlist_node *h)
62 {
63     h->next = NULL;
64     h->pprev = NULL;
65 }
66 
hlist_unhashed(const struct hlist_node * h)67 static inline int hlist_unhashed(const struct hlist_node *h)
68 {
69     return !h->pprev;
70 }
71 
hlist_empty(const struct hlist_head * h)72 static inline int hlist_empty(const struct hlist_head *h)
73 {
74     return !READ_ONCE(h->first);
75 }
76 
__hlist_del(struct hlist_node * n)77 static inline void __hlist_del(struct hlist_node *n)
78 {
79     struct hlist_node *next = n->next;
80     struct hlist_node **pprev = n->pprev;
81 
82     WRITE_ONCE(*pprev, next);
83     if (next)
84         next->pprev = pprev;
85 }
86 
hlist_del(struct hlist_node * n)87 static inline void hlist_del(struct hlist_node *n)
88 {
89     __hlist_del(n);
90     n->next = (struct hlist_node*)LIST_POISON1;
91     n->pprev = (struct hlist_node**)LIST_POISON2;
92 }
93 
hlist_del_init(struct hlist_node * n)94 static inline void hlist_del_init(struct hlist_node *n)
95 {
96     if (!hlist_unhashed(n)) {
97         __hlist_del(n);
98         INIT_HLIST_NODE(n);
99     }
100 }
101 
hlist_add_head(struct hlist_node * n,struct hlist_head * h)102 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
103 {
104     struct hlist_node *first = h->first;
105     n->next = first;
106     if (first)
107         first->pprev = &n->next;
108     WRITE_ONCE(h->first, n);
109     n->pprev = &h->first;
110 }
111 
hlist_add_before(struct hlist_node * n,struct hlist_node * next)112 static inline void hlist_add_before(struct hlist_node *n, struct hlist_node *next)
113 {
114     n->pprev = next->pprev;
115     n->next = next;
116     next->pprev = &n->next;
117     WRITE_ONCE(*(n->pprev), n);
118 }
119 
hlist_add_behind(struct hlist_node * n,struct hlist_node * prev)120 static inline void hlist_add_behind(struct hlist_node *n, struct hlist_node *prev)
121 {
122     n->next = prev->next;
123     WRITE_ONCE(prev->next, n);
124     n->pprev = &prev->next;
125 
126     if (n->next)
127         n->next->pprev  = &n->next;
128 }
129 
hlist_add_fake(struct hlist_node * n)130 static inline void hlist_add_fake(struct hlist_node *n)
131 {
132     n->pprev = &n->next;
133 }
134 
hlist_fake(struct hlist_node * h)135 static inline int hlist_fake(struct hlist_node *h)
136 {
137     return h->pprev == &h->next;
138 }
139 
140 static inline int
hlist_is_singular_node(struct hlist_node * n,struct hlist_head * h)141 hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
142 {
143     return !n->next && n->pprev == &h->first;
144 }
145 
hlist_move_list(struct hlist_head * old,struct hlist_head * _new)146 static inline void hlist_move_list(struct hlist_head *old,
147                                    struct hlist_head *_new)
148 {
149     _new->first = old->first;
150     if (_new->first)
151         _new->first->pprev = &_new->first;
152     old->first = NULL;
153 }
154 
155 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
156 
157 #define hlist_for_each(pos, head) \
158     for (pos = (head)->first; pos ; pos = pos->next)
159 
160 #define hlist_for_each_safe(pos, n, head) \
161     for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
162          pos = n)
163 
164 #define hlist_entry_safe(ptr, type, member) \
165     ({ typeof(ptr) ____ptr = (ptr); \
166        ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
167     })
168 
169 #define hlist_for_each_entry(pos, head, member)             \
170     for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
171          pos;                           \
172          pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
173 
174 #define hlist_for_each_entry_continue(pos, member)          \
175     for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
176          pos;                           \
177          pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
178 
179 #define hlist_for_each_entry_from(pos, member)              \
180     for (; pos;                         \
181          pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
182 
183 #define hlist_for_each_entry_safe(pos, n, head, member)         \
184     for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
185          pos && ({ n = pos->member.next; 1; });         \
186          pos = hlist_entry_safe(n, typeof(*pos), member))
187 
188 #define DEFINE_HASHTABLE(name, bits) \
189     struct hlist_head name[1 << (bits)] = \
190             { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
191 
192 #define DECLARE_HASHTABLE(name, bits) \
193     struct hlist_head name[1 << (bits)]
194 
195 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
196 
197 #define ilog2(n) \
198     ( \
199         (n) & (1ULL << 63) ? 63 : \
200         (n) & (1ULL << 62) ? 62 : \
201         (n) & (1ULL << 61) ? 61 : \
202         (n) & (1ULL << 60) ? 60 : \
203         (n) & (1ULL << 59) ? 59 : \
204         (n) & (1ULL << 58) ? 58 : \
205         (n) & (1ULL << 57) ? 57 : \
206         (n) & (1ULL << 56) ? 56 : \
207         (n) & (1ULL << 55) ? 55 : \
208         (n) & (1ULL << 54) ? 54 : \
209         (n) & (1ULL << 53) ? 53 : \
210         (n) & (1ULL << 52) ? 52 : \
211         (n) & (1ULL << 51) ? 51 : \
212         (n) & (1ULL << 50) ? 50 : \
213         (n) & (1ULL << 49) ? 49 : \
214         (n) & (1ULL << 48) ? 48 : \
215         (n) & (1ULL << 47) ? 47 : \
216         (n) & (1ULL << 46) ? 46 : \
217         (n) & (1ULL << 45) ? 45 : \
218         (n) & (1ULL << 44) ? 44 : \
219         (n) & (1ULL << 43) ? 43 : \
220         (n) & (1ULL << 42) ? 42 : \
221         (n) & (1ULL << 41) ? 41 : \
222         (n) & (1ULL << 40) ? 40 : \
223         (n) & (1ULL << 39) ? 39 : \
224         (n) & (1ULL << 38) ? 38 : \
225         (n) & (1ULL << 37) ? 37 : \
226         (n) & (1ULL << 36) ? 36 : \
227         (n) & (1ULL << 35) ? 35 : \
228         (n) & (1ULL << 34) ? 34 : \
229         (n) & (1ULL << 33) ? 33 : \
230         (n) & (1ULL << 32) ? 32 : \
231         (n) & (1ULL << 31) ? 31 : \
232         (n) & (1ULL << 30) ? 30 : \
233         (n) & (1ULL << 29) ? 29 : \
234         (n) & (1ULL << 28) ? 28 : \
235         (n) & (1ULL << 27) ? 27 : \
236         (n) & (1ULL << 26) ? 26 : \
237         (n) & (1ULL << 25) ? 25 : \
238         (n) & (1ULL << 24) ? 24 : \
239         (n) & (1ULL << 23) ? 23 : \
240         (n) & (1ULL << 22) ? 22 : \
241         (n) & (1ULL << 21) ? 21 : \
242         (n) & (1ULL << 20) ? 20 : \
243         (n) & (1ULL << 19) ? 19 : \
244         (n) & (1ULL << 18) ? 18 : \
245         (n) & (1ULL << 17) ? 17 : \
246         (n) & (1ULL << 16) ? 16 : \
247         (n) & (1ULL << 15) ? 15 : \
248         (n) & (1ULL << 14) ? 14 : \
249         (n) & (1ULL << 13) ? 13 : \
250         (n) & (1ULL << 12) ? 12 : \
251         (n) & (1ULL << 11) ? 11 : \
252         (n) & (1ULL << 10) ? 10 : \
253         (n) & (1ULL << 9) ? 9 : \
254         (n) & (1ULL << 8) ? 8 : \
255         (n) & (1ULL << 7) ? 7 : \
256         (n) & (1ULL << 6) ? 6 : \
257         (n) & (1ULL << 5) ? 5 : \
258         (n) & (1ULL << 4) ? 4 : \
259         (n) & (1ULL << 3) ? 3 : \
260         (n) & (1ULL << 2) ? 2 : \
261         (n) & (1ULL << 1) ? 1 : 0 \
262     )
263 
264 #define HASH_SIZE(name) (ARRAY_SIZE(name))
265 #define HASH_BITS(name) ilog2(HASH_SIZE(name))
266 
267 /* Use hash_32 when possible to allow for fast 32bit hashing in 64bit kernels. */
268 #define hash_min(val, bits) \
269     (sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
270 
271 #define hash_add(hashtable, node, key) \
272     hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
273 
274 /**
275  * hash_empty - check whether a hashtable is empty
276  * @hashtable: hashtable to check
277  *
278  * This has to be a macro since HASH_BITS() will not work on pointers since
279  * it calculates the size during preprocessing.
280  */
281 #define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
282 
283 /**
284  * hash_for_each - iterate over a hashtable
285  * @name: hashtable to iterate
286  * @bkt: integer to use as bucket loop cursor
287  * @obj: the type * to use as a loop cursor for each entry
288  * @member: the name of the hlist_node within the struct
289  */
290 #define hash_for_each(name, bkt, obj, member)                           \
291     for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name); \
292          (bkt)++)                                                       \
293     hlist_for_each_entry(obj, &name[bkt], member)
294 
295 /**
296  * hash_for_each_safe - iterate over a hashtable safe against removal of
297  * hash entry
298  * @name: hashtable to iterate
299  * @bkt: integer to use as bucket loop cursor
300  * @tmp: a &struct used for temporary storage
301  * @obj: the type * to use as a loop cursor for each entry
302  * @member: the name of the hlist_node within the struct
303  */
304 #define hash_for_each_safe(name, bkt, tmp, obj, member)                 \
305     for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name); \
306          (bkt)++)                                                       \
307     hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
308 
309 #define hash_for_each_possible(name, obj, member, key) \
310     hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)
311 
hash_32(RK_U32 val,unsigned int bits)312 static inline RK_U32 hash_32(RK_U32 val, unsigned int bits)
313 {
314     /* On some cpus multiply is faster, on others gcc will do shifts */
315     RK_U32 hash = val * GOLDEN_RATIO_32;
316 
317     /* High bits are more random, so use them. */
318     return hash >> (32 - bits);
319 }
320 
__hash_32(RK_U32 val)321 static inline RK_U32 __hash_32(RK_U32 val)
322 {
323     return val * GOLDEN_RATIO_32;
324 }
325 
hash_64(RK_U64 val,unsigned int bits)326 static inline RK_U32 hash_64(RK_U64 val, unsigned int bits)
327 {
328 #if __SIZEOF_POINTER__ == 8
329     /* 64x64-bit multiply is efficient on all 64-bit processors */
330     return val * GOLDEN_RATIO_64 >> (64 - bits);
331 #else
332     /* Hash 64 bits using only 32x32-bit multiply. */
333     return hash_32((RK_U32)val ^ ((val >> 32) * GOLDEN_RATIO_32), bits);
334 #endif
335 }
336 
hash_ptr(const void * ptr,unsigned int bits)337 static inline RK_U32 hash_ptr(const void *ptr, unsigned int bits)
338 {
339     return hash_long((unsigned long)ptr, bits);
340 }
341 
342 /* This really should be called fold32_ptr; it does no hashing to speak of. */
hash32_ptr(const void * ptr)343 static inline RK_U32 hash32_ptr(const void *ptr)
344 {
345     unsigned long val = (unsigned long)ptr;
346 
347 #if __SIZEOF_POINTER__ == 8
348     val ^= (val >> 32);
349 #endif
350     return (RK_U32)val;
351 }
352 
353 /**
354  * hash_hashed - check whether an object is in any hashtable
355  * @node: the &struct hlist_node of the object to be checked
356  */
hash_hashed(struct hlist_node * node)357 static inline bool hash_hashed(struct hlist_node *node)
358 {
359     return !hlist_unhashed(node);
360 }
361 
__hash_empty(struct hlist_head * ht,unsigned int sz)362 static inline bool __hash_empty(struct hlist_head *ht, unsigned int sz)
363 {
364     unsigned int i;
365 
366     for (i = 0; i < sz; i++)
367         if (!hlist_empty(&ht[i]))
368             return false;
369 
370     return true;
371 }
372 
373 /**
374  * hash_del - remove an object from a hashtable
375  * @node: &struct hlist_node of the object to remove
376  */
hash_del(struct hlist_node * node)377 static inline void hash_del(struct hlist_node *node)
378 {
379     hlist_del_init(node);
380 }
381 
382 #ifdef __cplusplus
383 }
384 #endif
385 
386 #endif /*__MPP_HASH_H__*/
387