// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package bytealg

import (
	
	
)

// Offsets into internal/cpu records for use in assembly.
const (
	offsetX86HasSSE42  = unsafe.Offsetof(cpu.X86.HasSSE42)
	offsetX86HasAVX2   = unsafe.Offsetof(cpu.X86.HasAVX2)
	offsetX86HasPOPCNT = unsafe.Offsetof(cpu.X86.HasPOPCNT)

	offsetS390xHasVX = unsafe.Offsetof(cpu.S390X.HasVX)

	offsetPPC64HasPOWER9 = unsafe.Offsetof(cpu.PPC64.IsPOWER9)
)

// MaxLen is the maximum length of the string to be searched for (argument b) in Index.
// If MaxLen is not 0, make sure MaxLen >= 4.
var MaxLen int

// PrimeRK is the prime base used in Rabin-Karp algorithm.
const PrimeRK = 16777619

// HashStr returns the hash and the appropriate multiplicative
// factor for use in Rabin-Karp algorithm.
func [ string | []byte]( ) (uint32, uint32) {
	 := uint32(0)
	for  := 0;  < len(); ++ {
		 = *PrimeRK + uint32([])
	}
	var ,  uint32 = 1, PrimeRK
	for  := len();  > 0;  >>= 1 {
		if &1 != 0 {
			 *= 
		}
		 *= 
	}
	return , 
}

// HashStrRev returns the hash of the reverse of sep and the
// appropriate multiplicative factor for use in Rabin-Karp algorithm.
func [ string | []byte]( ) (uint32, uint32) {
	 := uint32(0)
	for  := len() - 1;  >= 0; -- {
		 = *PrimeRK + uint32([])
	}
	var ,  uint32 = 1, PrimeRK
	for  := len();  > 0;  >>= 1 {
		if &1 != 0 {
			 *= 
		}
		 *= 
	}
	return , 
}

// IndexRabinKarp uses the Rabin-Karp search algorithm to return the index of the
// first occurrence of sep in s, or -1 if not present.
func [ string | []byte](,  ) int {
	// Rabin-Karp search
	,  := HashStr()
	 := len()
	var  uint32
	for  := 0;  < ; ++ {
		 = *PrimeRK + uint32([])
	}
	if  ==  && string([:]) == string() {
		return 0
	}
	for  := ;  < len(); {
		 *= PrimeRK
		 += uint32([])
		 -=  * uint32([-])
		++
		if  ==  && string([-:]) == string() {
			return  - 
		}
	}
	return -1
}

// LastIndexRabinKarp uses the Rabin-Karp search algorithm to return the last index of the
// occurrence of sep in s, or -1 if not present.
func [ string | []byte](,  ) int {
	// Rabin-Karp search from the end of the string
	,  := HashStrRev()
	 := len()
	 := len() - 
	var  uint32
	for  := len() - 1;  >= ; -- {
		 = *PrimeRK + uint32([])
	}
	if  ==  && string([:]) == string() {
		return 
	}
	for  :=  - 1;  >= 0; -- {
		 *= PrimeRK
		 += uint32([])
		 -=  * uint32([+])
		if  ==  && string([:+]) == string() {
			return 
		}
	}
	return -1
}

// MakeNoZero makes a slice of length n and capacity of at least n Bytes
// without zeroing the bytes (including the bytes between len and cap).
// It is the caller's responsibility to ensure uninitialized bytes
// do not leak to the end user.
func ( int) []byte