Last active
April 18, 2021 19:34
-
-
Save vurtun/462257211946a5f446c0fea270b57874 to your computer and use it in GitHub Desktop.
string split (line splitting)
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
static int* | |
str_split(const char *in, int siz, char delim) { | |
int *eol = 0; | |
__m128i msk = _mm_set1_epi8(0x80); | |
__m128i sub = _mm_set1_epi8(0x01); | |
__m128i neg = _mm_set1_epi64(-1LL); | |
__m128i sep = _mm_set1_epi8(delim); | |
__m128i endmsk = _mm_set1_epi32(-1); | |
if (siz & 15) { | |
endmsk = _mm_slli_si128(endmsk, 16 - (siz & 15)) | |
} | |
for (int i = 0; i < siz; i += 16) { | |
long long r[2]; | |
__m128i bmsk = (i + 16 < siz) ? _mm_set1_epi32(-1) : endmsk; | |
__m128i v = _mm_and_si128(_mm_loadu_si128(in + i), bmsk); | |
__m128i c = _mm_and_si128(_mm_xor_si128(v, neg), msk); | |
__m128i x = _mm_and_si128(_mm_sub_epi8(_mm_xor_si128(v, sep), sub), c); | |
_mm_storeu_si32(r, z); | |
int p = 0; | |
for (int j = 0; j < 2; ++j) { | |
char *rb = (char*)(r + j); | |
while ((p = ffsll(r[j]))) { | |
int b = (p - 1) >> 3; | |
dyn_add(eol, i + b + (j << 3)); | |
rb[b] = 0x0; | |
} | |
} | |
} | |
return eol; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment