Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * arch/alpha/lib/stxncpy.S
0004  * Contributed by Richard Henderson (rth@tamu.edu)
0005  *
0006  * Copy no more than COUNT bytes of the null-terminated string from
0007  * SRC to DST.
0008  *
0009  * This is an internal routine used by strncpy, stpncpy, and strncat.
0010  * As such, it uses special linkage conventions to make implementation
0011  * of these public functions more efficient.
0012  *
0013  * On input:
0014  *  t9 = return address
0015  *  a0 = DST
0016  *  a1 = SRC
0017  *  a2 = COUNT
0018  *
0019  * Furthermore, COUNT may not be zero.
0020  *
0021  * On output:
0022  *  t0  = last word written
0023  *  t10 = bitmask (with one bit set) indicating the byte position of
0024  *        the end of the range specified by COUNT
0025  *  t12 = bitmask (with one bit set) indicating the last byte written
0026  *  a0  = unaligned address of the last *word* written
0027  *  a2  = the number of full words left in COUNT
0028  *
0029  * Furthermore, v0, a3-a5, t11, and $at are untouched.
0030  */
0031 
0032 #include <asm/regdef.h>
0033 
0034     .set noat
0035     .set noreorder
0036 
0037     .text
0038 
0039 /* There is a problem with either gdb (as of 4.16) or gas (as of 2.7) that
0040    doesn't like putting the entry point for a procedure somewhere in the
0041    middle of the procedure descriptor.  Work around this by putting the
0042    aligned copy in its own procedure descriptor */
0043 
0044     .ent stxncpy_aligned
0045     .align 3
0046 stxncpy_aligned:
0047     .frame sp, 0, t9, 0
0048     .prologue 0
0049 
0050     /* On entry to this basic block:
0051        t0 == the first destination word for masking back in
0052        t1 == the first source word.  */
0053 
0054     /* Create the 1st output word and detect 0's in the 1st input word.  */
0055     lda t2, -1      # e1    : build a mask against false zero
0056     mskqh   t2, a1, t2  # e0    :   detection in the src word
0057     mskqh   t1, a1, t3  # e0    :
0058     ornot   t1, t2, t2  # .. e1 :
0059     mskql   t0, a1, t0  # e0    : assemble the first output word
0060     cmpbge  zero, t2, t8    # .. e1 : bits set iff null found
0061     or  t0, t3, t0  # e0    :
0062     beq a2, $a_eoc  # .. e1 :
0063     bne t8, $a_eos  # .. e1 :
0064 
0065     /* On entry to this basic block:
0066        t0 == a source word not containing a null.  */
0067 
0068 $a_loop:
0069     stq_u   t0, 0(a0)   # e0    :
0070     addq    a0, 8, a0   # .. e1 :
0071     ldq_u   t0, 0(a1)   # e0    :
0072     addq    a1, 8, a1   # .. e1 :
0073     subq    a2, 1, a2   # e0    :
0074     cmpbge  zero, t0, t8    # .. e1 (stall)
0075     beq a2, $a_eoc      # e1    :
0076     beq t8, $a_loop # e1    :
0077 
0078     /* Take care of the final (partial) word store.  At this point
0079        the end-of-count bit is set in t8 iff it applies.
0080 
0081        On entry to this basic block we have:
0082        t0 == the source word containing the null
0083        t8 == the cmpbge mask that found it.  */
0084 
0085 $a_eos:
0086     negq    t8, t12     # e0    : find low bit set
0087     and t8, t12, t12    # e1 (stall)
0088 
0089     /* For the sake of the cache, don't read a destination word
0090        if we're not going to need it.  */
0091     and t12, 0x80, t6   # e0    :
0092     bne t6, 1f      # .. e1 (zdb)
0093 
0094     /* We're doing a partial word store and so need to combine
0095        our source and original destination words.  */
0096     ldq_u   t1, 0(a0)   # e0    :
0097     subq    t12, 1, t6  # .. e1 :
0098     or  t12, t6, t8 # e0    :
0099     unop            #
0100     zapnot  t0, t8, t0  # e0    : clear src bytes > null
0101     zap t1, t8, t1  # .. e1 : clear dst bytes <= null
0102     or  t0, t1, t0  # e1    :
0103 
0104 1:  stq_u   t0, 0(a0)   # e0    :
0105     ret (t9)        # e1    :
0106 
0107     /* Add the end-of-count bit to the eos detection bitmask.  */
0108 $a_eoc:
0109     or  t10, t8, t8
0110     br  $a_eos
0111 
0112     .end stxncpy_aligned
0113 
0114     .align 3
0115     .ent __stxncpy
0116     .globl __stxncpy
0117 __stxncpy:
0118     .frame sp, 0, t9, 0
0119     .prologue 0
0120 
0121     /* Are source and destination co-aligned?  */
0122     xor a0, a1, t1  # e0    :
0123     and a0, 7, t0   # .. e1 : find dest misalignment
0124     and t1, 7, t1   # e0    :
0125     addq    a2, t0, a2  # .. e1 : bias count by dest misalignment
0126     subq    a2, 1, a2   # e0    :
0127     and a2, 7, t2   # e1    :
0128     srl a2, 3, a2   # e0    : a2 = loop counter = (count - 1)/8
0129     addq    zero, 1, t10    # .. e1 :
0130     sll t10, t2, t10    # e0    : t10 = bitmask of last count byte
0131     bne t1, $unaligned  # .. e1 :
0132 
0133     /* We are co-aligned; take care of a partial first word.  */
0134 
0135     ldq_u   t1, 0(a1)   # e0    : load first src word
0136     addq    a1, 8, a1   # .. e1 :
0137 
0138     beq t0, stxncpy_aligned     # avoid loading dest word if not needed
0139     ldq_u   t0, 0(a0)   # e0    :
0140     br  stxncpy_aligned # .. e1 :
0141 
0142 
0143 /* The source and destination are not co-aligned.  Align the destination
0144    and cope.  We have to be very careful about not reading too much and
0145    causing a SEGV.  */
0146 
0147     .align 3
0148 $u_head:
0149     /* We know just enough now to be able to assemble the first
0150        full source word.  We can still find a zero at the end of it
0151        that prevents us from outputting the whole thing.
0152 
0153        On entry to this basic block:
0154        t0 == the first dest word, unmasked
0155        t1 == the shifted low bits of the first source word
0156        t6 == bytemask that is -1 in dest word bytes */
0157 
0158     ldq_u   t2, 8(a1)   # e0    : load second src word
0159     addq    a1, 8, a1   # .. e1 :
0160     mskql   t0, a0, t0  # e0    : mask trailing garbage in dst
0161     extqh   t2, a1, t4  # e0    :
0162     or  t1, t4, t1  # e1    : first aligned src word complete
0163     mskqh   t1, a0, t1  # e0    : mask leading garbage in src
0164     or  t0, t1, t0  # e0    : first output word complete
0165     or  t0, t6, t6  # e1    : mask original data for zero test
0166     cmpbge  zero, t6, t8    # e0    :
0167     beq a2, $u_eocfin   # .. e1 :
0168     lda t6, -1      # e0    :
0169     bne t8, $u_final    # .. e1 :
0170 
0171     mskql   t6, a1, t6  # e0    : mask out bits already seen
0172     nop         # .. e1 :
0173     stq_u   t0, 0(a0)   # e0    : store first output word
0174     or      t6, t2, t2  # .. e1 :
0175     cmpbge  zero, t2, t8    # e0    : find nulls in second partial
0176     addq    a0, 8, a0   # .. e1 :
0177     subq    a2, 1, a2   # e0    :
0178     bne t8, $u_late_head_exit   # .. e1 :
0179 
0180     /* Finally, we've got all the stupid leading edge cases taken care
0181        of and we can set up to enter the main loop.  */
0182 
0183     extql   t2, a1, t1  # e0    : position hi-bits of lo word
0184     beq a2, $u_eoc  # .. e1 :
0185     ldq_u   t2, 8(a1)   # e0    : read next high-order source word
0186     addq    a1, 8, a1   # .. e1 :
0187     extqh   t2, a1, t0  # e0    : position lo-bits of hi word (stall)
0188     cmpbge  zero, t2, t8    # .. e1 :
0189     nop         # e0    :
0190     bne t8, $u_eos  # .. e1 :
0191 
0192     /* Unaligned copy main loop.  In order to avoid reading too much,
0193        the loop is structured to detect zeros in aligned source words.
0194        This has, unfortunately, effectively pulled half of a loop
0195        iteration out into the head and half into the tail, but it does
0196        prevent nastiness from accumulating in the very thing we want
0197        to run as fast as possible.
0198 
0199        On entry to this basic block:
0200        t0 == the shifted low-order bits from the current source word
0201        t1 == the shifted high-order bits from the previous source word
0202        t2 == the unshifted current source word
0203 
0204        We further know that t2 does not contain a null terminator.  */
0205 
0206     .align 3
0207 $u_loop:
0208     or  t0, t1, t0  # e0    : current dst word now complete
0209     subq    a2, 1, a2   # .. e1 : decrement word count
0210     stq_u   t0, 0(a0)   # e0    : save the current word
0211     addq    a0, 8, a0   # .. e1 :
0212     extql   t2, a1, t1  # e0    : extract high bits for next time
0213     beq a2, $u_eoc  # .. e1 :
0214     ldq_u   t2, 8(a1)   # e0    : load high word for next time
0215     addq    a1, 8, a1   # .. e1 :
0216     nop         # e0    :
0217     cmpbge  zero, t2, t8    # e1    : test new word for eos (stall)
0218     extqh   t2, a1, t0  # e0    : extract low bits for current word
0219     beq t8, $u_loop # .. e1 :
0220 
0221     /* We've found a zero somewhere in the source word we just read.
0222        If it resides in the lower half, we have one (probably partial)
0223        word to write out, and if it resides in the upper half, we
0224        have one full and one partial word left to write out.
0225 
0226        On entry to this basic block:
0227        t0 == the shifted low-order bits from the current source word
0228        t1 == the shifted high-order bits from the previous source word
0229        t2 == the unshifted current source word.  */
0230 $u_eos:
0231     or  t0, t1, t0  # e0    : first (partial) source word complete
0232     nop         # .. e1 :
0233     cmpbge  zero, t0, t8    # e0    : is the null in this first bit?
0234     bne t8, $u_final    # .. e1 (zdb)
0235 
0236     stq_u   t0, 0(a0)   # e0    : the null was in the high-order bits
0237     addq    a0, 8, a0   # .. e1 :
0238     subq    a2, 1, a2   # e1    :
0239 
0240 $u_late_head_exit:
0241     extql   t2, a1, t0  # .. e0 :
0242     cmpbge  zero, t0, t8    # e0    :
0243     or  t8, t10, t6 # e1    :
0244     cmoveq  a2, t6, t8  # e0    :
0245     nop         # .. e1 :
0246 
0247     /* Take care of a final (probably partial) result word.
0248        On entry to this basic block:
0249        t0 == assembled source word
0250        t8 == cmpbge mask that found the null.  */
0251 $u_final:
0252     negq    t8, t6      # e0    : isolate low bit set
0253     and t6, t8, t12 # e1    :
0254 
0255     and t12, 0x80, t6   # e0    : avoid dest word load if we can
0256     bne t6, 1f      # .. e1 (zdb)
0257 
0258     ldq_u   t1, 0(a0)   # e0    :
0259     subq    t12, 1, t6  # .. e1 :
0260     or  t6, t12, t8 # e0    :
0261     zapnot  t0, t8, t0  # .. e1 : kill source bytes > null
0262     zap t1, t8, t1  # e0    : kill dest bytes <= null
0263     or  t0, t1, t0  # e1    :
0264 
0265 1:  stq_u   t0, 0(a0)   # e0    :
0266     ret (t9)        # .. e1 :
0267 
0268     /* Got to end-of-count before end of string.  
0269        On entry to this basic block:
0270        t1 == the shifted high-order bits from the previous source word  */
0271 $u_eoc:
0272     and a1, 7, t6   # e1    :
0273     sll t10, t6, t6 # e0    :
0274     and t6, 0xff, t6    # e0    :
0275     bne t6, 1f      # .. e1 :
0276 
0277     ldq_u   t2, 8(a1)   # e0    : load final src word
0278     nop         # .. e1 :
0279     extqh   t2, a1, t0  # e0    : extract low bits for last word
0280     or  t1, t0, t1  # e1    :
0281 
0282 1:  cmpbge  zero, t1, t8
0283     mov t1, t0
0284 
0285 $u_eocfin:          # end-of-count, final word
0286     or  t10, t8, t8
0287     br  $u_final
0288 
0289     /* Unaligned copy entry point.  */
0290     .align 3
0291 $unaligned:
0292 
0293     ldq_u   t1, 0(a1)   # e0    : load first source word
0294 
0295     and a0, 7, t4   # .. e1 : find dest misalignment
0296     and a1, 7, t5   # e0    : find src misalignment
0297 
0298     /* Conditionally load the first destination word and a bytemask
0299        with 0xff indicating that the destination byte is sacrosanct.  */
0300 
0301     mov zero, t0    # .. e1 :
0302     mov zero, t6    # e0    :
0303     beq t4, 1f      # .. e1 :
0304     ldq_u   t0, 0(a0)   # e0    :
0305     lda t6, -1      # .. e1 :
0306     mskql   t6, a0, t6  # e0    :
0307     subq    a1, t4, a1  # .. e1 : sub dest misalignment from src addr
0308 
0309     /* If source misalignment is larger than dest misalignment, we need
0310        extra startup checks to avoid SEGV.  */
0311 
0312 1:  cmplt   t4, t5, t12 # e1    :
0313     extql   t1, a1, t1  # .. e0 : shift src into place
0314     lda t2, -1      # e0    : for creating masks later
0315     beq t12, $u_head    # .. e1 :
0316 
0317     extql   t2, a1, t2  # e0    :
0318     cmpbge  zero, t1, t8    # .. e1 : is there a zero?
0319     andnot  t2, t6, t2  # e0    : dest mask for a single word copy
0320     or  t8, t10, t5 # .. e1 : test for end-of-count too
0321     cmpbge  zero, t2, t3    # e0    :
0322     cmoveq  a2, t5, t8  # .. e1 :
0323     andnot  t8, t3, t8  # e0    :
0324     beq t8, $u_head # .. e1 (zdb)
0325 
0326     /* At this point we've found a zero in the first partial word of
0327        the source.  We need to isolate the valid source data and mask
0328        it into the original destination data.  (Incidentally, we know
0329        that we'll need at least one byte of that original dest word.) */
0330 
0331     ldq_u   t0, 0(a0)   # e0    :
0332     negq    t8, t6      # .. e1 : build bitmask of bytes <= zero
0333     mskqh   t1, t4, t1  # e0    :
0334     and t6, t8, t12 # .. e1 :
0335     subq    t12, 1, t6  # e0    :
0336     or  t6, t12, t8 # e1    :
0337 
0338     zapnot  t2, t8, t2  # e0    : prepare source word; mirror changes
0339     zapnot  t1, t8, t1  # .. e1 : to source validity mask
0340 
0341     andnot  t0, t2, t0  # e0    : zero place for source to reside
0342     or  t0, t1, t0  # e1    : and put it there
0343     stq_u   t0, 0(a0)   # e0    :
0344     ret (t9)        # .. e1 :
0345 
0346     .end __stxncpy