source: code/trunk/vendor/github.com/andybalholm/brotli/write_bits.go@ 145

Last change on this file since 145 was 145, checked in by Izuru Yakumo, 22 months ago

Updated the Makefile and vendored depedencies

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

File size: 1.5 KB
Line 
1package brotli
2
3import "encoding/binary"
4
5/* Copyright 2010 Google Inc. All Rights Reserved.
6
7 Distributed under MIT license.
8 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
9*/
10
11/* Write bits into a byte array. */
12
13/* This function writes bits into bytes in increasing addresses, and within
14 a byte least-significant-bit first.
15
16 The function can write up to 56 bits in one go with WriteBits
17 Example: let's assume that 3 bits (Rs below) have been written already:
18
19 BYTE-0 BYTE+1 BYTE+2
20
21 0000 0RRR 0000 0000 0000 0000
22
23 Now, we could write 5 or less bits in MSB by just sifting by 3
24 and OR'ing to BYTE-0.
25
26 For n bits, we take the last 5 bits, OR that with high bits in BYTE-0,
27 and locate the rest in BYTE+1, BYTE+2, etc. */
28func writeBits(n_bits uint, bits uint64, pos *uint, array []byte) {
29 /* This branch of the code can write up to 56 bits at a time,
30 7 bits are lost by being perhaps already in *p and at least
31 1 bit is needed to initialize the bit-stream ahead (i.e. if 7
32 bits are in *p and we write 57 bits, then the next write will
33 access a byte that was never initialized). */
34 p := array[*pos>>3:]
35 v := uint64(p[0])
36 v |= bits << (*pos & 7)
37 binary.LittleEndian.PutUint64(p, v)
38 *pos += n_bits
39}
40
41func writeSingleBit(bit bool, pos *uint, array []byte) {
42 if bit {
43 writeBits(1, 1, pos, array)
44 } else {
45 writeBits(1, 0, pos, array)
46 }
47}
48
49func writeBitsPrepareStorage(pos uint, array []byte) {
50 assert(pos&7 == 0)
51 array[pos>>3] = 0
52}
Note: See TracBrowser for help on using the repository browser.