xref: /linux/arch/arm64/lib/strnlen.S (revision bb29b957eaaa15be33243550d9ee576c2d601669)
1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright (C) 2013 ARM Ltd.
4 * Copyright (C) 2013 Linaro.
5 *
6 * This code is based on glibc cortex strings work originally authored by Linaro
7 * be found @
8 *
9 * http://bazaar.launchpad.net/~linaro-toolchain-dev/cortex-strings/trunk/
10 * files/head:/src/aarch64/
11 */
12
13#include <linux/linkage.h>
14#include <asm/assembler.h>
15
16/*
17 * determine the length of a fixed-size string
18 *
19 * Parameters:
20 *	x0 - const string pointer
21 *	x1 - maximal string length
22 * Returns:
23 *	x0 - the return length of specific string
24 */
25
26/* Arguments and results.  */
27srcin		.req	x0
28len		.req	x0
29limit		.req	x1
30
31/* Locals and temporaries.  */
32src		.req	x2
33data1		.req	x3
34data2		.req	x4
35data2a		.req	x5
36has_nul1	.req	x6
37has_nul2	.req	x7
38tmp1		.req	x8
39tmp2		.req	x9
40tmp3		.req	x10
41tmp4		.req	x11
42zeroones	.req	x12
43pos		.req	x13
44limit_wd	.req	x14
45
46#define REP8_01 0x0101010101010101
47#define REP8_7f 0x7f7f7f7f7f7f7f7f
48#define REP8_80 0x8080808080808080
49
50SYM_FUNC_START(__pi_strnlen)
51	cbz	limit, .Lhit_limit
52	mov	zeroones, #REP8_01
53	bic	src, srcin, #15
54	ands	tmp1, srcin, #15
55	b.ne	.Lmisaligned
56	/* Calculate the number of full and partial words -1.  */
57	sub	limit_wd, limit, #1 /* Limit != 0, so no underflow.  */
58	lsr	limit_wd, limit_wd, #4  /* Convert to Qwords.  */
59
60	/*
61	* NUL detection works on the principle that (X - 1) & (~X) & 0x80
62	* (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
63	* can be done in parallel across the entire word.
64	*/
65	/*
66	* The inner loop deals with two Dwords at a time.  This has a
67	* slightly higher start-up cost, but we should win quite quickly,
68	* especially on cores with a high number of issue slots per
69	* cycle, as we get much better parallelism out of the operations.
70	*/
71.Lloop:
72	ldp	data1, data2, [src], #16
73.Lrealigned:
74	sub	tmp1, data1, zeroones
75	orr	tmp2, data1, #REP8_7f
76	sub	tmp3, data2, zeroones
77	orr	tmp4, data2, #REP8_7f
78	bic	has_nul1, tmp1, tmp2
79	bic	has_nul2, tmp3, tmp4
80	subs	limit_wd, limit_wd, #1
81	orr	tmp1, has_nul1, has_nul2
82	ccmp	tmp1, #0, #0, pl    /* NZCV = 0000  */
83	b.eq	.Lloop
84
85	cbz	tmp1, .Lhit_limit   /* No null in final Qword.  */
86
87	/*
88	* We know there's a null in the final Qword. The easiest thing
89	* to do now is work out the length of the string and return
90	* MIN (len, limit).
91	*/
92	sub	len, src, srcin
93	cbz	has_nul1, .Lnul_in_data2
94
95	sub	len, len, #8
96	mov	has_nul2, has_nul1
97.Lnul_in_data2:
98	sub	len, len, #8
99	rev	has_nul2, has_nul2
100	clz	pos, has_nul2
101	add	len, len, pos, lsr #3       /* Bits to bytes.  */
102	cmp	len, limit
103	csel	len, len, limit, ls     /* Return the lower value.  */
104	ret
105
106.Lmisaligned:
107	/*
108	* Deal with a partial first word.
109	* We're doing two things in parallel here;
110	* 1) Calculate the number of words (but avoiding overflow if
111	* limit is near ULONG_MAX) - to do this we need to work out
112	* limit + tmp1 - 1 as a 65-bit value before shifting it;
113	* 2) Load and mask the initial data words - we force the bytes
114	* before the ones we are interested in to 0xff - this ensures
115	* early bytes will not hit any zero detection.
116	*/
117	ldp	data1, data2, [src], #16
118
119	sub	limit_wd, limit, #1
120	and	tmp3, limit_wd, #15
121	lsr	limit_wd, limit_wd, #4
122
123	add	tmp3, tmp3, tmp1
124	add	limit_wd, limit_wd, tmp3, lsr #4
125
126	neg	tmp4, tmp1
127	lsl	tmp4, tmp4, #3  /* Bytes beyond alignment -> bits.  */
128
129	mov	tmp2, #~0
130	/* Little-endian.  Early bytes are at LSB.  */
131	lsr	tmp2, tmp2, tmp4	/* Shift (tmp1 & 63).  */
132
133	cmp	tmp1, #8
134
135	orr	data1, data1, tmp2
136	orr	data2a, data2, tmp2
137
138	csinv	data1, data1, xzr, le
139	csel	data2, data2, data2a, le
140	b	.Lrealigned
141
142.Lhit_limit:
143	mov	len, limit
144	ret
145SYM_FUNC_END(__pi_strnlen)
146
147SYM_FUNC_ALIAS_WEAK(strnlen, __pi_strnlen)
148EXPORT_SYMBOL_NOKASAN(strnlen)
149