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