vppinfra: add bihash 12_4 template
[vpp.git] / src / vppinfra / bihash_12_4.h
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright(c) 2021 Cisco Systems, Inc.
3  */
4 #undef BIHASH_TYPE
5 #undef BIHASH_KVP_PER_PAGE
6 #undef BIHASH_32_64_SVM
7 #undef BIHASH_ENABLE_STATS
8 #undef BIHASH_KVP_AT_BUCKET_LEVEL
9 #undef BIHASH_LAZY_INSTANTIATE
10 #undef BIHASH_BUCKET_PREFETCH_CACHE_LINES
11 #undef BIHASH_USE_HEAP
12
13 #define BIHASH_TYPE                        _12_4
14 #define BIHASH_KVP_PER_PAGE                4
15 #define BIHASH_KVP_AT_BUCKET_LEVEL         0
16 #define BIHASH_LAZY_INSTANTIATE            1
17 #define BIHASH_BUCKET_PREFETCH_CACHE_LINES 1
18 #define BIHASH_USE_HEAP                    1
19
20 #ifndef __included_bihash_12_4_h__
21 #define __included_bihash_12_4_h__
22
23 #include <vppinfra/crc32.h>
24 #include <vppinfra/heap.h>
25 #include <vppinfra/format.h>
26 #include <vppinfra/pool.h>
27 #include <vppinfra/xxhash.h>
28
29 typedef union
30 {
31   struct
32   {
33     u32 key[3];
34     u32 value;
35   };
36   u64 as_u64[2];
37 } clib_bihash_kv_12_4_t;
38
39 static inline int
40 clib_bihash_is_free_12_4 (const clib_bihash_kv_12_4_t *v)
41 {
42   /* Free values are clib_memset to 0xff, check a bit... */
43   if (v->as_u64[0] == ~0ULL && v->value == ~0)
44     return 1;
45   return 0;
46 }
47
48 static inline u64
49 clib_bihash_hash_12_4 (const clib_bihash_kv_12_4_t *v)
50 {
51 #ifdef clib_crc32c_uses_intrinsics
52   return clib_crc32c ((u8 *) v->key, 12);
53 #else
54   u64 tmp = v->as_u64[0] ^ v->key[2];
55   return clib_xxhash (tmp);
56 #endif
57 }
58
59 static inline u8 *
60 format_bihash_kvp_12_4 (u8 *s, va_list *args)
61 {
62   clib_bihash_kv_12_4_t *v = va_arg (*args, clib_bihash_kv_12_4_t *);
63
64   s = format (s, "key %u %u %u value %u", v->key[0], v->key[1], v->key[2],
65               v->value);
66   return s;
67 }
68
69 static inline int
70 clib_bihash_key_compare_12_4 (u32 *a, u32 *b)
71 {
72 #if defined(CLIB_HAVE_VEC128)
73   u32x4 v = (*(u32x4u *) a) ^ (*(u32x4u *) b);
74   v[3] = 0;
75   return u32x4_is_all_zero (v);
76 #else
77   return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) == 0;
78 #endif
79 }
80
81 #undef __included_bihash_template_h__
82 #include <vppinfra/bihash_template.h>
83
84 #endif /* __included_bihash_12_4_h__ */