/src/abseil-cpp/absl/crc/internal/crc32c_inline.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2022 The Abseil Authors. |
2 | | // |
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 | | // https://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 | | #ifndef ABSL_CRC_INTERNAL_CRC32C_INLINE_H_ |
16 | | #define ABSL_CRC_INTERNAL_CRC32C_INLINE_H_ |
17 | | |
18 | | #include <cstdint> |
19 | | |
20 | | #include "absl/base/config.h" |
21 | | #include "absl/base/internal/endian.h" |
22 | | #include "absl/crc/internal/crc32_x86_arm_combined_simd.h" |
23 | | |
24 | | namespace absl { |
25 | | ABSL_NAMESPACE_BEGIN |
26 | | namespace crc_internal { |
27 | | |
28 | | // CRC32C implementation optimized for small inputs. |
29 | | // Either computes crc and return true, or if there is |
30 | | // no hardware support does nothing and returns false. |
31 | 0 | inline bool ExtendCrc32cInline(uint32_t* crc, const char* p, size_t n) { |
32 | 0 | #if defined(ABSL_CRC_INTERNAL_HAVE_ARM_SIMD) || \ |
33 | 0 | defined(ABSL_CRC_INTERNAL_HAVE_X86_SIMD) |
34 | 0 | constexpr uint32_t kCrc32Xor = 0xffffffffU; |
35 | 0 | *crc ^= kCrc32Xor; |
36 | 0 | if (n & 1) { |
37 | 0 | *crc = CRC32_u8(*crc, static_cast<uint8_t>(*p)); |
38 | 0 | n--; |
39 | 0 | p++; |
40 | 0 | } |
41 | 0 | if (n & 2) { |
42 | 0 | *crc = CRC32_u16(*crc, absl::little_endian::Load16(p)); |
43 | 0 | n -= 2; |
44 | 0 | p += 2; |
45 | 0 | } |
46 | 0 | if (n & 4) { |
47 | 0 | *crc = CRC32_u32(*crc, absl::little_endian::Load32(p)); |
48 | 0 | n -= 4; |
49 | 0 | p += 4; |
50 | 0 | } |
51 | 0 | while (n) { |
52 | 0 | *crc = CRC32_u64(*crc, absl::little_endian::Load64(p)); |
53 | 0 | n -= 8; |
54 | 0 | p += 8; |
55 | 0 | } |
56 | 0 | *crc ^= kCrc32Xor; |
57 | 0 | return true; |
58 | 0 | #else |
59 | 0 | // No hardware support, signal the need to fallback. |
60 | 0 | static_cast<void>(crc); |
61 | 0 | static_cast<void>(p); |
62 | 0 | static_cast<void>(n); |
63 | 0 | return false; |
64 | 0 | #endif // defined(ABSL_CRC_INTERNAL_HAVE_ARM_SIMD) || |
65 | 0 | // defined(ABSL_CRC_INTERNAL_HAVE_X86_SIMD) |
66 | 0 | } |
67 | | |
68 | | } // namespace crc_internal |
69 | | ABSL_NAMESPACE_END |
70 | | } // namespace absl |
71 | | |
72 | | #endif // ABSL_CRC_INTERNAL_CRC32C_INLINE_H_ |