File: arithmetic.go

package info (click to toggle)
golang-github-pointlander-compress 1.1.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 324 kB
  • sloc: makefile: 7
file content (198 lines) | stat: -rw-r--r-- 4,361 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package compress

import "io"

func (model Model) Code(out io.Writer) {
	var bits [1]byte
	var mask uint8 = 0x80

	output := func(bit uint16) {
		if bit != 0 {
			bits[0] |= mask
		}
		if mask >>= 1; mask == 0 {
			out.Write(bits[:])
			bits[0], mask = 0, 0x80
		}
	}

	var low, high uint16 = 0, 0xffff
	var underflow uint32
	for current := range model.Input {
		for _, s := range current {
			hl, scale := uint32(high - low) + 1, uint32(s.Scale)
			low, high = low + uint16((hl * uint32(s.Low)) / scale), low + uint16((hl * uint32(s.High)) / scale - 1)

			for {
				if low & 0x8000 == high & 0x8000 {
					output(high & 0x8000)
					for underflow > 0 {
						output(^high & 0x8000)
						underflow--
					}
				} else if low & 0x4000 != 0 && high & 0x4000 == 0 {
					low, high, underflow = low & 0x3fff, high | 0x4000, underflow + 1
				} else {
					break
				}
				low, high = low << 1, (high << 1) | 1
			}
		}
	}

	output(low & 0x4000)
	underflow++
	for underflow > 0 {
		output(^low & 0x4000)
		underflow--
	}
	if mask != 0x80 {
		out.Write(bits[:])
	}
}

func (model Model) Decode(in io.Reader) {
	var bits [1]byte
	var mask uint8 = 0x80
	var code uint16

	input := func() {
		if code <<= 1; bits[0] & mask != 0 {
			code |= 1
		}

		if mask >>= 1; mask == 0 {
			if n, _ := in.Read(bits[:]); n > 0 {
				mask = 0x80
			}
		}
	}

	in.Read(bits[:])
	for i := 0; i < 16; i++ {
		input()
	}

	var low, high uint16 = 0, 0xffff
	hl := uint32(high - low) + 1

	s := model.Output(uint16(((uint32(code - low) + 1) * model.Scale - 1) / hl))
	for s.High != 0 {
		low, high = low + uint16((hl * uint32(s.Low)) / model.Scale), low + uint16((hl * uint32(s.High)) / model.Scale - 1)

		for {
			if low & 0x8000 == high & 0x8000 {

			} else if low & 0x4000 != 0 && high & 0x4000 == 0 {
				low, high, code = low & 0x3fff, high | 0x4000, code ^ 0x4000
			} else {
				hl, model.Scale = uint32(high - low) + 1, uint32(s.Scale)
				break
			}
			input()
			low, high = low << 1, (high << 1) | 1
		}

		s = model.Output(uint16(((uint32(code - low) + 1) * model.Scale - 1) / hl))
	}
}

func (model Model32) Code(out io.Writer) {
	var bits [1]byte
	var mask uint8 = 0x80

	output := func(bit uint32) {
		if bit != 0 {
			bits[0] |= mask
		}
		if mask >>= 1; mask == 0 {
			out.Write(bits[:])
			bits[0], mask = 0, 0x80
		}
	}

	var low, high uint32 = 0, 0xffffffff
	var underflow uint32

	for current := range model.Input {
		for _, s := range current {
			hl, scale := uint64(high - low) + 1, uint64(s.Scale)
			low, high = low + uint32((hl * uint64(s.Low)) / scale), low + uint32((hl * uint64(s.High)) / scale - 1)

			for {
				if low & 0x80000000 == high & 0x80000000 {
					output(high & 0x80000000)
					for underflow > 0 {
						output(^high & 0x80000000)
						underflow--
					}
				} else if low & 0x40000000 != 0 && high & 0x40000000 == 0 {
					low, high, underflow = low & 0x3fffffff, high | 0x40000000, underflow + 1
				} else {
					break
				}
				low, high = low << 1, (high << 1) | 1
			}
		}
	}

	output(low & 0x40000000)
	underflow++
	for underflow > 0 {
		output(^low & 0x40000000)
		underflow--
	}
	if mask != 0x80 {
		out.Write(bits[:])
	}
}

func (model Model32) Decode(in io.Reader) {
	var bits [1]byte
	var mask uint8 = 0x80
	var code uint32

	input := func() {
		if code <<= 1; bits[0] & mask != 0 {
			code |= 1
		}

		if mask >>= 1; mask == 0 {
			if n, _ := in.Read(bits[:]); n > 0 {
				mask = 0x80
			}
		}
	}

	in.Read(bits[:])
	for i := 0; i < 32; i++ {
		input()
	}

	var low, high uint32 = 0, 0xffffffff
	hl := uint64(high - low) + 1

	s := model.Output(uint32(((uint64(code - low) + 1) * model.Scale - 1) / hl))
	for s.High != 0 {
		low, high = low + uint32((hl * uint64(s.Low)) / model.Scale), low + uint32((hl * uint64(s.High)) / model.Scale - 1)

		for {
			if low & 0x80000000 == high & 0x80000000 {

			} else if low & 0x40000000 != 0 && high & 0x40000000 == 0 {
				low, high, code = low & 0x3fffffff, high | 0x40000000, code ^ 0x40000000
			} else {
				hl, model.Scale = uint64(high - low) + 1, uint64(s.Scale)
				break
			}
			input()
			low, high = low << 1, (high << 1) | 1
		}

		s = model.Output(uint32(((uint64(code - low) + 1) * model.Scale - 1) / hl))
	}
}