prefix.go 1.3 KB

123456789101112131415161718192021222324252627282930
  1. package brotli
  2. /* 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
  5. */
  6. /* Functions for encoding of integers into prefix codes the amount of extra
  7. bits, and the actual values of the extra bits. */
  8. /* Here distance_code is an intermediate code, i.e. one of the special codes or
  9. the actual distance increased by BROTLI_NUM_DISTANCE_SHORT_CODES - 1. */
  10. func prefixEncodeCopyDistance(distance_code uint, num_direct_codes uint, postfix_bits uint, code *uint16, extra_bits *uint32) {
  11. if distance_code < numDistanceShortCodes+num_direct_codes {
  12. *code = uint16(distance_code)
  13. *extra_bits = 0
  14. return
  15. } else {
  16. var dist uint = (uint(1) << (postfix_bits + 2)) + (distance_code - numDistanceShortCodes - num_direct_codes)
  17. var bucket uint = uint(log2FloorNonZero(dist) - 1)
  18. var postfix_mask uint = (1 << postfix_bits) - 1
  19. var postfix uint = dist & postfix_mask
  20. var prefix uint = (dist >> bucket) & 1
  21. var offset uint = (2 + prefix) << bucket
  22. var nbits uint = bucket - postfix_bits
  23. *code = uint16(nbits<<10 | (numDistanceShortCodes + num_direct_codes + ((2*(nbits-1) + prefix) << postfix_bits) + postfix))
  24. *extra_bits = uint32((dist - offset) >> postfix_bits)
  25. }
  26. }