ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
crc32.c File Reference

⚡ Hardware-accelerated CRC32 checksum with ARM64 and x86_64 CPU feature detection More...

Go to the source code of this file.

Functions

uint32_t asciichat_crc32_hw (const void *data, size_t len)
 
bool crc32_hw_is_available (void)
 
uint32_t asciichat_crc32_sw (const void *data, size_t len)
 

Detailed Description

⚡ Hardware-accelerated CRC32 checksum with ARM64 and x86_64 CPU feature detection

Definition in file crc32.c.

Function Documentation

◆ asciichat_crc32_hw()

uint32_t asciichat_crc32_hw ( const void *  data,
size_t  len 
)

Definition at line 132 of file crc32.c.

132 {
133 check_crc32_hw_support();
134
135 if (!crc32_hw_available) {
136 // DEBUG: Log fallback to software
137 static bool logged_fallback = false;
138 if (!logged_fallback) {
139 log_debug("Using software CRC32 (no hardware acceleration)");
140 logged_fallback = true;
141 }
142 return asciichat_crc32_sw(data, len);
143 }
144
145#ifdef ARCH_ARM64
146 static bool logged_arm = false;
147 if (!logged_arm) {
148 log_debug("Using ARM64 hardware CRC32");
149 logged_arm = true;
150 }
151 return crc32_arm_hw(data, len);
152#elif defined(ARCH_X86_64)
153 static bool logged_intel = false;
154 if (!logged_intel) {
155 log_debug("Using Intel x86_64 hardware CRC32 (SSE4.2)");
156 logged_intel = true;
157 }
158 return crc32_intel_hw(data, len);
159#else
160 return asciichat_crc32_sw(data, len);
161#endif
162}
uint32_t asciichat_crc32_sw(const void *data, size_t len)
Definition crc32.c:171

References asciichat_crc32_sw().

◆ asciichat_crc32_sw()

uint32_t asciichat_crc32_sw ( const void *  data,
size_t  len 
)

Definition at line 171 of file crc32.c.

171 {
172 const uint8_t *bytes = (const uint8_t *)data;
173 uint32_t crc = 0xFFFFFFFF;
174
175 // CRC32-C (Castagnoli) polynomial: 0x1EDC6F41
176 // Reversed (for LSB-first): 0x82F63B78
177 for (size_t i = 0; i < len; i++) {
178 crc ^= bytes[i];
179 for (int j = 0; j < 8; j++) {
180 if (crc & 1) {
181 crc = (crc >> 1) ^ 0x82F63B78; // CRC32-C polynomial (reversed)
182 } else {
183 crc >>= 1;
184 }
185 }
186 }
187
188 return ~crc;
189}

Referenced by asciichat_crc32_hw(), and client_serialize_packet().

◆ crc32_hw_is_available()

bool crc32_hw_is_available ( void  )

Definition at line 164 of file crc32.c.

164 {
165 check_crc32_hw_support();
166 return crc32_hw_available;
167}