Created
March 24, 2013 17:57
-
-
Save hdante/5232848 to your computer and use it in GitHub Desktop.
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 <emmintrin.h> | |
#include <stdio.h> | |
#include <string.h> | |
int count(const __m128i *string1, const __m128i *string2, int size) | |
{ | |
const int inner_count = 255; | |
int max_loop; | |
int i, j, r; | |
__m128i acc8, acc32, acc32hi, s1, s2, equal, partial, zero; | |
r = 0; | |
max_loop = size/inner_count*inner_count; | |
acc32 = zero = _mm_setzero_si128(); | |
for (i = 0; i < max_loop; i = j) { | |
acc8 = _mm_set1_epi8(0); | |
for (j = i; j < i+inner_count; j++) { | |
equal = _mm_cmpeq_epi8(string1[j], string2[j]); | |
acc8 = _mm_sub_epi8(acc8, equal); | |
} | |
partial = _mm_sad_epu8(acc8, zero); | |
acc32 = _mm_add_epi32(acc32, partial); | |
} | |
acc32hi = _mm_srli_si128(acc32, 8); | |
acc32 = _mm_add_epi32(acc32, acc32hi); | |
r = _mm_cvtsi128_si32(acc32); | |
return r; | |
} | |
int count_simple(const char *string1, const char *string2, int size) | |
{ | |
int r = 0; | |
int i; | |
for (i = 0; i < size; i++) { | |
r += (string1[i] == string2[i]); | |
} | |
return r; | |
} | |
#define size 255*1024 | |
union aligned { | |
char s[size]; | |
__m128i v[size/16]; | |
}; | |
int main(void) | |
{ | |
union aligned a, b; | |
FILE *f1 = fopen("data1", "r"); | |
FILE *f2 = fopen("data2", "r"); | |
if (fread(a.s, size, 1, f1) < 1) | |
return -1; | |
if (fread(b.s, size, 1, f2) < 1) | |
return -1; | |
fclose(f1); | |
fclose(f2); | |
printf("%d\n", count(a.v, b.v, size/16)); | |
printf("%d\n", count_simple(a.s, b.s, size)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment