matchlen_generic.go raw

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