prefix.h 1.8 KB

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