1*4882a593Smuzhiyun/* SPDX-License-Identifier: GPL-2.0-only */ 2*4882a593Smuzhiyun/* 3*4882a593Smuzhiyun * Copyright (C) 2013 ARM Ltd. 4*4882a593Smuzhiyun * Copyright (C) 2013 Linaro. 5*4882a593Smuzhiyun * 6*4882a593Smuzhiyun * This code is based on glibc cortex strings work originally authored by Linaro 7*4882a593Smuzhiyun * be found @ 8*4882a593Smuzhiyun * 9*4882a593Smuzhiyun * http://bazaar.launchpad.net/~linaro-toolchain-dev/cortex-strings/trunk/ 10*4882a593Smuzhiyun * files/head:/src/aarch64/ 11*4882a593Smuzhiyun */ 12*4882a593Smuzhiyun 13*4882a593Smuzhiyun#include <linux/linkage.h> 14*4882a593Smuzhiyun#include <asm/assembler.h> 15*4882a593Smuzhiyun 16*4882a593Smuzhiyun/* 17*4882a593Smuzhiyun * compare two strings 18*4882a593Smuzhiyun * 19*4882a593Smuzhiyun * Parameters: 20*4882a593Smuzhiyun * x0 - const string 1 pointer 21*4882a593Smuzhiyun * x1 - const string 2 pointer 22*4882a593Smuzhiyun * x2 - the maximal length to be compared 23*4882a593Smuzhiyun * Returns: 24*4882a593Smuzhiyun * x0 - an integer less than, equal to, or greater than zero if s1 is found, 25*4882a593Smuzhiyun * respectively, to be less than, to match, or be greater than s2. 26*4882a593Smuzhiyun */ 27*4882a593Smuzhiyun 28*4882a593Smuzhiyun#define REP8_01 0x0101010101010101 29*4882a593Smuzhiyun#define REP8_7f 0x7f7f7f7f7f7f7f7f 30*4882a593Smuzhiyun#define REP8_80 0x8080808080808080 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun/* Parameters and result. */ 33*4882a593Smuzhiyunsrc1 .req x0 34*4882a593Smuzhiyunsrc2 .req x1 35*4882a593Smuzhiyunlimit .req x2 36*4882a593Smuzhiyunresult .req x0 37*4882a593Smuzhiyun 38*4882a593Smuzhiyun/* Internal variables. */ 39*4882a593Smuzhiyundata1 .req x3 40*4882a593Smuzhiyundata1w .req w3 41*4882a593Smuzhiyundata2 .req x4 42*4882a593Smuzhiyundata2w .req w4 43*4882a593Smuzhiyunhas_nul .req x5 44*4882a593Smuzhiyundiff .req x6 45*4882a593Smuzhiyunsyndrome .req x7 46*4882a593Smuzhiyuntmp1 .req x8 47*4882a593Smuzhiyuntmp2 .req x9 48*4882a593Smuzhiyuntmp3 .req x10 49*4882a593Smuzhiyunzeroones .req x11 50*4882a593Smuzhiyunpos .req x12 51*4882a593Smuzhiyunlimit_wd .req x13 52*4882a593Smuzhiyunmask .req x14 53*4882a593Smuzhiyunendloop .req x15 54*4882a593Smuzhiyun 55*4882a593SmuzhiyunSYM_FUNC_START_WEAK_PI(strncmp) 56*4882a593Smuzhiyun cbz limit, .Lret0 57*4882a593Smuzhiyun eor tmp1, src1, src2 58*4882a593Smuzhiyun mov zeroones, #REP8_01 59*4882a593Smuzhiyun tst tmp1, #7 60*4882a593Smuzhiyun b.ne .Lmisaligned8 61*4882a593Smuzhiyun ands tmp1, src1, #7 62*4882a593Smuzhiyun b.ne .Lmutual_align 63*4882a593Smuzhiyun /* Calculate the number of full and partial words -1. */ 64*4882a593Smuzhiyun /* 65*4882a593Smuzhiyun * when limit is mulitply of 8, if not sub 1, 66*4882a593Smuzhiyun * the judgement of last dword will wrong. 67*4882a593Smuzhiyun */ 68*4882a593Smuzhiyun sub limit_wd, limit, #1 /* limit != 0, so no underflow. */ 69*4882a593Smuzhiyun lsr limit_wd, limit_wd, #3 /* Convert to Dwords. */ 70*4882a593Smuzhiyun 71*4882a593Smuzhiyun /* 72*4882a593Smuzhiyun * NUL detection works on the principle that (X - 1) & (~X) & 0x80 73*4882a593Smuzhiyun * (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and 74*4882a593Smuzhiyun * can be done in parallel across the entire word. 75*4882a593Smuzhiyun */ 76*4882a593Smuzhiyun.Lloop_aligned: 77*4882a593Smuzhiyun ldr data1, [src1], #8 78*4882a593Smuzhiyun ldr data2, [src2], #8 79*4882a593Smuzhiyun.Lstart_realigned: 80*4882a593Smuzhiyun subs limit_wd, limit_wd, #1 81*4882a593Smuzhiyun sub tmp1, data1, zeroones 82*4882a593Smuzhiyun orr tmp2, data1, #REP8_7f 83*4882a593Smuzhiyun eor diff, data1, data2 /* Non-zero if differences found. */ 84*4882a593Smuzhiyun csinv endloop, diff, xzr, pl /* Last Dword or differences.*/ 85*4882a593Smuzhiyun bics has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */ 86*4882a593Smuzhiyun ccmp endloop, #0, #0, eq 87*4882a593Smuzhiyun b.eq .Lloop_aligned 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun /*Not reached the limit, must have found the end or a diff. */ 90*4882a593Smuzhiyun tbz limit_wd, #63, .Lnot_limit 91*4882a593Smuzhiyun 92*4882a593Smuzhiyun /* Limit % 8 == 0 => all bytes significant. */ 93*4882a593Smuzhiyun ands limit, limit, #7 94*4882a593Smuzhiyun b.eq .Lnot_limit 95*4882a593Smuzhiyun 96*4882a593Smuzhiyun lsl limit, limit, #3 /* Bits -> bytes. */ 97*4882a593Smuzhiyun mov mask, #~0 98*4882a593SmuzhiyunCPU_BE( lsr mask, mask, limit ) 99*4882a593SmuzhiyunCPU_LE( lsl mask, mask, limit ) 100*4882a593Smuzhiyun bic data1, data1, mask 101*4882a593Smuzhiyun bic data2, data2, mask 102*4882a593Smuzhiyun 103*4882a593Smuzhiyun /* Make sure that the NUL byte is marked in the syndrome. */ 104*4882a593Smuzhiyun orr has_nul, has_nul, mask 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun.Lnot_limit: 107*4882a593Smuzhiyun orr syndrome, diff, has_nul 108*4882a593Smuzhiyun b .Lcal_cmpresult 109*4882a593Smuzhiyun 110*4882a593Smuzhiyun.Lmutual_align: 111*4882a593Smuzhiyun /* 112*4882a593Smuzhiyun * Sources are mutually aligned, but are not currently at an 113*4882a593Smuzhiyun * alignment boundary. Round down the addresses and then mask off 114*4882a593Smuzhiyun * the bytes that precede the start point. 115*4882a593Smuzhiyun * We also need to adjust the limit calculations, but without 116*4882a593Smuzhiyun * overflowing if the limit is near ULONG_MAX. 117*4882a593Smuzhiyun */ 118*4882a593Smuzhiyun bic src1, src1, #7 119*4882a593Smuzhiyun bic src2, src2, #7 120*4882a593Smuzhiyun ldr data1, [src1], #8 121*4882a593Smuzhiyun neg tmp3, tmp1, lsl #3 /* 64 - bits(bytes beyond align). */ 122*4882a593Smuzhiyun ldr data2, [src2], #8 123*4882a593Smuzhiyun mov tmp2, #~0 124*4882a593Smuzhiyun sub limit_wd, limit, #1 /* limit != 0, so no underflow. */ 125*4882a593Smuzhiyun /* Big-endian. Early bytes are at MSB. */ 126*4882a593SmuzhiyunCPU_BE( lsl tmp2, tmp2, tmp3 ) /* Shift (tmp1 & 63). */ 127*4882a593Smuzhiyun /* Little-endian. Early bytes are at LSB. */ 128*4882a593SmuzhiyunCPU_LE( lsr tmp2, tmp2, tmp3 ) /* Shift (tmp1 & 63). */ 129*4882a593Smuzhiyun 130*4882a593Smuzhiyun and tmp3, limit_wd, #7 131*4882a593Smuzhiyun lsr limit_wd, limit_wd, #3 132*4882a593Smuzhiyun /* Adjust the limit. Only low 3 bits used, so overflow irrelevant.*/ 133*4882a593Smuzhiyun add limit, limit, tmp1 134*4882a593Smuzhiyun add tmp3, tmp3, tmp1 135*4882a593Smuzhiyun orr data1, data1, tmp2 136*4882a593Smuzhiyun orr data2, data2, tmp2 137*4882a593Smuzhiyun add limit_wd, limit_wd, tmp3, lsr #3 138*4882a593Smuzhiyun b .Lstart_realigned 139*4882a593Smuzhiyun 140*4882a593Smuzhiyun/*when src1 offset is not equal to src2 offset...*/ 141*4882a593Smuzhiyun.Lmisaligned8: 142*4882a593Smuzhiyun cmp limit, #8 143*4882a593Smuzhiyun b.lo .Ltiny8proc /*limit < 8... */ 144*4882a593Smuzhiyun /* 145*4882a593Smuzhiyun * Get the align offset length to compare per byte first. 146*4882a593Smuzhiyun * After this process, one string's address will be aligned.*/ 147*4882a593Smuzhiyun and tmp1, src1, #7 148*4882a593Smuzhiyun neg tmp1, tmp1 149*4882a593Smuzhiyun add tmp1, tmp1, #8 150*4882a593Smuzhiyun and tmp2, src2, #7 151*4882a593Smuzhiyun neg tmp2, tmp2 152*4882a593Smuzhiyun add tmp2, tmp2, #8 153*4882a593Smuzhiyun subs tmp3, tmp1, tmp2 154*4882a593Smuzhiyun csel pos, tmp1, tmp2, hi /*Choose the maximum. */ 155*4882a593Smuzhiyun /* 156*4882a593Smuzhiyun * Here, limit is not less than 8, so directly run .Ltinycmp 157*4882a593Smuzhiyun * without checking the limit.*/ 158*4882a593Smuzhiyun sub limit, limit, pos 159*4882a593Smuzhiyun.Ltinycmp: 160*4882a593Smuzhiyun ldrb data1w, [src1], #1 161*4882a593Smuzhiyun ldrb data2w, [src2], #1 162*4882a593Smuzhiyun subs pos, pos, #1 163*4882a593Smuzhiyun ccmp data1w, #1, #0, ne /* NZCV = 0b0000. */ 164*4882a593Smuzhiyun ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */ 165*4882a593Smuzhiyun b.eq .Ltinycmp 166*4882a593Smuzhiyun cbnz pos, 1f /*find the null or unequal...*/ 167*4882a593Smuzhiyun cmp data1w, #1 168*4882a593Smuzhiyun ccmp data1w, data2w, #0, cs 169*4882a593Smuzhiyun b.eq .Lstart_align /*the last bytes are equal....*/ 170*4882a593Smuzhiyun1: 171*4882a593Smuzhiyun sub result, data1, data2 172*4882a593Smuzhiyun ret 173*4882a593Smuzhiyun 174*4882a593Smuzhiyun.Lstart_align: 175*4882a593Smuzhiyun lsr limit_wd, limit, #3 176*4882a593Smuzhiyun cbz limit_wd, .Lremain8 177*4882a593Smuzhiyun /*process more leading bytes to make str1 aligned...*/ 178*4882a593Smuzhiyun ands xzr, src1, #7 179*4882a593Smuzhiyun b.eq .Lrecal_offset 180*4882a593Smuzhiyun add src1, src1, tmp3 /*tmp3 is positive in this branch.*/ 181*4882a593Smuzhiyun add src2, src2, tmp3 182*4882a593Smuzhiyun ldr data1, [src1], #8 183*4882a593Smuzhiyun ldr data2, [src2], #8 184*4882a593Smuzhiyun 185*4882a593Smuzhiyun sub limit, limit, tmp3 186*4882a593Smuzhiyun lsr limit_wd, limit, #3 187*4882a593Smuzhiyun subs limit_wd, limit_wd, #1 188*4882a593Smuzhiyun 189*4882a593Smuzhiyun sub tmp1, data1, zeroones 190*4882a593Smuzhiyun orr tmp2, data1, #REP8_7f 191*4882a593Smuzhiyun eor diff, data1, data2 /* Non-zero if differences found. */ 192*4882a593Smuzhiyun csinv endloop, diff, xzr, ne/*if limit_wd is 0,will finish the cmp*/ 193*4882a593Smuzhiyun bics has_nul, tmp1, tmp2 194*4882a593Smuzhiyun ccmp endloop, #0, #0, eq /*has_null is ZERO: no null byte*/ 195*4882a593Smuzhiyun b.ne .Lunequal_proc 196*4882a593Smuzhiyun /*How far is the current str2 from the alignment boundary...*/ 197*4882a593Smuzhiyun and tmp3, tmp3, #7 198*4882a593Smuzhiyun.Lrecal_offset: 199*4882a593Smuzhiyun neg pos, tmp3 200*4882a593Smuzhiyun.Lloopcmp_proc: 201*4882a593Smuzhiyun /* 202*4882a593Smuzhiyun * Divide the eight bytes into two parts. First,backwards the src2 203*4882a593Smuzhiyun * to an alignment boundary,load eight bytes from the SRC2 alignment 204*4882a593Smuzhiyun * boundary,then compare with the relative bytes from SRC1. 205*4882a593Smuzhiyun * If all 8 bytes are equal,then start the second part's comparison. 206*4882a593Smuzhiyun * Otherwise finish the comparison. 207*4882a593Smuzhiyun * This special handle can garantee all the accesses are in the 208*4882a593Smuzhiyun * thread/task space in avoid to overrange access. 209*4882a593Smuzhiyun */ 210*4882a593Smuzhiyun ldr data1, [src1,pos] 211*4882a593Smuzhiyun ldr data2, [src2,pos] 212*4882a593Smuzhiyun sub tmp1, data1, zeroones 213*4882a593Smuzhiyun orr tmp2, data1, #REP8_7f 214*4882a593Smuzhiyun bics has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */ 215*4882a593Smuzhiyun eor diff, data1, data2 /* Non-zero if differences found. */ 216*4882a593Smuzhiyun csinv endloop, diff, xzr, eq 217*4882a593Smuzhiyun cbnz endloop, .Lunequal_proc 218*4882a593Smuzhiyun 219*4882a593Smuzhiyun /*The second part process*/ 220*4882a593Smuzhiyun ldr data1, [src1], #8 221*4882a593Smuzhiyun ldr data2, [src2], #8 222*4882a593Smuzhiyun subs limit_wd, limit_wd, #1 223*4882a593Smuzhiyun sub tmp1, data1, zeroones 224*4882a593Smuzhiyun orr tmp2, data1, #REP8_7f 225*4882a593Smuzhiyun eor diff, data1, data2 /* Non-zero if differences found. */ 226*4882a593Smuzhiyun csinv endloop, diff, xzr, ne/*if limit_wd is 0,will finish the cmp*/ 227*4882a593Smuzhiyun bics has_nul, tmp1, tmp2 228*4882a593Smuzhiyun ccmp endloop, #0, #0, eq /*has_null is ZERO: no null byte*/ 229*4882a593Smuzhiyun b.eq .Lloopcmp_proc 230*4882a593Smuzhiyun 231*4882a593Smuzhiyun.Lunequal_proc: 232*4882a593Smuzhiyun orr syndrome, diff, has_nul 233*4882a593Smuzhiyun cbz syndrome, .Lremain8 234*4882a593Smuzhiyun.Lcal_cmpresult: 235*4882a593Smuzhiyun /* 236*4882a593Smuzhiyun * reversed the byte-order as big-endian,then CLZ can find the most 237*4882a593Smuzhiyun * significant zero bits. 238*4882a593Smuzhiyun */ 239*4882a593SmuzhiyunCPU_LE( rev syndrome, syndrome ) 240*4882a593SmuzhiyunCPU_LE( rev data1, data1 ) 241*4882a593SmuzhiyunCPU_LE( rev data2, data2 ) 242*4882a593Smuzhiyun /* 243*4882a593Smuzhiyun * For big-endian we cannot use the trick with the syndrome value 244*4882a593Smuzhiyun * as carry-propagation can corrupt the upper bits if the trailing 245*4882a593Smuzhiyun * bytes in the string contain 0x01. 246*4882a593Smuzhiyun * However, if there is no NUL byte in the dword, we can generate 247*4882a593Smuzhiyun * the result directly. We can't just subtract the bytes as the 248*4882a593Smuzhiyun * MSB might be significant. 249*4882a593Smuzhiyun */ 250*4882a593SmuzhiyunCPU_BE( cbnz has_nul, 1f ) 251*4882a593SmuzhiyunCPU_BE( cmp data1, data2 ) 252*4882a593SmuzhiyunCPU_BE( cset result, ne ) 253*4882a593SmuzhiyunCPU_BE( cneg result, result, lo ) 254*4882a593SmuzhiyunCPU_BE( ret ) 255*4882a593SmuzhiyunCPU_BE( 1: ) 256*4882a593Smuzhiyun /* Re-compute the NUL-byte detection, using a byte-reversed value.*/ 257*4882a593SmuzhiyunCPU_BE( rev tmp3, data1 ) 258*4882a593SmuzhiyunCPU_BE( sub tmp1, tmp3, zeroones ) 259*4882a593SmuzhiyunCPU_BE( orr tmp2, tmp3, #REP8_7f ) 260*4882a593SmuzhiyunCPU_BE( bic has_nul, tmp1, tmp2 ) 261*4882a593SmuzhiyunCPU_BE( rev has_nul, has_nul ) 262*4882a593SmuzhiyunCPU_BE( orr syndrome, diff, has_nul ) 263*4882a593Smuzhiyun /* 264*4882a593Smuzhiyun * The MS-non-zero bit of the syndrome marks either the first bit 265*4882a593Smuzhiyun * that is different, or the top bit of the first zero byte. 266*4882a593Smuzhiyun * Shifting left now will bring the critical information into the 267*4882a593Smuzhiyun * top bits. 268*4882a593Smuzhiyun */ 269*4882a593Smuzhiyun clz pos, syndrome 270*4882a593Smuzhiyun lsl data1, data1, pos 271*4882a593Smuzhiyun lsl data2, data2, pos 272*4882a593Smuzhiyun /* 273*4882a593Smuzhiyun * But we need to zero-extend (char is unsigned) the value and then 274*4882a593Smuzhiyun * perform a signed 32-bit subtraction. 275*4882a593Smuzhiyun */ 276*4882a593Smuzhiyun lsr data1, data1, #56 277*4882a593Smuzhiyun sub result, data1, data2, lsr #56 278*4882a593Smuzhiyun ret 279*4882a593Smuzhiyun 280*4882a593Smuzhiyun.Lremain8: 281*4882a593Smuzhiyun /* Limit % 8 == 0 => all bytes significant. */ 282*4882a593Smuzhiyun ands limit, limit, #7 283*4882a593Smuzhiyun b.eq .Lret0 284*4882a593Smuzhiyun.Ltiny8proc: 285*4882a593Smuzhiyun ldrb data1w, [src1], #1 286*4882a593Smuzhiyun ldrb data2w, [src2], #1 287*4882a593Smuzhiyun subs limit, limit, #1 288*4882a593Smuzhiyun 289*4882a593Smuzhiyun ccmp data1w, #1, #0, ne /* NZCV = 0b0000. */ 290*4882a593Smuzhiyun ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */ 291*4882a593Smuzhiyun b.eq .Ltiny8proc 292*4882a593Smuzhiyun sub result, data1, data2 293*4882a593Smuzhiyun ret 294*4882a593Smuzhiyun 295*4882a593Smuzhiyun.Lret0: 296*4882a593Smuzhiyun mov result, #0 297*4882a593Smuzhiyun ret 298*4882a593SmuzhiyunSYM_FUNC_END_PI(strncmp) 299*4882a593SmuzhiyunEXPORT_SYMBOL_NOKASAN(strncmp) 300