summaryrefslogtreecommitdiff
path: root/BaseTools/Source/C/BrotliCompress/enc/prefix.h
blob: 850c21e54bc1db522be1f3cf80a1f811061d8174 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* Copyright 2013 Google Inc. All Rights Reserved.

   Distributed under MIT license.
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/

/* Functions for encoding of integers into prefix codes the amount of extra
   bits, and the actual values of the extra bits. */

#ifndef BROTLI_ENC_PREFIX_H_
#define BROTLI_ENC_PREFIX_H_

#include "../common/constants.h"
#include "../common/port.h"
#include "../common/types.h"
#include "./fast_log.h"

#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif

static BROTLI_INLINE void PrefixEncodeCopyDistance(size_t distance_code,
                                                   size_t num_direct_codes,
                                                   size_t postfix_bits,
                                                   uint16_t* code,
                                                   uint32_t* extra_bits) {
  if (distance_code < BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_codes) {
    *code = (uint16_t)distance_code;
    *extra_bits = 0;
    return;
  } else {
    size_t dist = ((size_t)1 << (postfix_bits + 2u)) +
        (distance_code - BROTLI_NUM_DISTANCE_SHORT_CODES - num_direct_codes);
    size_t bucket = Log2FloorNonZero(dist) - 1;
    size_t postfix_mask = (1u << postfix_bits) - 1;
    size_t postfix = dist & postfix_mask;
    size_t prefix = (dist >> bucket) & 1;
    size_t offset = (2 + prefix) << bucket;
    size_t nbits = bucket - postfix_bits;
    *code = (uint16_t)(
        (BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_codes +
         ((2 * (nbits - 1) + prefix) << postfix_bits) + postfix));
    *extra_bits = (uint32_t)(
        (nbits << 24) | ((dist - offset) >> postfix_bits));
  }
}

#if defined(__cplusplus) || defined(c_plusplus)
}  /* extern "C" */
#endif

#endif  /* BROTLI_ENC_PREFIX_H_ */