17#if defined(__aarch64__)
20#elif defined(__x86_64__) && defined(HAVE_CRC32_HW)
31static bool crc32_hw_available =
false;
32static atomic_bool crc32_hw_checked =
false;
34static void check_crc32_hw_support(
void) {
36 if (atomic_load(&crc32_hw_checked)) {
41 bool expected =
false;
42 if (!atomic_compare_exchange_strong(&crc32_hw_checked, &expected,
true)) {
46 while (!atomic_load(&crc32_hw_checked)) {
48 if (spin_count > 100) {
68 crc32_hw_available =
true;
72 crc32_hw_available =
true;
75#elif defined(ARCH_X86_64)
81 crc32_hw_available = (cpu_info[2] & (1 << 20)) != 0;
83 unsigned int eax, ebx, ecx, edx;
84 if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
85 crc32_hw_available = (ecx & bit_SSE4_2) != 0;
87 crc32_hw_available =
false;
92 crc32_hw_available =
false;
108__attribute__((target(
"arch=armv8-a+crc"))) static
uint32_t crc32_arm_hw(const
void *data,
size_t len) {
114 for (
size_t i = 0; i < len; i++) {
115 crc = __crc32cb(crc, bytes[i]);
125static uint32_t crc32_intel_hw(
const void *data,
size_t len) {
130 for (
size_t i = 0; i < len; i++) {
131 crc = _mm_crc32_u8(crc, bytes[i]);
140 check_crc32_hw_support();
142 if (!crc32_hw_available) {
144 static bool logged_fallback =
false;
145 if (!logged_fallback) {
146 fprintf(stderr,
"[CRC32 DEBUG] Using software CRC32 (no hardware acceleration)\n");
147 logged_fallback =
true;
153 static bool logged_arm =
false;
155 fprintf(stderr,
"[CRC32 DEBUG] Using ARM64 hardware CRC32\n");
158 return crc32_arm_hw(data, len);
159#elif defined(ARCH_X86_64)
160 static bool logged_intel =
false;
162 fprintf(stderr,
"[CRC32 DEBUG] Using Intel x86_64 hardware CRC32 (SSE4.2)\n");
165 return crc32_intel_hw(data, len);
172 check_crc32_hw_support();
173 return crc32_hw_available;
184 for (
size_t i = 0; i < len; i++) {
186 for (
int j = 0; j < 8; j++) {
188 crc = (crc >> 1) ^ 0x82F63B78;
Hardware-Accelerated CRC32 Checksum Computation.
bool crc32_hw_is_available(void)
Check if hardware CRC32 acceleration is available at runtime.
uint32_t asciichat_crc32_sw(const void *data, size_t len)
Compute CRC32 checksum using software implementation only.
uint32_t asciichat_crc32_hw(const void *data, size_t len)
Compute CRC32 checksum with hardware acceleration (if available)