1 | //===-- checksum.h ----------------------------------------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #ifndef SCUDO_CHECKSUM_H_ |
10 | #define SCUDO_CHECKSUM_H_ |
11 | |
12 | #include "internal_defs.h" |
13 | |
14 | // Hardware CRC32 is supported at compilation via the following: |
15 | // - for i386 & x86_64: -mcrc32 (earlier: -msse4.2) |
16 | // - for ARM & AArch64: -march=armv8-a+crc or -mcrc |
17 | // An additional check must be performed at runtime as well to make sure the |
18 | // emitted instructions are valid on the target host. |
19 | |
20 | #if defined(__CRC32__) |
21 | // NB: clang has <crc32intrin.h> but GCC does not |
22 | #include <smmintrin.h> |
23 | #define CRC32_INTRINSIC \ |
24 | FIRST_32_SECOND_64(__builtin_ia32_crc32si, __builtin_ia32_crc32di) |
25 | #elif defined(__SSE4_2__) |
26 | #include <smmintrin.h> |
27 | #define CRC32_INTRINSIC FIRST_32_SECOND_64(_mm_crc32_u32, _mm_crc32_u64) |
28 | #endif |
29 | #ifdef __ARM_FEATURE_CRC32 |
30 | #include <arm_acle.h> |
31 | #define CRC32_INTRINSIC FIRST_32_SECOND_64(__crc32cw, __crc32cd) |
32 | #endif |
33 | #ifdef __loongarch__ |
34 | #include <larchintrin.h> |
35 | #define CRC32_INTRINSIC FIRST_32_SECOND_64(__crcc_w_w_w, __crcc_w_d_w) |
36 | #endif |
37 | |
38 | namespace scudo { |
39 | |
40 | enum class Checksum : u8 { |
41 | BSD = 0, |
42 | HardwareCRC32 = 1, |
43 | }; |
44 | |
45 | // BSD checksum, unlike a software CRC32, doesn't use any array lookup. We save |
46 | // significantly on memory accesses, as well as 1K of CRC32 table, on platforms |
47 | // that do no support hardware CRC32. The checksum itself is 16-bit, which is at |
48 | // odds with CRC32, but enough for our needs. |
49 | inline u16 computeBSDChecksum(u16 Sum, uptr Data) { |
50 | for (u8 I = 0; I < sizeof(Data); I++) { |
51 | Sum = static_cast<u16>((Sum >> 1) | ((Sum & 1) << 15)); |
52 | Sum = static_cast<u16>(Sum + (Data & 0xff)); |
53 | Data >>= 8; |
54 | } |
55 | return Sum; |
56 | } |
57 | |
58 | bool hasHardwareCRC32(); |
59 | WEAK u32 computeHardwareCRC32(u32 Crc, uptr Data); |
60 | |
61 | } // namespace scudo |
62 | |
63 | #endif // SCUDO_CHECKSUM_H_ |
64 | |