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> |
| 24 | |
Gabriel Ganne | 8e66b9b | 2017-12-14 16:20:37 +0100 | [diff] [blame] | 25 | #define crc32_u64 _mm_crc32_u64 |
Steven | 0d88301 | 2018-05-11 11:06:23 -0700 | [diff] [blame] | 26 | #define crc32_u32 _mm_crc32_u32 |
Gabriel Ganne | 8e66b9b | 2017-12-14 16:20:37 +0100 | [diff] [blame] | 27 | |
Damjan Marion | 0f68c79 | 2017-04-26 13:05:05 +0200 | [diff] [blame] | 28 | static_always_inline u32 |
| 29 | clib_crc32c (u8 * s, int len) |
| 30 | { |
| 31 | u32 v = 0; |
| 32 | |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 33 | #if defined(__x86_64__) |
Damjan Marion | 0f68c79 | 2017-04-26 13:05:05 +0200 | [diff] [blame] | 34 | for (; len >= 8; len -= 8, s += 8) |
| 35 | v = _mm_crc32_u64 (v, *((u64 *) s)); |
| 36 | #else |
| 37 | /* workaround weird GCC bug when using _mm_crc32_u32 |
| 38 | which happens with -O2 optimization */ |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 39 | #if !defined (__i686__) |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 40 | asm volatile ("":::"memory"); |
Damjan Marion | 0f68c79 | 2017-04-26 13:05:05 +0200 | [diff] [blame] | 41 | #endif |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 42 | #endif |
Damjan Marion | 0f68c79 | 2017-04-26 13:05:05 +0200 | [diff] [blame] | 43 | |
| 44 | for (; len >= 4; len -= 4, s += 4) |
| 45 | v = _mm_crc32_u32 (v, *((u32 *) s)); |
| 46 | |
| 47 | for (; len >= 2; len -= 2, s += 2) |
| 48 | v = _mm_crc32_u16 (v, *((u16 *) s)); |
| 49 | |
| 50 | for (; len >= 1; len -= 1, s += 1) |
| 51 | v = _mm_crc32_u8 (v, *((u16 *) s)); |
| 52 | |
| 53 | return v; |
| 54 | } |
| 55 | |
Christophe Fontaine | b4bd28a | 2017-05-31 11:27:19 +0200 | [diff] [blame] | 56 | #elif __ARM_FEATURE_CRC32 |
Gabriel Ganne | 9e12d77 | 2017-11-14 17:33:51 +0100 | [diff] [blame] | 57 | #define clib_crc32c_uses_intrinsics |
Christophe Fontaine | b4bd28a | 2017-05-31 11:27:19 +0200 | [diff] [blame] | 58 | #include <arm_acle.h> |
Damjan Marion | 0f68c79 | 2017-04-26 13:05:05 +0200 | [diff] [blame] | 59 | |
Gabriel Ganne | 8e66b9b | 2017-12-14 16:20:37 +0100 | [diff] [blame] | 60 | |
| 61 | #define crc32_u64 __crc32cd |
Steven | 0d88301 | 2018-05-11 11:06:23 -0700 | [diff] [blame] | 62 | #define crc32_u32 __crc32cw |
Gabriel Ganne | 8e66b9b | 2017-12-14 16:20:37 +0100 | [diff] [blame] | 63 | |
Christophe Fontaine | b4bd28a | 2017-05-31 11:27:19 +0200 | [diff] [blame] | 64 | static_always_inline u32 |
| 65 | clib_crc32c (u8 * s, int len) |
| 66 | { |
| 67 | u32 v = 0; |
| 68 | |
| 69 | for (; len >= 8; len -= 8, s += 8) |
| 70 | v = __crc32cd (v, *((u64 *) s)); |
| 71 | |
| 72 | for (; len >= 4; len -= 4, s += 4) |
| 73 | v = __crc32cw (v, *((u32 *) s)); |
| 74 | |
| 75 | for (; len >= 2; len -= 2, s += 2) |
| 76 | v = __crc32ch (v, *((u16 *) s)); |
| 77 | |
| 78 | for (; len >= 1; len -= 1, s += 1) |
| 79 | v = __crc32cb (v, *((u8 *) s)); |
| 80 | |
| 81 | return v; |
| 82 | } |
| 83 | |
| 84 | #endif |
Damjan Marion | 0f68c79 | 2017-04-26 13:05:05 +0200 | [diff] [blame] | 85 | #endif /* __included_crc32_h__ */ |
| 86 | |
| 87 | /* |
| 88 | * fd.io coding-style-patch-verification: ON |
| 89 | * |
| 90 | * Local Variables: |
| 91 | * eval: (c-set-style "gnu") |
| 92 | * End: |
| 93 | */ |