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