Add crc32c inline function, allows compilation on 32-bit systems
[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 #if __SSE4_2__
20 #include <x86intrin.h>
21
22 static_always_inline u32
23 clib_crc32c (u8 * s, int len)
24 {
25   u32 v = 0;
26
27 #if __x86_64__
28   for (; len >= 8; len -= 8, s += 8)
29     v = _mm_crc32_u64 (v, *((u64 *) s));
30 #else
31   /* workaround weird GCC bug when using _mm_crc32_u32
32      which happens with -O2 optimization */
33   volatile ("":::"memory");
34 #endif
35
36   for (; len >= 4; len -= 4, s += 4)
37     v = _mm_crc32_u32 (v, *((u32 *) s));
38
39   for (; len >= 2; len -= 2, s += 2)
40     v = _mm_crc32_u16 (v, *((u16 *) s));
41
42   for (; len >= 1; len -= 1, s += 1)
43     v = _mm_crc32_u8 (v, *((u16 *) s));
44
45   return v;
46 }
47
48 #endif
49
50 #endif /* __included_crc32_h__ */
51
52 /*
53  * fd.io coding-style-patch-verification: ON
54  *
55  * Local Variables:
56  * eval: (c-set-style "gnu")
57  * End:
58  */