memcpy.S 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. *
  3. * Optimized version of the standard memcpy() function
  4. *
  5. * Inputs:
  6. * in0: destination address
  7. * in1: source address
  8. * in2: number of bytes to copy
  9. * Output:
  10. * no return value
  11. *
  12. * Copyright (C) 2000-2001 Hewlett-Packard Co
  13. * Stephane Eranian <eranian@hpl.hp.com>
  14. * David Mosberger-Tang <davidm@hpl.hp.com>
  15. */
  16. #include <asm/asmmacro.h>
  17. GLOBAL_ENTRY(memcpy)
  18. # define MEM_LAT 21 /* latency to memory */
  19. # define dst r2
  20. # define src r3
  21. # define retval r8
  22. # define saved_pfs r9
  23. # define saved_lc r10
  24. # define saved_pr r11
  25. # define cnt r16
  26. # define src2 r17
  27. # define t0 r18
  28. # define t1 r19
  29. # define t2 r20
  30. # define t3 r21
  31. # define t4 r22
  32. # define src_end r23
  33. # define N (MEM_LAT + 4)
  34. # define Nrot ((N + 7) & ~7)
  35. /*
  36. * First, check if everything (src, dst, len) is a multiple of eight. If
  37. * so, we handle everything with no taken branches (other than the loop
  38. * itself) and a small icache footprint. Otherwise, we jump off to
  39. * the more general copy routine handling arbitrary
  40. * sizes/alignment etc.
  41. */
  42. .prologue
  43. .save ar.pfs, saved_pfs
  44. alloc saved_pfs=ar.pfs,3,Nrot,0,Nrot
  45. .save ar.lc, saved_lc
  46. mov saved_lc=ar.lc
  47. or t0=in0,in1
  48. ;;
  49. or t0=t0,in2
  50. .save pr, saved_pr
  51. mov saved_pr=pr
  52. .body
  53. cmp.eq p6,p0=in2,r0 // zero length?
  54. mov retval=in0 // return dst
  55. (p6) br.ret.spnt.many rp // zero length, return immediately
  56. ;;
  57. mov dst=in0 // copy because of rotation
  58. shr.u cnt=in2,3 // number of 8-byte words to copy
  59. mov pr.rot=1<<16
  60. ;;
  61. adds cnt=-1,cnt // br.ctop is repeat/until
  62. cmp.gtu p7,p0=16,in2 // copying less than 16 bytes?
  63. mov ar.ec=N
  64. ;;
  65. and t0=0x7,t0
  66. mov ar.lc=cnt
  67. ;;
  68. cmp.ne p6,p0=t0,r0
  69. mov src=in1 // copy because of rotation
  70. (p7) br.cond.spnt.few .memcpy_short
  71. (p6) br.cond.spnt.few .memcpy_long
  72. ;;
  73. nop.m 0
  74. ;;
  75. nop.m 0
  76. nop.i 0
  77. ;;
  78. nop.m 0
  79. ;;
  80. .rotr val[N]
  81. .rotp p[N]
  82. .align 32
  83. 1: { .mib
  84. (p[0]) ld8 val[0]=[src],8
  85. nop.i 0
  86. brp.loop.imp 1b, 2f
  87. }
  88. 2: { .mfb
  89. (p[N-1])st8 [dst]=val[N-1],8
  90. nop.f 0
  91. br.ctop.dptk.few 1b
  92. }
  93. ;;
  94. mov ar.lc=saved_lc
  95. mov pr=saved_pr,-1
  96. mov ar.pfs=saved_pfs
  97. br.ret.sptk.many rp
  98. /*
  99. * Small (<16 bytes) unaligned copying is done via a simple byte-at-the-time
  100. * copy loop. This performs relatively poorly on Itanium, but it doesn't
  101. * get used very often (gcc inlines small copies) and due to atomicity
  102. * issues, we want to avoid read-modify-write of entire words.
  103. */
  104. .align 32
  105. .memcpy_short:
  106. adds cnt=-1,in2 // br.ctop is repeat/until
  107. mov ar.ec=MEM_LAT
  108. brp.loop.imp 1f, 2f
  109. ;;
  110. mov ar.lc=cnt
  111. ;;
  112. nop.m 0
  113. ;;
  114. nop.m 0
  115. nop.i 0
  116. ;;
  117. nop.m 0
  118. ;;
  119. nop.m 0
  120. ;;
  121. /*
  122. * It is faster to put a stop bit in the loop here because it makes
  123. * the pipeline shorter (and latency is what matters on short copies).
  124. */
  125. .align 32
  126. 1: { .mib
  127. (p[0]) ld1 val[0]=[src],1
  128. nop.i 0
  129. brp.loop.imp 1b, 2f
  130. } ;;
  131. 2: { .mfb
  132. (p[MEM_LAT-1])st1 [dst]=val[MEM_LAT-1],1
  133. nop.f 0
  134. br.ctop.dptk.few 1b
  135. } ;;
  136. mov ar.lc=saved_lc
  137. mov pr=saved_pr,-1
  138. mov ar.pfs=saved_pfs
  139. br.ret.sptk.many rp
  140. /*
  141. * Large (>= 16 bytes) copying is done in a fancy way. Latency isn't
  142. * an overriding concern here, but throughput is. We first do
  143. * sub-word copying until the destination is aligned, then we check
  144. * if the source is also aligned. If so, we do a simple load/store-loop
  145. * until there are less than 8 bytes left over and then we do the tail,
  146. * by storing the last few bytes using sub-word copying. If the source
  147. * is not aligned, we branch off to the non-congruent loop.
  148. *
  149. * stage: op:
  150. * 0 ld
  151. * :
  152. * MEM_LAT+3 shrp
  153. * MEM_LAT+4 st
  154. *
  155. * On Itanium, the pipeline itself runs without stalls. However, br.ctop
  156. * seems to introduce an unavoidable bubble in the pipeline so the overall
  157. * latency is 2 cycles/iteration. This gives us a _copy_ throughput
  158. * of 4 byte/cycle. Still not bad.
  159. */
  160. # undef N
  161. # undef Nrot
  162. # define N (MEM_LAT + 5) /* number of stages */
  163. # define Nrot ((N+1 + 2 + 7) & ~7) /* number of rotating regs */
  164. #define LOG_LOOP_SIZE 6
  165. .memcpy_long:
  166. alloc t3=ar.pfs,3,Nrot,0,Nrot // resize register frame
  167. and t0=-8,src // t0 = src & ~7
  168. and t2=7,src // t2 = src & 7
  169. ;;
  170. ld8 t0=[t0] // t0 = 1st source word
  171. adds src2=7,src // src2 = (src + 7)
  172. sub t4=r0,dst // t4 = -dst
  173. ;;
  174. and src2=-8,src2 // src2 = (src + 7) & ~7
  175. shl t2=t2,3 // t2 = 8*(src & 7)
  176. shl t4=t4,3 // t4 = 8*(dst & 7)
  177. ;;
  178. ld8 t1=[src2] // t1 = 1st source word if src is 8-byte aligned, 2nd otherwise
  179. sub t3=64,t2 // t3 = 64-8*(src & 7)
  180. shr.u t0=t0,t2
  181. ;;
  182. add src_end=src,in2
  183. shl t1=t1,t3
  184. mov pr=t4,0x38 // (p5,p4,p3)=(dst & 7)
  185. ;;
  186. or t0=t0,t1
  187. mov cnt=r0
  188. adds src_end=-1,src_end
  189. ;;
  190. (p3) st1 [dst]=t0,1
  191. (p3) shr.u t0=t0,8
  192. (p3) adds cnt=1,cnt
  193. ;;
  194. (p4) st2 [dst]=t0,2
  195. (p4) shr.u t0=t0,16
  196. (p4) adds cnt=2,cnt
  197. ;;
  198. (p5) st4 [dst]=t0,4
  199. (p5) adds cnt=4,cnt
  200. and src_end=-8,src_end // src_end = last word of source buffer
  201. ;;
  202. // At this point, dst is aligned to 8 bytes and there at least 16-7=9 bytes left to copy:
  203. 1:{ add src=cnt,src // make src point to remainder of source buffer
  204. sub cnt=in2,cnt // cnt = number of bytes left to copy
  205. mov t4=ip
  206. } ;;
  207. and src2=-8,src // align source pointer
  208. adds t4=.memcpy_loops-1b,t4
  209. mov ar.ec=N
  210. and t0=7,src // t0 = src & 7
  211. shr.u t2=cnt,3 // t2 = number of 8-byte words left to copy
  212. shl cnt=cnt,3 // move bits 0-2 to 3-5
  213. ;;
  214. .rotr val[N+1], w[2]
  215. .rotp p[N]
  216. cmp.ne p6,p0=t0,r0 // is src aligned, too?
  217. shl t0=t0,LOG_LOOP_SIZE // t0 = 8*(src & 7)
  218. adds t2=-1,t2 // br.ctop is repeat/until
  219. ;;
  220. add t4=t0,t4
  221. mov pr=cnt,0x38 // set (p5,p4,p3) to # of bytes last-word bytes to copy
  222. mov ar.lc=t2
  223. ;;
  224. nop.m 0
  225. ;;
  226. nop.m 0
  227. nop.i 0
  228. ;;
  229. nop.m 0
  230. ;;
  231. (p6) ld8 val[1]=[src2],8 // prime the pump...
  232. mov b6=t4
  233. br.sptk.few b6
  234. ;;
  235. .memcpy_tail:
  236. // At this point, (p5,p4,p3) are set to the number of bytes left to copy (which is
  237. // less than 8) and t0 contains the last few bytes of the src buffer:
  238. (p5) st4 [dst]=t0,4
  239. (p5) shr.u t0=t0,32
  240. mov ar.lc=saved_lc
  241. ;;
  242. (p4) st2 [dst]=t0,2
  243. (p4) shr.u t0=t0,16
  244. mov ar.pfs=saved_pfs
  245. ;;
  246. (p3) st1 [dst]=t0
  247. mov pr=saved_pr,-1
  248. br.ret.sptk.many rp
  249. ///////////////////////////////////////////////////////
  250. .align 64
  251. #define COPY(shift,index) \
  252. 1: { .mib \
  253. (p[0]) ld8 val[0]=[src2],8; \
  254. (p[MEM_LAT+3]) shrp w[0]=val[MEM_LAT+3],val[MEM_LAT+4-index],shift; \
  255. brp.loop.imp 1b, 2f \
  256. }; \
  257. 2: { .mfb \
  258. (p[MEM_LAT+4]) st8 [dst]=w[1],8; \
  259. nop.f 0; \
  260. br.ctop.dptk.few 1b; \
  261. }; \
  262. ;; \
  263. ld8 val[N-1]=[src_end]; /* load last word (may be same as val[N]) */ \
  264. ;; \
  265. shrp t0=val[N-1],val[N-index],shift; \
  266. br .memcpy_tail
  267. .memcpy_loops:
  268. COPY(0, 1) /* no point special casing this---it doesn't go any faster without shrp */
  269. COPY(8, 0)
  270. COPY(16, 0)
  271. COPY(24, 0)
  272. COPY(32, 0)
  273. COPY(40, 0)
  274. COPY(48, 0)
  275. COPY(56, 0)
  276. END(memcpy)