Damjan Marion | 0f68c79 | 2017-04-26 13:05:05 +0200 | [diff] [blame] | 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 | |
Neale Ranns | b32fde5 | 2017-06-12 06:12:26 -0700 | [diff] [blame] | 19 | #include <vppinfra/clib.h> |
| 20 | |
Damjan Marion | 0f68c79 | 2017-04-26 13:05:05 +0200 | [diff] [blame] | 21 | #if __SSE4_2__ |
Christophe Fontaine | b4bd28a | 2017-05-31 11:27:19 +0200 | [diff] [blame] | 22 | #define clib_crc32c_uses_intrinsics |
Damjan Marion | 0f68c79 | 2017-04-26 13:05:05 +0200 | [diff] [blame] | 23 | #include <x86intrin.h> |
Damjan Marion | 0f68c79 | 2017-04-26 13:05:05 +0200 | [diff] [blame] | 24 | static_always_inline u32 |
Damjan Marion | 271daab | 2021-11-12 16:00:24 +0100 | [diff] [blame] | 25 | clib_crc32c_u8 (u32 last, u8 data) |
Damjan Marion | 0f68c79 | 2017-04-26 13:05:05 +0200 | [diff] [blame] | 26 | { |
Damjan Marion | 271daab | 2021-11-12 16:00:24 +0100 | [diff] [blame] | 27 | return _mm_crc32_u8 (last, data); |
Damjan Marion | 0f68c79 | 2017-04-26 13:05:05 +0200 | [diff] [blame] | 28 | } |
| 29 | |
Damjan Marion | 271daab | 2021-11-12 16:00:24 +0100 | [diff] [blame] | 30 | static_always_inline u32 |
| 31 | clib_crc32c_u16 (u32 last, u16 data) |
| 32 | { |
| 33 | return _mm_crc32_u16 (last, data); |
| 34 | } |
| 35 | |
| 36 | static_always_inline u32 |
| 37 | clib_crc32c_u32 (u32 last, u32 data) |
| 38 | { |
| 39 | return _mm_crc32_u32 (last, data); |
| 40 | } |
| 41 | |
| 42 | static_always_inline u32 |
| 43 | clib_crc32c_u64 (u32 last, u64 data) |
| 44 | { |
| 45 | return _mm_crc32_u64 (last, data); |
| 46 | } |
| 47 | #endif |
| 48 | |
| 49 | #if __ARM_FEATURE_CRC32 |
Gabriel Ganne | 9e12d77 | 2017-11-14 17:33:51 +0100 | [diff] [blame] | 50 | #define clib_crc32c_uses_intrinsics |
Christophe Fontaine | b4bd28a | 2017-05-31 11:27:19 +0200 | [diff] [blame] | 51 | #include <arm_acle.h> |
Damjan Marion | 271daab | 2021-11-12 16:00:24 +0100 | [diff] [blame] | 52 | static_always_inline u32 |
| 53 | clib_crc32c_u8 (u32 last, u8 data) |
| 54 | { |
Andrew Yourtchenko | 0d07a5d | 2023-03-14 14:38:01 +0000 | [diff] [blame] | 55 | return __crc32cb (last, data); |
Damjan Marion | 271daab | 2021-11-12 16:00:24 +0100 | [diff] [blame] | 56 | } |
Damjan Marion | 0f68c79 | 2017-04-26 13:05:05 +0200 | [diff] [blame] | 57 | |
Damjan Marion | 271daab | 2021-11-12 16:00:24 +0100 | [diff] [blame] | 58 | static_always_inline u32 |
| 59 | clib_crc32c_u16 (u32 last, u16 data) |
| 60 | { |
| 61 | return __crc32ch (last, data); |
| 62 | } |
Gabriel Ganne | 8e66b9b | 2017-12-14 16:20:37 +0100 | [diff] [blame] | 63 | |
Damjan Marion | 271daab | 2021-11-12 16:00:24 +0100 | [diff] [blame] | 64 | static_always_inline u32 |
| 65 | clib_crc32c_u32 (u32 last, u32 data) |
| 66 | { |
| 67 | return __crc32cw (last, data); |
| 68 | } |
Gabriel Ganne | 8e66b9b | 2017-12-14 16:20:37 +0100 | [diff] [blame] | 69 | |
Christophe Fontaine | b4bd28a | 2017-05-31 11:27:19 +0200 | [diff] [blame] | 70 | static_always_inline u32 |
Damjan Marion | 271daab | 2021-11-12 16:00:24 +0100 | [diff] [blame] | 71 | clib_crc32c_u64 (u32 last, u64 data) |
| 72 | { |
| 73 | return __crc32cd (last, data); |
| 74 | } |
| 75 | #endif |
| 76 | |
| 77 | #ifdef clib_crc32c_uses_intrinsics |
| 78 | static_always_inline u32 |
Nathan Skrzypczak | f284c14 | 2022-02-02 19:31:58 +0100 | [diff] [blame] | 79 | clib_crc32c_with_init (u8 *s, int len, u32 last) |
Christophe Fontaine | b4bd28a | 2017-05-31 11:27:19 +0200 | [diff] [blame] | 80 | { |
Christophe Fontaine | b4bd28a | 2017-05-31 11:27:19 +0200 | [diff] [blame] | 81 | for (; len >= 8; len -= 8, s += 8) |
Nathan Skrzypczak | f284c14 | 2022-02-02 19:31:58 +0100 | [diff] [blame] | 82 | last = clib_crc32c_u64 (last, *((u64u *) s)); |
Christophe Fontaine | b4bd28a | 2017-05-31 11:27:19 +0200 | [diff] [blame] | 83 | |
| 84 | for (; len >= 4; len -= 4, s += 4) |
Nathan Skrzypczak | f284c14 | 2022-02-02 19:31:58 +0100 | [diff] [blame] | 85 | last = clib_crc32c_u32 (last, *((u32u *) s)); |
Christophe Fontaine | b4bd28a | 2017-05-31 11:27:19 +0200 | [diff] [blame] | 86 | |
| 87 | for (; len >= 2; len -= 2, s += 2) |
Nathan Skrzypczak | f284c14 | 2022-02-02 19:31:58 +0100 | [diff] [blame] | 88 | last = clib_crc32c_u16 (last, *((u16u *) s)); |
Christophe Fontaine | b4bd28a | 2017-05-31 11:27:19 +0200 | [diff] [blame] | 89 | |
| 90 | for (; len >= 1; len -= 1, s += 1) |
Nathan Skrzypczak | f284c14 | 2022-02-02 19:31:58 +0100 | [diff] [blame] | 91 | last = clib_crc32c_u8 (last, *((u8 *) s)); |
Christophe Fontaine | b4bd28a | 2017-05-31 11:27:19 +0200 | [diff] [blame] | 92 | |
Nathan Skrzypczak | f284c14 | 2022-02-02 19:31:58 +0100 | [diff] [blame] | 93 | return last; |
| 94 | } |
| 95 | |
| 96 | static_always_inline u32 |
| 97 | clib_crc32c (u8 *s, int len) |
| 98 | { |
| 99 | return clib_crc32c_with_init (s, len, 0); |
Christophe Fontaine | b4bd28a | 2017-05-31 11:27:19 +0200 | [diff] [blame] | 100 | } |
Christophe Fontaine | b4bd28a | 2017-05-31 11:27:19 +0200 | [diff] [blame] | 101 | #endif |
Damjan Marion | 271daab | 2021-11-12 16:00:24 +0100 | [diff] [blame] | 102 | |
Damjan Marion | 0f68c79 | 2017-04-26 13:05:05 +0200 | [diff] [blame] | 103 | #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 | */ |