matchlen_generic.go 675 B

123456789101112131415161718192021222324252627282930313233
  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. "encoding/binary"
  8. "math/bits"
  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. for ; len(a) >= 8 && len(b) >= 8; a, b = a[8:], b[8:] {
  14. diff := binary.LittleEndian.Uint64(a) ^ binary.LittleEndian.Uint64(b)
  15. if diff != 0 {
  16. return n + bits.TrailingZeros64(diff)>>3
  17. }
  18. n += 8
  19. }
  20. for i := range a {
  21. if a[i] != b[i] {
  22. break
  23. }
  24. n++
  25. }
  26. return n
  27. }