matchlen_generic.go 647 B

12345678910111213141516171819202122232425262728293031323334353637
  1. //go:build !amd64 || appengine || !gc || noasm
  2. // Copyright 2019+ Klaus Post. All rights reserved.
  3. // License information can be found in the LICENSE file.
  4. package zstd
  5. import (
  6. "math/bits"
  7. "github.com/klauspost/compress/internal/le"
  8. )
  9. // matchLen returns the maximum common prefix length of a and b.
  10. // a must be the shortest of the two.
  11. func matchLen(a, b []byte) (n int) {
  12. left := len(a)
  13. for left >= 8 {
  14. diff := le.Load64(a, n) ^ le.Load64(b, n)
  15. if diff != 0 {
  16. return n + bits.TrailingZeros64(diff)>>3
  17. }
  18. n += 8
  19. left -= 8
  20. }
  21. a = a[n:]
  22. b = b[n:]
  23. for i := range a {
  24. if a[i] != b[i] {
  25. break
  26. }
  27. n++
  28. }
  29. return n
  30. }