Debian Codegraph @

flate/fast_encoder.go

raw
1 // Copyright 2011 The Snappy-Go Authors. All rights reserved.
2 // Modified for deflate by Klaus Post (c) 2015.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 package flate
7
8 import (
9 "fmt"
10
11 "github.com/klauspost/compress/internal/le"
12 "github.com/klauspost/compress/internal/regmask"
13 )
14
15 type fastEnc interface {
16 Encode(dst *tokens, src []byte)
17 Reset()
18 }
19
20 func newFastEnc(level int) fastEnc {
21 switch level {
22 case 1:
23 return &fastEncL1{fastGen: fastGen{cur: maxStoreBlockSize}}
24 case 2:
25 return &fastEncL2{fastGen: fastGen{cur: maxStoreBlockSize}}
26 case 3:
27 return &fastEncL3{fastGen: fastGen{cur: maxStoreBlockSize}}
28 case 4:
29 return &fastEncL4{fastGen: fastGen{cur: maxStoreBlockSize}}
30 case 5:
31 return &fastEncL5{fastGen: fastGen{cur: maxStoreBlockSize}}
32 case 6:
33 return &fastEncL6{fastGen: fastGen{cur: maxStoreBlockSize}}
34 default:
35 panic("invalid level specified")
36 }
37 }
38
39 const (
40 tableBits = 15 // Bits used in the table
41 tableSize = 1 << tableBits // Size of the table
42 tableShift = 32 - tableBits // Right-shift to get the tableBits most significant bits of a uint32.
43 baseMatchOffset = 1 // The smallest match offset
44 baseMatchLength = 3 // The smallest match length per the RFC section 3.2.5
45 maxMatchOffset = 1 << 15 // The largest match offset
46
47 bTableBits = 17 // Bits used in the big tables
48 bTableSize = 1 << bTableBits // Size of the table
49 allocHistory = maxStoreBlockSize * 5 // Size to preallocate for history.
50 bufferReset = (1 << 31) - allocHistory - maxStoreBlockSize - 1 // Reset the buffer offset when reaching this.
51 )
52
53 const (
54 prime3bytes = 506832829
55 prime4bytes = 2654435761
56 prime5bytes = 889523592379
57 prime6bytes = 227718039650203
58 prime7bytes = 58295818150454627
59 prime8bytes = 0xcf1bbcdcb7a56463
60 )
61
62 func load3232(b []byte, i int32) uint32 {
63 return le.Load32(b, i)
64 }
65
66 func load6432(b []byte, i int32) uint64 {
67 return le.Load64(b, i)
68 }
69
70 type tableEntry struct {
71 offset int32
72 }
73
74 // fastGen maintains the table for matches,
75 // and the previous byte block for level 2.
76 // This is the generic implementation.
77 type fastGen struct {
78 hist []byte
79 cur int32
80 }
81
82 func (e *fastGen) addBlock(src []byte) int32 {
83 // check if we have space already
84 if len(e.hist)+len(src) > cap(e.hist) {
85 if cap(e.hist) == 0 {
86 e.hist = make([]byte, 0, allocHistory)
87 } else {
88 if cap(e.hist) < maxMatchOffset*2 {
89 panic("unexpected buffer size")
90 }
91 // Move down
92 offset := int32(len(e.hist)) - maxMatchOffset
93 // copy(e.hist[0:maxMatchOffset], e.hist[offset:])
94 *(*[maxMatchOffset]byte)(e.hist) = *(*[maxMatchOffset]byte)(e.hist[offset:])
95 e.cur += offset
96 e.hist = e.hist[:maxMatchOffset]
97 }
98 }
99 s := int32(len(e.hist))
100 e.hist = append(e.hist, src...)
101 return s
102 }
103
104 type tableEntryPrev struct {
105 Cur tableEntry
106 Prev tableEntry
107 }
108
109 // hash7 returns the hash of the lowest 7 bytes of u to fit in a hash table with h bits.
110 // Preferably h should be a constant and should always be <64.
111 func hash7(u uint64, h uint8) uint32 {
112 return uint32(((u << (64 - 56)) * prime7bytes) >> ((64 - h) & regmask.Shift64ByUint8))
113 }
114
115 // hashLen returns a hash of the lowest mls bytes of with length output bits.
116 // mls must be >=3 and <=8. Any other value will return hash for 4 bytes.
117 // length should always be < 32.
118 // Preferably length and mls should be a constant for inlining.
119 func hashLen(u uint64, length, mls uint8) uint32 {
120 switch mls {
121 case 3:
122 return (uint32(u<<8) * prime3bytes) >> (32 - length)
123 case 5:
124 return uint32(((u << (64 - 40)) * prime5bytes) >> (64 - length))
125 case 6:
126 return uint32(((u << (64 - 48)) * prime6bytes) >> (64 - length))
127 case 7:
128 return uint32(((u << (64 - 56)) * prime7bytes) >> (64 - length))
129 case 8:
130 return uint32((u * prime8bytes) >> (64 - length))
131 default:
132 return (uint32(u) * prime4bytes) >> (32 - length)
133 }
134 }
135
136 // matchlen will return the match length between offsets and t in src.
137 // The maximum length returned is maxMatchLength - 4.
138 // It is assumed that s > t, that t >=0 and s < len(src).
139 func (e *fastGen) matchlen(s, t int, src []byte) int32 {
140 if debugDeflate {
141 if t >= s {
142 panic(fmt.Sprint("t >=s:", t, s))
143 }
144 if int(s) >= len(src) {
145 panic(fmt.Sprint("s >= len(src):", s, len(src)))
146 }
147 if t < 0 {
148 panic(fmt.Sprint("t < 0:", t))
149 }
150 if s-t > maxMatchOffset {
151 panic(fmt.Sprint(s, "-", t, "(", s-t, ") > maxMatchLength (", maxMatchOffset, ")"))
152 }
153 }
154 a := src[s:min(s+maxMatchLength-4, len(src))]
155 b := src[t:]
156 return int32(matchLen(a, b))
157 }
158
159 // matchlenLong will return the match length between offsets and t in src.
160 // It is assumed that s > t, that t >=0 and s < len(src).
161 func (e *fastGen) matchlenLong(s, t int, src []byte) int32 {
162 if debugDeflate {
163 if t >= s {
164 panic(fmt.Sprint("t >=s:", t, s))
165 }
166 if int(s) >= len(src) {
167 panic(fmt.Sprint("s >= len(src):", s, len(src)))
168 }
169 if t < 0 {
170 panic(fmt.Sprint("t < 0:", t))
171 }
172 if s-t > maxMatchOffset {
173 panic(fmt.Sprint(s, "-", t, "(", s-t, ") > maxMatchLength (", maxMatchOffset, ")"))
174 }
175 }
176 return int32(matchLen(src[s:], src[t:]))
177 }
178
179 // Reset the encoding table.
180 func (e *fastGen) Reset() {
181 if cap(e.hist) < allocHistory {
182 e.hist = make([]byte, 0, allocHistory)
183 }
184 // We offset current position so everything will be out of reach.
185 // If we are above the buffer reset it will be cleared anyway since len(hist) == 0.
186 if e.cur <= bufferReset {
187 e.cur += maxMatchOffset + int32(len(e.hist))
188 }
189 e.hist = e.hist[:0]
190 }