Skip to content

Instantly share code, notes, and snippets.

@evilpie
Created February 24, 2017 13:46
Show Gist options
  • Save evilpie/5b339dd034894346dc048635b436682d to your computer and use it in GitHub Desktop.
Save evilpie/5b339dd034894346dc048635b436682d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <emmintrin.h> // SSE2
#include <time.h>
static inline
bool is_foo(char c) {
return c == '"' || c == '\\' || c < ' ';
}
int main(void)
{
#define X 16000000
volatile char* str = (char*)malloc(X);
memset((void*) str, 'a', X);
str[300017] = ' ';
// asm("int $3");
clock_t t = clock();
__m128i a = _mm_set1_epi8('"');
__m128i b = _mm_set1_epi8('\\');
__m128i c = _mm_set1_epi8(' ');
for (int i = 0; i < X; i += 16) {
__m128i s = _mm_loadu_si128((__m128i const *)str);
__m128i hasA = _mm_cmpeq_epi8(s, a);
__m128i hasB = _mm_cmpeq_epi8(s, b);
__m128i hasAorB = _mm_or_si128(hasA, hasB);
__m128i hasC = _mm_cmplt_epi8(s, c);
__m128i hasAny = _mm_or_si128(hasAorB, hasC);
int x = _mm_movemask_epi8(hasAny);
if (x) {
printf("has at index: %d\n", i + __builtin_ctz(x & 0xffff));
}
str += 16;
}
// for (int i = 0; i < X; i++) {
// if (is_foo(str[i])) {
// printf("has at index: %d\n", i);
// }
// }
printf("time: %ld\n", clock() - t);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment