matchlen_generic.go 684 B

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