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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __included_crc32_h__
17 #define __included_crc32_h__
19 #include <vppinfra/clib.h>
22 #define clib_crc32c_uses_intrinsics
23 #include <x86intrin.h>
25 #define crc32_u64 _mm_crc32_u64
27 static_always_inline u32
28 clib_crc32c (u8 * s, int len)
33 for (; len >= 8; len -= 8, s += 8)
34 v = _mm_crc32_u64 (v, *((u64 *) s));
36 /* workaround weird GCC bug when using _mm_crc32_u32
37 which happens with -O2 optimization */
38 volatile ("":::"memory");
41 for (; len >= 4; len -= 4, s += 4)
42 v = _mm_crc32_u32 (v, *((u32 *) s));
44 for (; len >= 2; len -= 2, s += 2)
45 v = _mm_crc32_u16 (v, *((u16 *) s));
47 for (; len >= 1; len -= 1, s += 1)
48 v = _mm_crc32_u8 (v, *((u16 *) s));
53 #elif __ARM_FEATURE_CRC32
54 #define clib_crc32c_uses_intrinsics
58 #define crc32_u64 __crc32cd
60 static_always_inline u32
61 clib_crc32c (u8 * s, int len)
65 for (; len >= 8; len -= 8, s += 8)
66 v = __crc32cd (v, *((u64 *) s));
68 for (; len >= 4; len -= 4, s += 4)
69 v = __crc32cw (v, *((u32 *) s));
71 for (; len >= 2; len -= 2, s += 2)
72 v = __crc32ch (v, *((u16 *) s));
74 for (; len >= 1; len -= 1, s += 1)
75 v = __crc32cb (v, *((u8 *) s));
81 #endif /* __included_crc32_h__ */
84 * fd.io coding-style-patch-verification: ON
87 * eval: (c-set-style "gnu")