Remove calls to crc_u32 and add clib_crc32c for armv8+crc
[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 #define clib_crc32c_uses_intrinsics
21 #include <x86intrin.h>
22
23 static_always_inline u32
24 clib_crc32c (u8 * s, int len)
25 {
26   u32 v = 0;
27
28 #if __x86_64__
29   for (; len >= 8; len -= 8, s += 8)
30     v = _mm_crc32_u64 (v, *((u64 *) s));
31 #else
32   /* workaround weird GCC bug when using _mm_crc32_u32
33      which happens with -O2 optimization */
34   volatile ("":::"memory");
35 #endif
36
37   for (; len >= 4; len -= 4, s += 4)
38     v = _mm_crc32_u32 (v, *((u32 *) s));
39
40   for (; len >= 2; len -= 2, s += 2)
41     v = _mm_crc32_u16 (v, *((u16 *) s));
42
43   for (; len >= 1; len -= 1, s += 1)
44     v = _mm_crc32_u8 (v, *((u16 *) s));
45
46   return v;
47 }
48
49 #elif __ARM_FEATURE_CRC32
50 #define clib_crc32c_with_intrinsics
51 #include <arm_acle.h>
52
53 static_always_inline u32
54 clib_crc32c (u8 * s, int len)
55 {
56   u32 v = 0;
57
58   for (; len >= 8; len -= 8, s += 8)
59     v = __crc32cd (v, *((u64 *) s));
60
61   for (; len >= 4; len -= 4, s += 4)
62     v = __crc32cw (v, *((u32 *) s));
63
64   for (; len >= 2; len -= 2, s += 2)
65     v = __crc32ch (v, *((u16 *) s));
66
67   for (; len >= 1; len -= 1, s += 1)
68     v = __crc32cb (v, *((u8 *) s));
69
70   return v;
71 }
72
73 #endif
74 #endif /* __included_crc32_h__ */
75
76 /*
77  * fd.io coding-style-patch-verification: ON
78  *
79  * Local Variables:
80  * eval: (c-set-style "gnu")
81  * End:
82  */