]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/BrotliCompress/enc/hash_forgetful_chain_inc.h
BaseTools: Make brotli a submodule
[mirror_edk2.git] / BaseTools / Source / C / BrotliCompress / enc / hash_forgetful_chain_inc.h
diff --git a/BaseTools/Source/C/BrotliCompress/enc/hash_forgetful_chain_inc.h b/BaseTools/Source/C/BrotliCompress/enc/hash_forgetful_chain_inc.h
deleted file mode 100644 (file)
index 9caf429..0000000
+++ /dev/null
@@ -1,254 +0,0 @@
-/* NOLINT(build/header_guard) */\r
-/* Copyright 2016 Google Inc. All Rights Reserved.\r
-\r
-   Distributed under MIT license.\r
-   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT\r
-*/\r
-\r
-/* template parameters: FN, BUCKET_BITS, NUM_BANKS, BANK_BITS,\r
-                        NUM_LAST_DISTANCES_TO_CHECK */\r
-\r
-/* A (forgetful) hash table to the data seen by the compressor, to\r
-   help create backward references to previous data.\r
-\r
-   Hashes are stored in chains which are bucketed to groups. Group of chains\r
-   share a storage "bank". When more than "bank size" chain nodes are added,\r
-   oldest nodes are replaced; this way several chains may share a tail. */\r
-\r
-#define HashForgetfulChain HASHER()\r
-\r
-#define BANK_SIZE (1 << BANK_BITS)\r
-\r
-/* Number of hash buckets. */\r
-#define BUCKET_SIZE (1 << BUCKET_BITS)\r
-\r
-#define CAPPED_CHAINS 0\r
-\r
-static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; }\r
-static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 4; }\r
-\r
-/* HashBytes is the function that chooses the bucket to place the address in.*/\r
-static BROTLI_INLINE size_t FN(HashBytes)(const uint8_t* data) {\r
-  const uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32;\r
-  /* The higher bits contain more mixture from the multiplication,\r
-     so we take our results from there. */\r
-  return h >> (32 - BUCKET_BITS);\r
-}\r
-\r
-typedef struct FN(Slot) {\r
-  uint16_t delta;\r
-  uint16_t next;\r
-} FN(Slot);\r
-\r
-typedef struct FN(Bank) {\r
-  FN(Slot) slots[BANK_SIZE];\r
-} FN(Bank);\r
-\r
-typedef struct HashForgetfulChain {\r
-  uint32_t addr[BUCKET_SIZE];\r
-  uint16_t head[BUCKET_SIZE];\r
-  /* Truncated hash used for quick rejection of "distance cache" candidates. */\r
-  uint8_t tiny_hash[65536];\r
-  FN(Bank) banks[NUM_BANKS];\r
-  uint16_t free_slot_idx[NUM_BANKS];\r
-  size_t max_hops;\r
-} HashForgetfulChain;\r
-\r
-static BROTLI_INLINE HashForgetfulChain* FN(Self)(HasherHandle handle) {\r
-  return (HashForgetfulChain*)&(GetHasherCommon(handle)[1]);\r
-}\r
-\r
-static void FN(Initialize)(\r
-    HasherHandle handle, const BrotliEncoderParams* params) {\r
-  FN(Self)(handle)->max_hops =\r
-      (params->quality > 6 ? 7u : 8u) << (params->quality - 4);\r
-}\r
-\r
-static void FN(Prepare)(HasherHandle handle, BROTLI_BOOL one_shot,\r
-    size_t input_size, const uint8_t* data) {\r
-  HashForgetfulChain* self = FN(Self)(handle);\r
-  /* Partial preparation is 100 times slower (per socket). */\r
-  size_t partial_prepare_threshold = BUCKET_SIZE >> 6;\r
-  if (one_shot && input_size <= partial_prepare_threshold) {\r
-    size_t i;\r
-    for (i = 0; i < input_size; ++i) {\r
-      size_t bucket = FN(HashBytes)(&data[i]);\r
-      /* See InitEmpty comment. */\r
-      self->addr[bucket] = 0xCCCCCCCC;\r
-      self->head[bucket] = 0xCCCC;\r
-    }\r
-  } else {\r
-    /* Fill |addr| array with 0xCCCCCCCC value. Because of wrapping, position\r
-       processed by hasher never reaches 3GB + 64M; this makes all new chains\r
-       to be terminated after the first node. */\r
-    memset(self->addr, 0xCC, sizeof(self->addr));\r
-    memset(self->head, 0, sizeof(self->head));\r
-  }\r
-  memset(self->tiny_hash, 0, sizeof(self->tiny_hash));\r
-  memset(self->free_slot_idx, 0, sizeof(self->free_slot_idx));\r
-}\r
-\r
-static BROTLI_INLINE size_t FN(HashMemAllocInBytes)(\r
-    const BrotliEncoderParams* params, BROTLI_BOOL one_shot,\r
-    size_t input_size) {\r
-  BROTLI_UNUSED(params);\r
-  BROTLI_UNUSED(one_shot);\r
-  BROTLI_UNUSED(input_size);\r
-  return sizeof(HashForgetfulChain);\r
-}\r
-\r
-/* Look at 4 bytes at &data[ix & mask]. Compute a hash from these, and prepend\r
-   node to corresponding chain; also update tiny_hash for current position. */\r
-static BROTLI_INLINE void FN(Store)(HasherHandle BROTLI_RESTRICT handle,\r
-    const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {\r
-  HashForgetfulChain* self = FN(Self)(handle);\r
-  const size_t key = FN(HashBytes)(&data[ix & mask]);\r
-  const size_t bank = key & (NUM_BANKS - 1);\r
-  const size_t idx = self->free_slot_idx[bank]++ & (BANK_SIZE - 1);\r
-  size_t delta = ix - self->addr[key];\r
-  self->tiny_hash[(uint16_t)ix] = (uint8_t)key;\r
-  if (delta > 0xFFFF) delta = CAPPED_CHAINS ? 0 : 0xFFFF;\r
-  self->banks[bank].slots[idx].delta = (uint16_t)delta;\r
-  self->banks[bank].slots[idx].next = self->head[key];\r
-  self->addr[key] = (uint32_t)ix;\r
-  self->head[key] = (uint16_t)idx;\r
-}\r
-\r
-static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle,\r
-    const uint8_t* data, const size_t mask, const size_t ix_start,\r
-    const size_t ix_end) {\r
-  size_t i;\r
-  for (i = ix_start; i < ix_end; ++i) {\r
-    FN(Store)(handle, data, mask, i);\r
-  }\r
-}\r
-\r
-static BROTLI_INLINE void FN(StitchToPreviousBlock)(HasherHandle handle,\r
-    size_t num_bytes, size_t position, const uint8_t* ringbuffer,\r
-    size_t ring_buffer_mask) {\r
-  if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {\r
-    /* Prepare the hashes for three last bytes of the last write.\r
-       These could not be calculated before, since they require knowledge\r
-       of both the previous and the current block. */\r
-    FN(Store)(handle, ringbuffer, ring_buffer_mask, position - 3);\r
-    FN(Store)(handle, ringbuffer, ring_buffer_mask, position - 2);\r
-    FN(Store)(handle, ringbuffer, ring_buffer_mask, position - 1);\r
-  }\r
-}\r
-\r
-static BROTLI_INLINE void FN(PrepareDistanceCache)(\r
-    HasherHandle handle, int* BROTLI_RESTRICT distance_cache) {\r
-  BROTLI_UNUSED(handle);\r
-  PrepareDistanceCache(distance_cache, NUM_LAST_DISTANCES_TO_CHECK);\r
-}\r
-\r
-/* Find a longest backward match of &data[cur_ix] up to the length of\r
-   max_length and stores the position cur_ix in the hash table.\r
-\r
-   REQUIRES: FN(PrepareDistanceCache) must be invoked for current distance cache\r
-             values; if this method is invoked repeatedly with the same distance\r
-             cache values, it is enough to invoke FN(PrepareDistanceCache) once.\r
-\r
-   Does not look for matches longer than max_length.\r
-   Does not look for matches further away than max_backward.\r
-   Writes the best match into |out|.\r
-   |out|->score is updated only if a better match is found. */\r
-static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle,\r
-    const BrotliEncoderDictionary* dictionary,\r
-    const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,\r
-    const int* BROTLI_RESTRICT distance_cache,\r
-    const size_t cur_ix, const size_t max_length, const size_t max_backward,\r
-    const size_t gap, const size_t max_distance,\r
-    HasherSearchResult* BROTLI_RESTRICT out) {\r
-  HashForgetfulChain* self = FN(Self)(handle);\r
-  const size_t cur_ix_masked = cur_ix & ring_buffer_mask;\r
-  /* Don't accept a short copy from far away. */\r
-  score_t min_score = out->score;\r
-  score_t best_score = out->score;\r
-  size_t best_len = out->len;\r
-  size_t i;\r
-  const size_t key = FN(HashBytes)(&data[cur_ix_masked]);\r
-  const uint8_t tiny_hash = (uint8_t)(key);\r
-  out->len = 0;\r
-  out->len_code_delta = 0;\r
-  /* Try last distance first. */\r
-  for (i = 0; i < NUM_LAST_DISTANCES_TO_CHECK; ++i) {\r
-    const size_t backward = (size_t)distance_cache[i];\r
-    size_t prev_ix = (cur_ix - backward);\r
-    /* For distance code 0 we want to consider 2-byte matches. */\r
-    if (i > 0 && self->tiny_hash[(uint16_t)prev_ix] != tiny_hash) continue;\r
-    if (prev_ix >= cur_ix || backward > max_backward) {\r
-      continue;\r
-    }\r
-    prev_ix &= ring_buffer_mask;\r
-    {\r
-      const size_t len = FindMatchLengthWithLimit(&data[prev_ix],\r
-                                                  &data[cur_ix_masked],\r
-                                                  max_length);\r
-      if (len >= 2) {\r
-        score_t score = BackwardReferenceScoreUsingLastDistance(len);\r
-        if (best_score < score) {\r
-          if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);\r
-          if (best_score < score) {\r
-            best_score = score;\r
-            best_len = len;\r
-            out->len = best_len;\r
-            out->distance = backward;\r
-            out->score = best_score;\r
-          }\r
-        }\r
-      }\r
-    }\r
-  }\r
-  {\r
-    const size_t bank = key & (NUM_BANKS - 1);\r
-    size_t backward = 0;\r
-    size_t hops = self->max_hops;\r
-    size_t delta = cur_ix - self->addr[key];\r
-    size_t slot = self->head[key];\r
-    while (hops--) {\r
-      size_t prev_ix;\r
-      size_t last = slot;\r
-      backward += delta;\r
-      if (backward > max_backward || (CAPPED_CHAINS && !delta)) break;\r
-      prev_ix = (cur_ix - backward) & ring_buffer_mask;\r
-      slot = self->banks[bank].slots[last].next;\r
-      delta = self->banks[bank].slots[last].delta;\r
-      if (cur_ix_masked + best_len > ring_buffer_mask ||\r
-          prev_ix + best_len > ring_buffer_mask ||\r
-          data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {\r
-        continue;\r
-      }\r
-      {\r
-        const size_t len = FindMatchLengthWithLimit(&data[prev_ix],\r
-                                                    &data[cur_ix_masked],\r
-                                                    max_length);\r
-        if (len >= 4) {\r
-          /* Comparing for >= 3 does not change the semantics, but just saves\r
-             for a few unnecessary binary logarithms in backward reference\r
-             score, since we are not interested in such short matches. */\r
-          score_t score = BackwardReferenceScore(len, backward);\r
-          if (best_score < score) {\r
-            best_score = score;\r
-            best_len = len;\r
-            out->len = best_len;\r
-            out->distance = backward;\r
-            out->score = best_score;\r
-          }\r
-        }\r
-      }\r
-    }\r
-    FN(Store)(handle, data, ring_buffer_mask, cur_ix);\r
-  }\r
-  if (out->score == min_score) {\r
-    SearchInStaticDictionary(dictionary,\r
-        handle, &data[cur_ix_masked], max_length, max_backward + gap,\r
-        max_distance, out, BROTLI_FALSE);\r
-  }\r
-}\r
-\r
-#undef BANK_SIZE\r
-#undef BUCKET_SIZE\r
-#undef CAPPED_CHAINS\r
-\r
-#undef HashForgetfulChain\r