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 two strings 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * Parameters: 208c2ecf20Sopenharmony_ci * x0 - const string 1 pointer 218c2ecf20Sopenharmony_ci * x1 - const string 2 pointer 228c2ecf20Sopenharmony_ci * Returns: 238c2ecf20Sopenharmony_ci * x0 - an integer less than, equal to, or greater than zero 248c2ecf20Sopenharmony_ci * if s1 is found, respectively, to be less than, to match, 258c2ecf20Sopenharmony_ci * or be greater than s2. 268c2ecf20Sopenharmony_ci */ 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#define REP8_01 0x0101010101010101 298c2ecf20Sopenharmony_ci#define REP8_7f 0x7f7f7f7f7f7f7f7f 308c2ecf20Sopenharmony_ci#define REP8_80 0x8080808080808080 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci/* Parameters and result. */ 338c2ecf20Sopenharmony_cisrc1 .req x0 348c2ecf20Sopenharmony_cisrc2 .req x1 358c2ecf20Sopenharmony_ciresult .req x0 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci/* Internal variables. */ 388c2ecf20Sopenharmony_cidata1 .req x2 398c2ecf20Sopenharmony_cidata1w .req w2 408c2ecf20Sopenharmony_cidata2 .req x3 418c2ecf20Sopenharmony_cidata2w .req w3 428c2ecf20Sopenharmony_cihas_nul .req x4 438c2ecf20Sopenharmony_cidiff .req x5 448c2ecf20Sopenharmony_cisyndrome .req x6 458c2ecf20Sopenharmony_citmp1 .req x7 468c2ecf20Sopenharmony_citmp2 .req x8 478c2ecf20Sopenharmony_citmp3 .req x9 488c2ecf20Sopenharmony_cizeroones .req x10 498c2ecf20Sopenharmony_cipos .req x11 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ciSYM_FUNC_START_WEAK_PI(strcmp) 528c2ecf20Sopenharmony_ci eor tmp1, src1, src2 538c2ecf20Sopenharmony_ci mov zeroones, #REP8_01 548c2ecf20Sopenharmony_ci tst tmp1, #7 558c2ecf20Sopenharmony_ci b.ne .Lmisaligned8 568c2ecf20Sopenharmony_ci ands tmp1, src1, #7 578c2ecf20Sopenharmony_ci b.ne .Lmutual_align 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci /* 608c2ecf20Sopenharmony_ci * NUL detection works on the principle that (X - 1) & (~X) & 0x80 618c2ecf20Sopenharmony_ci * (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and 628c2ecf20Sopenharmony_ci * can be done in parallel across the entire word. 638c2ecf20Sopenharmony_ci */ 648c2ecf20Sopenharmony_ci.Lloop_aligned: 658c2ecf20Sopenharmony_ci ldr data1, [src1], #8 668c2ecf20Sopenharmony_ci ldr data2, [src2], #8 678c2ecf20Sopenharmony_ci.Lstart_realigned: 688c2ecf20Sopenharmony_ci sub tmp1, data1, zeroones 698c2ecf20Sopenharmony_ci orr tmp2, data1, #REP8_7f 708c2ecf20Sopenharmony_ci eor diff, data1, data2 /* Non-zero if differences found. */ 718c2ecf20Sopenharmony_ci bic has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */ 728c2ecf20Sopenharmony_ci orr syndrome, diff, has_nul 738c2ecf20Sopenharmony_ci cbz syndrome, .Lloop_aligned 748c2ecf20Sopenharmony_ci b .Lcal_cmpresult 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci.Lmutual_align: 778c2ecf20Sopenharmony_ci /* 788c2ecf20Sopenharmony_ci * Sources are mutually aligned, but are not currently at an 798c2ecf20Sopenharmony_ci * alignment boundary. Round down the addresses and then mask off 808c2ecf20Sopenharmony_ci * the bytes that preceed the start point. 818c2ecf20Sopenharmony_ci */ 828c2ecf20Sopenharmony_ci bic src1, src1, #7 838c2ecf20Sopenharmony_ci bic src2, src2, #7 848c2ecf20Sopenharmony_ci lsl tmp1, tmp1, #3 /* Bytes beyond alignment -> bits. */ 858c2ecf20Sopenharmony_ci ldr data1, [src1], #8 868c2ecf20Sopenharmony_ci neg tmp1, tmp1 /* Bits to alignment -64. */ 878c2ecf20Sopenharmony_ci ldr data2, [src2], #8 888c2ecf20Sopenharmony_ci mov tmp2, #~0 898c2ecf20Sopenharmony_ci /* Big-endian. Early bytes are at MSB. */ 908c2ecf20Sopenharmony_ciCPU_BE( lsl tmp2, tmp2, tmp1 ) /* Shift (tmp1 & 63). */ 918c2ecf20Sopenharmony_ci /* Little-endian. Early bytes are at LSB. */ 928c2ecf20Sopenharmony_ciCPU_LE( lsr tmp2, tmp2, tmp1 ) /* Shift (tmp1 & 63). */ 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci orr data1, data1, tmp2 958c2ecf20Sopenharmony_ci orr data2, data2, tmp2 968c2ecf20Sopenharmony_ci b .Lstart_realigned 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci.Lmisaligned8: 998c2ecf20Sopenharmony_ci /* 1008c2ecf20Sopenharmony_ci * Get the align offset length to compare per byte first. 1018c2ecf20Sopenharmony_ci * After this process, one string's address will be aligned. 1028c2ecf20Sopenharmony_ci */ 1038c2ecf20Sopenharmony_ci and tmp1, src1, #7 1048c2ecf20Sopenharmony_ci neg tmp1, tmp1 1058c2ecf20Sopenharmony_ci add tmp1, tmp1, #8 1068c2ecf20Sopenharmony_ci and tmp2, src2, #7 1078c2ecf20Sopenharmony_ci neg tmp2, tmp2 1088c2ecf20Sopenharmony_ci add tmp2, tmp2, #8 1098c2ecf20Sopenharmony_ci subs tmp3, tmp1, tmp2 1108c2ecf20Sopenharmony_ci csel pos, tmp1, tmp2, hi /*Choose the maximum. */ 1118c2ecf20Sopenharmony_ci.Ltinycmp: 1128c2ecf20Sopenharmony_ci ldrb data1w, [src1], #1 1138c2ecf20Sopenharmony_ci ldrb data2w, [src2], #1 1148c2ecf20Sopenharmony_ci subs pos, pos, #1 1158c2ecf20Sopenharmony_ci ccmp data1w, #1, #0, ne /* NZCV = 0b0000. */ 1168c2ecf20Sopenharmony_ci ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */ 1178c2ecf20Sopenharmony_ci b.eq .Ltinycmp 1188c2ecf20Sopenharmony_ci cbnz pos, 1f /*find the null or unequal...*/ 1198c2ecf20Sopenharmony_ci cmp data1w, #1 1208c2ecf20Sopenharmony_ci ccmp data1w, data2w, #0, cs 1218c2ecf20Sopenharmony_ci b.eq .Lstart_align /*the last bytes are equal....*/ 1228c2ecf20Sopenharmony_ci1: 1238c2ecf20Sopenharmony_ci sub result, data1, data2 1248c2ecf20Sopenharmony_ci ret 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci.Lstart_align: 1278c2ecf20Sopenharmony_ci ands xzr, src1, #7 1288c2ecf20Sopenharmony_ci b.eq .Lrecal_offset 1298c2ecf20Sopenharmony_ci /*process more leading bytes to make str1 aligned...*/ 1308c2ecf20Sopenharmony_ci add src1, src1, tmp3 1318c2ecf20Sopenharmony_ci add src2, src2, tmp3 1328c2ecf20Sopenharmony_ci /*load 8 bytes from aligned str1 and non-aligned str2..*/ 1338c2ecf20Sopenharmony_ci ldr data1, [src1], #8 1348c2ecf20Sopenharmony_ci ldr data2, [src2], #8 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci sub tmp1, data1, zeroones 1378c2ecf20Sopenharmony_ci orr tmp2, data1, #REP8_7f 1388c2ecf20Sopenharmony_ci bic has_nul, tmp1, tmp2 1398c2ecf20Sopenharmony_ci eor diff, data1, data2 /* Non-zero if differences found. */ 1408c2ecf20Sopenharmony_ci orr syndrome, diff, has_nul 1418c2ecf20Sopenharmony_ci cbnz syndrome, .Lcal_cmpresult 1428c2ecf20Sopenharmony_ci /*How far is the current str2 from the alignment boundary...*/ 1438c2ecf20Sopenharmony_ci and tmp3, tmp3, #7 1448c2ecf20Sopenharmony_ci.Lrecal_offset: 1458c2ecf20Sopenharmony_ci neg pos, tmp3 1468c2ecf20Sopenharmony_ci.Lloopcmp_proc: 1478c2ecf20Sopenharmony_ci /* 1488c2ecf20Sopenharmony_ci * Divide the eight bytes into two parts. First,backwards the src2 1498c2ecf20Sopenharmony_ci * to an alignment boundary,load eight bytes from the SRC2 alignment 1508c2ecf20Sopenharmony_ci * boundary,then compare with the relative bytes from SRC1. 1518c2ecf20Sopenharmony_ci * If all 8 bytes are equal,then start the second part's comparison. 1528c2ecf20Sopenharmony_ci * Otherwise finish the comparison. 1538c2ecf20Sopenharmony_ci * This special handle can garantee all the accesses are in the 1548c2ecf20Sopenharmony_ci * thread/task space in avoid to overrange access. 1558c2ecf20Sopenharmony_ci */ 1568c2ecf20Sopenharmony_ci ldr data1, [src1,pos] 1578c2ecf20Sopenharmony_ci ldr data2, [src2,pos] 1588c2ecf20Sopenharmony_ci sub tmp1, data1, zeroones 1598c2ecf20Sopenharmony_ci orr tmp2, data1, #REP8_7f 1608c2ecf20Sopenharmony_ci bic has_nul, tmp1, tmp2 1618c2ecf20Sopenharmony_ci eor diff, data1, data2 /* Non-zero if differences found. */ 1628c2ecf20Sopenharmony_ci orr syndrome, diff, has_nul 1638c2ecf20Sopenharmony_ci cbnz syndrome, .Lcal_cmpresult 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci /*The second part process*/ 1668c2ecf20Sopenharmony_ci ldr data1, [src1], #8 1678c2ecf20Sopenharmony_ci ldr data2, [src2], #8 1688c2ecf20Sopenharmony_ci sub tmp1, data1, zeroones 1698c2ecf20Sopenharmony_ci orr tmp2, data1, #REP8_7f 1708c2ecf20Sopenharmony_ci bic has_nul, tmp1, tmp2 1718c2ecf20Sopenharmony_ci eor diff, data1, data2 /* Non-zero if differences found. */ 1728c2ecf20Sopenharmony_ci orr syndrome, diff, has_nul 1738c2ecf20Sopenharmony_ci cbz syndrome, .Lloopcmp_proc 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci.Lcal_cmpresult: 1768c2ecf20Sopenharmony_ci /* 1778c2ecf20Sopenharmony_ci * reversed the byte-order as big-endian,then CLZ can find the most 1788c2ecf20Sopenharmony_ci * significant zero bits. 1798c2ecf20Sopenharmony_ci */ 1808c2ecf20Sopenharmony_ciCPU_LE( rev syndrome, syndrome ) 1818c2ecf20Sopenharmony_ciCPU_LE( rev data1, data1 ) 1828c2ecf20Sopenharmony_ciCPU_LE( rev data2, data2 ) 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci /* 1858c2ecf20Sopenharmony_ci * For big-endian we cannot use the trick with the syndrome value 1868c2ecf20Sopenharmony_ci * as carry-propagation can corrupt the upper bits if the trailing 1878c2ecf20Sopenharmony_ci * bytes in the string contain 0x01. 1888c2ecf20Sopenharmony_ci * However, if there is no NUL byte in the dword, we can generate 1898c2ecf20Sopenharmony_ci * the result directly. We cannot just subtract the bytes as the 1908c2ecf20Sopenharmony_ci * MSB might be significant. 1918c2ecf20Sopenharmony_ci */ 1928c2ecf20Sopenharmony_ciCPU_BE( cbnz has_nul, 1f ) 1938c2ecf20Sopenharmony_ciCPU_BE( cmp data1, data2 ) 1948c2ecf20Sopenharmony_ciCPU_BE( cset result, ne ) 1958c2ecf20Sopenharmony_ciCPU_BE( cneg result, result, lo ) 1968c2ecf20Sopenharmony_ciCPU_BE( ret ) 1978c2ecf20Sopenharmony_ciCPU_BE( 1: ) 1988c2ecf20Sopenharmony_ci /*Re-compute the NUL-byte detection, using a byte-reversed value. */ 1998c2ecf20Sopenharmony_ciCPU_BE( rev tmp3, data1 ) 2008c2ecf20Sopenharmony_ciCPU_BE( sub tmp1, tmp3, zeroones ) 2018c2ecf20Sopenharmony_ciCPU_BE( orr tmp2, tmp3, #REP8_7f ) 2028c2ecf20Sopenharmony_ciCPU_BE( bic has_nul, tmp1, tmp2 ) 2038c2ecf20Sopenharmony_ciCPU_BE( rev has_nul, has_nul ) 2048c2ecf20Sopenharmony_ciCPU_BE( orr syndrome, diff, has_nul ) 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci clz pos, syndrome 2078c2ecf20Sopenharmony_ci /* 2088c2ecf20Sopenharmony_ci * The MS-non-zero bit of the syndrome marks either the first bit 2098c2ecf20Sopenharmony_ci * that is different, or the top bit of the first zero byte. 2108c2ecf20Sopenharmony_ci * Shifting left now will bring the critical information into the 2118c2ecf20Sopenharmony_ci * top bits. 2128c2ecf20Sopenharmony_ci */ 2138c2ecf20Sopenharmony_ci lsl data1, data1, pos 2148c2ecf20Sopenharmony_ci lsl data2, data2, pos 2158c2ecf20Sopenharmony_ci /* 2168c2ecf20Sopenharmony_ci * But we need to zero-extend (char is unsigned) the value and then 2178c2ecf20Sopenharmony_ci * perform a signed 32-bit subtraction. 2188c2ecf20Sopenharmony_ci */ 2198c2ecf20Sopenharmony_ci lsr data1, data1, #56 2208c2ecf20Sopenharmony_ci sub result, data1, data2, lsr #56 2218c2ecf20Sopenharmony_ci ret 2228c2ecf20Sopenharmony_ciSYM_FUNC_END_PI(strcmp) 2238c2ecf20Sopenharmony_ciEXPORT_SYMBOL_NOKASAN(strcmp) 224