3 * Optimized version of the standard memcpy() function
6 * in0: destination address
8 * in2: number of bytes to copy
12 * Copyright (C) 2000-2001 Hewlett-Packard Co
13 * Stephane Eranian <eranian@hpl.hp.com>
14 * David Mosberger-Tang <davidm@hpl.hp.com>
16 #include <asm/asmmacro.h>
24 // gas doesn't handle control flow across procedures, so it doesn't
25 // realize that a stop bit is needed before the "alloc" instruction
36 # define MEM_LAT 21 /* latency to memory */
53 # define N (MEM_LAT + 4)
54 # define Nrot ((N + 7) & ~7)
57 * First, check if everything (src, dst, len) is a multiple of eight. If
58 * so, we handle everything with no taken branches (other than the loop
59 * itself) and a small icache footprint. Otherwise, we jump off to
60 * the more general copy routine handling arbitrary
61 * sizes/alignment etc.
64 .save ar.pfs, saved_pfs
65 alloc saved_pfs=ar.pfs,3,Nrot,0,Nrot
77 cmp.eq p6,p0=in2,r0 // zero length?
78 mov retval=in0 // return dst
79 (p6) br.ret.spnt.many rp // zero length, return immediately
82 mov dst=in0 // copy because of rotation
83 shr.u cnt=in2,3 // number of 8-byte words to copy
87 adds cnt=-1,cnt // br.ctop is repeat/until
88 cmp.gtu p7,p0=16,in2 // copying less than 16 bytes?
97 mov src=in1 // copy because of rotation
98 (p7) br.cond.spnt.few .memcpy_short
99 (p6) br.cond.spnt.few .memcpy_long
112 (p[0]) ld8 val[0]=[src],8
117 (p[N-1])st8 [dst]=val[N-1],8
128 * Small (<16 bytes) unaligned copying is done via a simple byte-at-the-time
129 * copy loop. This performs relatively poorly on Itanium, but it doesn't
130 * get used very often (gcc inlines small copies) and due to atomicity
131 * issues, we want to avoid read-modify-write of entire words.
135 adds cnt=-1,in2 // br.ctop is repeat/until
151 * It is faster to put a stop bit in the loop here because it makes
152 * the pipeline shorter (and latency is what matters on short copies).
156 (p[0]) ld1 val[0]=[src],1
161 (p[MEM_LAT-1])st1 [dst]=val[MEM_LAT-1],1
171 * Large (>= 16 bytes) copying is done in a fancy way. Latency isn't
172 * an overriding concern here, but throughput is. We first do
173 * sub-word copying until the destination is aligned, then we check
174 * if the source is also aligned. If so, we do a simple load/store-loop
175 * until there are less than 8 bytes left over and then we do the tail,
176 * by storing the last few bytes using sub-word copying. If the source
177 * is not aligned, we branch off to the non-congruent loop.
185 * On Itanium, the pipeline itself runs without stalls. However, br.ctop
186 * seems to introduce an unavoidable bubble in the pipeline so the overall
187 * latency is 2 cycles/iteration. This gives us a _copy_ throughput
188 * of 4 byte/cycle. Still not bad.
192 # define N (MEM_LAT + 5) /* number of stages */
193 # define Nrot ((N+1 + 2 + 7) & ~7) /* number of rotating regs */
195 #define LOG_LOOP_SIZE 6
198 alloc t3=ar.pfs,3,Nrot,0,Nrot // resize register frame
199 and t0=-8,src // t0 = src & ~7
200 and t2=7,src // t2 = src & 7
202 ld8 t0=[t0] // t0 = 1st source word
203 adds src2=7,src // src2 = (src + 7)
204 sub t4=r0,dst // t4 = -dst
206 and src2=-8,src2 // src2 = (src + 7) & ~7
207 shl t2=t2,3 // t2 = 8*(src & 7)
208 shl t4=t4,3 // t4 = 8*(dst & 7)
210 ld8 t1=[src2] // t1 = 1st source word if src is 8-byte aligned, 2nd otherwise
211 sub t3=64,t2 // t3 = 64-8*(src & 7)
216 mov pr=t4,0x38 // (p5,p4,p3)=(dst & 7)
220 adds src_end=-1,src_end
232 and src_end=-8,src_end // src_end = last word of source buffer
235 // At this point, dst is aligned to 8 bytes and there at least 16-7=9 bytes left to copy:
237 1:{ add src=cnt,src // make src point to remainder of source buffer
238 sub cnt=in2,cnt // cnt = number of bytes left to copy
241 and src2=-8,src // align source pointer
242 adds t4=.memcpy_loops-1b,t4
245 and t0=7,src // t0 = src & 7
246 shr.u t2=cnt,3 // t2 = number of 8-byte words left to copy
247 shl cnt=cnt,3 // move bits 0-2 to 3-5
253 cmp.ne p6,p0=t0,r0 // is src aligned, too?
254 shl t0=t0,LOG_LOOP_SIZE // t0 = 8*(src & 7)
255 adds t2=-1,t2 // br.ctop is repeat/until
258 mov pr=cnt,0x38 // set (p5,p4,p3) to # of bytes last-word bytes to copy
268 (p6) ld8 val[1]=[src2],8 // prime the pump...
274 // At this point, (p5,p4,p3) are set to the number of bytes left to copy (which is
275 // less than 8) and t0 contains the last few bytes of the src buffer:
288 ///////////////////////////////////////////////////////
291 #define COPY(shift,index) \
293 (p[0]) ld8 val[0]=[src2],8; \
294 (p[MEM_LAT+3]) shrp w[0]=val[MEM_LAT+3],val[MEM_LAT+4-index],shift; \
295 brp.loop.imp 1b, 2f \
298 (p[MEM_LAT+4]) st8 [dst]=w[1],8; \
300 br.ctop.dptk.few 1b; \
303 ld8 val[N-1]=[src_end]; /* load last word (may be same as val[N]) */ \
305 shrp t0=val[N-1],val[N-index],shift; \
308 COPY(0, 1) /* no point special casing this---it doesn't go any faster without shrp */