| 1 |
/* Copyright 2013 Google Inc. All Rights Reserved. |
| 2 |
|
| 3 |
Distributed under MIT license. |
| 4 |
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
/* Function to find backward reference copies. */ |
| 8 |
|
| 9 |
#include "backward_references.h" |
| 10 |
|
| 11 |
#include "../common/constants.h" |
| 12 |
#include "../common/context.h" |
| 13 |
#include "../common/platform.h" |
| 14 |
#include "command.h" |
| 15 |
#include "compound_dictionary.h" |
| 16 |
#include "encoder_dict.h" |
| 17 |
#include "hash.h" |
| 18 |
#include "params.h" |
| 19 |
#include "quality.h" /* IWYU pragma: keep for inc */ |
| 20 |
|
| 21 |
#if defined(__cplusplus) || defined(c_plusplus) |
| 22 |
extern "C" { |
| 23 |
#endif |
| 24 |
|
| 25 |
static BROTLI_INLINE size_t ComputeDistanceCode(size_t distance, |
| 26 |
size_t max_distance, |
| 27 |
const int* dist_cache) { |
| 28 |
if (distance <= max_distance) { |
| 29 |
size_t distance_plus_3 = distance + 3; |
| 30 |
size_t offset0 = distance_plus_3 - (size_t)dist_cache[0]; |
| 31 |
size_t offset1 = distance_plus_3 - (size_t)dist_cache[1]; |
| 32 |
if (distance == (size_t)dist_cache[0]) { |
| 33 |
return 0; |
| 34 |
} else if (distance == (size_t)dist_cache[1]) { |
| 35 |
return 1; |
| 36 |
} else if (offset0 < 7) { |
| 37 |
return (0x9750468 >> (4 * offset0)) & 0xF; |
| 38 |
} else if (offset1 < 7) { |
| 39 |
return (0xFDB1ACE >> (4 * offset1)) & 0xF; |
| 40 |
} else if (distance == (size_t)dist_cache[2]) { |
| 41 |
return 2; |
| 42 |
} else if (distance == (size_t)dist_cache[3]) { |
| 43 |
return 3; |
| 44 |
} |
| 45 |
} |
| 46 |
return distance + BROTLI_NUM_DISTANCE_SHORT_CODES - 1; |
| 47 |
} |
| 48 |
|
| 49 |
#define EXPAND_CAT(a, b) CAT(a, b) |
| 50 |
#define CAT(a, b) a ## b |
| 51 |
#define FN(X) EXPAND_CAT(X, HASHER()) |
| 52 |
#define EXPORT_FN(X) EXPAND_CAT(X, EXPAND_CAT(PREFIX(), HASHER())) |
| 53 |
|
| 54 |
#define PREFIX() N |
| 55 |
#define ENABLE_COMPOUND_DICTIONARY 0 |
| 56 |
|
| 57 |
#define HASHER() H2 |
| 58 |
/* NOLINTNEXTLINE(build/include) */ |
| 59 |
#include "backward_references_inc.h" |
| 60 |
#undef HASHER |
| 61 |
|
| 62 |
#define HASHER() H3 |
| 63 |
/* NOLINTNEXTLINE(build/include) */ |
| 64 |
#include "backward_references_inc.h" |
| 65 |
#undef HASHER |
| 66 |
|
| 67 |
#define HASHER() H4 |
| 68 |
/* NOLINTNEXTLINE(build/include) */ |
| 69 |
#include "backward_references_inc.h" |
| 70 |
#undef HASHER |
| 71 |
|
| 72 |
#define HASHER() H5 |
| 73 |
/* NOLINTNEXTLINE(build/include) */ |
| 74 |
#include "backward_references_inc.h" |
| 75 |
#undef HASHER |
| 76 |
|
| 77 |
#define HASHER() H6 |
| 78 |
/* NOLINTNEXTLINE(build/include) */ |
| 79 |
#include "backward_references_inc.h" |
| 80 |
#undef HASHER |
| 81 |
|
| 82 |
#define HASHER() H40 |
| 83 |
/* NOLINTNEXTLINE(build/include) */ |
| 84 |
#include "backward_references_inc.h" |
| 85 |
#undef HASHER |
| 86 |
|
| 87 |
#define HASHER() H41 |
| 88 |
/* NOLINTNEXTLINE(build/include) */ |
| 89 |
#include "backward_references_inc.h" |
| 90 |
#undef HASHER |
| 91 |
|
| 92 |
#define HASHER() H42 |
| 93 |
/* NOLINTNEXTLINE(build/include) */ |
| 94 |
#include "backward_references_inc.h" |
| 95 |
#undef HASHER |
| 96 |
|
| 97 |
#define HASHER() H54 |
| 98 |
/* NOLINTNEXTLINE(build/include) */ |
| 99 |
#include "backward_references_inc.h" |
| 100 |
#undef HASHER |
| 101 |
|
| 102 |
#define HASHER() H35 |
| 103 |
/* NOLINTNEXTLINE(build/include) */ |
| 104 |
#include "backward_references_inc.h" |
| 105 |
#undef HASHER |
| 106 |
|
| 107 |
#define HASHER() H55 |
| 108 |
/* NOLINTNEXTLINE(build/include) */ |
| 109 |
#include "backward_references_inc.h" |
| 110 |
#undef HASHER |
| 111 |
|
| 112 |
#define HASHER() H65 |
| 113 |
/* NOLINTNEXTLINE(build/include) */ |
| 114 |
#include "backward_references_inc.h" |
| 115 |
#undef HASHER |
| 116 |
|
| 117 |
#if defined(BROTLI_MAX_SIMD_QUALITY) |
| 118 |
#define HASHER() H58 |
| 119 |
/* NOLINTNEXTLINE(build/include) */ |
| 120 |
#include "backward_references_inc.h" |
| 121 |
#undef HASHER |
| 122 |
|
| 123 |
#define HASHER() H68 |
| 124 |
/* NOLINTNEXTLINE(build/include) */ |
| 125 |
#include "backward_references_inc.h" |
| 126 |
#undef HASHER |
| 127 |
#endif |
| 128 |
|
| 129 |
#undef ENABLE_COMPOUND_DICTIONARY |
| 130 |
#undef PREFIX |
| 131 |
#define PREFIX() D |
| 132 |
#define ENABLE_COMPOUND_DICTIONARY 1 |
| 133 |
|
| 134 |
#define HASHER() H5 |
| 135 |
/* NOLINTNEXTLINE(build/include) */ |
| 136 |
#include "backward_references_inc.h" |
| 137 |
#undef HASHER |
| 138 |
#define HASHER() H6 |
| 139 |
/* NOLINTNEXTLINE(build/include) */ |
| 140 |
#include "backward_references_inc.h" |
| 141 |
#undef HASHER |
| 142 |
#define HASHER() H40 |
| 143 |
/* NOLINTNEXTLINE(build/include) */ |
| 144 |
#include "backward_references_inc.h" |
| 145 |
#undef HASHER |
| 146 |
#define HASHER() H41 |
| 147 |
/* NOLINTNEXTLINE(build/include) */ |
| 148 |
#include "backward_references_inc.h" |
| 149 |
#undef HASHER |
| 150 |
#define HASHER() H42 |
| 151 |
/* NOLINTNEXTLINE(build/include) */ |
| 152 |
#include "backward_references_inc.h" |
| 153 |
#undef HASHER |
| 154 |
#define HASHER() H55 |
| 155 |
/* NOLINTNEXTLINE(build/include) */ |
| 156 |
#include "backward_references_inc.h" |
| 157 |
#undef HASHER |
| 158 |
#define HASHER() H65 |
| 159 |
/* NOLINTNEXTLINE(build/include) */ |
| 160 |
#include "backward_references_inc.h" |
| 161 |
#undef HASHER |
| 162 |
#if defined(BROTLI_MAX_SIMD_QUALITY) |
| 163 |
#define HASHER() H58 |
| 164 |
/* NOLINTNEXTLINE(build/include) */ |
| 165 |
#include "backward_references_inc.h" |
| 166 |
#undef HASHER |
| 167 |
#define HASHER() H68 |
| 168 |
/* NOLINTNEXTLINE(build/include) */ |
| 169 |
#include "backward_references_inc.h" |
| 170 |
#undef HASHER |
| 171 |
#endif |
| 172 |
|
| 173 |
#undef ENABLE_COMPOUND_DICTIONARY |
| 174 |
#undef PREFIX |
| 175 |
|
| 176 |
#undef EXPORT_FN |
| 177 |
#undef FN |
| 178 |
#undef CAT |
| 179 |
#undef EXPAND_CAT |
| 180 |
|
| 181 |
void BrotliCreateBackwardReferences(size_t num_bytes, |
| 182 |
size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask, |
| 183 |
ContextLut literal_context_lut, const BrotliEncoderParams* params, |
| 184 |
Hasher* hasher, int* dist_cache, size_t* last_insert_len, |
| 185 |
Command* commands, size_t* num_commands, size_t* num_literals) { |
| 186 |
if (params->dictionary.compound.num_chunks != 0) { |
| 187 |
switch (params->hasher.type) { |
| 188 |
#define CASE_(N) \ |
| 189 |
case N: \ |
| 190 |
CreateBackwardReferencesDH ## N(num_bytes, \ |
| 191 |
position, ringbuffer, ringbuffer_mask, \ |
| 192 |
literal_context_lut, params, hasher, dist_cache, \ |
| 193 |
last_insert_len, commands, num_commands, num_literals); \ |
| 194 |
return; |
| 195 |
CASE_(5) |
| 196 |
CASE_(6) |
| 197 |
#if defined(BROTLI_MAX_SIMD_QUALITY) |
| 198 |
CASE_(58) |
| 199 |
CASE_(68) |
| 200 |
#endif |
| 201 |
CASE_(40) |
| 202 |
CASE_(41) |
| 203 |
CASE_(42) |
| 204 |
CASE_(55) |
| 205 |
CASE_(65) |
| 206 |
#undef CASE_ |
| 207 |
default: |
| 208 |
BROTLI_DCHECK(BROTLI_FALSE); |
| 209 |
break; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
switch (params->hasher.type) { |
| 214 |
#define CASE_(N) \ |
| 215 |
case N: \ |
| 216 |
CreateBackwardReferencesNH ## N(num_bytes, \ |
| 217 |
position, ringbuffer, ringbuffer_mask, \ |
| 218 |
literal_context_lut, params, hasher, dist_cache, \ |
| 219 |
last_insert_len, commands, num_commands, num_literals); \ |
| 220 |
return; |
| 221 |
FOR_GENERIC_HASHERS(CASE_) |
| 222 |
#undef CASE_ |
| 223 |
default: |
| 224 |
BROTLI_DCHECK(BROTLI_FALSE); |
| 225 |
break; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
#if defined(__cplusplus) || defined(c_plusplus) |
| 230 |
} /* extern "C" */ |
| 231 |
#endif |