blob: 340b539558e56ed61814cb9104f742488d0acb23 [file] [log] [blame]
Damjan Marion0f68c792017-04-26 13:05:05 +02001/*
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
Neale Rannsb32fde52017-06-12 06:12:26 -070019#include <vppinfra/clib.h>
20
Damjan Marion0f68c792017-04-26 13:05:05 +020021#if __SSE4_2__
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020022#define clib_crc32c_uses_intrinsics
Damjan Marion0f68c792017-04-26 13:05:05 +020023#include <x86intrin.h>
24
Gabriel Ganne8e66b9b2017-12-14 16:20:37 +010025#define crc32_u64 _mm_crc32_u64
26
Damjan Marion0f68c792017-04-26 13:05:05 +020027static_always_inline u32
28clib_crc32c (u8 * s, int len)
29{
30 u32 v = 0;
31
32#if __x86_64__
33 for (; len >= 8; len -= 8, s += 8)
34 v = _mm_crc32_u64 (v, *((u64 *) s));
35#else
36 /* workaround weird GCC bug when using _mm_crc32_u32
37 which happens with -O2 optimization */
38 volatile ("":::"memory");
39#endif
40
41 for (; len >= 4; len -= 4, s += 4)
42 v = _mm_crc32_u32 (v, *((u32 *) s));
43
44 for (; len >= 2; len -= 2, s += 2)
45 v = _mm_crc32_u16 (v, *((u16 *) s));
46
47 for (; len >= 1; len -= 1, s += 1)
48 v = _mm_crc32_u8 (v, *((u16 *) s));
49
50 return v;
51}
52
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020053#elif __ARM_FEATURE_CRC32
Gabriel Ganne9e12d772017-11-14 17:33:51 +010054#define clib_crc32c_uses_intrinsics
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020055#include <arm_acle.h>
Damjan Marion0f68c792017-04-26 13:05:05 +020056
Gabriel Ganne8e66b9b2017-12-14 16:20:37 +010057
58#define crc32_u64 __crc32cd
59
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020060static_always_inline u32
61clib_crc32c (u8 * s, int len)
62{
63 u32 v = 0;
64
65 for (; len >= 8; len -= 8, s += 8)
66 v = __crc32cd (v, *((u64 *) s));
67
68 for (; len >= 4; len -= 4, s += 4)
69 v = __crc32cw (v, *((u32 *) s));
70
71 for (; len >= 2; len -= 2, s += 2)
72 v = __crc32ch (v, *((u16 *) s));
73
74 for (; len >= 1; len -= 1, s += 1)
75 v = __crc32cb (v, *((u8 *) s));
76
77 return v;
78}
79
80#endif
Damjan Marion0f68c792017-04-26 13:05:05 +020081#endif /* __included_crc32_h__ */
82
83/*
84 * fd.io coding-style-patch-verification: ON
85 *
86 * Local Variables:
87 * eval: (c-set-style "gnu")
88 * End:
89 */