memcpy.S 6.8 KB

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