1 /* Copyright 2013 Google Inc. All Rights Reserved.
3 Distributed under MIT license.
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
7 /* Functions for encoding of integers into prefix codes the amount of extra
8 bits, and the actual values of the extra bits. */
10 #ifndef BROTLI_ENC_PREFIX_H_
11 #define BROTLI_ENC_PREFIX_H_
13 #include "../common/constants.h"
14 #include "../common/port.h"
15 #include "../common/types.h"
16 #include "./fast_log.h"
18 #if defined(__cplusplus) || defined(c_plusplus)
22 static BROTLI_INLINE
void PrefixEncodeCopyDistance(size_t distance_code
,
23 size_t num_direct_codes
,
26 uint32_t* extra_bits
) {
27 if (distance_code
< BROTLI_NUM_DISTANCE_SHORT_CODES
+ num_direct_codes
) {
28 *code
= (uint16_t)distance_code
;
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
;
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
));
48 #if defined(__cplusplus) || defined(c_plusplus)
52 #endif /* BROTLI_ENC_PREFIX_H_ */