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
5package lzma
6
7// movebits defines the number of bits used for the updates of probability
8// values.
9const movebits = 5
10
11// probbits defines the number of bits of a probability value.
12const probbits = 11
13
14// probInit defines 0.5 as initial value for prob values.
15const probInit prob = 1 << (probbits - 1)
16
17// Type prob represents probabilities. The type can also be used to encode and
18// decode single bits.
19type prob uint16
20
21// Dec decreases the probability. The decrease is proportional to the
22// probability value.
23func (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.
29func (p *prob) inc() {
30	*p += ((1 << probbits) - *p) >> movebits
31}
32
33// Computes the new bound for a given range using the probability value.
34func (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.
40func (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.
46func (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.
51func (p *prob) Decode(d *rangeDecoder) (v uint32, err error) {
52	return d.DecodeBit(p)
53}
54