Debian Codegraph @

libs/brotli/enc/matching_tag_mask.h

raw
1 #ifndef THIRD_PARTY_BROTLI_ENC_MATCHING_TAG_MASK_H_
2 #define THIRD_PARTY_BROTLI_ENC_MATCHING_TAG_MASK_H_
3
4 #include "../common/platform.h"
5
6 #if defined(__SSE2__) || defined(_M_AMD64) || \
7 (defined(_M_IX86) && defined(_M_IX86_FP) && (_M_IX86_FP >= 2))
8 #define SUPPORTS_SSE_2
9 #endif
10
11 #if defined(SUPPORTS_SSE_2)
12 #include <immintrin.h>
13 #endif
14
15
16 static BROTLI_INLINE uint64_t GetMatchingTagMask(
17 size_t chunk_count, const uint8_t tag,
18 const uint8_t* BROTLI_RESTRICT tag_bucket, const size_t head) {
19 uint64_t matches = 0;
20 #if defined(SUPPORTS_SSE_2)
21 const __m128i comparison_mask = _mm_set1_epi8((char)tag);
22 size_t i;
23 for (i = 0; i < chunk_count && i < 4; i++) {
24 const __m128i chunk =
25 _mm_loadu_si128((const __m128i*)(const void*)(tag_bucket + 16 * i));
26 const __m128i equal_mask = _mm_cmpeq_epi8(chunk, comparison_mask);
27 matches |= (uint64_t)_mm_movemask_epi8(equal_mask) << 16 * i;
28 }
29 #else
30 const int chunk_size = sizeof(size_t);
31 const size_t shift_amount = ((chunk_size * 8) - chunk_size);
32 const size_t xFF = ~((size_t)0);
33 const size_t x01 = xFF / 0xFF;
34 const size_t x80 = x01 << 7;
35 const size_t splat_char = tag * x01;
36 int i = ((int)chunk_count * 16) - chunk_size;
37 BROTLI_DCHECK((sizeof(size_t) == 4) || (sizeof(size_t) == 8));
38 #if BROTLI_LITTLE_ENDIAN
39 const size_t extractMagic = (xFF / 0x7F) >> chunk_size;
40 do {
41 size_t chunk = BrotliUnalignedReadSizeT(&tag_bucket[i]);
42 chunk ^= splat_char;
43 chunk = (((chunk | x80) - x01) | chunk) & x80;
44 matches <<= chunk_size;
45 matches |= (chunk * extractMagic) >> shift_amount;
46 i -= chunk_size;
47 } while (i >= 0);
48 #else
49 const size_t msb = xFF ^ (xFF >> 1);
50 const size_t extractMagic = (msb / 0x1FF) | msb;
51 do {
52 size_t chunk = BrotliUnalignedReadSizeT(&tag_bucket[i]);
53 chunk ^= splat_char;
54 chunk = (((chunk | x80) - x01) | chunk) & x80;
55 matches <<= chunk_size;
56 matches |= ((chunk >> 7) * extractMagic) >> shift_amount;
57 i -= chunk_size;
58 } while (i >= 0);
59 #endif
60 matches = ~matches;
61 #endif
62 if (chunk_count == 1) return BrotliRotateRight16((uint16_t)matches, head);
63 if (chunk_count == 2) return BrotliRotateRight32((uint32_t)matches, head);
64 return BrotliRotateRight64(matches, head);
65 }
66
67 #undef SUPPORTS_SSE_2
68
69 #endif // THIRD_PARTY_BROTLI_ENC_MATCHING_TAG_MASK_H_