xref: /aosp_15_r20/external/arm-optimized-routines/string/aarch64/strlen-sve.S (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li/*
2*412f47f9SXin Li * __strlen_aarch64_sve - compute the length of a string
3*412f47f9SXin Li *
4*412f47f9SXin Li * Copyright (c) 2018-2022, Arm Limited.
5*412f47f9SXin Li * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*412f47f9SXin Li */
7*412f47f9SXin Li
8*412f47f9SXin Li#include "asmdefs.h"
9*412f47f9SXin Li
10*412f47f9SXin Li#if __ARM_FEATURE_SVE
11*412f47f9SXin Li/* Assumptions:
12*412f47f9SXin Li *
13*412f47f9SXin Li * ARMv8-a, AArch64
14*412f47f9SXin Li * SVE Available.
15*412f47f9SXin Li */
16*412f47f9SXin Li
17*412f47f9SXin LiENTRY (__strlen_aarch64_sve)
18*412f47f9SXin Li	PTR_ARG (0)
19*412f47f9SXin Li	setffr			/* initialize FFR */
20*412f47f9SXin Li	ptrue	p2.b		/* all ones; loop invariant */
21*412f47f9SXin Li	mov	x1, 0		/* initialize length */
22*412f47f9SXin Li
23*412f47f9SXin Li	/* Read a vector's worth of bytes, stopping on first fault.  */
24*412f47f9SXin Li	.p2align 4
25*412f47f9SXin Li0:	ldff1b	z0.b, p2/z, [x0, x1]
26*412f47f9SXin Li	rdffrs	p0.b, p2/z
27*412f47f9SXin Li	b.nlast	2f
28*412f47f9SXin Li
29*412f47f9SXin Li	/* First fault did not fail: the whole vector is valid.
30*412f47f9SXin Li	   Avoid depending on the contents of FFR beyond the branch.  */
31*412f47f9SXin Li	incb	x1, all			/* speculate increment */
32*412f47f9SXin Li	cmpeq	p1.b, p2/z, z0.b, 0	/* loop if no zeros */
33*412f47f9SXin Li	b.none	0b
34*412f47f9SXin Li	decb	x1, all			/* undo speculate */
35*412f47f9SXin Li
36*412f47f9SXin Li	/* Zero found.  Select the bytes before the first and count them.  */
37*412f47f9SXin Li1:	brkb	p0.b, p2/z, p1.b
38*412f47f9SXin Li	incp	x1, p0.b
39*412f47f9SXin Li	mov	x0, x1
40*412f47f9SXin Li	ret
41*412f47f9SXin Li
42*412f47f9SXin Li	/* First fault failed: only some of the vector is valid.
43*412f47f9SXin Li	   Perform the comparison only on the valid bytes.  */
44*412f47f9SXin Li2:	cmpeq	p1.b, p0/z, z0.b, 0
45*412f47f9SXin Li	b.any	1b
46*412f47f9SXin Li
47*412f47f9SXin Li	/* No zero found.  Re-init FFR, increment, and loop.  */
48*412f47f9SXin Li	setffr
49*412f47f9SXin Li	incp	x1, p0.b
50*412f47f9SXin Li	b	0b
51*412f47f9SXin Li
52*412f47f9SXin LiEND (__strlen_aarch64_sve)
53*412f47f9SXin Li
54*412f47f9SXin Li#endif
55*412f47f9SXin Li
56