18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2013 ARM Ltd.
48c2ecf20Sopenharmony_ci * Copyright (C) 2013 Linaro.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This code is based on glibc cortex strings work originally authored by Linaro
78c2ecf20Sopenharmony_ci * be found @
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * http://bazaar.launchpad.net/~linaro-toolchain-dev/cortex-strings/trunk/
108c2ecf20Sopenharmony_ci * files/head:/src/aarch64/
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/linkage.h>
148c2ecf20Sopenharmony_ci#include <asm/assembler.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/*
178c2ecf20Sopenharmony_ci* compare memory areas(when two memory areas' offset are different,
188c2ecf20Sopenharmony_ci* alignment handled by the hardware)
198c2ecf20Sopenharmony_ci*
208c2ecf20Sopenharmony_ci* Parameters:
218c2ecf20Sopenharmony_ci*  x0 - const memory area 1 pointer
228c2ecf20Sopenharmony_ci*  x1 - const memory area 2 pointer
238c2ecf20Sopenharmony_ci*  x2 - the maximal compare byte length
248c2ecf20Sopenharmony_ci* Returns:
258c2ecf20Sopenharmony_ci*  x0 - a compare result, maybe less than, equal to, or greater than ZERO
268c2ecf20Sopenharmony_ci*/
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* Parameters and result.  */
298c2ecf20Sopenharmony_cisrc1		.req	x0
308c2ecf20Sopenharmony_cisrc2		.req	x1
318c2ecf20Sopenharmony_cilimit		.req	x2
328c2ecf20Sopenharmony_ciresult		.req	x0
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/* Internal variables.  */
358c2ecf20Sopenharmony_cidata1		.req	x3
368c2ecf20Sopenharmony_cidata1w		.req	w3
378c2ecf20Sopenharmony_cidata2		.req	x4
388c2ecf20Sopenharmony_cidata2w		.req	w4
398c2ecf20Sopenharmony_cihas_nul		.req	x5
408c2ecf20Sopenharmony_cidiff		.req	x6
418c2ecf20Sopenharmony_ciendloop		.req	x7
428c2ecf20Sopenharmony_citmp1		.req	x8
438c2ecf20Sopenharmony_citmp2		.req	x9
448c2ecf20Sopenharmony_citmp3		.req	x10
458c2ecf20Sopenharmony_cipos		.req	x11
468c2ecf20Sopenharmony_cilimit_wd	.req	x12
478c2ecf20Sopenharmony_cimask		.req	x13
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ciSYM_FUNC_START_WEAK_PI(memcmp)
508c2ecf20Sopenharmony_ci	cbz	limit, .Lret0
518c2ecf20Sopenharmony_ci	eor	tmp1, src1, src2
528c2ecf20Sopenharmony_ci	tst	tmp1, #7
538c2ecf20Sopenharmony_ci	b.ne	.Lmisaligned8
548c2ecf20Sopenharmony_ci	ands	tmp1, src1, #7
558c2ecf20Sopenharmony_ci	b.ne	.Lmutual_align
568c2ecf20Sopenharmony_ci	sub	limit_wd, limit, #1 /* limit != 0, so no underflow.  */
578c2ecf20Sopenharmony_ci	lsr	limit_wd, limit_wd, #3 /* Convert to Dwords.  */
588c2ecf20Sopenharmony_ci	/*
598c2ecf20Sopenharmony_ci	* The input source addresses are at alignment boundary.
608c2ecf20Sopenharmony_ci	* Directly compare eight bytes each time.
618c2ecf20Sopenharmony_ci	*/
628c2ecf20Sopenharmony_ci.Lloop_aligned:
638c2ecf20Sopenharmony_ci	ldr	data1, [src1], #8
648c2ecf20Sopenharmony_ci	ldr	data2, [src2], #8
658c2ecf20Sopenharmony_ci.Lstart_realigned:
668c2ecf20Sopenharmony_ci	subs	limit_wd, limit_wd, #1
678c2ecf20Sopenharmony_ci	eor	diff, data1, data2	/* Non-zero if differences found.  */
688c2ecf20Sopenharmony_ci	csinv	endloop, diff, xzr, cs	/* Last Dword or differences.  */
698c2ecf20Sopenharmony_ci	cbz	endloop, .Lloop_aligned
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	/* Not reached the limit, must have found a diff.  */
728c2ecf20Sopenharmony_ci	tbz	limit_wd, #63, .Lnot_limit
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	/* Limit % 8 == 0 => the diff is in the last 8 bytes. */
758c2ecf20Sopenharmony_ci	ands	limit, limit, #7
768c2ecf20Sopenharmony_ci	b.eq	.Lnot_limit
778c2ecf20Sopenharmony_ci	/*
788c2ecf20Sopenharmony_ci	* The remained bytes less than 8. It is needed to extract valid data
798c2ecf20Sopenharmony_ci	* from last eight bytes of the intended memory range.
808c2ecf20Sopenharmony_ci	*/
818c2ecf20Sopenharmony_ci	lsl	limit, limit, #3	/* bytes-> bits.  */
828c2ecf20Sopenharmony_ci	mov	mask, #~0
838c2ecf20Sopenharmony_ciCPU_BE( lsr	mask, mask, limit )
848c2ecf20Sopenharmony_ciCPU_LE( lsl	mask, mask, limit )
858c2ecf20Sopenharmony_ci	bic	data1, data1, mask
868c2ecf20Sopenharmony_ci	bic	data2, data2, mask
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	orr	diff, diff, mask
898c2ecf20Sopenharmony_ci	b	.Lnot_limit
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci.Lmutual_align:
928c2ecf20Sopenharmony_ci	/*
938c2ecf20Sopenharmony_ci	* Sources are mutually aligned, but are not currently at an
948c2ecf20Sopenharmony_ci	* alignment boundary. Round down the addresses and then mask off
958c2ecf20Sopenharmony_ci	* the bytes that precede the start point.
968c2ecf20Sopenharmony_ci	*/
978c2ecf20Sopenharmony_ci	bic	src1, src1, #7
988c2ecf20Sopenharmony_ci	bic	src2, src2, #7
998c2ecf20Sopenharmony_ci	ldr	data1, [src1], #8
1008c2ecf20Sopenharmony_ci	ldr	data2, [src2], #8
1018c2ecf20Sopenharmony_ci	/*
1028c2ecf20Sopenharmony_ci	* We can not add limit with alignment offset(tmp1) here. Since the
1038c2ecf20Sopenharmony_ci	* addition probably make the limit overflown.
1048c2ecf20Sopenharmony_ci	*/
1058c2ecf20Sopenharmony_ci	sub	limit_wd, limit, #1/*limit != 0, so no underflow.*/
1068c2ecf20Sopenharmony_ci	and	tmp3, limit_wd, #7
1078c2ecf20Sopenharmony_ci	lsr	limit_wd, limit_wd, #3
1088c2ecf20Sopenharmony_ci	add	tmp3, tmp3, tmp1
1098c2ecf20Sopenharmony_ci	add	limit_wd, limit_wd, tmp3, lsr #3
1108c2ecf20Sopenharmony_ci	add	limit, limit, tmp1/* Adjust the limit for the extra.  */
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	lsl	tmp1, tmp1, #3/* Bytes beyond alignment -> bits.*/
1138c2ecf20Sopenharmony_ci	neg	tmp1, tmp1/* Bits to alignment -64.  */
1148c2ecf20Sopenharmony_ci	mov	tmp2, #~0
1158c2ecf20Sopenharmony_ci	/*mask off the non-intended bytes before the start address.*/
1168c2ecf20Sopenharmony_ciCPU_BE( lsl	tmp2, tmp2, tmp1 )/*Big-endian.Early bytes are at MSB*/
1178c2ecf20Sopenharmony_ci	/* Little-endian.  Early bytes are at LSB.  */
1188c2ecf20Sopenharmony_ciCPU_LE( lsr	tmp2, tmp2, tmp1 )
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	orr	data1, data1, tmp2
1218c2ecf20Sopenharmony_ci	orr	data2, data2, tmp2
1228c2ecf20Sopenharmony_ci	b	.Lstart_realigned
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	/*src1 and src2 have different alignment offset.*/
1258c2ecf20Sopenharmony_ci.Lmisaligned8:
1268c2ecf20Sopenharmony_ci	cmp	limit, #8
1278c2ecf20Sopenharmony_ci	b.lo	.Ltiny8proc /*limit < 8: compare byte by byte*/
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	and	tmp1, src1, #7
1308c2ecf20Sopenharmony_ci	neg	tmp1, tmp1
1318c2ecf20Sopenharmony_ci	add	tmp1, tmp1, #8/*valid length in the first 8 bytes of src1*/
1328c2ecf20Sopenharmony_ci	and	tmp2, src2, #7
1338c2ecf20Sopenharmony_ci	neg	tmp2, tmp2
1348c2ecf20Sopenharmony_ci	add	tmp2, tmp2, #8/*valid length in the first 8 bytes of src2*/
1358c2ecf20Sopenharmony_ci	subs	tmp3, tmp1, tmp2
1368c2ecf20Sopenharmony_ci	csel	pos, tmp1, tmp2, hi /*Choose the maximum.*/
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	sub	limit, limit, pos
1398c2ecf20Sopenharmony_ci	/*compare the proceeding bytes in the first 8 byte segment.*/
1408c2ecf20Sopenharmony_ci.Ltinycmp:
1418c2ecf20Sopenharmony_ci	ldrb	data1w, [src1], #1
1428c2ecf20Sopenharmony_ci	ldrb	data2w, [src2], #1
1438c2ecf20Sopenharmony_ci	subs	pos, pos, #1
1448c2ecf20Sopenharmony_ci	ccmp	data1w, data2w, #0, ne  /* NZCV = 0b0000.  */
1458c2ecf20Sopenharmony_ci	b.eq	.Ltinycmp
1468c2ecf20Sopenharmony_ci	cbnz	pos, 1f /*diff occurred before the last byte.*/
1478c2ecf20Sopenharmony_ci	cmp	data1w, data2w
1488c2ecf20Sopenharmony_ci	b.eq	.Lstart_align
1498c2ecf20Sopenharmony_ci1:
1508c2ecf20Sopenharmony_ci	sub	result, data1, data2
1518c2ecf20Sopenharmony_ci	ret
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci.Lstart_align:
1548c2ecf20Sopenharmony_ci	lsr	limit_wd, limit, #3
1558c2ecf20Sopenharmony_ci	cbz	limit_wd, .Lremain8
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	ands	xzr, src1, #7
1588c2ecf20Sopenharmony_ci	b.eq	.Lrecal_offset
1598c2ecf20Sopenharmony_ci	/*process more leading bytes to make src1 aligned...*/
1608c2ecf20Sopenharmony_ci	add	src1, src1, tmp3 /*backwards src1 to alignment boundary*/
1618c2ecf20Sopenharmony_ci	add	src2, src2, tmp3
1628c2ecf20Sopenharmony_ci	sub	limit, limit, tmp3
1638c2ecf20Sopenharmony_ci	lsr	limit_wd, limit, #3
1648c2ecf20Sopenharmony_ci	cbz	limit_wd, .Lremain8
1658c2ecf20Sopenharmony_ci	/*load 8 bytes from aligned SRC1..*/
1668c2ecf20Sopenharmony_ci	ldr	data1, [src1], #8
1678c2ecf20Sopenharmony_ci	ldr	data2, [src2], #8
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	subs	limit_wd, limit_wd, #1
1708c2ecf20Sopenharmony_ci	eor	diff, data1, data2  /*Non-zero if differences found.*/
1718c2ecf20Sopenharmony_ci	csinv	endloop, diff, xzr, ne
1728c2ecf20Sopenharmony_ci	cbnz	endloop, .Lunequal_proc
1738c2ecf20Sopenharmony_ci	/*How far is the current SRC2 from the alignment boundary...*/
1748c2ecf20Sopenharmony_ci	and	tmp3, tmp3, #7
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci.Lrecal_offset:/*src1 is aligned now..*/
1778c2ecf20Sopenharmony_ci	neg	pos, tmp3
1788c2ecf20Sopenharmony_ci.Lloopcmp_proc:
1798c2ecf20Sopenharmony_ci	/*
1808c2ecf20Sopenharmony_ci	* Divide the eight bytes into two parts. First,backwards the src2
1818c2ecf20Sopenharmony_ci	* to an alignment boundary,load eight bytes and compare from
1828c2ecf20Sopenharmony_ci	* the SRC2 alignment boundary. If all 8 bytes are equal,then start
1838c2ecf20Sopenharmony_ci	* the second part's comparison. Otherwise finish the comparison.
1848c2ecf20Sopenharmony_ci	* This special handle can garantee all the accesses are in the
1858c2ecf20Sopenharmony_ci	* thread/task space in avoid to overrange access.
1868c2ecf20Sopenharmony_ci	*/
1878c2ecf20Sopenharmony_ci	ldr	data1, [src1,pos]
1888c2ecf20Sopenharmony_ci	ldr	data2, [src2,pos]
1898c2ecf20Sopenharmony_ci	eor	diff, data1, data2  /* Non-zero if differences found.  */
1908c2ecf20Sopenharmony_ci	cbnz	diff, .Lnot_limit
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/*The second part process*/
1938c2ecf20Sopenharmony_ci	ldr	data1, [src1], #8
1948c2ecf20Sopenharmony_ci	ldr	data2, [src2], #8
1958c2ecf20Sopenharmony_ci	eor	diff, data1, data2  /* Non-zero if differences found.  */
1968c2ecf20Sopenharmony_ci	subs	limit_wd, limit_wd, #1
1978c2ecf20Sopenharmony_ci	csinv	endloop, diff, xzr, ne/*if limit_wd is 0,will finish the cmp*/
1988c2ecf20Sopenharmony_ci	cbz	endloop, .Lloopcmp_proc
1998c2ecf20Sopenharmony_ci.Lunequal_proc:
2008c2ecf20Sopenharmony_ci	cbz	diff, .Lremain8
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci/* There is difference occurred in the latest comparison. */
2038c2ecf20Sopenharmony_ci.Lnot_limit:
2048c2ecf20Sopenharmony_ci/*
2058c2ecf20Sopenharmony_ci* For little endian,reverse the low significant equal bits into MSB,then
2068c2ecf20Sopenharmony_ci* following CLZ can find how many equal bits exist.
2078c2ecf20Sopenharmony_ci*/
2088c2ecf20Sopenharmony_ciCPU_LE( rev	diff, diff )
2098c2ecf20Sopenharmony_ciCPU_LE( rev	data1, data1 )
2108c2ecf20Sopenharmony_ciCPU_LE( rev	data2, data2 )
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	/*
2138c2ecf20Sopenharmony_ci	* The MS-non-zero bit of DIFF marks either the first bit
2148c2ecf20Sopenharmony_ci	* that is different, or the end of the significant data.
2158c2ecf20Sopenharmony_ci	* Shifting left now will bring the critical information into the
2168c2ecf20Sopenharmony_ci	* top bits.
2178c2ecf20Sopenharmony_ci	*/
2188c2ecf20Sopenharmony_ci	clz	pos, diff
2198c2ecf20Sopenharmony_ci	lsl	data1, data1, pos
2208c2ecf20Sopenharmony_ci	lsl	data2, data2, pos
2218c2ecf20Sopenharmony_ci	/*
2228c2ecf20Sopenharmony_ci	* We need to zero-extend (char is unsigned) the value and then
2238c2ecf20Sopenharmony_ci	* perform a signed subtraction.
2248c2ecf20Sopenharmony_ci	*/
2258c2ecf20Sopenharmony_ci	lsr	data1, data1, #56
2268c2ecf20Sopenharmony_ci	sub	result, data1, data2, lsr #56
2278c2ecf20Sopenharmony_ci	ret
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci.Lremain8:
2308c2ecf20Sopenharmony_ci	/* Limit % 8 == 0 =>. all data are equal.*/
2318c2ecf20Sopenharmony_ci	ands	limit, limit, #7
2328c2ecf20Sopenharmony_ci	b.eq	.Lret0
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci.Ltiny8proc:
2358c2ecf20Sopenharmony_ci	ldrb	data1w, [src1], #1
2368c2ecf20Sopenharmony_ci	ldrb	data2w, [src2], #1
2378c2ecf20Sopenharmony_ci	subs	limit, limit, #1
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	ccmp	data1w, data2w, #0, ne  /* NZCV = 0b0000. */
2408c2ecf20Sopenharmony_ci	b.eq	.Ltiny8proc
2418c2ecf20Sopenharmony_ci	sub	result, data1, data2
2428c2ecf20Sopenharmony_ci	ret
2438c2ecf20Sopenharmony_ci.Lret0:
2448c2ecf20Sopenharmony_ci	mov	result, #0
2458c2ecf20Sopenharmony_ci	ret
2468c2ecf20Sopenharmony_ciSYM_FUNC_END_PI(memcmp)
2478c2ecf20Sopenharmony_ciEXPORT_SYMBOL_NOKASAN(memcmp)
248