Skip to content

Instantly share code, notes, and snippets.

@doertedev
Created July 28, 2025 19:47
Show Gist options
  • Select an option

  • Save doertedev/1ea3e017c4e25b3daf6237adee039d37 to your computer and use it in GitHub Desktop.

Select an option

Save doertedev/1ea3e017c4e25b3daf6237adee039d37 to your computer and use it in GitHub Desktop.
Grok cooked sse2 palindrome check - performance optimized
#include <emmintrin.h> // SSE2 intrinsics
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
// Check if a string is a palindrome using SIMD (SSE2) for high performance
bool is_palindrome(const char *str, size_t len) {
if (!str || len <= 1) return true;
// Align pointers to 16-byte boundary for SIMD
const char *start = str;
const char *end = str + len - 1;
// Process scalar characters until 16-byte alignment or end
while (start < end && ((uintptr_t)start & 15)) {
if (*start != *end) return false;
start++;
end--;
}
// SIMD processing for 16-byte chunks
while (start <= end - 16) {
// Load 16 bytes from start and end
__m128i s = _mm_load_si128((__m128i *)start);
__m128i e = _mm_load_si128((__m128i *)end - 15);
// Reverse end vector using shuffle (byte-by-byte reversal)
__m128i shuffle = _mm_setr_epi8(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
e = _mm_shuffle_epi8(e, shuffle);
// Compare vectors
__m128i cmp = _mm_cmpeq_epi8(s, e);
// Check if all bytes match (mask is all ones if equal)
if (_mm_movemask_epi8(cmp) != 0xFFFF) return false;
start += 16;
end -= 16;
}
// Handle remaining scalar characters
while (start < end) {
if (*start != *end) return false;
start++;
end--;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment