3 ms·
The (cheating, ASCII-only) SIMD implementation for reference: #if 0 /* shell polyglot */ exec ${CC:-cc} $CPPFLAGS -g -O2 -march=x86-64-v3 $CFLAGS -o "${0%.
by mananaysiempre 2y ago
The (cheating, ASCII-only) SIMD implementation for reference:
#if 0 /* shell polyglot */
exec ${CC:-cc} $CPPFLAGS -g -O2 -march=x86-64-v3 $CFLAGS -o "${0%.c}" "$0" || exit $?
#endif
/* SPDX-License-Identifier: CC0-1.0 */
#include <immintrin.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv) {
const char *const progname = argc ? argv[0] : "<unknown>";
unsigned long long bytes = 0, words = 0, lines = 0;
uint32_t prev = -1;
for (;;) {
static __m256i buf[1024 * 1024];
ssize_t k, n = 0;
do {
k = read(STDIN_FILENO, (char *)buf + n, sizeof buf - n);
} while (k > 0 && (n += k) < sizeof buf);
if (k < 0) { perror(progname); return 1; }
if (!n) break;
bytes += n;
while (n % sizeof *buf) ((char *)buf)[n++] = ' ';
n /= sizeof *buf;
for (size_t i = 0; i < n; i++) {
__m256i x = _mm256_load_si256(&buf[i]);
__m256i nl = _mm256_cmpeq_epi8(x, _mm256_set1_epi8('\n'));
lines += _mm_popcnt_u32(_mm256_movemask_epi8(nl));
const __m256i table = _mm256_broadcastsi128_si256(_mm_setr_epi8(
/* 0 */ ' ', 0, 0, 0, 0, 0, 0, 0,
/* 8 */ 0, '\t', '\n', '\v', '\f', '\r', 0, 0));
__m256i ws = _mm256_cmpeq_epi8(x, _mm256_shuffle_epi8(table,
_mm256_and_si256(x, _mm256_set1_epi8(0x0F))));
uint64_t mask = _mm256_movemask_epi8(ws);
words += _mm_popcnt_u32(mask ^ (mask << 32 | prev) >> 31);
prev = mask;
}
}
words += !(prev >> 31);
printf("%llu %llu %llu\n", lines, words / 2, bytes);
return 0;
}