* lto-partition.c: Include sreal.h
[official-gcc.git] / libgo / go / bytes / bytes_amd64.go
blob2fbbbb0d877cf843ffe97eb801b99a8a1e228995
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build ignore
7 package bytes
9 import "internal/cpu"
11 //go:noescape
13 // indexShortStr returns the index of the first instance of c in s, or -1 if c is not present in s.
14 // indexShortStr requires 2 <= len(c) <= shortStringLen
15 func indexShortStr(s, c []byte) int // ../runtime/asm_amd64.s
16 func countByte(s []byte, c byte) int // ../runtime/asm_amd64.s
18 var shortStringLen int
20 func init() {
21 if cpu.X86.HasAVX2 {
22 shortStringLen = 63
23 } else {
24 shortStringLen = 31
28 // Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
29 func Index(s, sep []byte) int {
30 n := len(sep)
31 switch {
32 case n == 0:
33 return 0
34 case n == 1:
35 return IndexByte(s, sep[0])
36 case n == len(s):
37 if Equal(sep, s) {
38 return 0
40 return -1
41 case n > len(s):
42 return -1
43 case n <= shortStringLen:
44 // Use brute force when s and sep both are small
45 if len(s) <= 64 {
46 return indexShortStr(s, sep)
48 c := sep[0]
49 i := 0
50 t := s[:len(s)-n+1]
51 fails := 0
52 for i < len(t) {
53 if t[i] != c {
54 // IndexByte skips 16/32 bytes per iteration,
55 // so it's faster than indexShortStr.
56 o := IndexByte(t[i:], c)
57 if o < 0 {
58 return -1
60 i += o
62 if Equal(s[i:i+n], sep) {
63 return i
65 fails++
66 i++
67 // Switch to indexShortStr when IndexByte produces too many false positives.
68 // Too many means more that 1 error per 8 characters.
69 // Allow some errors in the beginning.
70 if fails > (i+16)/8 {
71 r := indexShortStr(s[i:], sep)
72 if r >= 0 {
73 return r + i
75 return -1
78 return -1
80 return indexRabinKarp(s, sep)
83 // Count counts the number of non-overlapping instances of sep in s.
84 // If sep is an empty slice, Count returns 1 + the number of UTF-8-encoded code points in s.
85 func Count(s, sep []byte) int {
86 if len(sep) == 1 && cpu.X86.HasPOPCNT {
87 return countByte(s, sep[0])
89 return countGeneric(s, sep)