about summary refs log tree commit diff
path: root/sysdeps/x86_64/multiarch/strrchr-evex-base.S
blob: 77830741c20265e99e0232a137b0611f433e2527 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* Placeholder function, not used by any processor at the moment.
   Copyright (C) 2022-2023 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

/* UNUSED. Exists purely as reference implementation.  */

#include <isa-level.h>

#if ISA_SHOULD_BUILD (4)

# include <sysdep.h>

# ifdef USE_AS_WCSRCHR
#  define CHAR_SIZE	4
#  define VPBROADCAST   vpbroadcastd
#  define VPCMPEQ	vpcmpeqd
#  define VPMINU	vpminud
#  define VPTESTN	vptestnmd
# else
#  define CHAR_SIZE	1
#  define VPBROADCAST   vpbroadcastb
#  define VPCMPEQ	vpcmpeqb
#  define VPMINU	vpminub
#  define VPTESTN	vptestnmb
# endif

# define PAGE_SIZE	4096
# define CHAR_PER_VEC	(VEC_SIZE / CHAR_SIZE)

	.section SECTION(.text), "ax", @progbits
/* Aligning entry point to 64 byte, provides better performance for
   one vector length string.  */
ENTRY_P2ALIGN (STRRCHR, 6)

	/* Broadcast CHAR to VMM(0).  */
	VPBROADCAST %esi, %VMM(0)
	movl	%edi, %eax
	sall	$20, %eax
	cmpl	$((PAGE_SIZE - VEC_SIZE) << 20), %eax
	ja	L(page_cross)

L(page_cross_continue):
	/* Compare [w]char for null, mask bit will be set for match.  */
	VMOVU	(%rdi), %VMM(1)

	VPTESTN	%VMM(1), %VMM(1), %k1
	KMOV	%k1, %VRCX
	test	%VRCX, %VRCX
	jz	L(align_more)

	VPCMPEQ	%VMM(1), %VMM(0), %k0
	KMOV	%k0, %VRAX
	BLSMSK	%VRCX, %VRCX
	and	%VRCX, %VRAX
	jz	L(ret)

	BSR	%VRAX, %VRAX
# ifdef USE_AS_WCSRCHR
	leaq	(%rdi, %rax, CHAR_SIZE), %rax
# else
	add	%rdi, %rax
# endif
L(ret):
	ret

L(vector_x2_end):
	VPCMPEQ	%VMM(2), %VMM(0), %k2
	KMOV	%k2, %VRAX
	BLSMSK	%VRCX, %VRCX
	and	%VRCX, %VRAX
	jz	L(vector_x1_ret)

	BSR	%VRAX, %VRAX
	leaq	(VEC_SIZE)(%rdi, %rax, CHAR_SIZE), %rax
	ret

	/* Check the first vector at very last to look for match.  */
L(vector_x1_ret):
	VPCMPEQ %VMM(1), %VMM(0), %k2
	KMOV	%k2, %VRAX
	test	%VRAX, %VRAX
	jz	L(ret)

	BSR	%VRAX, %VRAX
# ifdef USE_AS_WCSRCHR
	leaq	(%rsi, %rax, CHAR_SIZE), %rax
# else
	add	%rsi, %rax
# endif
	ret

L(align_more):
	/* Zero r8 to store match result.  */
	xorl	%r8d, %r8d
	/* Save pointer of first vector, in case if no match found.  */
	movq	%rdi, %rsi
	/* Align pointer to vector size.  */
	andq	$-VEC_SIZE, %rdi
	/* Loop unroll for 2 vector loop.  */
	VMOVA	(VEC_SIZE)(%rdi), %VMM(2)
	VPTESTN	%VMM(2), %VMM(2), %k0
	KMOV	%k0, %VRCX
	test	%VRCX, %VRCX
	jnz	L(vector_x2_end)

	/* Save pointer of second vector, in case if no match
	   found.  */
	movq	%rdi, %r9
	/* Align address to VEC_SIZE * 2 for loop.  */
	andq	$-(VEC_SIZE * 2), %rdi

	.p2align 4,,11
L(loop):
	/* 2 vector loop, as it provide better performance as compared
	   to 4 vector loop.  */
	VMOVA	(VEC_SIZE * 2)(%rdi), %VMM(3)
	VMOVA	(VEC_SIZE * 3)(%rdi), %VMM(4)
	VPCMPEQ	%VMM(3), %VMM(0), %k1
	VPCMPEQ	%VMM(4), %VMM(0), %k2
	VPMINU	%VMM(3), %VMM(4), %VMM(5)
	VPTESTN	%VMM(5), %VMM(5), %k0
	KOR	%k1, %k2, %k3
	subq	$-(VEC_SIZE * 2), %rdi
	/* If k0 and k3 zero, match and end of string not found.  */
	KORTEST	%k0, %k3
	jz	L(loop)

	/* If k0 is non zero, end of string found.  */
	KORTEST %k0, %k0
	jnz	L(endloop)

	lea	VEC_SIZE(%rdi), %r8
	/* A match found, it need to be stored in r8 before loop
	   continue.  */
	/* Check second vector first.  */
	KMOV	%k2, %VRDX
	test	%VRDX, %VRDX
	jnz	L(loop_vec_x2_match)

	KMOV	%k1, %VRDX
	/* Match is in first vector, rdi offset need to be substracted
	  by VEC_SIZE.  */
	sub	$VEC_SIZE, %r8

	/* If second vector doesn't have match, first vector must
	   have match.  */
L(loop_vec_x2_match):
	BSR	%VRDX, %VRDX
# ifdef USE_AS_WCSRCHR
	sal	$2, %rdx
# endif
	add	%rdx, %r8
	jmp	L(loop)

L(endloop):
	/* Check if string end in first loop vector.  */
	VPTESTN	%VMM(3), %VMM(3), %k0
	KMOV	%k0, %VRCX
	test	%VRCX, %VRCX
	jnz	L(loop_vector_x1_end)

	/* Check if it has match in first loop vector.  */
	KMOV	%k1, %VRAX
	test	%VRAX, %VRAX
	jz	L(loop_vector_x2_end)

	BSR	%VRAX, %VRAX
	leaq	(%rdi, %rax, CHAR_SIZE), %r8

	/* String must end in second loop vector.  */
L(loop_vector_x2_end):
	VPTESTN	%VMM(4), %VMM(4), %k0
	KMOV	%k0, %VRCX
	KMOV	%k2, %VRAX
	BLSMSK	%VRCX, %VRCX
	/* Check if it has match in second loop vector.  */
	and	%VRCX, %VRAX
	jz	L(check_last_match)

	BSR	%VRAX, %VRAX
	leaq	(VEC_SIZE)(%rdi, %rax, CHAR_SIZE), %rax
	ret

	/* String end in first loop vector.  */
L(loop_vector_x1_end):
	KMOV	%k1, %VRAX
	BLSMSK	%VRCX, %VRCX
	/* Check if it has match in second loop vector.  */
	and	%VRCX, %VRAX
	jz	L(check_last_match)

	BSR	%VRAX, %VRAX
	leaq	(%rdi, %rax, CHAR_SIZE), %rax
	ret

	/* No match in first and second loop vector.  */
L(check_last_match):
	/* Check if any match recorded in r8.  */
	test	%r8, %r8
	jz	L(vector_x2_ret)
	movq	%r8, %rax
	ret

	/* No match recorded in r8. Check the second saved vector
	   in begining.  */
L(vector_x2_ret):
	VPCMPEQ %VMM(2), %VMM(0), %k2
	KMOV	%k2, %VRAX
	test	%VRAX, %VRAX
	jz	L(vector_x1_ret)

	/* Match found in the second saved vector.  */
	BSR	%VRAX, %VRAX
	leaq	(VEC_SIZE)(%r9, %rax, CHAR_SIZE), %rax
	ret

L(page_cross):
	mov	%rdi, %rax
	movl	%edi, %ecx

# ifdef USE_AS_WCSRCHR
	/* Calculate number of compare result bits to be skipped for
	   wide string alignment adjustment.  */
	andl	$(VEC_SIZE - 1), %ecx
	sarl	$2, %ecx
# endif
	/* ecx contains number of w[char] to be skipped as a result
	   of address alignment.  */
	andq    $-VEC_SIZE, %rax
	VMOVA	(%rax), %VMM(1)
	VPTESTN	%VMM(1), %VMM(1), %k1
	KMOV	%k1, %VRAX
	SHR     %cl, %VRAX
	jz	L(page_cross_continue)
	VPCMPEQ	%VMM(1), %VMM(0), %k0
	KMOV	%k0, %VRDX
	SHR     %cl, %VRDX
	BLSMSK	%VRAX, %VRAX
	and	%VRDX, %VRAX
	jz	L(ret)
	BSR	%VRAX, %VRAX
# ifdef USE_AS_WCSRCHR
	leaq	(%rdi, %rax, CHAR_SIZE), %rax
# else
	add	%rdi, %rax
# endif

	ret
END (STRRCHR)
#endif