1// Copyright 2018 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.
4
5#include "go_asm.h"
6#include "textflag.h"
7
8TEXT ·IndexByte(SB),NOSPLIT|NOFRAME,$0-40
9	MOVD	b_base+0(FP), R3// b_base => R3
10	MOVD	b_len+8(FP), R4 // b_len => R4
11	MOVBZ	c+24(FP), R5    // c => R5
12	MOVD	$ret+32(FP), R2 // &ret => R9
13	BR	indexbytebody<>(SB)
14
15TEXT ·IndexByteString(SB),NOSPLIT|NOFRAME,$0-32
16	MOVD	s_base+0(FP), R3// s_base => R3
17	MOVD	s_len+8(FP), R4 // s_len => R4
18	MOVBZ	c+16(FP), R5    // c => R5
19	MOVD	$ret+24(FP), R2 // &ret => R9
20	BR	indexbytebody<>(SB)
21
22// input:
23// R3: s
24// R4: s_len
25// R5: c -- byte sought
26// R2: &ret -- address to put index into
27TEXT indexbytebody<>(SB),NOSPLIT|NOFRAME,$0
28	CMPBEQ	R4, $0, notfound
29	MOVD	R3, R6          // store base for later
30	ADD	R3, R4, R8      // the address after the end of the string
31	//if the length is small, use loop; otherwise, use vector or srst search
32	CMPBGE	R4, $16, large
33
34residual:
35	CMPBEQ	R3, R8, notfound
36	MOVBZ	0(R3), R7
37	LA	1(R3), R3
38	CMPBNE	R7, R5, residual
39
40found:
41	SUB	R6, R3
42	SUB	$1, R3
43	MOVD	R3, 0(R2)
44	RET
45
46notfound:
47	MOVD	$-1, 0(R2)
48	RET
49
50large:
51	MOVBZ	internalcpu·S390X+const_offsetS390xHasVX(SB), R1
52	CMPBNE	R1, $0, vectorimpl
53
54srstimpl:                       // no vector facility
55	MOVBZ	R5, R0          // c needs to be in R0, leave until last minute as currently R0 is expected to be 0
56srstloop:
57	WORD	$0xB25E0083     // srst %r8, %r3 (search the range [R3, R8))
58	BVS	srstloop        // interrupted - continue
59	BGT	notfoundr0
60foundr0:
61	XOR	R0, R0          // reset R0
62	SUB	R6, R8          // remove base
63	MOVD	R8, 0(R2)
64	RET
65notfoundr0:
66	XOR	R0, R0          // reset R0
67	MOVD	$-1, 0(R2)
68	RET
69
70vectorimpl:
71	//if the address is not 16byte aligned, use loop for the header
72	MOVD	R3, R8
73	AND	$15, R8
74	CMPBGT	R8, $0, notaligned
75
76aligned:
77	ADD	R6, R4, R8
78	MOVD	R8, R7
79	AND	$-16, R7
80	// replicate c across V17
81	VLVGB	$0, R5, V19
82	VREPB	$0, V19, V17
83
84vectorloop:
85	CMPBGE	R3, R7, residual
86	VL	0(R3), V16    // load string to be searched into V16
87	ADD	$16, R3
88	VFEEBS	V16, V17, V18 // search V17 in V16 and set conditional code accordingly
89	BVS	vectorloop
90
91	// when vector search found c in the string
92	VLGVB	$7, V18, R7   // load 7th element of V18 containing index into R7
93	SUB	$16, R3
94	SUB	R6, R3
95	ADD	R3, R7
96	MOVD	R7, 0(R2)
97	RET
98
99notaligned:
100	MOVD	R3, R8
101	AND	$-16, R8
102	ADD     $16, R8
103notalignedloop:
104	CMPBEQ	R3, R8, aligned
105	MOVBZ	0(R3), R7
106	LA	1(R3), R3
107	CMPBNE	R7, R5, notalignedloop
108	BR	found
109