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/platform.h" |
15 | #include <brotli/types.h> |
16 | #include "./fast_log.h" |
17 | |
18 | #if defined(__cplusplus) || defined(c_plusplus) |
19 | extern "C"{ |
20 | #endif |
21 | |
22 | /* Here distance_code is an intermediate code, i.e. one of the special codes or |
23 | the actual distance increased by BROTLI_NUM_DISTANCE_SHORT_CODES - 1. */ |
24 | static BROTLI_INLINE void PrefixEncodeCopyDistance(size_t distance_code, |
25 | size_t num_direct_codes, |
26 | size_t postfix_bits, |
27 | uint16_t* code, |
28 | uint32_t* extra_bits) { |
29 | if (distance_code < BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_codes) { |
30 | *code = (uint16_t)distance_code; |
31 | *extra_bits = 0; |
32 | return; |
33 | } else { |
34 | size_t dist = ((size_t)1 << (postfix_bits + 2u)) + |
35 | (distance_code - BROTLI_NUM_DISTANCE_SHORT_CODES - num_direct_codes); |
36 | size_t bucket = Log2FloorNonZero(dist) - 1; |
37 | size_t postfix_mask = (1u << postfix_bits) - 1; |
38 | size_t postfix = dist & postfix_mask; |
39 | size_t prefix = (dist >> bucket) & 1; |
40 | size_t offset = (2 + prefix) << bucket; |
41 | size_t nbits = bucket - postfix_bits; |
42 | *code = (uint16_t)((nbits << 10) | |
43 | (BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_codes + |
44 | ((2 * (nbits - 1) + prefix) << postfix_bits) + postfix)); |
45 | *extra_bits = (uint32_t)((dist - offset) >> postfix_bits); |
46 | } |
47 | } |
48 | |
49 | #if defined(__cplusplus) || defined(c_plusplus) |
50 | } /* extern "C" */ |
51 | #endif |
52 | |
53 | #endif /* BROTLI_ENC_PREFIX_H_ */ |
54 |