vppinfra: toeplitz hash
[vpp.git] / src / vppinfra / crc32.h
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef __included_crc32_h__
17 #define __included_crc32_h__
18
19 #include <vppinfra/clib.h>
20
21 #if __SSE4_2__
22 #define clib_crc32c_uses_intrinsics
23 #include <x86intrin.h>
24 static_always_inline u32
25 clib_crc32c_u8 (u32 last, u8 data)
26 {
27   return _mm_crc32_u8 (last, data);
28 }
29
30 static_always_inline u32
31 clib_crc32c_u16 (u32 last, u16 data)
32 {
33   return _mm_crc32_u16 (last, data);
34 }
35
36 static_always_inline u32
37 clib_crc32c_u32 (u32 last, u32 data)
38 {
39   return _mm_crc32_u32 (last, data);
40 }
41
42 static_always_inline u32
43 clib_crc32c_u64 (u32 last, u64 data)
44 {
45   return _mm_crc32_u64 (last, data);
46 }
47 #endif
48
49 #if __ARM_FEATURE_CRC32
50 #define clib_crc32c_uses_intrinsics
51 #include <arm_acle.h>
52 static_always_inline u32
53 clib_crc32c_u8 (u32 last, u8 data)
54 {
55   return __crc32cd (last, data);
56 }
57
58 static_always_inline u32
59 clib_crc32c_u16 (u32 last, u16 data)
60 {
61   return __crc32ch (last, data);
62 }
63
64 static_always_inline u32
65 clib_crc32c_u32 (u32 last, u32 data)
66 {
67   return __crc32cw (last, data);
68 }
69
70 static_always_inline u32
71 clib_crc32c_u64 (u32 last, u64 data)
72 {
73   return __crc32cd (last, data);
74 }
75 #endif
76
77 #ifdef clib_crc32c_uses_intrinsics
78 static_always_inline u32
79 clib_crc32c (u8 * s, int len)
80 {
81   u32 v = 0;
82
83   for (; len >= 8; len -= 8, s += 8)
84     v = clib_crc32c_u64 (v, *((u64u *) s));
85
86   for (; len >= 4; len -= 4, s += 4)
87     v = clib_crc32c_u32 (v, *((u32u *) s));
88
89   for (; len >= 2; len -= 2, s += 2)
90     v = clib_crc32c_u16 (v, *((u16u *) s));
91
92   for (; len >= 1; len -= 1, s += 1)
93     v = clib_crc32c_u8 (v, *((u8 *) s));
94
95   return v;
96 }
97 #endif
98
99 #endif /* __included_crc32_h__ */
100
101 /*
102  * fd.io coding-style-patch-verification: ON
103  *
104  * Local Variables:
105  * eval: (c-set-style "gnu")
106  * End:
107  */