crc32c_inline.h (2108B)
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 inline bool ExtendCrc32cInline(uint32_t* crc, const char* p, size_t n) { 32 #if defined(ABSL_CRC_INTERNAL_HAVE_ARM_SIMD) || \ 33 defined(ABSL_CRC_INTERNAL_HAVE_X86_SIMD) 34 constexpr uint32_t kCrc32Xor = 0xffffffffU; 35 *crc ^= kCrc32Xor; 36 if (n & 1) { 37 *crc = CRC32_u8(*crc, static_cast<uint8_t>(*p)); 38 n--; 39 p++; 40 } 41 if (n & 2) { 42 *crc = CRC32_u16(*crc, absl::little_endian::Load16(p)); 43 n -= 2; 44 p += 2; 45 } 46 if (n & 4) { 47 *crc = CRC32_u32(*crc, absl::little_endian::Load32(p)); 48 n -= 4; 49 p += 4; 50 } 51 while (n) { 52 *crc = CRC32_u64(*crc, absl::little_endian::Load64(p)); 53 n -= 8; 54 p += 8; 55 } 56 *crc ^= kCrc32Xor; 57 return true; 58 #else 59 // No hardware support, signal the need to fallback. 60 static_cast<void>(crc); 61 static_cast<void>(p); 62 static_cast<void>(n); 63 return false; 64 #endif // defined(ABSL_CRC_INTERNAL_HAVE_ARM_SIMD) || 65 // defined(ABSL_CRC_INTERNAL_HAVE_X86_SIMD) 66 } 67 68 } // namespace crc_internal 69 ABSL_NAMESPACE_END 70 } // namespace absl 71 72 #endif // ABSL_CRC_INTERNAL_CRC32C_INLINE_H_