| 1 |
//go:build (!amd64 && !arm64) || appengine || !gc || noasm |
| 2 |
|
| 3 |
package s2 |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"math/bits" |
| 8 |
) |
| 9 |
|
| 10 |
const hasAsm = false |
| 11 |
|
| 12 |
// encodeBlock encodes a non-empty src to a guaranteed-large-enough dst. It |
| 13 |
// assumes that the varint-encoded length of the decompressed bytes has already |
| 14 |
// been written. |
| 15 |
// |
| 16 |
// It also assumes that: |
| 17 |
// |
| 18 |
// len(dst) >= MaxEncodedLen(len(src)) |
| 19 |
func encodeBlock(dst, src []byte) (d int) { |
| 20 |
if len(src) < minNonLiteralBlockSize { |
| 21 |
return 0 |
| 22 |
} |
| 23 |
if len(src) <= 64<<10 { |
| 24 |
return encodeBlockGo64K(dst, src) |
| 25 |
} |
| 26 |
return encodeBlockGo(dst, src) |
| 27 |
} |
| 28 |
|
| 29 |
// encodeBlockBetter encodes a non-empty src to a guaranteed-large-enough dst. It |
| 30 |
// assumes that the varint-encoded length of the decompressed bytes has already |
| 31 |
// been written. |
| 32 |
// |
| 33 |
// It also assumes that: |
| 34 |
// |
| 35 |
// len(dst) >= MaxEncodedLen(len(src)) |
| 36 |
func encodeBlockBetter(dst, src []byte) (d int) { |
| 37 |
if len(src) <= 64<<10 { |
| 38 |
return encodeBlockBetterGo64K(dst, src) |
| 39 |
} |
| 40 |
return encodeBlockBetterGo(dst, src) |
| 41 |
} |
| 42 |
|
| 43 |
// encodeBlockBetter encodes a non-empty src to a guaranteed-large-enough dst. It |
| 44 |
// assumes that the varint-encoded length of the decompressed bytes has already |
| 45 |
// been written. |
| 46 |
// |
| 47 |
// It also assumes that: |
| 48 |
// |
| 49 |
// len(dst) >= MaxEncodedLen(len(src)) |
| 50 |
func encodeBlockBetterSnappy(dst, src []byte) (d int) { |
| 51 |
if len(src) <= 64<<10 { |
| 52 |
return encodeBlockBetterSnappyGo64K(dst, src) |
| 53 |
} |
| 54 |
return encodeBlockBetterSnappyGo(dst, src) |
| 55 |
} |
| 56 |
|
| 57 |
// encodeBlock encodes a non-empty src to a guaranteed-large-enough dst. It |
| 58 |
// assumes that the varint-encoded length of the decompressed bytes has already |
| 59 |
// been written. |
| 60 |
// |
| 61 |
// It also assumes that: |
| 62 |
// |
| 63 |
// len(dst) >= MaxEncodedLen(len(src)) |
| 64 |
func encodeBlockSnappy(dst, src []byte) (d int) { |
| 65 |
if len(src) < minNonLiteralBlockSize { |
| 66 |
return 0 |
| 67 |
} |
| 68 |
if len(src) <= 64<<10 { |
| 69 |
return encodeBlockSnappyGo64K(dst, src) |
| 70 |
} |
| 71 |
return encodeBlockSnappyGo(dst, src) |
| 72 |
} |
| 73 |
|
| 74 |
// emitLiteral writes a literal chunk and returns the number of bytes written. |
| 75 |
// |
| 76 |
// It assumes that: |
| 77 |
// |
| 78 |
// dst is long enough to hold the encoded bytes |
| 79 |
// 0 <= len(lit) && len(lit) <= math.MaxUint32 |
| 80 |
func emitLiteral(dst, lit []byte) int { |
| 81 |
if len(lit) == 0 { |
| 82 |
return 0 |
| 83 |
} |
| 84 |
const num = 63<<2 | tagLiteral |
| 85 |
i, n := 0, uint(len(lit)-1) |
| 86 |
switch { |
| 87 |
case n < 60: |
| 88 |
dst[0] = uint8(n)<<2 | tagLiteral |
| 89 |
i = 1 |
| 90 |
case n < 1<<8: |
| 91 |
dst[1] = uint8(n) |
| 92 |
dst[0] = 60<<2 | tagLiteral |
| 93 |
i = 2 |
| 94 |
case n < 1<<16: |
| 95 |
dst[2] = uint8(n >> 8) |
| 96 |
dst[1] = uint8(n) |
| 97 |
dst[0] = 61<<2 | tagLiteral |
| 98 |
i = 3 |
| 99 |
case n < 1<<24: |
| 100 |
dst[3] = uint8(n >> 16) |
| 101 |
dst[2] = uint8(n >> 8) |
| 102 |
dst[1] = uint8(n) |
| 103 |
dst[0] = 62<<2 | tagLiteral |
| 104 |
i = 4 |
| 105 |
default: |
| 106 |
dst[4] = uint8(n >> 24) |
| 107 |
dst[3] = uint8(n >> 16) |
| 108 |
dst[2] = uint8(n >> 8) |
| 109 |
dst[1] = uint8(n) |
| 110 |
dst[0] = 63<<2 | tagLiteral |
| 111 |
i = 5 |
| 112 |
} |
| 113 |
return i + copy(dst[i:], lit) |
| 114 |
} |
| 115 |
|
| 116 |
// emitRepeat writes a repeat chunk and returns the number of bytes written. |
| 117 |
// Length must be at least 4 and < 1<<24 |
| 118 |
func emitRepeat(dst []byte, offset, length int) int { |
| 119 |
// Repeat offset, make length cheaper |
| 120 |
length -= 4 |
| 121 |
if length <= 4 { |
| 122 |
dst[0] = uint8(length)<<2 | tagCopy1 |
| 123 |
dst[1] = 0 |
| 124 |
return 2 |
| 125 |
} |
| 126 |
if length < 8 && offset < 2048 { |
| 127 |
// Encode WITH offset |
| 128 |
dst[1] = uint8(offset) |
| 129 |
dst[0] = uint8(offset>>8)<<5 | uint8(length)<<2 | tagCopy1 |
| 130 |
return 2 |
| 131 |
} |
| 132 |
if length < (1<<8)+4 { |
| 133 |
length -= 4 |
| 134 |
dst[2] = uint8(length) |
| 135 |
dst[1] = 0 |
| 136 |
dst[0] = 5<<2 | tagCopy1 |
| 137 |
return 3 |
| 138 |
} |
| 139 |
if length < (1<<16)+(1<<8) { |
| 140 |
length -= 1 << 8 |
| 141 |
dst[3] = uint8(length >> 8) |
| 142 |
dst[2] = uint8(length >> 0) |
| 143 |
dst[1] = 0 |
| 144 |
dst[0] = 6<<2 | tagCopy1 |
| 145 |
return 4 |
| 146 |
} |
| 147 |
const maxRepeat = (1 << 24) - 1 |
| 148 |
length -= 1 << 16 |
| 149 |
left := 0 |
| 150 |
if length > maxRepeat { |
| 151 |
left = length - maxRepeat + 4 |
| 152 |
length = maxRepeat - 4 |
| 153 |
} |
| 154 |
dst[4] = uint8(length >> 16) |
| 155 |
dst[3] = uint8(length >> 8) |
| 156 |
dst[2] = uint8(length >> 0) |
| 157 |
dst[1] = 0 |
| 158 |
dst[0] = 7<<2 | tagCopy1 |
| 159 |
if left > 0 { |
| 160 |
return 5 + emitRepeat(dst[5:], offset, left) |
| 161 |
} |
| 162 |
return 5 |
| 163 |
} |
| 164 |
|
| 165 |
// emitCopy writes a copy chunk and returns the number of bytes written. |
| 166 |
// |
| 167 |
// It assumes that: |
| 168 |
// |
| 169 |
// dst is long enough to hold the encoded bytes |
| 170 |
// 1 <= offset && offset <= math.MaxUint32 |
| 171 |
// 4 <= length && length <= 1 << 24 |
| 172 |
func emitCopy(dst []byte, offset, length int) int { |
| 173 |
if offset >= 65536 { |
| 174 |
i := 0 |
| 175 |
if length > 64 { |
| 176 |
// Emit a length 64 copy, encoded as 5 bytes. |
| 177 |
dst[4] = uint8(offset >> 24) |
| 178 |
dst[3] = uint8(offset >> 16) |
| 179 |
dst[2] = uint8(offset >> 8) |
| 180 |
dst[1] = uint8(offset) |
| 181 |
dst[0] = 63<<2 | tagCopy4 |
| 182 |
length -= 64 |
| 183 |
if length >= 4 { |
| 184 |
// Emit remaining as repeats |
| 185 |
return 5 + emitRepeat(dst[5:], offset, length) |
| 186 |
} |
| 187 |
i = 5 |
| 188 |
} |
| 189 |
if length == 0 { |
| 190 |
return i |
| 191 |
} |
| 192 |
// Emit a copy, offset encoded as 4 bytes. |
| 193 |
dst[i+0] = uint8(length-1)<<2 | tagCopy4 |
| 194 |
dst[i+1] = uint8(offset) |
| 195 |
dst[i+2] = uint8(offset >> 8) |
| 196 |
dst[i+3] = uint8(offset >> 16) |
| 197 |
dst[i+4] = uint8(offset >> 24) |
| 198 |
return i + 5 |
| 199 |
} |
| 200 |
|
| 201 |
// Offset no more than 2 bytes. |
| 202 |
if length > 64 { |
| 203 |
off := 3 |
| 204 |
if offset < 2048 { |
| 205 |
// emit 8 bytes as tagCopy1, rest as repeats. |
| 206 |
dst[1] = uint8(offset) |
| 207 |
dst[0] = uint8(offset>>8)<<5 | uint8(8-4)<<2 | tagCopy1 |
| 208 |
length -= 8 |
| 209 |
off = 2 |
| 210 |
} else { |
| 211 |
// Emit a length 60 copy, encoded as 3 bytes. |
| 212 |
// Emit remaining as repeat value (minimum 4 bytes). |
| 213 |
dst[2] = uint8(offset >> 8) |
| 214 |
dst[1] = uint8(offset) |
| 215 |
dst[0] = 59<<2 | tagCopy2 |
| 216 |
length -= 60 |
| 217 |
} |
| 218 |
// Emit remaining as repeats, at least 4 bytes remain. |
| 219 |
return off + emitRepeat(dst[off:], offset, length) |
| 220 |
} |
| 221 |
if length >= 12 || offset >= 2048 { |
| 222 |
// Emit the remaining copy, encoded as 3 bytes. |
| 223 |
dst[2] = uint8(offset >> 8) |
| 224 |
dst[1] = uint8(offset) |
| 225 |
dst[0] = uint8(length-1)<<2 | tagCopy2 |
| 226 |
return 3 |
| 227 |
} |
| 228 |
// Emit the remaining copy, encoded as 2 bytes. |
| 229 |
dst[1] = uint8(offset) |
| 230 |
dst[0] = uint8(offset>>8)<<5 | uint8(length-4)<<2 | tagCopy1 |
| 231 |
return 2 |
| 232 |
} |
| 233 |
|
| 234 |
// emitCopyNoRepeat writes a copy chunk and returns the number of bytes written. |
| 235 |
// |
| 236 |
// It assumes that: |
| 237 |
// |
| 238 |
// dst is long enough to hold the encoded bytes |
| 239 |
// 1 <= offset && offset <= math.MaxUint32 |
| 240 |
// 4 <= length && length <= 1 << 24 |
| 241 |
func emitCopyNoRepeat(dst []byte, offset, length int) int { |
| 242 |
if offset >= 65536 { |
| 243 |
i := 0 |
| 244 |
if length > 64 { |
| 245 |
// Emit a length 64 copy, encoded as 5 bytes. |
| 246 |
dst[4] = uint8(offset >> 24) |
| 247 |
dst[3] = uint8(offset >> 16) |
| 248 |
dst[2] = uint8(offset >> 8) |
| 249 |
dst[1] = uint8(offset) |
| 250 |
dst[0] = 63<<2 | tagCopy4 |
| 251 |
length -= 64 |
| 252 |
if length >= 4 { |
| 253 |
// Emit remaining as repeats |
| 254 |
return 5 + emitCopyNoRepeat(dst[5:], offset, length) |
| 255 |
} |
| 256 |
i = 5 |
| 257 |
} |
| 258 |
if length == 0 { |
| 259 |
return i |
| 260 |
} |
| 261 |
// Emit a copy, offset encoded as 4 bytes. |
| 262 |
dst[i+0] = uint8(length-1)<<2 | tagCopy4 |
| 263 |
dst[i+1] = uint8(offset) |
| 264 |
dst[i+2] = uint8(offset >> 8) |
| 265 |
dst[i+3] = uint8(offset >> 16) |
| 266 |
dst[i+4] = uint8(offset >> 24) |
| 267 |
return i + 5 |
| 268 |
} |
| 269 |
|
| 270 |
// Offset no more than 2 bytes. |
| 271 |
if length > 64 { |
| 272 |
// Emit a length 60 copy, encoded as 3 bytes. |
| 273 |
// Emit remaining as repeat value (minimum 4 bytes). |
| 274 |
dst[2] = uint8(offset >> 8) |
| 275 |
dst[1] = uint8(offset) |
| 276 |
dst[0] = 59<<2 | tagCopy2 |
| 277 |
length -= 60 |
| 278 |
// Emit remaining as repeats, at least 4 bytes remain. |
| 279 |
return 3 + emitCopyNoRepeat(dst[3:], offset, length) |
| 280 |
} |
| 281 |
if length >= 12 || offset >= 2048 { |
| 282 |
// Emit the remaining copy, encoded as 3 bytes. |
| 283 |
dst[2] = uint8(offset >> 8) |
| 284 |
dst[1] = uint8(offset) |
| 285 |
dst[0] = uint8(length-1)<<2 | tagCopy2 |
| 286 |
return 3 |
| 287 |
} |
| 288 |
// Emit the remaining copy, encoded as 2 bytes. |
| 289 |
dst[1] = uint8(offset) |
| 290 |
dst[0] = uint8(offset>>8)<<5 | uint8(length-4)<<2 | tagCopy1 |
| 291 |
return 2 |
| 292 |
} |
| 293 |
|
| 294 |
// matchLen returns how many bytes match in a and b |
| 295 |
// |
| 296 |
// It assumes that: |
| 297 |
// |
| 298 |
// len(a) <= len(b) |
| 299 |
func matchLen(a []byte, b []byte) int { |
| 300 |
b = b[:len(a)] |
| 301 |
var checked int |
| 302 |
if len(a) > 4 { |
| 303 |
// Try 4 bytes first |
| 304 |
if diff := load32(a, 0) ^ load32(b, 0); diff != 0 { |
| 305 |
return bits.TrailingZeros32(diff) >> 3 |
| 306 |
} |
| 307 |
// Switch to 8 byte matching. |
| 308 |
checked = 4 |
| 309 |
a = a[4:] |
| 310 |
b = b[4:] |
| 311 |
for len(a) >= 8 { |
| 312 |
b = b[:len(a)] |
| 313 |
if diff := load64(a, 0) ^ load64(b, 0); diff != 0 { |
| 314 |
return checked + (bits.TrailingZeros64(diff) >> 3) |
| 315 |
} |
| 316 |
checked += 8 |
| 317 |
a = a[8:] |
| 318 |
b = b[8:] |
| 319 |
} |
| 320 |
} |
| 321 |
b = b[:len(a)] |
| 322 |
for i := range a { |
| 323 |
if a[i] != b[i] { |
| 324 |
return int(i) + checked |
| 325 |
} |
| 326 |
} |
| 327 |
return len(a) + checked |
| 328 |
} |
| 329 |
|
| 330 |
// input must be > inputMargin |
| 331 |
func calcBlockSize(src []byte, _ *[32768]byte) (d int) { |
| 332 |
// Initialize the hash table. |
| 333 |
const ( |
| 334 |
tableBits = 13 |
| 335 |
maxTableSize = 1 << tableBits |
| 336 |
) |
| 337 |
|
| 338 |
var table [maxTableSize]uint32 |
| 339 |
|
| 340 |
// sLimit is when to stop looking for offset/length copies. The inputMargin |
| 341 |
// lets us use a fast path for emitLiteral in the main loop, while we are |
| 342 |
// looking for copies. |
| 343 |
sLimit := len(src) - inputMargin |
| 344 |
|
| 345 |
// Bail if we can't compress to at least this. |
| 346 |
dstLimit := len(src) - len(src)>>5 - 5 |
| 347 |
|
| 348 |
// nextEmit is where in src the next emitLiteral should start from. |
| 349 |
nextEmit := 0 |
| 350 |
|
| 351 |
// The encoded form must start with a literal, as there are no previous |
| 352 |
// bytes to copy, so we start looking for hash matches at s == 1. |
| 353 |
s := 1 |
| 354 |
cv := load64(src, s) |
| 355 |
|
| 356 |
// We search for a repeat at -1, but don't output repeats when nextEmit == 0 |
| 357 |
repeat := 1 |
| 358 |
|
| 359 |
for { |
| 360 |
candidate := 0 |
| 361 |
for { |
| 362 |
// Next src position to check |
| 363 |
nextS := s + (s-nextEmit)>>6 + 4 |
| 364 |
if nextS > sLimit { |
| 365 |
goto emitRemainder |
| 366 |
} |
| 367 |
hash0 := hash6(cv, tableBits) |
| 368 |
hash1 := hash6(cv>>8, tableBits) |
| 369 |
candidate = int(table[hash0]) |
| 370 |
candidate2 := int(table[hash1]) |
| 371 |
table[hash0] = uint32(s) |
| 372 |
table[hash1] = uint32(s + 1) |
| 373 |
hash2 := hash6(cv>>16, tableBits) |
| 374 |
|
| 375 |
// Check repeat at offset checkRep. |
| 376 |
const checkRep = 1 |
| 377 |
if uint32(cv>>(checkRep*8)) == load32(src, s-repeat+checkRep) { |
| 378 |
base := s + checkRep |
| 379 |
// Extend back |
| 380 |
for i := base - repeat; base > nextEmit && i > 0 && src[i-1] == src[base-1]; { |
| 381 |
i-- |
| 382 |
base-- |
| 383 |
} |
| 384 |
d += emitLiteralSize(src[nextEmit:base]) |
| 385 |
|
| 386 |
// Extend forward |
| 387 |
candidate := s - repeat + 4 + checkRep |
| 388 |
s += 4 + checkRep |
| 389 |
for s <= sLimit { |
| 390 |
if diff := load64(src, s) ^ load64(src, candidate); diff != 0 { |
| 391 |
s += bits.TrailingZeros64(diff) >> 3 |
| 392 |
break |
| 393 |
} |
| 394 |
s += 8 |
| 395 |
candidate += 8 |
| 396 |
} |
| 397 |
|
| 398 |
d += emitCopyNoRepeatSize(repeat, s-base) |
| 399 |
nextEmit = s |
| 400 |
if s >= sLimit { |
| 401 |
goto emitRemainder |
| 402 |
} |
| 403 |
|
| 404 |
cv = load64(src, s) |
| 405 |
continue |
| 406 |
} |
| 407 |
|
| 408 |
if uint32(cv) == load32(src, candidate) { |
| 409 |
break |
| 410 |
} |
| 411 |
candidate = int(table[hash2]) |
| 412 |
if uint32(cv>>8) == load32(src, candidate2) { |
| 413 |
table[hash2] = uint32(s + 2) |
| 414 |
candidate = candidate2 |
| 415 |
s++ |
| 416 |
break |
| 417 |
} |
| 418 |
table[hash2] = uint32(s + 2) |
| 419 |
if uint32(cv>>16) == load32(src, candidate) { |
| 420 |
s += 2 |
| 421 |
break |
| 422 |
} |
| 423 |
|
| 424 |
cv = load64(src, nextS) |
| 425 |
s = nextS |
| 426 |
} |
| 427 |
|
| 428 |
// Extend backwards |
| 429 |
for candidate > 0 && s > nextEmit && src[candidate-1] == src[s-1] { |
| 430 |
candidate-- |
| 431 |
s-- |
| 432 |
} |
| 433 |
|
| 434 |
// Bail if we exceed the maximum size. |
| 435 |
if d+(s-nextEmit) > dstLimit { |
| 436 |
return 0 |
| 437 |
} |
| 438 |
|
| 439 |
// A 4-byte match has been found. We'll later see if more than 4 bytes |
| 440 |
// match. But, prior to the match, src[nextEmit:s] are unmatched. Emit |
| 441 |
// them as literal bytes. |
| 442 |
|
| 443 |
d += emitLiteralSize(src[nextEmit:s]) |
| 444 |
|
| 445 |
// Call emitCopy, and then see if another emitCopy could be our next |
| 446 |
// move. Repeat until we find no match for the input immediately after |
| 447 |
// what was consumed by the last emitCopy call. |
| 448 |
// |
| 449 |
// If we exit this loop normally then we need to call emitLiteral next, |
| 450 |
// though we don't yet know how big the literal will be. We handle that |
| 451 |
// by proceeding to the next iteration of the main loop. We also can |
| 452 |
// exit this loop via goto if we get close to exhausting the input. |
| 453 |
for { |
| 454 |
// Invariant: we have a 4-byte match at s, and no need to emit any |
| 455 |
// literal bytes prior to s. |
| 456 |
base := s |
| 457 |
repeat = base - candidate |
| 458 |
|
| 459 |
// Extend the 4-byte match as long as possible. |
| 460 |
s += 4 |
| 461 |
candidate += 4 |
| 462 |
for s <= len(src)-8 { |
| 463 |
if diff := load64(src, s) ^ load64(src, candidate); diff != 0 { |
| 464 |
s += bits.TrailingZeros64(diff) >> 3 |
| 465 |
break |
| 466 |
} |
| 467 |
s += 8 |
| 468 |
candidate += 8 |
| 469 |
} |
| 470 |
|
| 471 |
d += emitCopyNoRepeatSize(repeat, s-base) |
| 472 |
if false { |
| 473 |
// Validate match. |
| 474 |
a := src[base:s] |
| 475 |
b := src[base-repeat : base-repeat+(s-base)] |
| 476 |
if !bytes.Equal(a, b) { |
| 477 |
panic("mismatch") |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
nextEmit = s |
| 482 |
if s >= sLimit { |
| 483 |
goto emitRemainder |
| 484 |
} |
| 485 |
|
| 486 |
if d > dstLimit { |
| 487 |
// Do we have space for more, if not bail. |
| 488 |
return 0 |
| 489 |
} |
| 490 |
// Check for an immediate match, otherwise start search at s+1 |
| 491 |
x := load64(src, s-2) |
| 492 |
m2Hash := hash6(x, tableBits) |
| 493 |
currHash := hash6(x>>16, tableBits) |
| 494 |
candidate = int(table[currHash]) |
| 495 |
table[m2Hash] = uint32(s - 2) |
| 496 |
table[currHash] = uint32(s) |
| 497 |
if uint32(x>>16) != load32(src, candidate) { |
| 498 |
cv = load64(src, s+1) |
| 499 |
s++ |
| 500 |
break |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
emitRemainder: |
| 506 |
if nextEmit < len(src) { |
| 507 |
// Bail if we exceed the maximum size. |
| 508 |
if d+len(src)-nextEmit > dstLimit { |
| 509 |
return 0 |
| 510 |
} |
| 511 |
d += emitLiteralSize(src[nextEmit:]) |
| 512 |
} |
| 513 |
return d |
| 514 |
} |
| 515 |
|
| 516 |
// length must be > inputMargin. |
| 517 |
func calcBlockSizeSmall(src []byte, _ *[2048]byte) (d int) { |
| 518 |
// Initialize the hash table. |
| 519 |
const ( |
| 520 |
tableBits = 9 |
| 521 |
maxTableSize = 1 << tableBits |
| 522 |
) |
| 523 |
|
| 524 |
var table [maxTableSize]uint32 |
| 525 |
|
| 526 |
// sLimit is when to stop looking for offset/length copies. The inputMargin |
| 527 |
// lets us use a fast path for emitLiteral in the main loop, while we are |
| 528 |
// looking for copies. |
| 529 |
sLimit := len(src) - inputMargin |
| 530 |
|
| 531 |
// Bail if we can't compress to at least this. |
| 532 |
dstLimit := len(src) - len(src)>>5 - 5 |
| 533 |
|
| 534 |
// nextEmit is where in src the next emitLiteral should start from. |
| 535 |
nextEmit := 0 |
| 536 |
|
| 537 |
// The encoded form must start with a literal, as there are no previous |
| 538 |
// bytes to copy, so we start looking for hash matches at s == 1. |
| 539 |
s := 1 |
| 540 |
cv := load64(src, s) |
| 541 |
|
| 542 |
// We search for a repeat at -1, but don't output repeats when nextEmit == 0 |
| 543 |
repeat := 1 |
| 544 |
|
| 545 |
for { |
| 546 |
candidate := 0 |
| 547 |
for { |
| 548 |
// Next src position to check |
| 549 |
nextS := s + (s-nextEmit)>>6 + 4 |
| 550 |
if nextS > sLimit { |
| 551 |
goto emitRemainder |
| 552 |
} |
| 553 |
hash0 := hash6(cv, tableBits) |
| 554 |
hash1 := hash6(cv>>8, tableBits) |
| 555 |
candidate = int(table[hash0]) |
| 556 |
candidate2 := int(table[hash1]) |
| 557 |
table[hash0] = uint32(s) |
| 558 |
table[hash1] = uint32(s + 1) |
| 559 |
hash2 := hash6(cv>>16, tableBits) |
| 560 |
|
| 561 |
// Check repeat at offset checkRep. |
| 562 |
const checkRep = 1 |
| 563 |
if uint32(cv>>(checkRep*8)) == load32(src, s-repeat+checkRep) { |
| 564 |
base := s + checkRep |
| 565 |
// Extend back |
| 566 |
for i := base - repeat; base > nextEmit && i > 0 && src[i-1] == src[base-1]; { |
| 567 |
i-- |
| 568 |
base-- |
| 569 |
} |
| 570 |
d += emitLiteralSize(src[nextEmit:base]) |
| 571 |
|
| 572 |
// Extend forward |
| 573 |
candidate := s - repeat + 4 + checkRep |
| 574 |
s += 4 + checkRep |
| 575 |
for s <= sLimit { |
| 576 |
if diff := load64(src, s) ^ load64(src, candidate); diff != 0 { |
| 577 |
s += bits.TrailingZeros64(diff) >> 3 |
| 578 |
break |
| 579 |
} |
| 580 |
s += 8 |
| 581 |
candidate += 8 |
| 582 |
} |
| 583 |
|
| 584 |
d += emitCopyNoRepeatSize(repeat, s-base) |
| 585 |
nextEmit = s |
| 586 |
if s >= sLimit { |
| 587 |
goto emitRemainder |
| 588 |
} |
| 589 |
|
| 590 |
cv = load64(src, s) |
| 591 |
continue |
| 592 |
} |
| 593 |
|
| 594 |
if uint32(cv) == load32(src, candidate) { |
| 595 |
break |
| 596 |
} |
| 597 |
candidate = int(table[hash2]) |
| 598 |
if uint32(cv>>8) == load32(src, candidate2) { |
| 599 |
table[hash2] = uint32(s + 2) |
| 600 |
candidate = candidate2 |
| 601 |
s++ |
| 602 |
break |
| 603 |
} |
| 604 |
table[hash2] = uint32(s + 2) |
| 605 |
if uint32(cv>>16) == load32(src, candidate) { |
| 606 |
s += 2 |
| 607 |
break |
| 608 |
} |
| 609 |
|
| 610 |
cv = load64(src, nextS) |
| 611 |
s = nextS |
| 612 |
} |
| 613 |
|
| 614 |
// Extend backwards |
| 615 |
for candidate > 0 && s > nextEmit && src[candidate-1] == src[s-1] { |
| 616 |
candidate-- |
| 617 |
s-- |
| 618 |
} |
| 619 |
|
| 620 |
// Bail if we exceed the maximum size. |
| 621 |
if d+(s-nextEmit) > dstLimit { |
| 622 |
return 0 |
| 623 |
} |
| 624 |
|
| 625 |
// A 4-byte match has been found. We'll later see if more than 4 bytes |
| 626 |
// match. But, prior to the match, src[nextEmit:s] are unmatched. Emit |
| 627 |
// them as literal bytes. |
| 628 |
|
| 629 |
d += emitLiteralSize(src[nextEmit:s]) |
| 630 |
|
| 631 |
// Call emitCopy, and then see if another emitCopy could be our next |
| 632 |
// move. Repeat until we find no match for the input immediately after |
| 633 |
// what was consumed by the last emitCopy call. |
| 634 |
// |
| 635 |
// If we exit this loop normally then we need to call emitLiteral next, |
| 636 |
// though we don't yet know how big the literal will be. We handle that |
| 637 |
// by proceeding to the next iteration of the main loop. We also can |
| 638 |
// exit this loop via goto if we get close to exhausting the input. |
| 639 |
for { |
| 640 |
// Invariant: we have a 4-byte match at s, and no need to emit any |
| 641 |
// literal bytes prior to s. |
| 642 |
base := s |
| 643 |
repeat = base - candidate |
| 644 |
|
| 645 |
// Extend the 4-byte match as long as possible. |
| 646 |
s += 4 |
| 647 |
candidate += 4 |
| 648 |
for s <= len(src)-8 { |
| 649 |
if diff := load64(src, s) ^ load64(src, candidate); diff != 0 { |
| 650 |
s += bits.TrailingZeros64(diff) >> 3 |
| 651 |
break |
| 652 |
} |
| 653 |
s += 8 |
| 654 |
candidate += 8 |
| 655 |
} |
| 656 |
|
| 657 |
d += emitCopyNoRepeatSize(repeat, s-base) |
| 658 |
if false { |
| 659 |
// Validate match. |
| 660 |
a := src[base:s] |
| 661 |
b := src[base-repeat : base-repeat+(s-base)] |
| 662 |
if !bytes.Equal(a, b) { |
| 663 |
panic("mismatch") |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
nextEmit = s |
| 668 |
if s >= sLimit { |
| 669 |
goto emitRemainder |
| 670 |
} |
| 671 |
|
| 672 |
if d > dstLimit { |
| 673 |
// Do we have space for more, if not bail. |
| 674 |
return 0 |
| 675 |
} |
| 676 |
// Check for an immediate match, otherwise start search at s+1 |
| 677 |
x := load64(src, s-2) |
| 678 |
m2Hash := hash6(x, tableBits) |
| 679 |
currHash := hash6(x>>16, tableBits) |
| 680 |
candidate = int(table[currHash]) |
| 681 |
table[m2Hash] = uint32(s - 2) |
| 682 |
table[currHash] = uint32(s) |
| 683 |
if uint32(x>>16) != load32(src, candidate) { |
| 684 |
cv = load64(src, s+1) |
| 685 |
s++ |
| 686 |
break |
| 687 |
} |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
emitRemainder: |
| 692 |
if nextEmit < len(src) { |
| 693 |
// Bail if we exceed the maximum size. |
| 694 |
if d+len(src)-nextEmit > dstLimit { |
| 695 |
return 0 |
| 696 |
} |
| 697 |
d += emitLiteralSize(src[nextEmit:]) |
| 698 |
} |
| 699 |
return d |
| 700 |
} |
| 701 |
|
| 702 |
// emitLiteral writes a literal chunk and returns the number of bytes written. |
| 703 |
// |
| 704 |
// It assumes that: |
| 705 |
// |
| 706 |
// dst is long enough to hold the encoded bytes |
| 707 |
// 0 <= len(lit) && len(lit) <= math.MaxUint32 |
| 708 |
func emitLiteralSize(lit []byte) int { |
| 709 |
if len(lit) == 0 { |
| 710 |
return 0 |
| 711 |
} |
| 712 |
switch { |
| 713 |
case len(lit) <= 60: |
| 714 |
return len(lit) + 1 |
| 715 |
case len(lit) <= 1<<8: |
| 716 |
return len(lit) + 2 |
| 717 |
case len(lit) <= 1<<16: |
| 718 |
return len(lit) + 3 |
| 719 |
case len(lit) <= 1<<24: |
| 720 |
return len(lit) + 4 |
| 721 |
default: |
| 722 |
return len(lit) + 5 |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
func cvtLZ4BlockAsm(dst []byte, src []byte) (uncompressed int, dstUsed int) { |
| 727 |
panic("cvtLZ4BlockAsm should be unreachable") |
| 728 |
} |
| 729 |
|
| 730 |
func cvtLZ4BlockSnappyAsm(dst []byte, src []byte) (uncompressed int, dstUsed int) { |
| 731 |
panic("cvtLZ4BlockSnappyAsm should be unreachable") |
| 732 |
} |
| 733 |
|
| 734 |
func cvtLZ4sBlockAsm(dst []byte, src []byte) (uncompressed int, dstUsed int) { |
| 735 |
panic("cvtLZ4sBlockAsm should be unreachable") |
| 736 |
} |
| 737 |
|
| 738 |
func cvtLZ4sBlockSnappyAsm(dst []byte, src []byte) (uncompressed int, dstUsed int) { |
| 739 |
panic("cvtLZ4sBlockSnappyAsm should be unreachable") |
| 740 |
} |