]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/BrotliCompress/enc/prefix.h
BaseTools: Copy Brotli algorithm 3rd party source code for tool
[mirror_edk2.git] / BaseTools / Source / C / BrotliCompress / enc / prefix.h
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 /* Functions for encoding of integers into prefix codes the amount of extra
8 bits, and the actual values of the extra bits. */
9
10 #ifndef BROTLI_ENC_PREFIX_H_
11 #define BROTLI_ENC_PREFIX_H_
12
13 #include "../common/constants.h"
14 #include "../common/port.h"
15 #include "../common/types.h"
16 #include "./fast_log.h"
17
18 #if defined(__cplusplus) || defined(c_plusplus)
19 extern "C" {
20 #endif
21
22 static BROTLI_INLINE void PrefixEncodeCopyDistance(size_t distance_code,
23 size_t num_direct_codes,
24 size_t postfix_bits,
25 uint16_t* code,
26 uint32_t* extra_bits) {
27 if (distance_code < BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_codes) {
28 *code = (uint16_t)distance_code;
29 *extra_bits = 0;
30 return;
31 } else {
32 size_t dist = ((size_t)1 << (postfix_bits + 2u)) +
33 (distance_code - BROTLI_NUM_DISTANCE_SHORT_CODES - num_direct_codes);
34 size_t bucket = Log2FloorNonZero(dist) - 1;
35 size_t postfix_mask = (1u << postfix_bits) - 1;
36 size_t postfix = dist & postfix_mask;
37 size_t prefix = (dist >> bucket) & 1;
38 size_t offset = (2 + prefix) << bucket;
39 size_t nbits = bucket - postfix_bits;
40 *code = (uint16_t)(
41 (BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_codes +
42 ((2 * (nbits - 1) + prefix) << postfix_bits) + postfix));
43 *extra_bits = (uint32_t)(
44 (nbits << 24) | ((dist - offset) >> postfix_bits));
45 }
46 }
47
48 #if defined(__cplusplus) || defined(c_plusplus)
49 } /* extern "C" */
50 #endif
51
52 #endif /* BROTLI_ENC_PREFIX_H_ */