memcpy.S 6.8 KB

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