Fix numerous typos in comments
[official-gcc.git] / libgo / go / bytes / bytes_s390x.go
bloba05ca47aa1628ebb930e11ff409fbc745ec7562b
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 //go:noescape
11 // indexShortStr returns the index of the first instance of sep in s,
12 // or -1 if sep is not present in s.
13 // indexShortStr requires 2 <= len(sep) <= shortStringLen
14 func indexShortStr(s, c []byte) int // ../runtime/asm_s390x.s
16 // supportsVX reports whether the vector facility is available.
17 // indexShortStr must not be called if the vector facility is not
18 // available.
19 func supportsVX() bool // ../runtime/asm_s390x.s
21 var shortStringLen = -1
23 func init() {
24 if supportsVX() {
25 shortStringLen = 64
29 // Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
30 func Index(s, sep []byte) int {
31 n := len(sep)
32 switch {
33 case n == 0:
34 return 0
35 case n == 1:
36 return IndexByte(s, sep[0])
37 case n == len(s):
38 if Equal(sep, s) {
39 return 0
41 return -1
42 case n > len(s):
43 return -1
44 case n <= shortStringLen:
45 // Use brute force when s and sep both are small
46 if len(s) <= 64 {
47 return indexShortStr(s, sep)
49 c := sep[0]
50 i := 0
51 t := s[:len(s)-n+1]
52 fails := 0
53 for i < len(t) {
54 if t[i] != c {
55 // IndexByte skips 16/32 bytes per iteration,
56 // so it's faster than indexShortStr.
57 o := IndexByte(t[i:], c)
58 if o < 0 {
59 return -1
61 i += o
63 if Equal(s[i:i+n], sep) {
64 return i
66 fails++
67 i++
68 // Switch to indexShortStr when IndexByte produces too many false positives.
69 // Too many means more that 1 error per 8 characters.
70 // Allow some errors in the beginning.
71 if fails > (i+16)/8 {
72 r := indexShortStr(s[i:], sep)
73 if r >= 0 {
74 return r + i
76 return -1
79 return -1
81 // Rabin-Karp search
82 hashsep, pow := hashStr(sep)
83 var h uint32
84 for i := 0; i < n; i++ {
85 h = h*primeRK + uint32(s[i])
87 if h == hashsep && Equal(s[:n], sep) {
88 return 0
90 for i := n; i < len(s); {
91 h *= primeRK
92 h += uint32(s[i])
93 h -= pow * uint32(s[i-n])
94 i++
95 if h == hashsep && Equal(s[i-n:i], sep) {
96 return i - n
99 return -1
102 // primeRK is the prime base used in Rabin-Karp algorithm.
103 const primeRK = 16777619
105 // hashStr returns the hash and the appropriate multiplicative
106 // factor for use in Rabin-Karp algorithm.
107 func hashStr(sep []byte) (uint32, uint32) {
108 hash := uint32(0)
109 for i := 0; i < len(sep); i++ {
110 hash = hash*primeRK + uint32(sep[i])
112 var pow, sq uint32 = 1, primeRK
113 for i := len(sep); i > 0; i >>= 1 {
114 if i&1 != 0 {
115 pow *= sq
117 sq *= sq
119 return hash, pow