]>
Commit | Line | Data |
---|---|---|
11b7501a SB |
1 | /* Copyright 2013 Google Inc. All Rights Reserved.\r |
2 | \r | |
3 | Distributed under MIT license.\r | |
4 | See file LICENSE for detail or copy at https://opensource.org/licenses/MIT\r | |
5 | */\r | |
6 | \r | |
7 | /* Functions for encoding of integers into prefix codes the amount of extra\r | |
8 | bits, and the actual values of the extra bits. */\r | |
9 | \r | |
10 | #ifndef BROTLI_ENC_PREFIX_H_\r | |
11 | #define BROTLI_ENC_PREFIX_H_\r | |
12 | \r | |
13 | #include "../common/constants.h"\r | |
dd4f667e LG |
14 | #include "../common/platform.h"\r |
15 | #include <brotli/types.h>\r | |
11b7501a SB |
16 | #include "./fast_log.h"\r |
17 | \r | |
18 | #if defined(__cplusplus) || defined(c_plusplus)\r | |
19 | extern "C" {\r | |
20 | #endif\r | |
21 | \r | |
dd4f667e LG |
22 | /* Here distance_code is an intermediate code, i.e. one of the special codes or\r |
23 | the actual distance increased by BROTLI_NUM_DISTANCE_SHORT_CODES - 1. */\r | |
11b7501a SB |
24 | static BROTLI_INLINE void PrefixEncodeCopyDistance(size_t distance_code,\r |
25 | size_t num_direct_codes,\r | |
26 | size_t postfix_bits,\r | |
27 | uint16_t* code,\r | |
28 | uint32_t* extra_bits) {\r | |
29 | if (distance_code < BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_codes) {\r | |
30 | *code = (uint16_t)distance_code;\r | |
31 | *extra_bits = 0;\r | |
32 | return;\r | |
33 | } else {\r | |
34 | size_t dist = ((size_t)1 << (postfix_bits + 2u)) +\r | |
35 | (distance_code - BROTLI_NUM_DISTANCE_SHORT_CODES - num_direct_codes);\r | |
36 | size_t bucket = Log2FloorNonZero(dist) - 1;\r | |
37 | size_t postfix_mask = (1u << postfix_bits) - 1;\r | |
38 | size_t postfix = dist & postfix_mask;\r | |
39 | size_t prefix = (dist >> bucket) & 1;\r | |
40 | size_t offset = (2 + prefix) << bucket;\r | |
41 | size_t nbits = bucket - postfix_bits;\r | |
dd4f667e | 42 | *code = (uint16_t)((nbits << 10) |\r |
11b7501a SB |
43 | (BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_codes +\r |
44 | ((2 * (nbits - 1) + prefix) << postfix_bits) + postfix));\r | |
dd4f667e | 45 | *extra_bits = (uint32_t)((dist - offset) >> postfix_bits);\r |
11b7501a SB |
46 | }\r |
47 | }\r | |
48 | \r | |
49 | #if defined(__cplusplus) || defined(c_plusplus)\r | |
50 | } /* extern "C" */\r | |
51 | #endif\r | |
52 | \r | |
53 | #endif /* BROTLI_ENC_PREFIX_H_ */\r |