Debian Codegraph @

internal/bytealg/index_native.go

raw
1 // Copyright 2023 Charlie Vieth. All rights reserved.
2 // Use of this source code is governed by the MIT license.
3
4 //go:build amd64 || arm64 || s390x || ppc64le || ppc64
5
6 package bytealg
7
8 import (
9 "runtime"
10 _ "unsafe"
11 )
12
13 // NativeIndex is true if we have a fast native (assembly) implementation
14 // of Index and IndexString. Otherwise bytes.Index and strings.Index are used.
15 const NativeIndex = true
16
17 // TODO: remove if not used.
18 //
19 //go:linkname MaxBruteForce internal/bytealg.MaxBruteForce
20 var MaxBruteForce int
21
22 // Cutover reports the number of failures of IndexByte we should tolerate
23 // before switching over to Index.
24 // n is the number of bytes processed so far.
25 // See the bytes.Index implementation for details.
26 //
27 // Implemented here instead of linked to internal/bytealg.Cutover since this
28 // is a hot function and we want it to be inlined.
29 //
30 // NOTE: This needs to be kept up-to-date with internal/bytealg.Cutover.
31 func Cutover(n int) int {
32 if runtime.GOARCH == "arm64" {
33 // 1 error per 16 characters, plus a few slop to start.
34 return 4 + n>>4
35 }
36 // 1 error per 8 characters, plus a few slop to start.
37 return (n + 16) / 8
38 }
39
40 //go:noescape
41 //go:linkname Index internal/bytealg.Index
42 func Index(s, substr []byte) int
43
44 //go:noescape
45 //go:linkname IndexString internal/bytealg.IndexString
46 func IndexString(s, substr string) int