Skip to content

Instantly share code, notes, and snippets.

@yoh2
Created November 11, 2019 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoh2/9ef822c60bcb5e33dc3d2fdbe35ddc3a to your computer and use it in GitHub Desktop.
Save yoh2/9ef822c60bcb5e33dc3d2fdbe35ddc3a to your computer and use it in GitHub Desktop.
大雑把改行カウント
size_t simple_count(const char buf[], size_t n)
{
size_t count = 0;
for(size_t i = 0; i < n; i++)
{
if(buf[i] == '\n')
{
++count;
}
}
return count;
}
// 手抜きで buf は 32 バイトアライン限定
size_t simd_count(const char buf[], size_t n)
{
const __m256i lfs = _mm256_set1_epi8('\n');
const __m256i ones = _mm256_set1_epi8(1);
size_t count = 0;
size_t i;
for(i = 0; i + 32 <= n; i += 32)
{
__m256i x = _mm256_load_si256((const __m256i *)(buf + i));
x = _mm256_cmpeq_epi8(x, lfs) & ones;
x = _mm256_hadd_epi16(x, _mm256_permute2x128_si256(x, x, 1));
x = _mm256_hadd_epi16(x, x);
x = _mm256_hadd_epi16(x, x);
x = _mm256_hadd_epi16(x, x);
size_t c = (x[0] & 0xffff);
count += (c >> 8) + (c & 0xff);
}
count += simple_count(buf + i, n - i);
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment