1/*
2 * strncmp - compare two strings with limit
3 *
4 * Copyright (c) 2018-2022, Arm Limited.
5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6 */
7
8#include "asmdefs.h"
9
10#if __ARM_FEATURE_SVE
11/* Assumptions:
12 *
13 * ARMv8-a, AArch64
14 * SVE Available.
15 */
16
17ENTRY (__strncmp_aarch64_sve)
18	PTR_ARG (0)
19	PTR_ARG (1)
20	SIZE_ARG (2)
21	setffr				/* initialize FFR */
22	mov	x3, 0			/* initialize off */
23
240:	whilelo	p0.b, x3, x2		/* while off < max */
25	b.none	9f
26
27	ldff1b	z0.b, p0/z, [x0, x3]
28	ldff1b	z1.b, p0/z, [x1, x3]
29	rdffrs	p1.b, p0/z
30	b.nlast	2f
31
32	/* First fault did not fail: the vector up to max is valid.
33	   Avoid depending on the contents of FFR beyond the branch.
34	   Increment for a whole vector, even if we've only read a partial.
35	   This is significantly cheaper than INCP, and since OFF is not
36	   used after the loop it is ok to increment OFF past MAX.  */
37	incb	x3
38	cmpeq	p1.b, p0/z, z0.b, z1.b	/* compare strings */
39	cmpne	p2.b, p0/z, z0.b, 0	/* search for ~zero */
40	nands	p2.b, p0/z, p1.b, p2.b	/* ~(eq & ~zero) -> ne | zero */
41	b.none	0b
42
43	/* Found end-of-string or inequality.  */
441:	brkb	p2.b, p0/z, p2.b	/* find first such */
45	lasta	w0, p2, z0.b		/* extract each char */
46	lasta	w1, p2, z1.b
47	sub	x0, x0, x1		/* return comparison */
48	ret
49
50	/* First fault failed: only some of the vector is valid.
51	   Perform the comparison only on the valid bytes.  */
522:	cmpeq	p2.b, p1/z, z0.b, z1.b	/* compare strings, as above */
53	cmpne	p3.b, p1/z, z0.b, 0
54	nands	p2.b, p1/z, p2.b, p3.b
55	b.any	1b
56
57	/* No inequality or zero found.  Re-init FFR, incr and loop.  */
58	setffr
59	incp	x3, p1.b
60	b	0b
61
62	/* Found end-of-count.  */
639:	mov	x0, 0			/* return equal */
64	ret
65
66END (__strncmp_aarch64_sve)
67
68#endif
69
70