1/* Vector optimized 32/64 bit S/390 version of strncmp.
2   Copyright (C) 2015-2021 Free Software Foundation, Inc.
3   This file is part of the GNU C Library.
4
5   The GNU C Library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9
10   The GNU C Library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with the GNU C Library; if not, see
17   <https://www.gnu.org/licenses/>.  */
18
19#include <ifunc-strncmp.h>
20
21#if HAVE_STRNCMP_Z13
22
23# include "sysdep.h"
24# include "asm-syntax.h"
25
26	.text
27
28/* int strncmp (const char *s1, const char *s2, size_t n)
29   Compare at most n characters of two strings.
30
31   Register usage:
32   -r0=tmp
33   -r1=tmp
34   -r2=s1
35   -r3=s2
36   -r4=n
37   -r5=current_len
38   -v16=part of s1
39   -v17=part of s2
40   -v18=index of unequal
41*/
42ENTRY(STRNCMP_Z13)
43	.machine "z13"
44	.machinemode "zarch_nohighgprs"
45
46# if !defined __s390x__
47	llgfr	%r4,%r4
48# endif /* !defined __s390x__ */
49
50	clgije	%r4,0,.Lend_equal /* Nothing to do if n == 0,  */
51	lghi	%r5,0		/* current_len = 0.  */
52
53.Lloop:
54	vlbb	%v16,0(%r5,%r2),6 /* Load s1 to block boundary.  */
55	vlbb	%v17,0(%r5,%r3),6 /* Load s2 to block boundary.  */
56	lcbb	%r0,0(%r5,%r2),6 /* Get loaded byte count of s1.  */
57	jo	.Llt16_1	/* Jump away if vr is not fully loaded.  */
58	lcbb	%r1,0(%r5,%r3),6 /* Get loaded byte count of s2.  */
59	jo	.Llt16_2	/* Jump away if vr is not fully loaded.  */
60	aghi	%r5,16		/* Both vrs are fully loaded.  */
61	clgrjhe	%r5,%r4,.Llastcmp /* If current_len >= n ->last compare.  */
62	vfenezbs %v18,%v16,%v17	/* Compare not equal with zero search.  */
63	jno	.Lfound
64
65	vlbb	%v16,0(%r5,%r2),6
66	vlbb	%v17,0(%r5,%r3),6
67	lcbb	%r0,0(%r5,%r2),6
68	jo	.Llt16_1
69	lcbb	%r1,0(%r5,%r3),6
70	jo	.Llt16_2
71	aghi	%r5,16
72	clgrjhe	%r5,%r4,.Llastcmp
73	vfenezbs %v18,%v16,%v17
74	jno	.Lfound
75
76	vlbb	%v16,0(%r5,%r2),6
77	vlbb	%v17,0(%r5,%r3),6
78	lcbb	%r0,0(%r5,%r2),6
79	jo	.Llt16_1
80	lcbb	%r1,0(%r5,%r3),6
81	jo	.Llt16_2
82	aghi	%r5,16
83	clgrjhe	%r5,%r4,.Llastcmp
84	vfenezbs %v18,%v16,%v17
85	jno	.Lfound
86
87	vlbb	%v16,0(%r5,%r2),6
88	vlbb	%v17,0(%r5,%r3),6
89	lcbb	%r0,0(%r5,%r2),6
90	jo	.Llt16_1
91	lcbb	%r1,0(%r5,%r3),6
92	jo	.Llt16_2
93	aghi	%r5,16
94	clgrjhe	%r5,%r4,.Llastcmp
95	vfenezbs %v18,%v16,%v17
96	jno	.Lfound
97	j	.Lloop
98
99.Llt16_1:
100	lcbb	%r1,0(%r5,%r3),6 /* Get loaded byte count ofs2.  */
101.Llt16_2:
102	clr	%r0,%r1		/* Compare logical.  */
103	locrh	%r0,%r1		/* Compute minimum of bytes loaded.  */
104	algfr	%r5,%r0		/* Add smallest loaded bytes to current_len.  */
105	clgrj	%r5,%r4,10,.Llastcmp /* If current_len >= n ->last compare.  */
106	vfenezbs %v18,%v16,%v17	/* Compare not equal with zero search.  */
107	vlgvb	%r1,%v18,7	/* Get not equal index or 16 if all equal.  */
108	clrjl	%r1,%r0,.Lfound /* Jump away if miscompare is within
109				    loaded bytes (index < loaded-bytes) */
110	j	.Lloop
111
112.Llastcmp:
113	/* Use comparision result only if located within first n characters.
114	   %r0: loaded byte count in vreg;
115	   %r5: current_len;
116	   %r4: n;
117	   (current_len - n): [0...16[
118	   First ignored match index: loaded bytes - (current_len-n): ]0...16]
119	*/
120	slgr	%r5,%r4		/* %r5 = current_len - n.  */
121	slr	%r0,%r5		/* %r0 = first ignored match index.  */
122	vfenezbs %v18,%v16,%v17	/* Compare not equal with zero search.  */
123	vlgvb	%r1,%v18,7	/* Get not equal index or 16 if all equal.  */
124	clrjl	%r1,%r0,.Lfound /* Jump away if miscompare is within
125				    loaded bytes and below n bytes.  */
126	j	.Lend_equal	/* Miscompare after n-bytes -> end equal.  */
127
128.Lfound:
129	/* Difference or end of string.  */
130	je	.Lend_equal
131	lghi	%r2,1
132	lghi	%r1,-1
133	locgrl	%r2,%r1
134	br	%r14
135.Lend_equal:
136	lghi	%r2,0
137	br	%r14
138END(STRNCMP_Z13)
139
140# if ! HAVE_STRNCMP_IFUNC
141strong_alias (STRNCMP_Z13, strncmp)
142# endif
143
144# if ! HAVE_STRNCMP_C && defined SHARED && IS_IN (libc)
145strong_alias (STRNCMP_Z13, __GI_strncmp)
146# endif
147
148#endif /* HAVE_STRNCMP_Z13  */
149