blob: f88193777fad84b8e63e31ce1c33fdce649e2ee1 [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{
Andrew Yourtchenko0d07a5d2023-03-14 14:38:01 +000055 return __crc32cb (last, data);
Damjan Marion271daab2021-11-12 16:00:24 +010056}
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
Nathan Skrzypczakf284c142022-02-02 19:31:58 +010079clib_crc32c_with_init (u8 *s, int len, u32 last)
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020080{
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020081 for (; len >= 8; len -= 8, s += 8)
Nathan Skrzypczakf284c142022-02-02 19:31:58 +010082 last = clib_crc32c_u64 (last, *((u64u *) s));
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020083
84 for (; len >= 4; len -= 4, s += 4)
Nathan Skrzypczakf284c142022-02-02 19:31:58 +010085 last = clib_crc32c_u32 (last, *((u32u *) s));
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020086
87 for (; len >= 2; len -= 2, s += 2)
Nathan Skrzypczakf284c142022-02-02 19:31:58 +010088 last = clib_crc32c_u16 (last, *((u16u *) s));
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020089
90 for (; len >= 1; len -= 1, s += 1)
Nathan Skrzypczakf284c142022-02-02 19:31:58 +010091 last = clib_crc32c_u8 (last, *((u8 *) s));
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020092
Nathan Skrzypczakf284c142022-02-02 19:31:58 +010093 return last;
94}
95
96static_always_inline u32
97clib_crc32c (u8 *s, int len)
98{
99 return clib_crc32c_with_init (s, len, 0);
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +0200100}
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +0200101#endif
Damjan Marion271daab2021-11-12 16:00:24 +0100102
Damjan Marion0f68c792017-04-26 13:05:05 +0200103#endif /* __included_crc32_h__ */
104
105/*
106 * fd.io coding-style-patch-verification: ON
107 *
108 * Local Variables:
109 * eval: (c-set-style "gnu")
110 * End:
111 */