diff --git a/src/main.c b/src/main.c index f9b45b8..91aac32 100644 --- a/src/main.c +++ b/src/main.c @@ -662,6 +662,8 @@ static int pick_threads(size_t n); /* the slicers below pick their width */ static long page_size(void); /* a page-aligned size is not trusted */ +static size_t find_high(const unsigned char *s, size_t n, size_t from); + static void count_mapped(const unsigned char *p, size_t n, counts_t *c, int need_high, int *high); @@ -693,7 +695,7 @@ static count_lw_fn pick_kernel(void) * itself - hence the declaration here and the table down there. */ typedef long long (*width_fn)(const unsigned char *s, size_t n, - long long linepos, long long *maxlen); + long long linepos, long long *maxlen, int *high); static width_fn width_count; static width_fn pick_width(void); @@ -806,8 +808,14 @@ static void maxlen_single(const unsigned char *p, size_t n, long long *linepos, } static long long width_chunk_scalar(const unsigned char *s, size_t n, - long long linepos, long long *maxlen) + long long linepos, long long *maxlen, + int *high) { + if (high != NULL && find_high(s, n, 0) < n) + { + *high = 1; + return linepos; + } maxlen_single(s, n, &linepos, maxlen); return linepos; } @@ -857,7 +865,7 @@ static long long width_run(long long linepos, uint32_t pr, uint32_t tb, */ __attribute__((target("avx2"))) static long long width_chunk_avx2(const unsigned char *s, size_t n, long long linepos, - long long *maxlen) + long long *maxlen, int *high) { const __m256i tab = _mm256_set1_epi8('\t'); const __m256i nl = _mm256_set1_epi8('\n'); @@ -872,6 +880,11 @@ width_chunk_avx2(const unsigned char *s, size_t n, long long linepos, __m256i v = _mm256_loadu_si256((const void *)(s + i)); uint32_t pr = (uint32_t)_mm256_movemask_epi8(_mm256_and_si256( _mm256_cmpgt_epi8(v, lo), _mm256_cmpgt_epi8(hi, v))); + if (high != NULL && _mm256_movemask_epi8(v)) + { + *high = 1; + return linepos; + } uint32_t tb = (uint32_t)_mm256_movemask_epi8(_mm256_cmpeq_epi8(v, tab)); uint32_t e = (uint32_t)_mm256_movemask_epi8(_mm256_cmpeq_epi8(v, nl)) | (uint32_t)_mm256_movemask_epi8(_mm256_cmpeq_epi8(v, cr)) | @@ -905,6 +918,11 @@ width_chunk_avx2(const unsigned char *s, size_t n, long long linepos, { unsigned char c = s[i]; + if (high != NULL && (c & 0x80)) + { + *high = 1; + return linepos; + } if (c == '\t') linepos += 8 - (linepos % 8); else if (c == '\r' || c == '\f' || c == '\n') @@ -956,7 +974,7 @@ static long long width_run64(long long linepos, uint64_t pr, uint64_t tb, */ __attribute__((target("avx512f,avx512bw"))) static long long width_chunk_avx512(const unsigned char *s, size_t n, long long linepos, - long long *maxlen) + long long *maxlen, int *high) { const __m512i tab = _mm512_set1_epi8('\t'); const __m512i nl = _mm512_set1_epi8('\n'); @@ -969,6 +987,11 @@ width_chunk_avx512(const unsigned char *s, size_t n, long long linepos, for (; i + 64 <= n; i += 64) { __m512i v = _mm512_loadu_si512((const void *)(s + i)); + if (high != NULL && _mm512_movepi8_mask(v)) + { + *high = 1; + return linepos; + } uint64_t pr = _mm512_cmple_epu8_mask(_mm512_sub_epi8(v, sp), span); uint64_t tb = _mm512_cmpeq_epi8_mask(v, tab); uint64_t e = _mm512_cmpeq_epi8_mask(v, nl) | @@ -1001,6 +1024,11 @@ width_chunk_avx512(const unsigned char *s, size_t n, long long linepos, { unsigned char c = s[i]; + if (high != NULL && (c & 0x80)) + { + *high = 1; + return linepos; + } if (c == '\t') linepos += 8 - (linepos % 8); else if (c == '\r' || c == '\f' || c == '\n') @@ -1055,7 +1083,7 @@ prefix_bytes(__m512i v) __attribute__(( target("avx512f,avx512bw,avx512vbmi,avx512vbmi2"))) static long long width_chunk_avx512b(const unsigned char *s, size_t n, long long linepos, - long long *maxlen) + long long *maxlen, int *high) { static const unsigned char seq[64] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, @@ -1077,6 +1105,11 @@ width_chunk_avx512b(const unsigned char *s, size_t n, long long linepos, for (; i + 64 <= n; i += 64) { __m512i v = _mm512_loadu_si512((const void *)(s + i)); + if (high != NULL && _mm512_movepi8_mask(v)) + { + *high = 1; + return linepos; + } uint64_t pr = _mm512_cmple_epu8_mask(_mm512_sub_epi8(v, sp), span); uint64_t tb = _mm512_cmpeq_epi8_mask(v, tab); uint64_t e = _mm512_cmpeq_epi8_mask(v, nl) | @@ -1147,6 +1180,11 @@ width_chunk_avx512b(const unsigned char *s, size_t n, long long linepos, { unsigned char c = s[i]; + if (high != NULL && (c & 0x80)) + { + *high = 1; + return linepos; + } if (c == '\t') linepos += 8 - (linepos % 8); else if (c == '\r' || c == '\f' || c == '\n') @@ -1753,7 +1791,7 @@ static int count_chunk_mb(const unsigned char *s, size_t n, mbjob_t *j) if (need_chars) j->chars += (long long)(e - i); if (need_maxlen) - linepos = width_count(s + i, e - i, linepos, &maxlen); + linepos = width_count(s + i, e - i, linepos, &maxlen, NULL); i = e; block_budget = 8; /* a fresh stretch, a fresh chance */ } @@ -2185,13 +2223,15 @@ typedef struct size_t n; long long maxlen; /* widest line that ends inside this slice */ long long linepos; /* line position at the slice's end */ + int high; /* set when asked to probe and a high byte was seen */ } wjob_t; static void *width_worker(void *arg) { wjob_t *j = arg; - j->linepos = width_count(j->s, j->n, 0, &j->maxlen); + j->linepos = + width_count(j->s, j->n, 0, &j->maxlen, j->high ? &j->high : NULL); return NULL; } @@ -2203,7 +2243,7 @@ static void *width_worker(void *arg) * buffer with no line ends at all is one slice - correct, just serial. */ static void count_width_sliced(const unsigned char *p, size_t n, int nt, - long long *maxlen) + long long *maxlen, int *high) { wjob_t jobs[MAX_THREADS]; pthread_t th[MAX_THREADS]; @@ -2236,6 +2276,7 @@ static void count_width_sliced(const unsigned char *p, size_t n, int nt, jobs[i].n = start[i + 1] - start[i]; jobs[i].maxlen = 0; jobs[i].linepos = 0; + jobs[i].high = 0; th[i] = 0; if (jobs[i].n > 0 && pthread_create(&th[i], NULL, width_worker, &jobs[i]) != 0) @@ -2250,6 +2291,8 @@ static void count_width_sliced(const unsigned char *p, size_t n, int nt, mx = jobs[i].maxlen; if (i == nj - 1 && jobs[i].linepos > mx) mx = jobs[i].linepos; /* the trailing line, unterminated */ + if (jobs[i].high) + *high = 1; } *maxlen = mx; @@ -2410,42 +2453,59 @@ static void count_mapped(const unsigned char *p, size_t n, counts_t *c, if (flags & F_CHARS) /* single-byte locale: chars == bytes */ c->chars += (long long)n; - if (nt <= 1) - { - int prev_ws = 1; - lw_t r = - count_lw(p, n, &prev_ws, need_lines, need_words, need_high, high); + /* -L alone still has to know whether the file is multibyte, but it + * has no lines or words for the byte kernel to count, so the width + * pass does the probing itself and the file is read once. */ + int probe_width = need_high && !need_lines && !need_words && + (flags & F_MAXLEN) != 0 && high != NULL; - if (need_high && *high) - return; /* partial counts; caller re-decodes */ - lines = r.lines; - words = r.words; - } - else + if (need_lines || need_words) { - count_sliced(p, n, nt, need_lines, need_words, need_high, high, &lines, - &words); - if (need_high && *high) - return; /* partial counts; caller re-decodes */ + if (nt <= 1) + { + int prev_ws = 1; + lw_t r = count_lw(p, n, &prev_ws, need_lines, need_words, need_high, + high); + + if (need_high && *high) + return; /* partial counts; caller re-decodes */ + lines = r.lines; + words = r.words; + } + else + { + count_sliced(p, n, nt, need_lines, need_words, need_high, high, + &lines, &words); + if (need_high && *high) + return; /* partial counts; caller re-decodes */ + } + + if (flags & F_LINES) + c->lines += lines; + if (flags & F_WORDS) + c->words += words; } - if (flags & F_LINES) - c->lines += lines; - if (flags & F_WORDS) - c->words += words; if (flags & F_MAXLEN) { long long maxlen = 0; if (nt <= 1) { - long long linepos = width_count(p, n, 0, &maxlen); + long long linepos = + width_count(p, n, 0, &maxlen, probe_width ? high : NULL); + if (probe_width && *high) + return; if (linepos > maxlen) maxlen = linepos; } else - count_width_sliced(p, n, nt, &maxlen); + { + count_width_sliced(p, n, nt, &maxlen, probe_width ? high : NULL); + if (probe_width && *high) + return; + } c->maxlen = maxlen; } } @@ -3582,7 +3642,7 @@ static int check_width(const char *name, width_fn fn) { long long a = carry, b = carry, ma = 0, mb = 0; - a = fn(buf, n, a, &ma); + a = fn(buf, n, a, &ma, NULL); maxlen_single(buf, n, &b, &mb); if (a != b || ma != mb) { @@ -3602,7 +3662,7 @@ static int check_width(const char *name, width_fn fn) for (size_t k = 0; k < n; k++) buf[k] = pat[rng32() % (sizeof pat - 1)]; - a = fn(buf, n, a, &ma); + a = fn(buf, n, a, &ma, NULL); maxlen_single(buf, n, &b, &mb); if (a != b || ma != mb) { @@ -3635,14 +3695,14 @@ static int check_width_sliced(void) buf[k] = (unsigned char)"\t\n\r\f\v" "\x80" "ab c ."[rng32() % 13]; - lp = width_count(buf, n, 0, &want); + lp = width_count(buf, n, 0, &want, NULL); expect = lp > want ? lp : want; for (size_t ti = 0; ti < sizeof tcs / sizeof tcs[0]; ti++) { long long got = 0; - count_width_sliced(buf, n, tcs[ti], &got); + count_width_sliced(buf, n, tcs[ti], &got, NULL); if (got != expect) { printf("width-sliced: n=%zu nt=%d %lld/%lld\n", n, tcs[ti], got,