blob: 3b81daf28cadb9b6eb5eabd5800defebf896bcda [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>
Damjan Marion0f68c792017-04-26 13:05:05 +020024static_always_inline u32
Damjan Marion271daab2021-11-12 16:00:24 +010025clib_crc32c_u8 (u32 last, u8 data)
Damjan Marion0f68c792017-04-26 13:05:05 +020026{
Damjan Marion271daab2021-11-12 16:00:24 +010027 return _mm_crc32_u8 (last, data);
Damjan Marion0f68c792017-04-26 13:05:05 +020028}
29
Damjan Marion271daab2021-11-12 16:00:24 +010030static_always_inline u32
31clib_crc32c_u16 (u32 last, u16 data)
32{
33 return _mm_crc32_u16 (last, data);
34}
35
36static_always_inline u32
37clib_crc32c_u32 (u32 last, u32 data)
38{
39 return _mm_crc32_u32 (last, data);
40}
41
42static_always_inline u32
43clib_crc32c_u64 (u32 last, u64 data)
44{
45 return _mm_crc32_u64 (last, data);
46}
47#endif
48
49#if __ARM_FEATURE_CRC32
Gabriel Ganne9e12d772017-11-14 17:33:51 +010050#define clib_crc32c_uses_intrinsics
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020051#include <arm_acle.h>
Damjan Marion271daab2021-11-12 16:00:24 +010052static_always_inline u32
53clib_crc32c_u8 (u32 last, u8 data)
54{
55 return __crc32cd (last, data);
56}
Damjan Marion0f68c792017-04-26 13:05:05 +020057
Damjan Marion271daab2021-11-12 16:00:24 +010058static_always_inline u32
59clib_crc32c_u16 (u32 last, u16 data)
60{
61 return __crc32ch (last, data);
62}
Gabriel Ganne8e66b9b2017-12-14 16:20:37 +010063
Damjan Marion271daab2021-11-12 16:00:24 +010064static_always_inline u32
65clib_crc32c_u32 (u32 last, u32 data)
66{
67 return __crc32cw (last, data);
68}
Gabriel Ganne8e66b9b2017-12-14 16:20:37 +010069
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020070static_always_inline u32
Damjan Marion271daab2021-11-12 16:00:24 +010071clib_crc32c_u64 (u32 last, u64 data)
72{
73 return __crc32cd (last, data);
74}
75#endif
76
77#ifdef clib_crc32c_uses_intrinsics
78static_always_inline u32
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020079clib_crc32c (u8 * s, int len)
80{
81 u32 v = 0;
82
83 for (; len >= 8; len -= 8, s += 8)
Damjan Marion271daab2021-11-12 16:00:24 +010084 v = clib_crc32c_u64 (v, *((u64u *) s));
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020085
86 for (; len >= 4; len -= 4, s += 4)
Damjan Marion271daab2021-11-12 16:00:24 +010087 v = clib_crc32c_u32 (v, *((u32u *) s));
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020088
89 for (; len >= 2; len -= 2, s += 2)
Damjan Marion271daab2021-11-12 16:00:24 +010090 v = clib_crc32c_u16 (v, *((u16u *) s));
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020091
92 for (; len >= 1; len -= 1, s += 1)
Damjan Marion271daab2021-11-12 16:00:24 +010093 v = clib_crc32c_u8 (v, *((u8 *) s));
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020094
95 return v;
96}
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020097#endif
Damjan Marion271daab2021-11-12 16:00:24 +010098
Damjan Marion0f68c792017-04-26 13:05:05 +020099#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 */