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, $0-40
9	I64Load b_base+0(FP)
10	I32WrapI64
11	I32Load8U c+24(FP)
12	I64Load b_len+8(FP)
13	I32WrapI64
14	Call memchr<>(SB)
15	I64ExtendI32U
16	Set R0
17
18	Get SP
19	I64Const $-1
20	Get R0
21	I64Load b_base+0(FP)
22	I64Sub
23	Get R0
24	I64Eqz $0
25	Select
26	I64Store ret+32(FP)
27
28	RET
29
30TEXT ·IndexByteString(SB), NOSPLIT, $0-32
31	Get SP
32	I64Load s_base+0(FP)
33	I32WrapI64
34	I32Load8U c+16(FP)
35	I64Load s_len+8(FP)
36	I32WrapI64
37	Call memchr<>(SB)
38	I64ExtendI32U
39	Set R0
40
41	I64Const $-1
42	Get R0
43	I64Load s_base+0(FP)
44	I64Sub
45	Get R0
46	I64Eqz $0
47	Select
48	I64Store ret+24(FP)
49
50	RET
51
52// initially compiled with emscripten and then modified over time.
53// params:
54//   R0: s
55//   R1: c
56//   R2: len
57// ret: index
58TEXT memchr<>(SB), NOSPLIT, $0
59	Get R1
60	Set R4
61	Block
62		Block
63			Get R2
64			I32Const $0
65			I32Ne
66			Tee R3
67			Get R0
68			I32Const $3
69			I32And
70			I32Const $0
71			I32Ne
72			I32And
73			If
74				Loop
75					Get R0
76					I32Load8U $0
77					Get R1
78					I32Eq
79					BrIf $2
80					Get R2
81					I32Const $-1
82					I32Add
83					Tee R2
84					I32Const $0
85					I32Ne
86					Tee R3
87					Get R0
88					I32Const $1
89					I32Add
90					Tee R0
91					I32Const $3
92					I32And
93					I32Const $0
94					I32Ne
95					I32And
96					BrIf $0
97				End
98			End
99			Get R3
100			BrIf $0
101			I32Const $0
102			Set R1
103			Br $1
104		End
105		Get R0
106		I32Load8U $0
107		Get R4
108		Tee R3
109		I32Eq
110		If
111			Get R2
112			Set R1
113		Else
114			Get R4
115			I32Const $16843009
116			I32Mul
117			Set R4
118			Block
119				Block
120					Get R2
121					I32Const $3
122					I32GtU
123					If
124						Get R2
125						Set R1
126						Loop
127							Get R0
128							I32Load $0
129							Get R4
130							I32Xor
131							Tee R2
132							I32Const $-2139062144
133							I32And
134							I32Const $-2139062144
135							I32Xor
136							Get R2
137							I32Const $-16843009
138							I32Add
139							I32And
140							I32Eqz
141							If
142								Get R0
143								I32Const $4
144								I32Add
145								Set R0
146								Get R1
147								I32Const $-4
148								I32Add
149								Tee R1
150								I32Const $3
151								I32GtU
152								BrIf $1
153								Br $3
154							End
155						End
156					Else
157						Get R2
158						Set R1
159						Br $1
160					End
161					Br $1
162				End
163				Get R1
164				I32Eqz
165				If
166					I32Const $0
167					Set R1
168					Br $3
169				End
170			End
171			Loop
172				Get R0
173				I32Load8U $0
174				Get R3
175				I32Eq
176				BrIf $2
177				Get R0
178				I32Const $1
179				I32Add
180				Set R0
181				Get R1
182				I32Const $-1
183				I32Add
184				Tee R1
185				BrIf $0
186				I32Const $0
187				Set R1
188			End
189		End
190	End
191	Get R0
192	I32Const $0
193	Get R1
194	Select
195	Return
196