1*4882a593Smuzhiyun/* SPDX-License-Identifier: GPL-2.0 */ 2*4882a593Smuzhiyun/* 3*4882a593Smuzhiyun * arch/alpha/lib/ev6-stxcpy.S 4*4882a593Smuzhiyun * 21264 version contributed by Rick Gorton <rick.gorton@alpha-processor.com> 5*4882a593Smuzhiyun * 6*4882a593Smuzhiyun * Copy a null-terminated string from SRC to DST. 7*4882a593Smuzhiyun * 8*4882a593Smuzhiyun * This is an internal routine used by strcpy, stpcpy, and strcat. 9*4882a593Smuzhiyun * As such, it uses special linkage conventions to make implementation 10*4882a593Smuzhiyun * of these public functions more efficient. 11*4882a593Smuzhiyun * 12*4882a593Smuzhiyun * On input: 13*4882a593Smuzhiyun * t9 = return address 14*4882a593Smuzhiyun * a0 = DST 15*4882a593Smuzhiyun * a1 = SRC 16*4882a593Smuzhiyun * 17*4882a593Smuzhiyun * On output: 18*4882a593Smuzhiyun * t12 = bitmask (with one bit set) indicating the last byte written 19*4882a593Smuzhiyun * a0 = unaligned address of the last *word* written 20*4882a593Smuzhiyun * 21*4882a593Smuzhiyun * Furthermore, v0, a3-a5, t11, and t12 are untouched. 22*4882a593Smuzhiyun * 23*4882a593Smuzhiyun * Much of the information about 21264 scheduling/coding comes from: 24*4882a593Smuzhiyun * Compiler Writer's Guide for the Alpha 21264 25*4882a593Smuzhiyun * abbreviated as 'CWG' in other comments here 26*4882a593Smuzhiyun * ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html 27*4882a593Smuzhiyun * Scheduling notation: 28*4882a593Smuzhiyun * E - either cluster 29*4882a593Smuzhiyun * U - upper subcluster; U0 - subcluster U0; U1 - subcluster U1 30*4882a593Smuzhiyun * L - lower subcluster; L0 - subcluster L0; L1 - subcluster L1 31*4882a593Smuzhiyun * Try not to change the actual algorithm if possible for consistency. 32*4882a593Smuzhiyun */ 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun#include <asm/regdef.h> 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun .set noat 37*4882a593Smuzhiyun .set noreorder 38*4882a593Smuzhiyun 39*4882a593Smuzhiyun .text 40*4882a593Smuzhiyun 41*4882a593Smuzhiyun/* There is a problem with either gdb (as of 4.16) or gas (as of 2.7) that 42*4882a593Smuzhiyun doesn't like putting the entry point for a procedure somewhere in the 43*4882a593Smuzhiyun middle of the procedure descriptor. Work around this by putting the 44*4882a593Smuzhiyun aligned copy in its own procedure descriptor */ 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun 47*4882a593Smuzhiyun .ent stxcpy_aligned 48*4882a593Smuzhiyun .align 4 49*4882a593Smuzhiyunstxcpy_aligned: 50*4882a593Smuzhiyun .frame sp, 0, t9 51*4882a593Smuzhiyun .prologue 0 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun /* On entry to this basic block: 54*4882a593Smuzhiyun t0 == the first destination word for masking back in 55*4882a593Smuzhiyun t1 == the first source word. */ 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun /* Create the 1st output word and detect 0's in the 1st input word. */ 58*4882a593Smuzhiyun lda t2, -1 # E : build a mask against false zero 59*4882a593Smuzhiyun mskqh t2, a1, t2 # U : detection in the src word (stall) 60*4882a593Smuzhiyun mskqh t1, a1, t3 # U : 61*4882a593Smuzhiyun ornot t1, t2, t2 # E : (stall) 62*4882a593Smuzhiyun 63*4882a593Smuzhiyun mskql t0, a1, t0 # U : assemble the first output word 64*4882a593Smuzhiyun cmpbge zero, t2, t8 # E : bits set iff null found 65*4882a593Smuzhiyun or t0, t3, t1 # E : (stall) 66*4882a593Smuzhiyun bne t8, $a_eos # U : (stall) 67*4882a593Smuzhiyun 68*4882a593Smuzhiyun /* On entry to this basic block: 69*4882a593Smuzhiyun t0 == the first destination word for masking back in 70*4882a593Smuzhiyun t1 == a source word not containing a null. */ 71*4882a593Smuzhiyun /* Nops here to separate store quads from load quads */ 72*4882a593Smuzhiyun 73*4882a593Smuzhiyun$a_loop: 74*4882a593Smuzhiyun stq_u t1, 0(a0) # L : 75*4882a593Smuzhiyun addq a0, 8, a0 # E : 76*4882a593Smuzhiyun nop 77*4882a593Smuzhiyun nop 78*4882a593Smuzhiyun 79*4882a593Smuzhiyun ldq_u t1, 0(a1) # L : Latency=3 80*4882a593Smuzhiyun addq a1, 8, a1 # E : 81*4882a593Smuzhiyun cmpbge zero, t1, t8 # E : (3 cycle stall) 82*4882a593Smuzhiyun beq t8, $a_loop # U : (stall for t8) 83*4882a593Smuzhiyun 84*4882a593Smuzhiyun /* Take care of the final (partial) word store. 85*4882a593Smuzhiyun On entry to this basic block we have: 86*4882a593Smuzhiyun t1 == the source word containing the null 87*4882a593Smuzhiyun t8 == the cmpbge mask that found it. */ 88*4882a593Smuzhiyun$a_eos: 89*4882a593Smuzhiyun negq t8, t6 # E : find low bit set 90*4882a593Smuzhiyun and t8, t6, t12 # E : (stall) 91*4882a593Smuzhiyun /* For the sake of the cache, don't read a destination word 92*4882a593Smuzhiyun if we're not going to need it. */ 93*4882a593Smuzhiyun and t12, 0x80, t6 # E : (stall) 94*4882a593Smuzhiyun bne t6, 1f # U : (stall) 95*4882a593Smuzhiyun 96*4882a593Smuzhiyun /* We're doing a partial word store and so need to combine 97*4882a593Smuzhiyun our source and original destination words. */ 98*4882a593Smuzhiyun ldq_u t0, 0(a0) # L : Latency=3 99*4882a593Smuzhiyun subq t12, 1, t6 # E : 100*4882a593Smuzhiyun zapnot t1, t6, t1 # U : clear src bytes >= null (stall) 101*4882a593Smuzhiyun or t12, t6, t8 # E : (stall) 102*4882a593Smuzhiyun 103*4882a593Smuzhiyun zap t0, t8, t0 # E : clear dst bytes <= null 104*4882a593Smuzhiyun or t0, t1, t1 # E : (stall) 105*4882a593Smuzhiyun nop 106*4882a593Smuzhiyun nop 107*4882a593Smuzhiyun 108*4882a593Smuzhiyun1: stq_u t1, 0(a0) # L : 109*4882a593Smuzhiyun ret (t9) # L0 : Latency=3 110*4882a593Smuzhiyun nop 111*4882a593Smuzhiyun nop 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun .end stxcpy_aligned 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun .align 4 116*4882a593Smuzhiyun .ent __stxcpy 117*4882a593Smuzhiyun .globl __stxcpy 118*4882a593Smuzhiyun__stxcpy: 119*4882a593Smuzhiyun .frame sp, 0, t9 120*4882a593Smuzhiyun .prologue 0 121*4882a593Smuzhiyun 122*4882a593Smuzhiyun /* Are source and destination co-aligned? */ 123*4882a593Smuzhiyun xor a0, a1, t0 # E : 124*4882a593Smuzhiyun unop # E : 125*4882a593Smuzhiyun and t0, 7, t0 # E : (stall) 126*4882a593Smuzhiyun bne t0, $unaligned # U : (stall) 127*4882a593Smuzhiyun 128*4882a593Smuzhiyun /* We are co-aligned; take care of a partial first word. */ 129*4882a593Smuzhiyun ldq_u t1, 0(a1) # L : load first src word 130*4882a593Smuzhiyun and a0, 7, t0 # E : take care not to load a word ... 131*4882a593Smuzhiyun addq a1, 8, a1 # E : 132*4882a593Smuzhiyun beq t0, stxcpy_aligned # U : ... if we wont need it (stall) 133*4882a593Smuzhiyun 134*4882a593Smuzhiyun ldq_u t0, 0(a0) # L : 135*4882a593Smuzhiyun br stxcpy_aligned # L0 : Latency=3 136*4882a593Smuzhiyun nop 137*4882a593Smuzhiyun nop 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun 140*4882a593Smuzhiyun/* The source and destination are not co-aligned. Align the destination 141*4882a593Smuzhiyun and cope. We have to be very careful about not reading too much and 142*4882a593Smuzhiyun causing a SEGV. */ 143*4882a593Smuzhiyun 144*4882a593Smuzhiyun .align 4 145*4882a593Smuzhiyun$u_head: 146*4882a593Smuzhiyun /* We know just enough now to be able to assemble the first 147*4882a593Smuzhiyun full source word. We can still find a zero at the end of it 148*4882a593Smuzhiyun that prevents us from outputting the whole thing. 149*4882a593Smuzhiyun 150*4882a593Smuzhiyun On entry to this basic block: 151*4882a593Smuzhiyun t0 == the first dest word, for masking back in, if needed else 0 152*4882a593Smuzhiyun t1 == the low bits of the first source word 153*4882a593Smuzhiyun t6 == bytemask that is -1 in dest word bytes */ 154*4882a593Smuzhiyun 155*4882a593Smuzhiyun ldq_u t2, 8(a1) # L : 156*4882a593Smuzhiyun addq a1, 8, a1 # E : 157*4882a593Smuzhiyun extql t1, a1, t1 # U : (stall on a1) 158*4882a593Smuzhiyun extqh t2, a1, t4 # U : (stall on a1) 159*4882a593Smuzhiyun 160*4882a593Smuzhiyun mskql t0, a0, t0 # U : 161*4882a593Smuzhiyun or t1, t4, t1 # E : 162*4882a593Smuzhiyun mskqh t1, a0, t1 # U : (stall on t1) 163*4882a593Smuzhiyun or t0, t1, t1 # E : (stall on t1) 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun or t1, t6, t6 # E : 166*4882a593Smuzhiyun cmpbge zero, t6, t8 # E : (stall) 167*4882a593Smuzhiyun lda t6, -1 # E : for masking just below 168*4882a593Smuzhiyun bne t8, $u_final # U : (stall) 169*4882a593Smuzhiyun 170*4882a593Smuzhiyun mskql t6, a1, t6 # U : mask out the bits we have 171*4882a593Smuzhiyun or t6, t2, t2 # E : already extracted before (stall) 172*4882a593Smuzhiyun cmpbge zero, t2, t8 # E : testing eos (stall) 173*4882a593Smuzhiyun bne t8, $u_late_head_exit # U : (stall) 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun /* Finally, we've got all the stupid leading edge cases taken care 176*4882a593Smuzhiyun of and we can set up to enter the main loop. */ 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun stq_u t1, 0(a0) # L : store first output word 179*4882a593Smuzhiyun addq a0, 8, a0 # E : 180*4882a593Smuzhiyun extql t2, a1, t0 # U : position ho-bits of lo word 181*4882a593Smuzhiyun ldq_u t2, 8(a1) # U : read next high-order source word 182*4882a593Smuzhiyun 183*4882a593Smuzhiyun addq a1, 8, a1 # E : 184*4882a593Smuzhiyun cmpbge zero, t2, t8 # E : (stall for t2) 185*4882a593Smuzhiyun nop # E : 186*4882a593Smuzhiyun bne t8, $u_eos # U : (stall) 187*4882a593Smuzhiyun 188*4882a593Smuzhiyun /* Unaligned copy main loop. In order to avoid reading too much, 189*4882a593Smuzhiyun the loop is structured to detect zeros in aligned source words. 190*4882a593Smuzhiyun This has, unfortunately, effectively pulled half of a loop 191*4882a593Smuzhiyun iteration out into the head and half into the tail, but it does 192*4882a593Smuzhiyun prevent nastiness from accumulating in the very thing we want 193*4882a593Smuzhiyun to run as fast as possible. 194*4882a593Smuzhiyun 195*4882a593Smuzhiyun On entry to this basic block: 196*4882a593Smuzhiyun t0 == the shifted high-order bits from the previous source word 197*4882a593Smuzhiyun t2 == the unshifted current source word 198*4882a593Smuzhiyun 199*4882a593Smuzhiyun We further know that t2 does not contain a null terminator. */ 200*4882a593Smuzhiyun 201*4882a593Smuzhiyun .align 3 202*4882a593Smuzhiyun$u_loop: 203*4882a593Smuzhiyun extqh t2, a1, t1 # U : extract high bits for current word 204*4882a593Smuzhiyun addq a1, 8, a1 # E : (stall) 205*4882a593Smuzhiyun extql t2, a1, t3 # U : extract low bits for next time (stall) 206*4882a593Smuzhiyun addq a0, 8, a0 # E : 207*4882a593Smuzhiyun 208*4882a593Smuzhiyun or t0, t1, t1 # E : current dst word now complete 209*4882a593Smuzhiyun ldq_u t2, 0(a1) # L : Latency=3 load high word for next time 210*4882a593Smuzhiyun stq_u t1, -8(a0) # L : save the current word (stall) 211*4882a593Smuzhiyun mov t3, t0 # E : 212*4882a593Smuzhiyun 213*4882a593Smuzhiyun cmpbge zero, t2, t8 # E : test new word for eos 214*4882a593Smuzhiyun beq t8, $u_loop # U : (stall) 215*4882a593Smuzhiyun nop 216*4882a593Smuzhiyun nop 217*4882a593Smuzhiyun 218*4882a593Smuzhiyun /* We've found a zero somewhere in the source word we just read. 219*4882a593Smuzhiyun If it resides in the lower half, we have one (probably partial) 220*4882a593Smuzhiyun word to write out, and if it resides in the upper half, we 221*4882a593Smuzhiyun have one full and one partial word left to write out. 222*4882a593Smuzhiyun 223*4882a593Smuzhiyun On entry to this basic block: 224*4882a593Smuzhiyun t0 == the shifted high-order bits from the previous source word 225*4882a593Smuzhiyun t2 == the unshifted current source word. */ 226*4882a593Smuzhiyun$u_eos: 227*4882a593Smuzhiyun extqh t2, a1, t1 # U : 228*4882a593Smuzhiyun or t0, t1, t1 # E : first (partial) source word complete (stall) 229*4882a593Smuzhiyun cmpbge zero, t1, t8 # E : is the null in this first bit? (stall) 230*4882a593Smuzhiyun bne t8, $u_final # U : (stall) 231*4882a593Smuzhiyun 232*4882a593Smuzhiyun$u_late_head_exit: 233*4882a593Smuzhiyun stq_u t1, 0(a0) # L : the null was in the high-order bits 234*4882a593Smuzhiyun addq a0, 8, a0 # E : 235*4882a593Smuzhiyun extql t2, a1, t1 # U : 236*4882a593Smuzhiyun cmpbge zero, t1, t8 # E : (stall) 237*4882a593Smuzhiyun 238*4882a593Smuzhiyun /* Take care of a final (probably partial) result word. 239*4882a593Smuzhiyun On entry to this basic block: 240*4882a593Smuzhiyun t1 == assembled source word 241*4882a593Smuzhiyun t8 == cmpbge mask that found the null. */ 242*4882a593Smuzhiyun$u_final: 243*4882a593Smuzhiyun negq t8, t6 # E : isolate low bit set 244*4882a593Smuzhiyun and t6, t8, t12 # E : (stall) 245*4882a593Smuzhiyun and t12, 0x80, t6 # E : avoid dest word load if we can (stall) 246*4882a593Smuzhiyun bne t6, 1f # U : (stall) 247*4882a593Smuzhiyun 248*4882a593Smuzhiyun ldq_u t0, 0(a0) # E : 249*4882a593Smuzhiyun subq t12, 1, t6 # E : 250*4882a593Smuzhiyun or t6, t12, t8 # E : (stall) 251*4882a593Smuzhiyun zapnot t1, t6, t1 # U : kill source bytes >= null (stall) 252*4882a593Smuzhiyun 253*4882a593Smuzhiyun zap t0, t8, t0 # U : kill dest bytes <= null (2 cycle data stall) 254*4882a593Smuzhiyun or t0, t1, t1 # E : (stall) 255*4882a593Smuzhiyun nop 256*4882a593Smuzhiyun nop 257*4882a593Smuzhiyun 258*4882a593Smuzhiyun1: stq_u t1, 0(a0) # L : 259*4882a593Smuzhiyun ret (t9) # L0 : Latency=3 260*4882a593Smuzhiyun nop 261*4882a593Smuzhiyun nop 262*4882a593Smuzhiyun 263*4882a593Smuzhiyun /* Unaligned copy entry point. */ 264*4882a593Smuzhiyun .align 4 265*4882a593Smuzhiyun$unaligned: 266*4882a593Smuzhiyun 267*4882a593Smuzhiyun ldq_u t1, 0(a1) # L : load first source word 268*4882a593Smuzhiyun and a0, 7, t4 # E : find dest misalignment 269*4882a593Smuzhiyun and a1, 7, t5 # E : find src misalignment 270*4882a593Smuzhiyun /* Conditionally load the first destination word and a bytemask 271*4882a593Smuzhiyun with 0xff indicating that the destination byte is sacrosanct. */ 272*4882a593Smuzhiyun mov zero, t0 # E : 273*4882a593Smuzhiyun 274*4882a593Smuzhiyun mov zero, t6 # E : 275*4882a593Smuzhiyun beq t4, 1f # U : 276*4882a593Smuzhiyun ldq_u t0, 0(a0) # L : 277*4882a593Smuzhiyun lda t6, -1 # E : 278*4882a593Smuzhiyun 279*4882a593Smuzhiyun mskql t6, a0, t6 # U : 280*4882a593Smuzhiyun nop 281*4882a593Smuzhiyun nop 282*4882a593Smuzhiyun nop 283*4882a593Smuzhiyun1: 284*4882a593Smuzhiyun subq a1, t4, a1 # E : sub dest misalignment from src addr 285*4882a593Smuzhiyun /* If source misalignment is larger than dest misalignment, we need 286*4882a593Smuzhiyun extra startup checks to avoid SEGV. */ 287*4882a593Smuzhiyun cmplt t4, t5, t12 # E : 288*4882a593Smuzhiyun beq t12, $u_head # U : 289*4882a593Smuzhiyun lda t2, -1 # E : mask out leading garbage in source 290*4882a593Smuzhiyun 291*4882a593Smuzhiyun mskqh t2, t5, t2 # U : 292*4882a593Smuzhiyun ornot t1, t2, t3 # E : (stall) 293*4882a593Smuzhiyun cmpbge zero, t3, t8 # E : is there a zero? (stall) 294*4882a593Smuzhiyun beq t8, $u_head # U : (stall) 295*4882a593Smuzhiyun 296*4882a593Smuzhiyun /* At this point we've found a zero in the first partial word of 297*4882a593Smuzhiyun the source. We need to isolate the valid source data and mask 298*4882a593Smuzhiyun it into the original destination data. (Incidentally, we know 299*4882a593Smuzhiyun that we'll need at least one byte of that original dest word.) */ 300*4882a593Smuzhiyun 301*4882a593Smuzhiyun ldq_u t0, 0(a0) # L : 302*4882a593Smuzhiyun negq t8, t6 # E : build bitmask of bytes <= zero 303*4882a593Smuzhiyun and t6, t8, t12 # E : (stall) 304*4882a593Smuzhiyun and a1, 7, t5 # E : 305*4882a593Smuzhiyun 306*4882a593Smuzhiyun subq t12, 1, t6 # E : 307*4882a593Smuzhiyun or t6, t12, t8 # E : (stall) 308*4882a593Smuzhiyun srl t12, t5, t12 # U : adjust final null return value 309*4882a593Smuzhiyun zapnot t2, t8, t2 # U : prepare source word; mirror changes (stall) 310*4882a593Smuzhiyun 311*4882a593Smuzhiyun and t1, t2, t1 # E : to source validity mask 312*4882a593Smuzhiyun extql t2, a1, t2 # U : 313*4882a593Smuzhiyun extql t1, a1, t1 # U : (stall) 314*4882a593Smuzhiyun andnot t0, t2, t0 # .. e1 : zero place for source to reside (stall) 315*4882a593Smuzhiyun 316*4882a593Smuzhiyun or t0, t1, t1 # e1 : and put it there 317*4882a593Smuzhiyun stq_u t1, 0(a0) # .. e0 : (stall) 318*4882a593Smuzhiyun ret (t9) # e1 : 319*4882a593Smuzhiyun nop 320*4882a593Smuzhiyun 321*4882a593Smuzhiyun .end __stxcpy 322*4882a593Smuzhiyun 323