2015-11-27 10:27:11 +00:00
|
|
|
/* Copyright 2013 Google Inc. All Rights Reserved.
|
|
|
|
|
2015-12-11 10:11:51 +00:00
|
|
|
Distributed under MIT license.
|
2015-11-27 10:27:11 +00:00
|
|
|
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
|
|
|
*/
|
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Functions for encoding of integers into prefix codes the amount of extra
|
|
|
|
bits, and the actual values of the extra bits. */
|
2013-10-23 11:06:13 +00:00
|
|
|
|
|
|
|
#ifndef BROTLI_ENC_PREFIX_H_
|
|
|
|
#define BROTLI_ENC_PREFIX_H_
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#include "../common/constants.h"
|
|
|
|
#include "../common/port.h"
|
2016-06-03 08:51:04 +00:00
|
|
|
#include "../common/types.h"
|
2014-10-28 12:25:22 +00:00
|
|
|
#include "./fast_log.h"
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#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;
|
2014-10-28 12:25:22 +00:00
|
|
|
*extra_bits = 0;
|
|
|
|
return;
|
2016-06-13 09:01:04 +00:00
|
|
|
} else {
|
|
|
|
size_t dist = (1u << (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));
|
2014-10-28 12:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} /* extern "C" */
|
|
|
|
#endif
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
#endif /* BROTLI_ENC_PREFIX_H_ */
|