Path: blob/main/contrib/llvm-project/compiler-rt/lib/scudo/standalone/checksum.h
35291 views
//===-- checksum.h ----------------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef SCUDO_CHECKSUM_H_9#define SCUDO_CHECKSUM_H_1011#include "internal_defs.h"1213// Hardware CRC32 is supported at compilation via the following:14// - for i386 & x86_64: -mcrc32 (earlier: -msse4.2)15// - for ARM & AArch64: -march=armv8-a+crc or -mcrc16// An additional check must be performed at runtime as well to make sure the17// emitted instructions are valid on the target host.1819#if defined(__CRC32__)20// NB: clang has <crc32intrin.h> but GCC does not21#include <smmintrin.h>22#define CRC32_INTRINSIC \23FIRST_32_SECOND_64(__builtin_ia32_crc32si, __builtin_ia32_crc32di)24#elif defined(__SSE4_2__)25#include <smmintrin.h>26#define CRC32_INTRINSIC FIRST_32_SECOND_64(_mm_crc32_u32, _mm_crc32_u64)27#endif28#ifdef __ARM_FEATURE_CRC3229#include <arm_acle.h>30#define CRC32_INTRINSIC FIRST_32_SECOND_64(__crc32cw, __crc32cd)31#endif32#ifdef __loongarch__33#include <larchintrin.h>34#define CRC32_INTRINSIC FIRST_32_SECOND_64(__crcc_w_w_w, __crcc_w_d_w)35#endif3637namespace scudo {3839enum class Checksum : u8 {40BSD = 0,41HardwareCRC32 = 1,42};4344// BSD checksum, unlike a software CRC32, doesn't use any array lookup. We save45// significantly on memory accesses, as well as 1K of CRC32 table, on platforms46// that do no support hardware CRC32. The checksum itself is 16-bit, which is at47// odds with CRC32, but enough for our needs.48inline u16 computeBSDChecksum(u16 Sum, uptr Data) {49for (u8 I = 0; I < sizeof(Data); I++) {50Sum = static_cast<u16>((Sum >> 1) | ((Sum & 1) << 15));51Sum = static_cast<u16>(Sum + (Data & 0xff));52Data >>= 8;53}54return Sum;55}5657bool hasHardwareCRC32();58WEAK u32 computeHardwareCRC32(u32 Crc, uptr Data);5960} // namespace scudo6162#endif // SCUDO_CHECKSUM_H_636465