Skip to content

Instantly share code, notes, and snippets.

@tanakamura
Last active August 29, 2015 14:27
Show Gist options
  • Save tanakamura/1fbea1df9e71dbe1ef4d to your computer and use it in GitHub Desktop.
Save tanakamura/1fbea1df9e71dbe1ef4d to your computer and use it in GitHub Desktop.
#include <immintrin.h>
#include <stdio.h>
#include <intrin.h>
/*
lookup8:
subq $12, %rsp
.seh_stackalloc 12
.seh_endprologue
movl %edx, (%rsp)
vmovdqu (%rcx), %ymm0
vbroadcastss (%rsp), %ymm1
vpcmpeqd %ymm0, %ymm1, %ymm0
vptest %ymm0, %ymm0
je .L3
vpmovmskb %ymm0, %eax
bsrl %eax, %eax
shrl $2, %eax
.L4:
vzeroupper
addq $12, %rsp
ret
.L3:
movl $-1, %eax
jmp .L4
*/
int table[8] = {
50,
13,
9,
12,
15,
89,
93,
92
};
int
lookup8(int *table, int val)
{
__m256i v256 = _mm256_set1_epi32(val);
__m256i cmp = _mm256_cmpeq_epi32(v256, _mm256_loadu_si256((__m256i*)table));
if (_mm256_testz_si256(cmp,cmp)) {
return -1;
}
int bits = _mm256_movemask_epi8(cmp);
//printf("%x\n", bits);
#ifdef __GNUC__
unsigned int pos = _bit_scan_reverse(bits);
#else
unsigned int pos;
_BitScanReverse(&pos, bits);
#endif
return pos >> 2;
}
int main(int argc, char **argv)
{
if (argc < 2) {
puts("usage : lookup8 val");
return 1;
}
int val = atoi(argv[1]);
printf("%d\n", lookup8(table, val));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment