From f7661ff016c6a832c80e026d84c1e4ac248f65aa Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Thu, 2 Apr 2026 20:11:54 +0200
Subject: [PATCH] Avoid unaligned memory access while testing SSE4.2 CRC
intrinsics
---
test/testautomation_intrinsics.c | 23 ++++++++++++++++++++---
1 file changed, 20 insertions(+), 3 deletions(-)
diff --git a/test/testautomation_intrinsics.c b/test/testautomation_intrinsics.c
index 8338d33d35ff5..9b0c28b1f8756 100644
--- a/test/testautomation_intrinsics.c
+++ b/test/testautomation_intrinsics.c
@@ -256,11 +256,26 @@ SDL_TARGETING("sse4.2") static Uint32 calculate_crc32c_sse4_2(const char *text)
Uint32 crc32c = ~0u;
size_t len = SDL_strlen(text);
+ if (len >= 1 && ((uintptr_t)text & 0x1)) {
+ crc32c = (Uint32)_mm_crc32_u8(crc32c, *text);
+ len -= 1;
+ text += 1;
+ }
+ if (len >= 2 && ((uintptr_t)text & 0x2)) {
+ crc32c = (Uint32)_mm_crc32_u16(crc32c, *(Sint16*)text);
+ len -= 2;
+ text += 2;
+ }
#if defined(__x86_64__) || defined(_M_X64)
+ if (len >= 4 && ((uintptr_t)text & 0x4)) {
+ crc32c = (Uint32)_mm_crc32_u32(crc32c, *(Sint32*)text);
+ len -= 4;
+ text += 4;
+ }
for (; len >= 8; len -= 8, text += 8) {
crc32c = (Uint32)_mm_crc32_u64(crc32c, *(Sint64*)text);
}
- if (len >= 4) {
+ if (len & 0x4) {
crc32c = (Uint32)_mm_crc32_u32(crc32c, *(Sint32*)text);
len -= 4;
text += 4;
@@ -270,13 +285,15 @@ SDL_TARGETING("sse4.2") static Uint32 calculate_crc32c_sse4_2(const char *text)
crc32c = (Uint32)_mm_crc32_u32(crc32c, *(Sint32*)text);
}
#endif
- if (len >= 2) {
+ if (len & 0x2) {
crc32c = (Uint32)_mm_crc32_u16(crc32c, *(Sint16*)text);
len -= 2;
text += 2;
}
- if (len) {
+ if (len & 0x1) {
crc32c = (Uint32)_mm_crc32_u8(crc32c, *text);
+ len -= 1;
+ text += 1;
}
return ~crc32c;
}