2 * arch/alpha/lib/stxncpy.S
3 * Contributed by Richard Henderson (rth@tamu.edu)
5 * Copy no more than COUNT bytes of the null-terminated string from
8 * This is an internal routine used by strncpy, stpncpy, and strncat.
9 * As such, it uses special linkage conventions to make implementation
10 * of these public functions more efficient.
18 * Furthermore, COUNT may not be zero.
21 * t0 = last word written
22 * t10 = bitmask (with one bit set) indicating the byte position of
23 * the end of the range specified by COUNT
24 * t12 = bitmask (with one bit set) indicating the last byte written
25 * a0 = unaligned address of the last *word* written
26 * a2 = the number of full words left in COUNT
28 * Furthermore, v0, a3-a5, t11, t12, and $at are untouched.
31 #include <alpha/regdef.h>
38 /* There is a problem with either gdb (as of 4.16) or gas (as of 2.7) that
39 doesn't like putting the entry point for a procedure somewhere in the
40 middle of the procedure descriptor. Work around this by putting the
41 aligned copy in its own procedure descriptor */
49 /* On entry to this basic block:
50 t0 == the first destination word for masking back in
51 t1 == the first source word. */
53 /* Create the 1st output word and detect 0's in the 1st input word. */
54 lda t2, -1 # e1 : build a mask against false zero
55 mskqh t2, a1, t2 # e0 : detection in the src word
56 mskqh t1, a1, t3 # e0 :
57 ornot t1, t2, t2 # .. e1 :
58 mskql t0, a1, t0 # e0 : assemble the first output word
59 cmpbge zero, t2, t8 # .. e1 : bits set iff null found
61 beq a2, $a_eoc # .. e1 :
62 bne t8, $a_eos # .. e1 :
64 /* On entry to this basic block:
65 t0 == a source word not containing a null. */
68 stq_u t0, 0(a0) # e0 :
69 addq a0, 8, a0 # .. e1 :
70 ldq_u t0, 0(a1) # e0 :
71 addq a1, 8, a1 # .. e1 :
73 cmpbge zero, t0, t8 # .. e1 (stall)
75 beq t8, $a_loop # e1 :
77 /* Take care of the final (partial) word store. At this point
78 the end-of-count bit is set in t8 iff it applies.
80 On entry to this basic block we have:
81 t0 == the source word containing the null
82 t8 == the cmpbge mask that found it. */
85 negq t8, t12 # e0 : find low bit set
86 and t8, t12, t12 # e1 (stall)
88 /* For the sake of the cache, don't read a destination word
89 if we're not going to need it. */
90 and t12, 0x80, t6 # e0 :
91 bne t6, 1f # .. e1 (zdb)
93 /* We're doing a partial word store and so need to combine
94 our source and original destination words. */
95 ldq_u t1, 0(a0) # e0 :
96 subq t12, 1, t6 # .. e1 :
99 zapnot t0, t8, t0 # e0 : clear src bytes > null
100 zap t1, t8, t1 # .. e1 : clear dst bytes <= null
103 1: stq_u t0, 0(a0) # e0 :
106 /* Add the end-of-count bit to the eos detection bitmask. */
120 /* Are source and destination co-aligned? */
121 xor a0, a1, t1 # e0 :
122 and a0, 7, t0 # .. e1 : find dest misalignment
124 addq a2, t0, a2 # .. e1 : bias count by dest misalignment
125 subq a2, 1, a2 # e0 :
127 srl a2, 3, a2 # e0 : a2 = loop counter = (count - 1)/8
128 addq zero, 1, t10 # .. e1 :
129 sll t10, t2, t10 # e0 : t10 = bitmask of last count byte
130 bne t1, $unaligned # .. e1 :
132 /* We are co-aligned; take care of a partial first word. */
134 ldq_u t1, 0(a1) # e0 : load first src word
135 addq a1, 8, a1 # .. e1 :
137 beq t0, stxncpy_aligned # avoid loading dest word if not needed
138 ldq_u t0, 0(a0) # e0 :
139 br stxncpy_aligned # .. e1 :
142 /* The source and destination are not co-aligned. Align the destination
143 and cope. We have to be very careful about not reading too much and
148 /* We know just enough now to be able to assemble the first
149 full source word. We can still find a zero at the end of it
150 that prevents us from outputting the whole thing.
152 On entry to this basic block:
153 t0 == the first dest word, unmasked
154 t1 == the shifted low bits of the first source word
155 t6 == bytemask that is -1 in dest word bytes */
157 ldq_u t2, 8(a1) # e0 : load second src word
158 addq a1, 8, a1 # .. e1 :
159 mskql t0, a0, t0 # e0 : mask trailing garbage in dst
160 extqh t2, a1, t4 # e0 :
161 or t1, t4, t1 # e1 : first aligned src word complete
162 mskqh t1, a0, t1 # e0 : mask leading garbage in src
163 or t0, t1, t0 # e0 : first output word complete
164 or t0, t6, t6 # e1 : mask original data for zero test
165 cmpbge zero, t6, t8 # e0 :
166 beq a2, $u_eocfin # .. e1 :
167 bne t8, $u_final # e1 :
169 lda t6, -1 # e1 : mask out the bits we have
170 mskql t6, a1, t6 # e0 : already seen
171 stq_u t0, 0(a0) # e0 : store first output word
172 or t6, t2, t2 # .. e1 :
173 cmpbge zero, t2, t8 # e0 : find nulls in second partial
174 addq a0, 8, a0 # .. e1 :
175 subq a2, 1, a2 # e0 :
176 bne t8, $u_late_head_exit # .. e1 :
178 /* Finally, we've got all the stupid leading edge cases taken care
179 of and we can set up to enter the main loop. */
181 extql t2, a1, t1 # e0 : position hi-bits of lo word
182 ldq_u t2, 8(a1) # .. e1 : read next high-order source word
183 addq a1, 8, a1 # e0 :
184 cmpbge zero, t2, t8 # e1 (stall)
185 beq a2, $u_eoc # e1 :
186 bne t8, $u_eos # e1 :
188 /* Unaligned copy main loop. In order to avoid reading too much,
189 the loop is structured to detect zeros in aligned source words.
190 This has, unfortunately, effectively pulled half of a loop
191 iteration out into the head and half into the tail, but it does
192 prevent nastiness from accumulating in the very thing we want
193 to run as fast as possible.
195 On entry to this basic block:
196 t1 == the shifted high-order bits from the previous source word
197 t2 == the unshifted current source word
199 We further know that t2 does not contain a null terminator. */
203 extqh t2, a1, t0 # e0 : extract high bits for current word
204 addq a1, 8, a1 # .. e1 :
205 extql t2, a1, t3 # e0 : extract low bits for next time
206 addq a0, 8, a0 # .. e1 :
207 or t0, t1, t0 # e0 : current dst word now complete
208 ldq_u t2, 0(a1) # .. e1 : load high word for next time
209 stq_u t0, -8(a0) # e0 : save the current word
211 subq a2, 1, a2 # e0 :
212 cmpbge zero, t2, t8 # .. e1 : test new word for eos
213 beq a2, $u_eoc # e1 :
214 beq t8, $u_loop # e1 :
216 /* We've found a zero somewhere in the source word we just read.
217 If it resides in the lower half, we have one (probably partial)
218 word to write out, and if it resides in the upper half, we
219 have one full and one partial word left to write out.
221 On entry to this basic block:
222 t1 == the shifted high-order bits from the previous source word
223 t2 == the unshifted current source word. */
225 extqh t2, a1, t0 # e0 :
226 or t0, t1, t0 # e1 : first (partial) source word complete
228 cmpbge zero, t0, t8 # e0 : is the null in this first bit?
229 bne t8, $u_final # .. e1 (zdb)
231 stq_u t0, 0(a0) # e0 : the null was in the high-order bits
232 addq a0, 8, a0 # .. e1 :
233 subq a2, 1, a2 # e1 :
236 extql t2, a1, t0 # .. e0 :
237 cmpbge zero, t0, t8 # e0 :
238 or t8, t10, t6 # e1 :
239 cmoveq a2, t6, t8 # e0 :
242 /* Take care of a final (probably partial) result word.
243 On entry to this basic block:
244 t0 == assembled source word
245 t8 == cmpbge mask that found the null. */
247 negq t8, t6 # e0 : isolate low bit set
248 and t6, t8, t12 # e1 :
250 and t12, 0x80, t6 # e0 : avoid dest word load if we can
251 bne t6, 1f # .. e1 (zdb)
253 ldq_u t1, 0(a0) # e0 :
254 subq t12, 1, t6 # .. e1 :
255 or t6, t12, t8 # e0 :
256 zapnot t0, t8, t0 # .. e1 : kill source bytes > null
257 zap t1, t8, t1 # e0 : kill dest bytes <= null
260 1: stq_u t0, 0(a0) # e0 :
263 $u_eoc: # end-of-count
268 $u_eocfin: # end-of-count, final word
272 /* Unaligned copy entry point. */
276 ldq_u t1, 0(a1) # e0 : load first source word
278 and a0, 7, t4 # .. e1 : find dest misalignment
279 and a1, 7, t5 # e0 : find src misalignment
281 /* Conditionally load the first destination word and a bytemask
282 with 0xff indicating that the destination byte is sacrosanct. */
284 mov zero, t0 # .. e1 :
287 ldq_u t0, 0(a0) # e0 :
289 mskql t6, a0, t6 # e0 :
291 subq a1, t4, a1 # .. e1 : sub dest misalignment from src addr
293 /* If source misalignment is larger than dest misalignment, we need
294 extra startup checks to avoid SEGV. */
296 cmplt t4, t5, t12 # e1 :
297 extql t1, a1, t1 # .. e0 : shift src into place
298 lda t2, -1 # e0 : for creating masks later
299 beq t12, $u_head # e1 :
301 mskqh t2, t5, t2 # e0 : begin src byte validity mask
302 cmpbge zero, t1, t8 # .. e1 : is there a zero?
303 extql t2, a1, t2 # e0 :
304 or t8, t10, t5 # .. e1 : test for end-of-count too
305 cmpbge zero, t2, t3 # e0 :
306 cmoveq a2, t5, t8 # .. e1 :
307 andnot t8, t3, t8 # e0 :
308 beq t8, $u_head # .. e1 (zdb)
310 /* At this point we've found a zero in the first partial word of
311 the source. We need to isolate the valid source data and mask
312 it into the original destination data. (Incidentally, we know
313 that we'll need at least one byte of that original dest word.) */
315 ldq_u t0, 0(a0) # e0 :
316 negq t8, t6 # .. e1 : build bitmask of bytes <= zero
317 mskqh t1, t4, t1 # e0 :
318 and t6, t8, t12 # .. e1 :
319 subq t12, 1, t6 # e0 :
320 or t6, t12, t8 # e1 :
322 zapnot t2, t8, t2 # e0 : prepare source word; mirror changes
323 zapnot t1, t8, t1 # .. e1 : to source validity mask
325 andnot t0, t2, t0 # e0 : zero place for source to reside
326 or t0, t1, t0 # e1 : and put it there
327 stq_u t0, 0(a0) # e0 :