Skip to content

Commit 597f976

Browse files
authored
[scudo] Add support for LoongArch hardware CRC32 checksumming (#83113)
One has to probe for platform capability prior to use with HWCAP, according to LoongArch documentation.
1 parent 28b354a commit 597f976

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

compiler-rt/lib/scudo/standalone/checksum.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#else
2020
#include <sys/auxv.h>
2121
#endif
22+
#elif defined(__loongarch__)
23+
#include <sys/auxv.h>
2224
#endif
2325

2426
namespace scudo {
@@ -75,6 +77,20 @@ bool hasHardwareCRC32() {
7577
return !!(getauxval(AT_HWCAP) & HWCAP_CRC32);
7678
#endif // SCUDO_FUCHSIA
7779
}
80+
#elif defined(__loongarch__)
81+
// The definition is only pulled in by <sys/auxv.h> since glibc 2.38, so
82+
// supply it if missing.
83+
#ifndef HWCAP_LOONGARCH_CRC32
84+
#define HWCAP_LOONGARCH_CRC32 (1 << 6)
85+
#endif
86+
// Query HWCAP for platform capability, according to *Software Development and
87+
// Build Convention for LoongArch Architectures* v0.1, Section 9.1.
88+
//
89+
// Link:
90+
// https://github.com/loongson/la-softdev-convention/blob/v0.1/la-softdev-convention.adoc#kernel-development
91+
bool hasHardwareCRC32() {
92+
return !!(getauxval(AT_HWCAP) & HWCAP_LOONGARCH_CRC32);
93+
}
7894
#else
7995
// No hardware CRC32 implemented in Scudo for other architectures.
8096
bool hasHardwareCRC32() { return false; }

compiler-rt/lib/scudo/standalone/checksum.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
#include <arm_acle.h>
3131
#define CRC32_INTRINSIC FIRST_32_SECOND_64(__crc32cw, __crc32cd)
3232
#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
3337

3438
namespace scudo {
3539

compiler-rt/lib/scudo/standalone/crc32_hw.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,13 @@ u32 computeHardwareCRC32(u32 Crc, uptr Data) {
1717
#endif // defined(__CRC32__) || defined(__SSE4_2__) ||
1818
// defined(__ARM_FEATURE_CRC32)
1919

20+
#if defined(__loongarch__)
21+
u32 computeHardwareCRC32(u32 Crc, uptr Data) {
22+
// The LoongArch CRC intrinsics have the two input arguments swapped, and
23+
// expect them to be signed.
24+
return static_cast<u32>(
25+
CRC32_INTRINSIC(static_cast<long>(Data), static_cast<int>(Crc)));
26+
}
27+
#endif // defined(__loongarch__)
28+
2029
} // namespace scudo

0 commit comments

Comments
 (0)