github.com/primecitizens/pcz/std@v0.2.1/core/bytealg/index_amd64.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  //
     4  // Copyright 2018 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  //go:build pcz && amd64
     9  
    10  package bytealg
    11  
    12  import (
    13  	"github.com/primecitizens/pcz/std/core/cpu"
    14  )
    15  
    16  var (
    17  	hasSSE42  bool
    18  	hasAVX2   bool
    19  	hasPOPCNT bool
    20  
    21  	// indexArgBMaxLen is the maximum length of the string to be searched for (argument b) in Index.
    22  	// If indexArgBMaxLen is not 0, make sure indexArgBMaxLen >= 4.
    23  	indexArgBMaxLen int
    24  )
    25  
    26  func init() {
    27  	if cpu.X86.HasAll(cpu.X86Feature_avx2) {
    28  		indexArgBMaxLen = 63
    29  		hasAVX2 = true
    30  	} else {
    31  		indexArgBMaxLen = 31
    32  	}
    33  
    34  	hasSSE42 = cpu.X86.HasAll(cpu.X86Feature_sse42)
    35  	hasPOPCNT = cpu.X86.HasAll(cpu.X86Feature_popcnt)
    36  }
    37  
    38  const MaxBruteForce = 64
    39  
    40  // cutover reports the number of failures of IndexByte we should tolerate
    41  // before switching over to Index.
    42  // n is the number of bytes processed so far.
    43  // See the bytes.Index implementation for details.
    44  func cutover(n int) int {
    45  	// 1 error per 8 characters, plus a few slop to start.
    46  	return (n + 16) / 8
    47  }
    48  
    49  // A backup implementation to use by assembly.
    50  func countGeneric(b []byte, c byte) int {
    51  	n := 0
    52  	for _, x := range b {
    53  		if x == c {
    54  			n++
    55  		}
    56  	}
    57  	return n
    58  }
    59  
    60  func countGenericString(s string, c byte) int {
    61  	n := 0
    62  	for i := 0; i < len(s); i++ {
    63  		if s[i] == c {
    64  			n++
    65  		}
    66  	}
    67  	return n
    68  }