1#ifndef THIRD_PARTY_BROTLI_ENC_MATCHING_TAG_MASK_H_
2#define THIRD_PARTY_BROTLI_ENC_MATCHING_TAG_MASK_H_
4#include "../common/platform.h"
6#if defined(__SSE2__) || defined(_M_AMD64) || (defined (_M_IX86) && defined(_M_IX86_FP) && (_M_IX86_FP >= 2))
10#if defined(SUPPORTS_SSE_2)
15static BROTLI_INLINE uint64_t GetMatchingTagMask(
16 size_t chunk_count,
const uint8_t tag,
17 const uint8_t* BROTLI_RESTRICT tag_bucket,
const size_t head) {
19#if defined(SUPPORTS_SSE_2)
20 const __m128i comparison_mask = _mm_set1_epi8((
char)tag);
22 for (i = 0; i < chunk_count && i < 4; i++) {
24 _mm_loadu_si128((
const __m128i*)(
const void*)(tag_bucket + 16 * i));
25 const __m128i equal_mask = _mm_cmpeq_epi8(chunk, comparison_mask);
26 matches |= (uint64_t)_mm_movemask_epi8(equal_mask) << 16 * i;
29 const int chunk_size =
sizeof(size_t);
30 const size_t shift_amount = ((chunk_size * 8) - chunk_size);
31 const size_t xFF = ~((size_t)0);
32 const size_t x01 = xFF / 0xFF;
33 const size_t x80 = x01 << 7;
34 const size_t splat_char = tag * x01;
35 int i = ((int)chunk_count * 16) - chunk_size;
36 BROTLI_DCHECK((
sizeof(
size_t) == 4) || (
sizeof(
size_t) == 8));
37#if BROTLI_LITTLE_ENDIAN
38 const size_t extractMagic = (xFF / 0x7F) >> chunk_size;
40 size_t chunk = BrotliUnalignedReadSizeT(&tag_bucket[i]);
42 chunk = (((chunk | x80) - x01) | chunk) & x80;
43 matches <<= chunk_size;
44 matches |= (chunk * extractMagic) >> shift_amount;
48 const size_t msb = xFF ^ (xFF >> 1);
49 const size_t extractMagic = (msb / 0x1FF) | msb;
51 size_t chunk = BrotliUnalignedReadSizeT(&tag_bucket[i]);
53 chunk = (((chunk | x80) - x01) | chunk) & x80;
54 matches <<= chunk_size;
55 matches |= ((chunk >> 7) * extractMagic) >> shift_amount;
61 if (chunk_count == 1)
return BrotliRotateRight16((uint16_t)matches, head);
62 if (chunk_count == 2)
return BrotliRotateRight32((uint32_t)matches, head);
63 return BrotliRotateRight64(matches, head);