fast_encoder.go (6185B)
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 "encoding/binary" 10 "fmt" 11 "math/bits" 12 ) 13 14 type fastEnc interface { 15 Encode(dst *tokens, src []byte) 16 Reset() 17 } 18 19 func newFastEnc(level int) fastEnc { 20 switch level { 21 case 1: 22 return &fastEncL1{fastGen: fastGen{cur: maxStoreBlockSize}} 23 case 2: 24 return &fastEncL2{fastGen: fastGen{cur: maxStoreBlockSize}} 25 case 3: 26 return &fastEncL3{fastGen: fastGen{cur: maxStoreBlockSize}} 27 case 4: 28 return &fastEncL4{fastGen: fastGen{cur: maxStoreBlockSize}} 29 case 5: 30 return &fastEncL5{fastGen: fastGen{cur: maxStoreBlockSize}} 31 case 6: 32 return &fastEncL6{fastGen: fastGen{cur: maxStoreBlockSize}} 33 default: 34 panic("invalid level specified") 35 } 36 } 37 38 const ( 39 tableBits = 15 // Bits used in the table 40 tableSize = 1 << tableBits // Size of the table 41 tableShift = 32 - tableBits // Right-shift to get the tableBits most significant bits of a uint32. 42 baseMatchOffset = 1 // The smallest match offset 43 baseMatchLength = 3 // The smallest match length per the RFC section 3.2.5 44 maxMatchOffset = 1 << 15 // The largest match offset 45 46 bTableBits = 17 // Bits used in the big tables 47 bTableSize = 1 << bTableBits // Size of the table 48 allocHistory = maxStoreBlockSize * 5 // Size to preallocate for history. 49 bufferReset = (1 << 31) - allocHistory - maxStoreBlockSize - 1 // Reset the buffer offset when reaching this. 50 ) 51 52 const ( 53 prime3bytes = 506832829 54 prime4bytes = 2654435761 55 prime5bytes = 889523592379 56 prime6bytes = 227718039650203 57 prime7bytes = 58295818150454627 58 prime8bytes = 0xcf1bbcdcb7a56463 59 ) 60 61 func load3232(b []byte, i int32) uint32 { 62 return binary.LittleEndian.Uint32(b[i:]) 63 } 64 65 func load6432(b []byte, i int32) uint64 { 66 return binary.LittleEndian.Uint64(b[i:]) 67 } 68 69 type tableEntry struct { 70 offset int32 71 } 72 73 // fastGen maintains the table for matches, 74 // and the previous byte block for level 2. 75 // This is the generic implementation. 76 type fastGen struct { 77 hist []byte 78 cur int32 79 } 80 81 func (e *fastGen) addBlock(src []byte) int32 { 82 // check if we have space already 83 if len(e.hist)+len(src) > cap(e.hist) { 84 if cap(e.hist) == 0 { 85 e.hist = make([]byte, 0, allocHistory) 86 } else { 87 if cap(e.hist) < maxMatchOffset*2 { 88 panic("unexpected buffer size") 89 } 90 // Move down 91 offset := int32(len(e.hist)) - maxMatchOffset 92 // copy(e.hist[0:maxMatchOffset], e.hist[offset:]) 93 *(*[maxMatchOffset]byte)(e.hist) = *(*[maxMatchOffset]byte)(e.hist[offset:]) 94 e.cur += offset 95 e.hist = e.hist[:maxMatchOffset] 96 } 97 } 98 s := int32(len(e.hist)) 99 e.hist = append(e.hist, src...) 100 return s 101 } 102 103 type tableEntryPrev struct { 104 Cur tableEntry 105 Prev tableEntry 106 } 107 108 // hash7 returns the hash of the lowest 7 bytes of u to fit in a hash table with h bits. 109 // Preferably h should be a constant and should always be <64. 110 func hash7(u uint64, h uint8) uint32 { 111 return uint32(((u << (64 - 56)) * prime7bytes) >> ((64 - h) & reg8SizeMask64)) 112 } 113 114 // hashLen returns a hash of the lowest mls bytes of with length output bits. 115 // mls must be >=3 and <=8. Any other value will return hash for 4 bytes. 116 // length should always be < 32. 117 // Preferably length and mls should be a constant for inlining. 118 func hashLen(u uint64, length, mls uint8) uint32 { 119 switch mls { 120 case 3: 121 return (uint32(u<<8) * prime3bytes) >> (32 - length) 122 case 5: 123 return uint32(((u << (64 - 40)) * prime5bytes) >> (64 - length)) 124 case 6: 125 return uint32(((u << (64 - 48)) * prime6bytes) >> (64 - length)) 126 case 7: 127 return uint32(((u << (64 - 56)) * prime7bytes) >> (64 - length)) 128 case 8: 129 return uint32((u * prime8bytes) >> (64 - length)) 130 default: 131 return (uint32(u) * prime4bytes) >> (32 - length) 132 } 133 } 134 135 // matchlen will return the match length between offsets and t in src. 136 // The maximum length returned is maxMatchLength - 4. 137 // It is assumed that s > t, that t >=0 and s < len(src). 138 func (e *fastGen) matchlen(s, t int32, src []byte) int32 { 139 if debugDecode { 140 if t >= s { 141 panic(fmt.Sprint("t >=s:", t, s)) 142 } 143 if int(s) >= len(src) { 144 panic(fmt.Sprint("s >= len(src):", s, len(src))) 145 } 146 if t < 0 { 147 panic(fmt.Sprint("t < 0:", t)) 148 } 149 if s-t > maxMatchOffset { 150 panic(fmt.Sprint(s, "-", t, "(", s-t, ") > maxMatchLength (", maxMatchOffset, ")")) 151 } 152 } 153 s1 := int(s) + maxMatchLength - 4 154 if s1 > len(src) { 155 s1 = len(src) 156 } 157 158 // Extend the match to be as long as possible. 159 return int32(matchLen(src[s:s1], src[t:])) 160 } 161 162 // matchlenLong will return the match length between offsets and t in src. 163 // It is assumed that s > t, that t >=0 and s < len(src). 164 func (e *fastGen) matchlenLong(s, t int32, src []byte) int32 { 165 if debugDeflate { 166 if t >= s { 167 panic(fmt.Sprint("t >=s:", t, s)) 168 } 169 if int(s) >= len(src) { 170 panic(fmt.Sprint("s >= len(src):", s, len(src))) 171 } 172 if t < 0 { 173 panic(fmt.Sprint("t < 0:", t)) 174 } 175 if s-t > maxMatchOffset { 176 panic(fmt.Sprint(s, "-", t, "(", s-t, ") > maxMatchLength (", maxMatchOffset, ")")) 177 } 178 } 179 // Extend the match to be as long as possible. 180 return int32(matchLen(src[s:], src[t:])) 181 } 182 183 // Reset the encoding table. 184 func (e *fastGen) Reset() { 185 if cap(e.hist) < allocHistory { 186 e.hist = make([]byte, 0, allocHistory) 187 } 188 // We offset current position so everything will be out of reach. 189 // If we are above the buffer reset it will be cleared anyway since len(hist) == 0. 190 if e.cur <= bufferReset { 191 e.cur += maxMatchOffset + int32(len(e.hist)) 192 } 193 e.hist = e.hist[:0] 194 } 195 196 // matchLen returns the maximum length. 197 // 'a' must be the shortest of the two. 198 func matchLen(a, b []byte) int { 199 var checked int 200 201 for len(a) >= 8 { 202 if diff := binary.LittleEndian.Uint64(a) ^ binary.LittleEndian.Uint64(b); diff != 0 { 203 return checked + (bits.TrailingZeros64(diff) >> 3) 204 } 205 checked += 8 206 a = a[8:] 207 b = b[8:] 208 } 209 b = b[:len(a)] 210 for i := range a { 211 if a[i] != b[i] { 212 return i + checked 213 } 214 } 215 return len(a) + checked 216 }