Avoid is_constant calls in vectorizable_bswap
[official-gcc.git] / libgo / go / strings / strings_s390x.go
blobe74e4cd2fefb88c5967c3073ace38a76a445f863
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 strings
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, sep string) int // ../runtime/asm_$GOARCH.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 substr in s, or -1 if substr is not present in s.
30 func Index(s, substr string) int {
31 n := len(substr)
32 switch {
33 case n == 0:
34 return 0
35 case n == 1:
36 return IndexByte(s, substr[0])
37 case n == len(s):
38 if substr == 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 substr both are small
46 if len(s) <= 64 {
47 return indexShortStr(s, substr)
49 c := substr[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 s[i:i+n] == substr {
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:], substr)
73 if r >= 0 {
74 return r + i
76 return -1
79 return -1
81 return indexRabinKarp(s, substr)
84 // Count counts the number of non-overlapping instances of substr in s.
85 // If substr is an empty string, Count returns 1 + the number of Unicode code points in s.
86 func Count(s, substr string) int {
87 return countGeneric(s, substr)