Created
April 25, 2023 03:13
-
-
Save rob-p/9c28aa00e7a9aa53992d79a029c0ac73 to your computer and use it in GitHub Desktop.
AVX2 LCP take 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <immintrin.h> | |
const char* longest_common_prefix(const char* str1, const char* str2, int len) { | |
int i = 0; | |
for (; i <= len - 32; i += 32) { | |
__m256i v1 = _mm256_loadu_si256((__m256i*)(str1 + i)); | |
__m256i v2 = _mm256_loadu_si256((__m256i*)(str2 + i)); | |
__m256i cmp = _mm256_cmpeq_epi8(v1, v2); | |
int mask = _mm256_movemask_epi8(cmp); | |
if (mask != 0xFFFFFFFF) { | |
int j = __builtin_ctz(~mask) + i; | |
return str1 + j; | |
} | |
} | |
for (; i < len; i++) { | |
if (str1[i] != str2[i]) { | |
break; | |
} | |
} | |
return str1 + i; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment