github.com/andybalholm/brotli@v1.0.6/prefix.go (about)

     1  package brotli
     2  
     3  /* Copyright 2013 Google Inc. All Rights Reserved.
     4  
     5     Distributed under MIT license.
     6     See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
     7  */
     8  
     9  /* Functions for encoding of integers into prefix codes the amount of extra
    10     bits, and the actual values of the extra bits. */
    11  
    12  /* Here distance_code is an intermediate code, i.e. one of the special codes or
    13     the actual distance increased by BROTLI_NUM_DISTANCE_SHORT_CODES - 1. */
    14  func prefixEncodeCopyDistance(distance_code uint, num_direct_codes uint, postfix_bits uint, code *uint16, extra_bits *uint32) {
    15  	if distance_code < numDistanceShortCodes+num_direct_codes {
    16  		*code = uint16(distance_code)
    17  		*extra_bits = 0
    18  		return
    19  	} else {
    20  		var dist uint = (uint(1) << (postfix_bits + 2)) + (distance_code - numDistanceShortCodes - num_direct_codes)
    21  		var bucket uint = uint(log2FloorNonZero(dist) - 1)
    22  		var postfix_mask uint = (1 << postfix_bits) - 1
    23  		var postfix uint = dist & postfix_mask
    24  		var prefix uint = (dist >> bucket) & 1
    25  		var offset uint = (2 + prefix) << bucket
    26  		var nbits uint = bucket - postfix_bits
    27  		*code = uint16(nbits<<10 | (numDistanceShortCodes + num_direct_codes + ((2*(nbits-1) + prefix) << postfix_bits) + postfix))
    28  		*extra_bits = uint32((dist - offset) >> postfix_bits)
    29  	}
    30  }