New upstream version 17.11-rc3
[deb_dpdk.git] / examples / ip_pipeline / pipeline / hash_func.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #ifndef __INCLUDE_HASH_FUNC_H__
34 #define __INCLUDE_HASH_FUNC_H__
35
36 static inline uint64_t
37 hash_xor_key8(void *key, void *mask, __rte_unused uint32_t key_size,
38         uint64_t seed)
39 {
40         uint64_t *k = key;
41         uint64_t *m = mask;
42         uint64_t xor0;
43
44         xor0 = seed ^ (k[0] & m[0]);
45
46         return (xor0 >> 32) ^ xor0;
47 }
48
49 static inline uint64_t
50 hash_xor_key16(void *key, void *mask, __rte_unused uint32_t key_size,
51         uint64_t seed)
52 {
53         uint64_t *k = key;
54         uint64_t *m = mask;
55         uint64_t xor0;
56
57         xor0 = ((k[0] & m[0]) ^ seed) ^ (k[1] & m[1]);
58
59         return (xor0 >> 32) ^ xor0;
60 }
61
62 static inline uint64_t
63 hash_xor_key24(void *key, void *mask, __rte_unused uint32_t key_size,
64         uint64_t seed)
65 {
66         uint64_t *k = key;
67         uint64_t *m = mask;
68         uint64_t xor0;
69
70         xor0 = ((k[0] & m[0]) ^ seed) ^ (k[1] & m[1]);
71
72         xor0 ^= k[2] & m[2];
73
74         return (xor0 >> 32) ^ xor0;
75 }
76
77 static inline uint64_t
78 hash_xor_key32(void *key, void *mask, __rte_unused uint32_t key_size,
79         uint64_t seed)
80 {
81         uint64_t *k = key;
82         uint64_t *m = mask;
83         uint64_t xor0, xor1;
84
85         xor0 = ((k[0] & m[0]) ^ seed) ^ (k[1] & m[1]);
86         xor1 = (k[2] & m[2]) ^ (k[3] & m[3]);
87
88         xor0 ^= xor1;
89
90         return (xor0 >> 32) ^ xor0;
91 }
92
93 static inline uint64_t
94 hash_xor_key40(void *key, void *mask, __rte_unused uint32_t key_size,
95         uint64_t seed)
96 {
97         uint64_t *k = key;
98         uint64_t *m = mask;
99         uint64_t xor0, xor1;
100
101         xor0 = ((k[0] & m[0]) ^ seed) ^ (k[1] & m[1]);
102         xor1 = (k[2] & m[2]) ^ (k[3] & m[3]);
103
104         xor0 ^= xor1;
105
106         xor0 ^= k[4] & m[4];
107
108         return (xor0 >> 32) ^ xor0;
109 }
110
111 static inline uint64_t
112 hash_xor_key48(void *key, void *mask, __rte_unused uint32_t key_size,
113         uint64_t seed)
114 {
115         uint64_t *k = key;
116         uint64_t *m = mask;
117         uint64_t xor0, xor1, xor2;
118
119         xor0 = ((k[0] & m[0]) ^ seed) ^ (k[1] & m[1]);
120         xor1 = (k[2] & m[2]) ^ (k[3] & m[3]);
121         xor2 = (k[4] & m[4]) ^ (k[5] & m[5]);
122
123         xor0 ^= xor1;
124
125         xor0 ^= xor2;
126
127         return (xor0 >> 32) ^ xor0;
128 }
129
130 static inline uint64_t
131 hash_xor_key56(void *key, void *mask, __rte_unused uint32_t key_size,
132         uint64_t seed)
133 {
134         uint64_t *k = key;
135         uint64_t *m = mask;
136         uint64_t xor0, xor1, xor2;
137
138         xor0 = ((k[0] & m[0]) ^ seed) ^ (k[1] & m[1]);
139         xor1 = (k[2] & m[2]) ^ (k[3] & m[3]);
140         xor2 = (k[4] & m[4]) ^ (k[5] & m[5]);
141
142         xor0 ^= xor1;
143         xor2 ^= k[6] & m[6];
144
145         xor0 ^= xor2;
146
147         return (xor0 >> 32) ^ xor0;
148 }
149
150 static inline uint64_t
151 hash_xor_key64(void *key, void *mask, __rte_unused uint32_t key_size,
152         uint64_t seed)
153 {
154         uint64_t *k = key;
155         uint64_t *m = mask;
156         uint64_t xor0, xor1, xor2, xor3;
157
158         xor0 = ((k[0] & m[0]) ^ seed) ^ (k[1] & m[1]);
159         xor1 = (k[2] & m[2]) ^ (k[3] & m[3]);
160         xor2 = (k[4] & m[4]) ^ (k[5] & m[5]);
161         xor3 = (k[6] & m[6]) ^ (k[7] & m[7]);
162
163         xor0 ^= xor1;
164         xor2 ^= xor3;
165
166         xor0 ^= xor2;
167
168         return (xor0 >> 32) ^ xor0;
169 }
170
171 #if defined(RTE_ARCH_X86_64)
172
173 #include <x86intrin.h>
174
175 static inline uint64_t
176 hash_crc_key8(void *key, void *mask, __rte_unused uint32_t key_size,
177         uint64_t seed)
178 {
179         uint64_t *k = key;
180         uint64_t *m = mask;
181         uint64_t crc0;
182
183         crc0 = _mm_crc32_u64(seed, k[0] & m[0]);
184
185         return crc0;
186 }
187
188 static inline uint64_t
189 hash_crc_key16(void *key, void *mask, __rte_unused uint32_t key_size,
190         uint64_t seed)
191 {
192         uint64_t *k = key;
193         uint64_t *m = mask;
194         uint64_t k0, crc0, crc1;
195
196         k0 = k[0] & m[0];
197
198         crc0 = _mm_crc32_u64(k0, seed);
199         crc1 = _mm_crc32_u64(k0 >> 32, k[1] & m[1]);
200
201         crc0 ^= crc1;
202
203         return crc0;
204 }
205
206 static inline uint64_t
207 hash_crc_key24(void *key, void *mask, __rte_unused uint32_t key_size,
208         uint64_t seed)
209 {
210         uint64_t *k = key;
211         uint64_t *m = mask;
212         uint64_t k0, k2, crc0, crc1;
213
214         k0 = k[0] & m[0];
215         k2 = k[2] & m[2];
216
217         crc0 = _mm_crc32_u64(k0, seed);
218         crc1 = _mm_crc32_u64(k0 >> 32, k[1] & m[1]);
219
220         crc0 = _mm_crc32_u64(crc0, k2);
221
222         crc0 ^= crc1;
223
224         return crc0;
225 }
226
227 static inline uint64_t
228 hash_crc_key32(void *key, void *mask, __rte_unused uint32_t key_size,
229         uint64_t seed)
230 {
231         uint64_t *k = key;
232         uint64_t *m = mask;
233         uint64_t k0, k2, crc0, crc1, crc2, crc3;
234
235         k0 = k[0] & m[0];
236         k2 = k[2] & m[2];
237
238         crc0 = _mm_crc32_u64(k0, seed);
239         crc1 = _mm_crc32_u64(k0 >> 32, k[1] & m[1]);
240
241         crc2 = _mm_crc32_u64(k2, k[3] & m[3]);
242         crc3 = k2 >> 32;
243
244         crc0 = _mm_crc32_u64(crc0, crc1);
245         crc1 = _mm_crc32_u64(crc2, crc3);
246
247         crc0 ^= crc1;
248
249         return crc0;
250 }
251
252 static inline uint64_t
253 hash_crc_key40(void *key, void *mask, __rte_unused uint32_t key_size,
254         uint64_t seed)
255 {
256         uint64_t *k = key;
257         uint64_t *m = mask;
258         uint64_t k0, k2, crc0, crc1, crc2, crc3;
259
260         k0 = k[0] & m[0];
261         k2 = k[2] & m[2];
262
263         crc0 = _mm_crc32_u64(k0, seed);
264         crc1 = _mm_crc32_u64(k0 >> 32, k[1] & m[1]);
265
266         crc2 = _mm_crc32_u64(k2, k[3] & m[3]);
267         crc3 = _mm_crc32_u64(k2 >> 32, k[4] & m[4]);
268
269         crc0 = _mm_crc32_u64(crc0, crc1);
270         crc1 = _mm_crc32_u64(crc2, crc3);
271
272         crc0 ^= crc1;
273
274         return crc0;
275 }
276
277 static inline uint64_t
278 hash_crc_key48(void *key, void *mask, __rte_unused uint32_t key_size,
279         uint64_t seed)
280 {
281         uint64_t *k = key;
282         uint64_t *m = mask;
283         uint64_t k0, k2, k5, crc0, crc1, crc2, crc3;
284
285         k0 = k[0] & m[0];
286         k2 = k[2] & m[2];
287         k5 = k[5] & m[5];
288
289         crc0 = _mm_crc32_u64(k0, seed);
290         crc1 = _mm_crc32_u64(k0 >> 32, k[1] & m[1]);
291
292         crc2 = _mm_crc32_u64(k2, k[3] & m[3]);
293         crc3 = _mm_crc32_u64(k2 >> 32, k[4] & m[4]);
294
295         crc0 = _mm_crc32_u64(crc0, (crc1 << 32) ^ crc2);
296         crc1 = _mm_crc32_u64(crc3, k5);
297
298         crc0 ^= crc1;
299
300         return crc0;
301 }
302
303 static inline uint64_t
304 hash_crc_key56(void *key, void *mask, __rte_unused uint32_t key_size,
305         uint64_t seed)
306 {
307         uint64_t *k = key;
308         uint64_t *m = mask;
309         uint64_t k0, k2, k5, crc0, crc1, crc2, crc3, crc4, crc5;
310
311         k0 = k[0] & m[0];
312         k2 = k[2] & m[2];
313         k5 = k[5] & m[5];
314
315         crc0 = _mm_crc32_u64(k0, seed);
316         crc1 = _mm_crc32_u64(k0 >> 32, k[1] & m[1]);
317
318         crc2 = _mm_crc32_u64(k2, k[3] & m[3]);
319         crc3 = _mm_crc32_u64(k2 >> 32, k[4] & m[4]);
320
321         crc4 = _mm_crc32_u64(k5, k[6] & m[6]);
322         crc5 = k5 >> 32;
323
324         crc0 = _mm_crc32_u64(crc0, (crc1 << 32) ^ crc2);
325         crc1 = _mm_crc32_u64(crc3, (crc4 << 32) ^ crc5);
326
327         crc0 ^= crc1;
328
329         return crc0;
330 }
331
332 static inline uint64_t
333 hash_crc_key64(void *key, void *mask, __rte_unused uint32_t key_size,
334         uint64_t seed)
335 {
336         uint64_t *k = key;
337         uint64_t *m = mask;
338         uint64_t k0, k2, k5, crc0, crc1, crc2, crc3, crc4, crc5;
339
340         k0 = k[0] & m[0];
341         k2 = k[2] & m[2];
342         k5 = k[5] & m[5];
343
344         crc0 = _mm_crc32_u64(k0, seed);
345         crc1 = _mm_crc32_u64(k0 >> 32, k[1] & m[1]);
346
347         crc2 = _mm_crc32_u64(k2, k[3] & m[3]);
348         crc3 = _mm_crc32_u64(k2 >> 32, k[4] & m[4]);
349
350         crc4 = _mm_crc32_u64(k5, k[6] & m[6]);
351         crc5 = _mm_crc32_u64(k5 >> 32, k[7] & m[7]);
352
353         crc0 = _mm_crc32_u64(crc0, (crc1 << 32) ^ crc2);
354         crc1 = _mm_crc32_u64(crc3, (crc4 << 32) ^ crc5);
355
356         crc0 ^= crc1;
357
358         return crc0;
359 }
360
361 #define hash_default_key8                       hash_crc_key8
362 #define hash_default_key16                      hash_crc_key16
363 #define hash_default_key24                      hash_crc_key24
364 #define hash_default_key32                      hash_crc_key32
365 #define hash_default_key40                      hash_crc_key40
366 #define hash_default_key48                      hash_crc_key48
367 #define hash_default_key56                      hash_crc_key56
368 #define hash_default_key64                      hash_crc_key64
369
370 #elif defined(RTE_ARCH_ARM64)
371 #include "hash_func_arm64.h"
372 #else
373
374 #define hash_default_key8                       hash_xor_key8
375 #define hash_default_key16                      hash_xor_key16
376 #define hash_default_key24                      hash_xor_key24
377 #define hash_default_key32                      hash_xor_key32
378 #define hash_default_key40                      hash_xor_key40
379 #define hash_default_key48                      hash_xor_key48
380 #define hash_default_key56                      hash_xor_key56
381 #define hash_default_key64                      hash_xor_key64
382
383 #endif
384
385 #endif