gateway: Created common code for routing in gateway
[samplevnf.git] / common / vnf_common / hash_func.h
1 /*
2 // Copyright (c) 2017 Intel Corporation
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 #ifndef __INCLUDE_HASH_FUNC_H__
17 #define __INCLUDE_HASH_FUNC_H__
18
19 static inline uint64_t
20 hash_xor_key8(void *key, __rte_unused uint32_t key_size, uint64_t seed)
21 {
22         uint64_t *k = key;
23         uint64_t xor0;
24
25         xor0 = seed ^ k[0];
26
27         return (xor0 >> 32) ^ xor0;
28 }
29
30 static inline uint64_t
31 hash_xor_key16(void *key, __rte_unused uint32_t key_size, uint64_t seed)
32 {
33         uint64_t *k = key;
34         uint64_t xor0;
35
36         xor0 = (k[0] ^ seed) ^ k[1];
37
38         return (xor0 >> 32) ^ xor0;
39 }
40
41 static inline uint64_t
42 hash_xor_key24(void *key, __rte_unused uint32_t key_size, uint64_t seed)
43 {
44         uint64_t *k = key;
45         uint64_t xor0;
46
47         xor0 = (k[0] ^ seed) ^ k[1];
48
49         xor0 ^= k[2];
50
51         return (xor0 >> 32) ^ xor0;
52 }
53
54 static inline uint64_t
55 hash_xor_key32(void *key, __rte_unused uint32_t key_size, uint64_t seed)
56 {
57         uint64_t *k = key;
58         uint64_t xor0, xor1;
59
60         xor0 = (k[0] ^ seed) ^ k[1];
61         xor1 = k[2] ^ k[3];
62
63         xor0 ^= xor1;
64
65         return (xor0 >> 32) ^ xor0;
66 }
67
68 static inline uint64_t
69 hash_xor_key40(void *key, __rte_unused uint32_t key_size, uint64_t seed)
70 {
71         uint64_t *k = key;
72         uint64_t xor0, xor1;
73
74         xor0 = (k[0] ^ seed) ^ k[1];
75         xor1 = k[2] ^ k[3];
76
77         xor0 ^= xor1;
78
79         xor0 ^= k[4];
80
81         return (xor0 >> 32) ^ xor0;
82 }
83
84 static inline uint64_t
85 hash_xor_key48(void *key, __rte_unused uint32_t key_size, uint64_t seed)
86 {
87         uint64_t *k = key;
88         uint64_t xor0, xor1, xor2;
89
90         xor0 = (k[0] ^ seed) ^ k[1];
91         xor1 = k[2] ^ k[3];
92         xor2 = k[4] ^ k[5];
93
94         xor0 ^= xor1;
95
96         xor0 ^= xor2;
97
98         return (xor0 >> 32) ^ xor0;
99 }
100
101 static inline uint64_t
102 hash_xor_key56(void *key, __rte_unused uint32_t key_size, uint64_t seed)
103 {
104         uint64_t *k = key;
105         uint64_t xor0, xor1, xor2;
106
107         xor0 = (k[0] ^ seed) ^ k[1];
108         xor1 = k[2] ^ k[3];
109         xor2 = k[4] ^ k[5];
110
111         xor0 ^= xor1;
112         xor2 ^= k[6];
113
114         xor0 ^= xor2;
115
116         return (xor0 >> 32) ^ xor0;
117 }
118
119 static inline uint64_t
120 hash_xor_key64(void *key, __rte_unused uint32_t key_size, uint64_t seed)
121 {
122         uint64_t *k = key;
123         uint64_t xor0, xor1, xor2, xor3;
124
125         xor0 = (k[0] ^ seed) ^ k[1];
126         xor1 = k[2] ^ k[3];
127         xor2 = k[4] ^ k[5];
128         xor3 = k[6] ^ k[7];
129
130         xor0 ^= xor1;
131         xor2 ^= xor3;
132
133         xor0 ^= xor2;
134
135         return (xor0 >> 32) ^ xor0;
136 }
137
138 #if defined(RTE_ARCH_X86_64) && defined(RTE_MACHINE_CPUFLAG_SSE4_2)
139
140 #include <x86intrin.h>
141
142 static inline uint64_t
143 hash_crc_key8(void *key, __rte_unused uint32_t key_size, uint64_t seed)
144 {
145         uint64_t *k = key;
146         uint64_t crc0;
147
148         crc0 = _mm_crc32_u64(seed, k[0]);
149
150         return crc0;
151 }
152
153 static inline uint64_t
154 hash_crc_key16(void *key, __rte_unused uint32_t key_size, uint64_t seed)
155 {
156         uint64_t *k = key;
157         uint64_t k0, crc0, crc1;
158
159         k0 = k[0];
160
161         crc0 = _mm_crc32_u64(k0, seed);
162         crc1 = _mm_crc32_u64(k0 >> 32, k[1]);
163
164         crc0 ^= crc1;
165
166         return crc0;
167 }
168
169 static inline uint64_t
170 hash_crc_key24(void *key, __rte_unused uint32_t key_size, uint64_t seed)
171 {
172         uint64_t *k = key;
173         uint64_t k0, k2, crc0, crc1;
174
175         k0 = k[0];
176         k2 = k[2];
177
178         crc0 = _mm_crc32_u64(k0, seed);
179         crc1 = _mm_crc32_u64(k0 >> 32, k[1]);
180
181         crc0 = _mm_crc32_u64(crc0, k2);
182
183         crc0 ^= crc1;
184
185         return crc0;
186 }
187
188 static inline uint64_t
189 hash_crc_key32(void *key, __rte_unused uint32_t key_size, uint64_t seed)
190 {
191         uint64_t *k = key;
192         uint64_t k0, k2, crc0, crc1, crc2, crc3;
193
194         k0 = k[0];
195         k2 = k[2];
196
197         crc0 = _mm_crc32_u64(k0, seed);
198         crc1 = _mm_crc32_u64(k0 >> 32, k[1]);
199
200         crc2 = _mm_crc32_u64(k2, k[3]);
201         crc3 = k2 >> 32;
202
203         crc0 = _mm_crc32_u64(crc0, crc1);
204         crc1 = _mm_crc32_u64(crc2, crc3);
205
206         crc0 ^= crc1;
207
208         return crc0;
209 }
210
211 static inline uint64_t
212 hash_crc_key40(void *key, __rte_unused uint32_t key_size, uint64_t seed)
213 {
214         uint64_t *k = key;
215         uint64_t k0, k2, crc0, crc1, crc2, crc3;
216
217         k0 = k[0];
218         k2 = k[2];
219
220         crc0 = _mm_crc32_u64(k0, seed);
221         crc1 = _mm_crc32_u64(k0 >> 32, k[1]);
222
223         crc2 = _mm_crc32_u64(k2, k[3]);
224         crc3 = _mm_crc32_u64(k2 >> 32, k[4]);
225
226         crc0 = _mm_crc32_u64(crc0, crc1);
227         crc1 = _mm_crc32_u64(crc2, crc3);
228
229         crc0 ^= crc1;
230
231         return crc0;
232 }
233
234 static inline uint64_t
235 hash_crc_key48(void *key, __rte_unused uint32_t key_size, uint64_t seed)
236 {
237         uint64_t *k = key;
238         uint64_t k0, k2, k5, crc0, crc1, crc2, crc3;
239
240         k0 = k[0];
241         k2 = k[2];
242         k5 = k[5];
243
244         crc0 = _mm_crc32_u64(k0, seed);
245         crc1 = _mm_crc32_u64(k0 >> 32, k[1]);
246
247         crc2 = _mm_crc32_u64(k2, k[3]);
248         crc3 = _mm_crc32_u64(k2 >> 32, k[4]);
249
250         crc0 = _mm_crc32_u64(crc0, (crc1 << 32) ^ crc2);
251         crc1 = _mm_crc32_u64(crc3, k5);
252
253         crc0 ^= crc1;
254
255         return crc0;
256 }
257
258 static inline uint64_t
259 hash_crc_key56(void *key, __rte_unused uint32_t key_size, uint64_t seed)
260 {
261         uint64_t *k = key;
262         uint64_t k0, k2, k5, crc0, crc1, crc2, crc3, crc4, crc5;
263
264         k0 = k[0];
265         k2 = k[2];
266         k5 = k[5];
267
268         crc0 = _mm_crc32_u64(k0, seed);
269         crc1 = _mm_crc32_u64(k0 >> 32, k[1]);
270
271         crc2 = _mm_crc32_u64(k2, k[3]);
272         crc3 = _mm_crc32_u64(k2 >> 32, k[4]);
273
274         crc4 = _mm_crc32_u64(k5, k[6]);
275         crc5 = k5 >> 32;
276
277         crc0 = _mm_crc32_u64(crc0, (crc1 << 32) ^ crc2);
278         crc1 = _mm_crc32_u64(crc3, (crc4 << 32) ^ crc5);
279
280         crc0 ^= crc1;
281
282         return crc0;
283 }
284
285 static inline uint64_t
286 hash_crc_key64(void *key, __rte_unused uint32_t key_size, uint64_t seed)
287 {
288         uint64_t *k = key;
289         uint64_t k0, k2, k5, crc0, crc1, crc2, crc3, crc4, crc5;
290
291         k0 = k[0];
292         k2 = k[2];
293         k5 = k[5];
294
295         crc0 = _mm_crc32_u64(k0, seed);
296         crc1 = _mm_crc32_u64(k0 >> 32, k[1]);
297
298         crc2 = _mm_crc32_u64(k2, k[3]);
299         crc3 = _mm_crc32_u64(k2 >> 32, k[4]);
300
301         crc4 = _mm_crc32_u64(k5, k[6]);
302         crc5 = _mm_crc32_u64(k5 >> 32, k[7]);
303
304         crc0 = _mm_crc32_u64(crc0, (crc1 << 32) ^ crc2);
305         crc1 = _mm_crc32_u64(crc3, (crc4 << 32) ^ crc5);
306
307         crc0 ^= crc1;
308
309         return crc0;
310 }
311
312 #define hash_default_key8                       hash_crc_key8
313 #define hash_default_key16                      hash_crc_key16
314 #define hash_default_key24                      hash_crc_key24
315 #define hash_default_key32                      hash_crc_key32
316 #define hash_default_key40                      hash_crc_key40
317 #define hash_default_key48                      hash_crc_key48
318 #define hash_default_key56                      hash_crc_key56
319 #define hash_default_key64                      hash_crc_key64
320
321 #else
322
323 #define hash_default_key8                       hash_xor_key8
324 #define hash_default_key16                      hash_xor_key16
325 #define hash_default_key24                      hash_xor_key24
326 #define hash_default_key32                      hash_xor_key32
327 #define hash_default_key40                      hash_xor_key40
328 #define hash_default_key48                      hash_xor_key48
329 #define hash_default_key56                      hash_xor_key56
330 #define hash_default_key64                      hash_xor_key64
331
332 #endif
333
334 #endif