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