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 * x2 - the maximal length to be compared 238c2ecf20Sopenharmony_ci * Returns: 248c2ecf20Sopenharmony_ci * x0 - an integer less than, equal to, or greater than zero if s1 is found, 258c2ecf20Sopenharmony_ci * respectively, to be less than, to match, 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_cilimit .req x2 368c2ecf20Sopenharmony_ciresult .req x0 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci/* Internal variables. */ 398c2ecf20Sopenharmony_cidata1 .req x3 408c2ecf20Sopenharmony_cidata1w .req w3 418c2ecf20Sopenharmony_cidata2 .req x4 428c2ecf20Sopenharmony_cidata2w .req w4 438c2ecf20Sopenharmony_cihas_nul .req x5 448c2ecf20Sopenharmony_cidiff .req x6 458c2ecf20Sopenharmony_cisyndrome .req x7 468c2ecf20Sopenharmony_citmp1 .req x8 478c2ecf20Sopenharmony_citmp2 .req x9 488c2ecf20Sopenharmony_citmp3 .req x10 498c2ecf20Sopenharmony_cizeroones .req x11 508c2ecf20Sopenharmony_cipos .req x12 518c2ecf20Sopenharmony_cilimit_wd .req x13 528c2ecf20Sopenharmony_cimask .req x14 538c2ecf20Sopenharmony_ciendloop .req x15 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ciSYM_FUNC_START_WEAK_PI(strncmp) 568c2ecf20Sopenharmony_ci cbz limit, .Lret0 578c2ecf20Sopenharmony_ci eor tmp1, src1, src2 588c2ecf20Sopenharmony_ci mov zeroones, #REP8_01 598c2ecf20Sopenharmony_ci tst tmp1, #7 608c2ecf20Sopenharmony_ci b.ne .Lmisaligned8 618c2ecf20Sopenharmony_ci ands tmp1, src1, #7 628c2ecf20Sopenharmony_ci b.ne .Lmutual_align 638c2ecf20Sopenharmony_ci /* Calculate the number of full and partial words -1. */ 648c2ecf20Sopenharmony_ci /* 658c2ecf20Sopenharmony_ci * when limit is mulitply of 8, if not sub 1, 668c2ecf20Sopenharmony_ci * the judgement of last dword will wrong. 678c2ecf20Sopenharmony_ci */ 688c2ecf20Sopenharmony_ci sub limit_wd, limit, #1 /* limit != 0, so no underflow. */ 698c2ecf20Sopenharmony_ci lsr limit_wd, limit_wd, #3 /* Convert to Dwords. */ 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci /* 728c2ecf20Sopenharmony_ci * NUL detection works on the principle that (X - 1) & (~X) & 0x80 738c2ecf20Sopenharmony_ci * (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and 748c2ecf20Sopenharmony_ci * can be done in parallel across the entire word. 758c2ecf20Sopenharmony_ci */ 768c2ecf20Sopenharmony_ci.Lloop_aligned: 778c2ecf20Sopenharmony_ci ldr data1, [src1], #8 788c2ecf20Sopenharmony_ci ldr data2, [src2], #8 798c2ecf20Sopenharmony_ci.Lstart_realigned: 808c2ecf20Sopenharmony_ci subs limit_wd, limit_wd, #1 818c2ecf20Sopenharmony_ci sub tmp1, data1, zeroones 828c2ecf20Sopenharmony_ci orr tmp2, data1, #REP8_7f 838c2ecf20Sopenharmony_ci eor diff, data1, data2 /* Non-zero if differences found. */ 848c2ecf20Sopenharmony_ci csinv endloop, diff, xzr, pl /* Last Dword or differences.*/ 858c2ecf20Sopenharmony_ci bics has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */ 868c2ecf20Sopenharmony_ci ccmp endloop, #0, #0, eq 878c2ecf20Sopenharmony_ci b.eq .Lloop_aligned 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci /*Not reached the limit, must have found the end or a diff. */ 908c2ecf20Sopenharmony_ci tbz limit_wd, #63, .Lnot_limit 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci /* Limit % 8 == 0 => all bytes significant. */ 938c2ecf20Sopenharmony_ci ands limit, limit, #7 948c2ecf20Sopenharmony_ci b.eq .Lnot_limit 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci lsl limit, limit, #3 /* Bits -> bytes. */ 978c2ecf20Sopenharmony_ci mov mask, #~0 988c2ecf20Sopenharmony_ciCPU_BE( lsr mask, mask, limit ) 998c2ecf20Sopenharmony_ciCPU_LE( lsl mask, mask, limit ) 1008c2ecf20Sopenharmony_ci bic data1, data1, mask 1018c2ecf20Sopenharmony_ci bic data2, data2, mask 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci /* Make sure that the NUL byte is marked in the syndrome. */ 1048c2ecf20Sopenharmony_ci orr has_nul, has_nul, mask 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci.Lnot_limit: 1078c2ecf20Sopenharmony_ci orr syndrome, diff, has_nul 1088c2ecf20Sopenharmony_ci b .Lcal_cmpresult 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci.Lmutual_align: 1118c2ecf20Sopenharmony_ci /* 1128c2ecf20Sopenharmony_ci * Sources are mutually aligned, but are not currently at an 1138c2ecf20Sopenharmony_ci * alignment boundary. Round down the addresses and then mask off 1148c2ecf20Sopenharmony_ci * the bytes that precede the start point. 1158c2ecf20Sopenharmony_ci * We also need to adjust the limit calculations, but without 1168c2ecf20Sopenharmony_ci * overflowing if the limit is near ULONG_MAX. 1178c2ecf20Sopenharmony_ci */ 1188c2ecf20Sopenharmony_ci bic src1, src1, #7 1198c2ecf20Sopenharmony_ci bic src2, src2, #7 1208c2ecf20Sopenharmony_ci ldr data1, [src1], #8 1218c2ecf20Sopenharmony_ci neg tmp3, tmp1, lsl #3 /* 64 - bits(bytes beyond align). */ 1228c2ecf20Sopenharmony_ci ldr data2, [src2], #8 1238c2ecf20Sopenharmony_ci mov tmp2, #~0 1248c2ecf20Sopenharmony_ci sub limit_wd, limit, #1 /* limit != 0, so no underflow. */ 1258c2ecf20Sopenharmony_ci /* Big-endian. Early bytes are at MSB. */ 1268c2ecf20Sopenharmony_ciCPU_BE( lsl tmp2, tmp2, tmp3 ) /* Shift (tmp1 & 63). */ 1278c2ecf20Sopenharmony_ci /* Little-endian. Early bytes are at LSB. */ 1288c2ecf20Sopenharmony_ciCPU_LE( lsr tmp2, tmp2, tmp3 ) /* Shift (tmp1 & 63). */ 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci and tmp3, limit_wd, #7 1318c2ecf20Sopenharmony_ci lsr limit_wd, limit_wd, #3 1328c2ecf20Sopenharmony_ci /* Adjust the limit. Only low 3 bits used, so overflow irrelevant.*/ 1338c2ecf20Sopenharmony_ci add limit, limit, tmp1 1348c2ecf20Sopenharmony_ci add tmp3, tmp3, tmp1 1358c2ecf20Sopenharmony_ci orr data1, data1, tmp2 1368c2ecf20Sopenharmony_ci orr data2, data2, tmp2 1378c2ecf20Sopenharmony_ci add limit_wd, limit_wd, tmp3, lsr #3 1388c2ecf20Sopenharmony_ci b .Lstart_realigned 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci/*when src1 offset is not equal to src2 offset...*/ 1418c2ecf20Sopenharmony_ci.Lmisaligned8: 1428c2ecf20Sopenharmony_ci cmp limit, #8 1438c2ecf20Sopenharmony_ci b.lo .Ltiny8proc /*limit < 8... */ 1448c2ecf20Sopenharmony_ci /* 1458c2ecf20Sopenharmony_ci * Get the align offset length to compare per byte first. 1468c2ecf20Sopenharmony_ci * After this process, one string's address will be aligned.*/ 1478c2ecf20Sopenharmony_ci and tmp1, src1, #7 1488c2ecf20Sopenharmony_ci neg tmp1, tmp1 1498c2ecf20Sopenharmony_ci add tmp1, tmp1, #8 1508c2ecf20Sopenharmony_ci and tmp2, src2, #7 1518c2ecf20Sopenharmony_ci neg tmp2, tmp2 1528c2ecf20Sopenharmony_ci add tmp2, tmp2, #8 1538c2ecf20Sopenharmony_ci subs tmp3, tmp1, tmp2 1548c2ecf20Sopenharmony_ci csel pos, tmp1, tmp2, hi /*Choose the maximum. */ 1558c2ecf20Sopenharmony_ci /* 1568c2ecf20Sopenharmony_ci * Here, limit is not less than 8, so directly run .Ltinycmp 1578c2ecf20Sopenharmony_ci * without checking the limit.*/ 1588c2ecf20Sopenharmony_ci sub limit, limit, pos 1598c2ecf20Sopenharmony_ci.Ltinycmp: 1608c2ecf20Sopenharmony_ci ldrb data1w, [src1], #1 1618c2ecf20Sopenharmony_ci ldrb data2w, [src2], #1 1628c2ecf20Sopenharmony_ci subs pos, pos, #1 1638c2ecf20Sopenharmony_ci ccmp data1w, #1, #0, ne /* NZCV = 0b0000. */ 1648c2ecf20Sopenharmony_ci ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */ 1658c2ecf20Sopenharmony_ci b.eq .Ltinycmp 1668c2ecf20Sopenharmony_ci cbnz pos, 1f /*find the null or unequal...*/ 1678c2ecf20Sopenharmony_ci cmp data1w, #1 1688c2ecf20Sopenharmony_ci ccmp data1w, data2w, #0, cs 1698c2ecf20Sopenharmony_ci b.eq .Lstart_align /*the last bytes are equal....*/ 1708c2ecf20Sopenharmony_ci1: 1718c2ecf20Sopenharmony_ci sub result, data1, data2 1728c2ecf20Sopenharmony_ci ret 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci.Lstart_align: 1758c2ecf20Sopenharmony_ci lsr limit_wd, limit, #3 1768c2ecf20Sopenharmony_ci cbz limit_wd, .Lremain8 1778c2ecf20Sopenharmony_ci /*process more leading bytes to make str1 aligned...*/ 1788c2ecf20Sopenharmony_ci ands xzr, src1, #7 1798c2ecf20Sopenharmony_ci b.eq .Lrecal_offset 1808c2ecf20Sopenharmony_ci add src1, src1, tmp3 /*tmp3 is positive in this branch.*/ 1818c2ecf20Sopenharmony_ci add src2, src2, tmp3 1828c2ecf20Sopenharmony_ci ldr data1, [src1], #8 1838c2ecf20Sopenharmony_ci ldr data2, [src2], #8 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci sub limit, limit, tmp3 1868c2ecf20Sopenharmony_ci lsr limit_wd, limit, #3 1878c2ecf20Sopenharmony_ci subs limit_wd, limit_wd, #1 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci sub tmp1, data1, zeroones 1908c2ecf20Sopenharmony_ci orr tmp2, data1, #REP8_7f 1918c2ecf20Sopenharmony_ci eor diff, data1, data2 /* Non-zero if differences found. */ 1928c2ecf20Sopenharmony_ci csinv endloop, diff, xzr, ne/*if limit_wd is 0,will finish the cmp*/ 1938c2ecf20Sopenharmony_ci bics has_nul, tmp1, tmp2 1948c2ecf20Sopenharmony_ci ccmp endloop, #0, #0, eq /*has_null is ZERO: no null byte*/ 1958c2ecf20Sopenharmony_ci b.ne .Lunequal_proc 1968c2ecf20Sopenharmony_ci /*How far is the current str2 from the alignment boundary...*/ 1978c2ecf20Sopenharmony_ci and tmp3, tmp3, #7 1988c2ecf20Sopenharmony_ci.Lrecal_offset: 1998c2ecf20Sopenharmony_ci neg pos, tmp3 2008c2ecf20Sopenharmony_ci.Lloopcmp_proc: 2018c2ecf20Sopenharmony_ci /* 2028c2ecf20Sopenharmony_ci * Divide the eight bytes into two parts. First,backwards the src2 2038c2ecf20Sopenharmony_ci * to an alignment boundary,load eight bytes from the SRC2 alignment 2048c2ecf20Sopenharmony_ci * boundary,then compare with the relative bytes from SRC1. 2058c2ecf20Sopenharmony_ci * If all 8 bytes are equal,then start the second part's comparison. 2068c2ecf20Sopenharmony_ci * Otherwise finish the comparison. 2078c2ecf20Sopenharmony_ci * This special handle can garantee all the accesses are in the 2088c2ecf20Sopenharmony_ci * thread/task space in avoid to overrange access. 2098c2ecf20Sopenharmony_ci */ 2108c2ecf20Sopenharmony_ci ldr data1, [src1,pos] 2118c2ecf20Sopenharmony_ci ldr data2, [src2,pos] 2128c2ecf20Sopenharmony_ci sub tmp1, data1, zeroones 2138c2ecf20Sopenharmony_ci orr tmp2, data1, #REP8_7f 2148c2ecf20Sopenharmony_ci bics has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */ 2158c2ecf20Sopenharmony_ci eor diff, data1, data2 /* Non-zero if differences found. */ 2168c2ecf20Sopenharmony_ci csinv endloop, diff, xzr, eq 2178c2ecf20Sopenharmony_ci cbnz endloop, .Lunequal_proc 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci /*The second part process*/ 2208c2ecf20Sopenharmony_ci ldr data1, [src1], #8 2218c2ecf20Sopenharmony_ci ldr data2, [src2], #8 2228c2ecf20Sopenharmony_ci subs limit_wd, limit_wd, #1 2238c2ecf20Sopenharmony_ci sub tmp1, data1, zeroones 2248c2ecf20Sopenharmony_ci orr tmp2, data1, #REP8_7f 2258c2ecf20Sopenharmony_ci eor diff, data1, data2 /* Non-zero if differences found. */ 2268c2ecf20Sopenharmony_ci csinv endloop, diff, xzr, ne/*if limit_wd is 0,will finish the cmp*/ 2278c2ecf20Sopenharmony_ci bics has_nul, tmp1, tmp2 2288c2ecf20Sopenharmony_ci ccmp endloop, #0, #0, eq /*has_null is ZERO: no null byte*/ 2298c2ecf20Sopenharmony_ci b.eq .Lloopcmp_proc 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci.Lunequal_proc: 2328c2ecf20Sopenharmony_ci orr syndrome, diff, has_nul 2338c2ecf20Sopenharmony_ci cbz syndrome, .Lremain8 2348c2ecf20Sopenharmony_ci.Lcal_cmpresult: 2358c2ecf20Sopenharmony_ci /* 2368c2ecf20Sopenharmony_ci * reversed the byte-order as big-endian,then CLZ can find the most 2378c2ecf20Sopenharmony_ci * significant zero bits. 2388c2ecf20Sopenharmony_ci */ 2398c2ecf20Sopenharmony_ciCPU_LE( rev syndrome, syndrome ) 2408c2ecf20Sopenharmony_ciCPU_LE( rev data1, data1 ) 2418c2ecf20Sopenharmony_ciCPU_LE( rev data2, data2 ) 2428c2ecf20Sopenharmony_ci /* 2438c2ecf20Sopenharmony_ci * For big-endian we cannot use the trick with the syndrome value 2448c2ecf20Sopenharmony_ci * as carry-propagation can corrupt the upper bits if the trailing 2458c2ecf20Sopenharmony_ci * bytes in the string contain 0x01. 2468c2ecf20Sopenharmony_ci * However, if there is no NUL byte in the dword, we can generate 2478c2ecf20Sopenharmony_ci * the result directly. We can't just subtract the bytes as the 2488c2ecf20Sopenharmony_ci * MSB might be significant. 2498c2ecf20Sopenharmony_ci */ 2508c2ecf20Sopenharmony_ciCPU_BE( cbnz has_nul, 1f ) 2518c2ecf20Sopenharmony_ciCPU_BE( cmp data1, data2 ) 2528c2ecf20Sopenharmony_ciCPU_BE( cset result, ne ) 2538c2ecf20Sopenharmony_ciCPU_BE( cneg result, result, lo ) 2548c2ecf20Sopenharmony_ciCPU_BE( ret ) 2558c2ecf20Sopenharmony_ciCPU_BE( 1: ) 2568c2ecf20Sopenharmony_ci /* Re-compute the NUL-byte detection, using a byte-reversed value.*/ 2578c2ecf20Sopenharmony_ciCPU_BE( rev tmp3, data1 ) 2588c2ecf20Sopenharmony_ciCPU_BE( sub tmp1, tmp3, zeroones ) 2598c2ecf20Sopenharmony_ciCPU_BE( orr tmp2, tmp3, #REP8_7f ) 2608c2ecf20Sopenharmony_ciCPU_BE( bic has_nul, tmp1, tmp2 ) 2618c2ecf20Sopenharmony_ciCPU_BE( rev has_nul, has_nul ) 2628c2ecf20Sopenharmony_ciCPU_BE( orr syndrome, diff, has_nul ) 2638c2ecf20Sopenharmony_ci /* 2648c2ecf20Sopenharmony_ci * The MS-non-zero bit of the syndrome marks either the first bit 2658c2ecf20Sopenharmony_ci * that is different, or the top bit of the first zero byte. 2668c2ecf20Sopenharmony_ci * Shifting left now will bring the critical information into the 2678c2ecf20Sopenharmony_ci * top bits. 2688c2ecf20Sopenharmony_ci */ 2698c2ecf20Sopenharmony_ci clz pos, syndrome 2708c2ecf20Sopenharmony_ci lsl data1, data1, pos 2718c2ecf20Sopenharmony_ci lsl data2, data2, pos 2728c2ecf20Sopenharmony_ci /* 2738c2ecf20Sopenharmony_ci * But we need to zero-extend (char is unsigned) the value and then 2748c2ecf20Sopenharmony_ci * perform a signed 32-bit subtraction. 2758c2ecf20Sopenharmony_ci */ 2768c2ecf20Sopenharmony_ci lsr data1, data1, #56 2778c2ecf20Sopenharmony_ci sub result, data1, data2, lsr #56 2788c2ecf20Sopenharmony_ci ret 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci.Lremain8: 2818c2ecf20Sopenharmony_ci /* Limit % 8 == 0 => all bytes significant. */ 2828c2ecf20Sopenharmony_ci ands limit, limit, #7 2838c2ecf20Sopenharmony_ci b.eq .Lret0 2848c2ecf20Sopenharmony_ci.Ltiny8proc: 2858c2ecf20Sopenharmony_ci ldrb data1w, [src1], #1 2868c2ecf20Sopenharmony_ci ldrb data2w, [src2], #1 2878c2ecf20Sopenharmony_ci subs limit, limit, #1 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci ccmp data1w, #1, #0, ne /* NZCV = 0b0000. */ 2908c2ecf20Sopenharmony_ci ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */ 2918c2ecf20Sopenharmony_ci b.eq .Ltiny8proc 2928c2ecf20Sopenharmony_ci sub result, data1, data2 2938c2ecf20Sopenharmony_ci ret 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci.Lret0: 2968c2ecf20Sopenharmony_ci mov result, #0 2978c2ecf20Sopenharmony_ci ret 2988c2ecf20Sopenharmony_ciSYM_FUNC_END_PI(strncmp) 2998c2ecf20Sopenharmony_ciEXPORT_SYMBOL_NOKASAN(strncmp) 300