diff options
Diffstat (limited to 'vendor/github.com/ulikunitz/xz/lzma/prob.go')
-rw-r--r-- | vendor/github.com/ulikunitz/xz/lzma/prob.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/vendor/github.com/ulikunitz/xz/lzma/prob.go b/vendor/github.com/ulikunitz/xz/lzma/prob.go new file mode 100644 index 0000000..24d50ec --- /dev/null +++ b/vendor/github.com/ulikunitz/xz/lzma/prob.go | |||
@@ -0,0 +1,53 @@ | |||
1 | // Copyright 2014-2017 Ulrich Kunitz. All rights reserved. | ||
2 | // Use of this source code is governed by a BSD-style | ||
3 | // license that can be found in the LICENSE file. | ||
4 | |||
5 | package lzma | ||
6 | |||
7 | // movebits defines the number of bits used for the updates of probability | ||
8 | // values. | ||
9 | const movebits = 5 | ||
10 | |||
11 | // probbits defines the number of bits of a probability value. | ||
12 | const probbits = 11 | ||
13 | |||
14 | // probInit defines 0.5 as initial value for prob values. | ||
15 | const probInit prob = 1 << (probbits - 1) | ||
16 | |||
17 | // Type prob represents probabilities. The type can also be used to encode and | ||
18 | // decode single bits. | ||
19 | type prob uint16 | ||
20 | |||
21 | // Dec decreases the probability. The decrease is proportional to the | ||
22 | // probability value. | ||
23 | func (p *prob) dec() { | ||
24 | *p -= *p >> movebits | ||
25 | } | ||
26 | |||
27 | // Inc increases the probability. The Increase is proportional to the | ||
28 | // difference of 1 and the probability value. | ||
29 | func (p *prob) inc() { | ||
30 | *p += ((1 << probbits) - *p) >> movebits | ||
31 | } | ||
32 | |||
33 | // Computes the new bound for a given range using the probability value. | ||
34 | func (p prob) bound(r uint32) uint32 { | ||
35 | return (r >> probbits) * uint32(p) | ||
36 | } | ||
37 | |||
38 | // Bits returns 1. One is the number of bits that can be encoded or decoded | ||
39 | // with a single prob value. | ||
40 | func (p prob) Bits() int { | ||
41 | return 1 | ||
42 | } | ||
43 | |||
44 | // Encode encodes the least-significant bit of v. Note that the p value will be | ||
45 | // changed. | ||
46 | func (p *prob) Encode(e *rangeEncoder, v uint32) error { | ||
47 | return e.EncodeBit(v, p) | ||
48 | } | ||
49 | |||
50 | // Decode decodes a single bit. Note that the p value will change. | ||
51 | func (p *prob) Decode(d *rangeDecoder) (v uint32, err error) { | ||
52 | return d.DecodeBit(p) | ||
53 | } | ||