memcpy.S 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. /*
  2. * "memcpy" implementation of SuperH
  3. *
  4. * Copyright (C) 1999 Niibe Yutaka
  5. * Copyright (c) 2002 STMicroelectronics Ltd
  6. * Modified from memcpy.S and micro-optimised for SH4
  7. * Stuart Menefy (stuart.menefy@st.com)
  8. *
  9. * Copyright (c) 2009 STMicroelectronics Ltd
  10. * Optimised using prefetching and 64bit data transfer via FPU
  11. * Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  12. */
  13. /*
  14. * void *memcpy(void *dst, const void *src, size_t n);
  15. *
  16. * It is assumed that there is no overlap between src and dst.
  17. * If there is an overlap, then the results are undefined.
  18. */
  19. #include <sysdep.h>
  20. #include <endian.h>
  21. #if defined (__LITTLE_ENDIAN__) && defined (__SH_FPU_ANY__)
  22. #define MEMCPY_USES_FPU
  23. /* Use paired single precision load or store mode for 64-bit tranfering.
  24. * FPSCR.SZ=1,FPSCR.SZ=0 is well defined on both SH4-200 and SH4-300.
  25. * Currenlty it has been only implemented and tested for little endian mode. */
  26. .macro FPU_SET_PAIRED_PREC
  27. sts fpscr, r7
  28. mov #0x10, r0 ! PR=0 SZ=1
  29. shll16 r0
  30. lds r0, fpscr
  31. .endm
  32. .macro RESTORE_FPSCR
  33. lds r7, fpscr
  34. .endm
  35. .macro DALLOC
  36. ! Cache allocate + store on dst-32.
  37. add #-32, r1
  38. movca.l r0, @r1
  39. add #32, r1
  40. .endm
  41. #endif
  42. !
  43. ! GHIJ KLMN OPQR --> ...G HIJK LMNO PQR.
  44. !
  45. ! Size is 16 or greater, and may have trailing bytes
  46. .balign 32
  47. .Lcase1:
  48. ! Read a long word and write a long word at once
  49. ! At the start of each iteration, r7 contains last long load
  50. add #-1,r5 ! 79 EX
  51. mov r4,r2 ! 5 MT (0 cycles latency)
  52. mov.l @(r0,r5),r7 ! 21 LS (2 cycles latency)
  53. add #-4,r5 ! 50 EX
  54. add #7,r2 ! 79 EX
  55. !
  56. #ifdef __LITTLE_ENDIAN__
  57. ! 6 cycles, 4 bytes per iteration
  58. 3: mov.l @(r0,r5),r1 ! 21 LS (latency=2) ! NMLK
  59. mov r7, r3 ! 5 MT (latency=0) ! RQPO
  60. cmp/hi r2,r0 ! 57 MT
  61. shll16 r3 ! 103 EX
  62. mov r1,r6 ! 5 MT (latency=0)
  63. shll8 r3 ! 102 EX ! Oxxx
  64. shlr8 r6 ! 106 EX ! xNML
  65. mov r1, r7 ! 5 MT (latency=0)
  66. or r6,r3 ! 82 EX ! ONML
  67. bt/s 3b ! 109 BR
  68. mov.l r3,@-r0 ! 30 LS
  69. #else
  70. 3: mov.l @(r0,r5),r1 ! 21 LS (latency=2) ! KLMN
  71. mov r7,r3 ! 5 MT (latency=0) ! OPQR
  72. cmp/hi r2,r0 ! 57 MT
  73. shlr16 r3 ! 107 EX
  74. shlr8 r3 ! 106 EX ! xxxO
  75. mov r1,r6 ! 5 MT (latency=0)
  76. shll8 r6 ! 102 EX ! LMNx
  77. mov r1,r7 ! 5 MT (latency=0)
  78. or r6,r3 ! 82 EX ! LMNO
  79. bt/s 3b ! 109 BR
  80. mov.l r3,@-r0 ! 30 LS
  81. #endif
  82. ! Finally, copy a byte at once, if necessary
  83. add #4,r5 ! 50 EX
  84. cmp/eq r4,r0 ! 54 MT
  85. add #-6,r2 ! 50 EX
  86. bt 9f ! 109 BR
  87. 8: cmp/hi r2,r0 ! 57 MT
  88. mov.b @(r0,r5),r1 ! 20 LS (latency=2)
  89. bt/s 8b ! 109 BR
  90. mov.b r1,@-r0 ! 29 LS
  91. 9: rts
  92. nop
  93. !
  94. ! GHIJ KLMN OPQR --> .GHI JKLM NOPQ R...
  95. !
  96. ! Size is 16 or greater, and may have trailing bytes
  97. .balign 32
  98. .Lcase3:
  99. ! Read a long word and write a long word at once
  100. ! At the start of each iteration, r7 contains last long load
  101. add #-3,r5 ! 79 EX
  102. mov r4,r2 ! 5 MT (0 cycles latency)
  103. mov.l @(r0,r5),r7 ! 21 LS (2 cycles latency)
  104. add #-4,r5 ! 50 EX
  105. add #7,r2 ! 79 EX
  106. !
  107. #ifdef __LITTLE_ENDIAN__
  108. ! 6 cycles, 4 bytes per iteration
  109. 3: mov.l @(r0,r5),r1 ! 21 LS (latency=2) ! NMLK
  110. mov r7, r3 ! 5 MT (latency=0) ! RQPO
  111. cmp/hi r2,r0 ! 57 MT
  112. shll8 r3 ! 102 EX ! QPOx
  113. mov r1,r6 ! 5 MT (latency=0)
  114. shlr16 r6 ! 107 EX
  115. shlr8 r6 ! 106 EX ! xxxN
  116. mov r1, r7 ! 5 MT (latency=0)
  117. or r6,r3 ! 82 EX ! QPON
  118. bt/s 3b ! 109 BR
  119. mov.l r3,@-r0 ! 30 LS
  120. #else
  121. 3: mov r7,r3 ! OPQR
  122. shlr8 r3 ! xOPQ
  123. mov.l @(r0,r5),r7 ! KLMN
  124. mov r7,r6
  125. shll16 r6
  126. shll8 r6 ! Nxxx
  127. or r6,r3 ! NOPQ
  128. cmp/hi r2,r0
  129. bt/s 3b
  130. mov.l r3,@-r0
  131. #endif
  132. ! Finally, copy a byte at once, if necessary
  133. add #6,r5 ! 50 EX
  134. cmp/eq r4,r0 ! 54 MT
  135. add #-6,r2 ! 50 EX
  136. bt 9f ! 109 BR
  137. 8: cmp/hi r2,r0 ! 57 MT
  138. mov.b @(r0,r5),r1 ! 20 LS (latency=2)
  139. bt/s 8b ! 109 BR
  140. mov.b r1,@-r0 ! 29 LS
  141. 9: rts
  142. nop
  143. ENTRY(memcpy)
  144. ! Calculate the invariants which will be used in the remainder
  145. ! of the code:
  146. !
  147. ! r4 --> [ ... ] DST [ ... ] SRC
  148. ! [ ... ] [ ... ]
  149. ! : :
  150. ! r0 --> [ ... ] r0+r5 --> [ ... ]
  151. !
  152. !
  153. ! Short circuit the common case of src, dst and len being 32 bit aligned
  154. ! and test for zero length move
  155. mov r6, r0 ! 5 MT (0 cycle latency)
  156. or r4, r0 ! 82 EX
  157. or r5, r0 ! 82 EX
  158. tst r6, r6 ! 86 MT
  159. bt/s 99f ! 111 BR (zero len)
  160. tst #3, r0 ! 87 MT
  161. mov r4, r0 ! 5 MT (0 cycle latency)
  162. add r6, r0 ! 49 EX
  163. bt/s .Lcase00 ! 111 BR (aligned)
  164. sub r4, r5 ! 75 EX
  165. ! Arguments are not nicely long word aligned or zero len.
  166. ! Check for small copies, and if so do a simple byte at a time copy.
  167. !
  168. ! Deciding on an exact value of 'small' is not easy, as the point at which
  169. ! using the optimised routines become worthwhile varies (these are the
  170. ! cycle counts for differnet sizes using byte-at-a-time vs. optimised):
  171. ! size byte-at-time long word byte
  172. ! 16 42 39-40 46-50 50-55
  173. ! 24 58 43-44 54-58 62-67
  174. ! 36 82 49-50 66-70 80-85
  175. ! However the penalty for getting it 'wrong' is much higher for long word
  176. ! aligned data (and this is more common), so use a value of 16.
  177. mov #16, r1 ! 6 EX
  178. cmp/gt r6,r1 ! 56 MT
  179. add #-1,r5 ! 50 EX
  180. bf/s 6f ! 108 BR (not small)
  181. mov r5, r3 ! 5 MT (latency=0)
  182. shlr r6 ! 104 EX
  183. mov.b @(r0,r5),r1 ! 20 LS (latency=2)
  184. bf/s 4f ! 111 BR
  185. add #-1,r3 ! 50 EX
  186. tst r6, r6 ! 86 MT
  187. bt/s 98f ! 110 BR
  188. mov.b r1,@-r0 ! 29 LS
  189. ! 4 cycles, 2 bytes per iteration
  190. 3: mov.b @(r0,r5),r1 ! 20 LS (latency=2)
  191. 4: mov.b @(r0,r3),r2 ! 20 LS (latency=2)
  192. dt r6 ! 67 EX
  193. mov.b r1,@-r0 ! 29 LS
  194. bf/s 3b ! 111 BR
  195. mov.b r2,@-r0 ! 29 LS
  196. 98:
  197. rts
  198. nop
  199. 99: rts
  200. mov r4, r0
  201. ! Size is not small, so its worthwhile looking for optimisations.
  202. ! First align destination to a long word boundary.
  203. !
  204. ! r5 = normal value -1
  205. 6: tst #3, r0 ! 87 MT
  206. mov #3, r3 ! 6 EX
  207. bt/s 2f ! 111 BR
  208. and r0,r3 ! 78 EX
  209. ! 3 cycles, 1 byte per iteration
  210. 1: dt r3 ! 67 EX
  211. mov.b @(r0,r5),r1 ! 19 LS (latency=2)
  212. add #-1, r6 ! 79 EX
  213. bf/s 1b ! 109 BR
  214. mov.b r1,@-r0 ! 28 LS
  215. 2: add #1, r5 ! 79 EX
  216. ! Now select the appropriate bulk transfer code based on relative
  217. ! alignment of src and dst.
  218. mov r0, r3 ! 5 MT (latency=0)
  219. mov r5, r0 ! 5 MT (latency=0)
  220. tst #1, r0 ! 87 MT
  221. bf/s 1f ! 111 BR
  222. mov #64, r7 ! 6 EX
  223. ! bit 0 clear
  224. cmp/ge r7, r6 ! 55 MT
  225. bt/s 2f ! 111 BR
  226. tst #2, r0 ! 87 MT
  227. ! small
  228. bt/s .Lcase0
  229. mov r3, r0
  230. bra .Lcase2
  231. nop
  232. ! big
  233. 2: bt/s .Lcase0b
  234. mov r3, r0
  235. bra .Lcase2b
  236. nop
  237. ! bit 0 set
  238. 1: tst #2, r0 ! 87 MT
  239. bt/s .Lcase1
  240. mov r3, r0
  241. bra .Lcase3
  242. nop
  243. !
  244. ! GHIJ KLMN OPQR --> GHIJ KLMN OPQR
  245. !
  246. ! src, dst and size are all long word aligned
  247. ! size is non-zero
  248. .balign 32
  249. .Lcase00:
  250. mov #64, r1 ! 6 EX
  251. mov r5, r3 ! 5 MT (latency=0)
  252. cmp/gt r6, r1 ! 56 MT
  253. add #-4, r5 ! 50 EX
  254. bf .Lcase00b ! 108 BR (big loop)
  255. shlr2 r6 ! 105 EX
  256. shlr r6 ! 104 EX
  257. mov.l @(r0, r5), r1 ! 21 LS (latency=2)
  258. bf/s 4f ! 111 BR
  259. add #-8, r3 ! 50 EX
  260. tst r6, r6 ! 86 MT
  261. bt/s 5f ! 110 BR
  262. mov.l r1,@-r0 ! 30 LS
  263. ! 4 cycles, 2 long words per iteration
  264. 3: mov.l @(r0, r5), r1 ! 21 LS (latency=2)
  265. 4: mov.l @(r0, r3), r2 ! 21 LS (latency=2)
  266. dt r6 ! 67 EX
  267. mov.l r1, @-r0 ! 30 LS
  268. bf/s 3b ! 109 BR
  269. mov.l r2, @-r0 ! 30 LS
  270. 5: rts
  271. nop
  272. ! Size is 16 or greater and less than 64, but may have trailing bytes
  273. .balign 32
  274. .Lcase0:
  275. add #-4, r5 ! 50 EX
  276. mov r4, r7 ! 5 MT (latency=0)
  277. mov.l @(r0, r5), r1 ! 21 LS (latency=2)
  278. mov #4, r2 ! 6 EX
  279. add #11, r7 ! 50 EX
  280. tst r2, r6 ! 86 MT
  281. mov r5, r3 ! 5 MT (latency=0)
  282. bt/s 4f ! 111 BR
  283. add #-4, r3 ! 50 EX
  284. mov.l r1,@-r0 ! 30 LS
  285. ! 4 cycles, 2 long words per iteration
  286. 3: mov.l @(r0, r5), r1 ! 21 LS (latency=2)
  287. 4: mov.l @(r0, r3), r2 ! 21 LS (latency=2)
  288. cmp/hi r7, r0
  289. mov.l r1, @-r0 ! 30 LS
  290. bt/s 3b ! 109 BR
  291. mov.l r2, @-r0 ! 30 LS
  292. ! Copy the final 0-3 bytes
  293. add #3,r5 ! 50 EX
  294. cmp/eq r0, r4 ! 54 MT
  295. add #-10, r7 ! 50 EX
  296. bt 9f ! 110 BR
  297. ! 3 cycles, 1 byte per iteration
  298. 1: mov.b @(r0,r5),r1 ! 19 LS
  299. cmp/hi r7,r0 ! 57 MT
  300. bt/s 1b ! 111 BR
  301. mov.b r1,@-r0 ! 28 LS
  302. 9: rts
  303. nop
  304. ! Size is at least 64 bytes, so will be going round the big loop at least once.
  305. !
  306. ! r2 = rounded up r4
  307. ! r3 = rounded down r0
  308. .balign 32
  309. .Lcase0b:
  310. add #-4, r5 ! 50 EX
  311. .Lcase00b:
  312. mov r0, r3 ! 5 MT (latency=0)
  313. mov #(~0x1f), r1 ! 6 EX
  314. and r1, r3 ! 78 EX
  315. mov r4, r2 ! 5 MT (latency=0)
  316. cmp/eq r3, r0 ! 54 MT
  317. add #0x1f, r2 ! 50 EX
  318. bt/s 1f ! 110 BR
  319. and r1, r2 ! 78 EX
  320. ! copy initial words until cache line aligned
  321. mov.l @(r0, r5), r1 ! 21 LS (latency=2)
  322. tst #4, r0 ! 87 MT
  323. mov r5, r6 ! 5 MT (latency=0)
  324. add #-4, r6 ! 50 EX
  325. bt/s 4f ! 111 BR
  326. add #8, r3 ! 50 EX
  327. tst #0x18, r0 ! 87 MT
  328. bt/s 1f ! 109 BR
  329. mov.l r1,@-r0 ! 30 LS
  330. ! 4 cycles, 2 long words per iteration
  331. 3: mov.l @(r0, r5), r1 ! 21 LS (latency=2)
  332. 4: mov.l @(r0, r6), r7 ! 21 LS (latency=2)
  333. cmp/eq r3, r0 ! 54 MT
  334. mov.l r1, @-r0 ! 30 LS
  335. bf/s 3b ! 109 BR
  336. mov.l r7, @-r0 ! 30 LS
  337. #ifdef MEMCPY_USES_FPU
  338. ! Copy the cache line aligned blocks by using the FPU registers.
  339. ! If src and dst are well aligned adopt 64-bit data transfer.
  340. ! We also need r0 as a temporary (for movca), so 'undo' the invariant:
  341. ! r5: src (was r0+r5)
  342. ! r1: dest (was r0)
  343. 1:
  344. add r0, r5
  345. mov r0, r1
  346. mov r1, r3 ! MT
  347. sub r2, r3 ! EX (r3 - r2 -> r3)
  348. mov #-5, r0
  349. shld r0, r3 ! number of the cache lines
  350. mov #8, r0
  351. cmp/ge r0, r3 ! Check if there are many cache lines to copy.
  352. bf 45f ! Copy cache line aligned blocks without pref.
  353. mov r5, r0
  354. add #-0x7c, r0
  355. tst #7, r0 ! src is 8byte aligned
  356. bf 45f
  357. ! Many cache lines have to be copied and the buffers are well aligned.
  358. ! Aggressive prefetching and FPU in single paired precision.
  359. mov r0, r5
  360. mov r5, r6
  361. add #-0x80, r6 ! prefetch head
  362. FPU_SET_PAIRED_PREC
  363. mov #4, r0
  364. 67:
  365. add #-0x20, r6
  366. pref @r6
  367. add #-0x20, r6
  368. pref @r6
  369. fmov @r5+, dr0
  370. fmov @r5+, dr2
  371. fmov @r5+, dr4
  372. fmov @r5+, dr6
  373. fmov @r5+, dr8
  374. fmov @r5+, dr10
  375. fmov @r5+, dr12
  376. fmov @r5+, dr14
  377. fmov @r5+, xd0
  378. fmov @r5+, xd2
  379. fmov @r5+, xd4
  380. fmov @r5+, xd6
  381. fmov @r5+, xd8
  382. fmov @r5+, xd10
  383. fmov @r5+, xd12
  384. fmov @r5+, xd14
  385. DALLOC
  386. fmov xd14, @-r1
  387. fmov xd12, @-r1
  388. fmov xd10, @-r1
  389. fmov xd8, @-r1
  390. DALLOC
  391. fmov xd6, @-r1
  392. fmov xd4, @-r1
  393. fmov xd2, @-r1
  394. fmov xd0, @-r1
  395. DALLOC
  396. fmov dr14, @-r1
  397. fmov dr12, @-r1
  398. fmov dr10, @-r1
  399. fmov dr8, @-r1
  400. DALLOC
  401. fmov dr6, @-r1
  402. add #-0x80, r5
  403. fmov dr4, @-r1
  404. add #-0x80, r5
  405. fmov dr2, @-r1
  406. add #-0x20, r6
  407. fmov dr0, @-r1
  408. add #-4, r3
  409. pref @r6
  410. add #-0x20, r6
  411. cmp/ge r0, r3
  412. bt/s 67b
  413. pref @r6
  414. ! Other cache lines could be copied: so use the FPU in single paired
  415. ! precision without prefetching. No check for alignment is necessary.
  416. mov #1, r0
  417. cmp/ge r0, r3
  418. bt/s 4f
  419. add #0x60, r5
  420. RESTORE_FPSCR
  421. bra 5f
  422. nop
  423. ! No prefetch and FPU in single precision.
  424. 45:
  425. add #-0x1c, r5
  426. mov r5, r0
  427. tst #7, r0
  428. bt 3f
  429. 2: fmov.s @r5+, fr0
  430. fmov.s @r5+, fr1
  431. fmov.s @r5+, fr2
  432. fmov.s @r5+, fr3
  433. fmov.s @r5+, fr4
  434. fmov.s @r5+, fr5
  435. fmov.s @r5+, fr6
  436. fmov.s @r5+, fr7
  437. DALLOC
  438. fmov.s fr7, @-r1
  439. fmov.s fr6, @-r1
  440. fmov.s fr5, @-r1
  441. fmov.s fr4, @-r1
  442. fmov.s fr3, @-r1
  443. fmov.s fr2, @-r1
  444. fmov.s fr1, @-r1
  445. fmov.s fr0, @-r1
  446. cmp/eq r2,r1
  447. bf/s 2b
  448. add #-0x40, r5
  449. bra 5f
  450. nop
  451. ! No prefetch and FPU in single paired precision.
  452. 3: FPU_SET_PAIRED_PREC
  453. 4: fmov @r5+, dr0
  454. fmov @r5+, dr2
  455. fmov @r5+, dr4
  456. fmov @r5+, dr6
  457. DALLOC
  458. fmov dr6, @-r1
  459. fmov dr4, @-r1
  460. fmov dr2, @-r1
  461. fmov dr0, @-r1
  462. cmp/eq r2,r1
  463. bf/s 4b
  464. add #-0x40, r5
  465. RESTORE_FPSCR
  466. 5: mov r1, r0
  467. cmp/eq r4, r0 ! 54 MT
  468. bf/s 1f ! 109 BR
  469. sub r1, r5 ! 75 EX
  470. rts
  471. nop
  472. 1:
  473. #else
  474. ! Copy the cache line aligned blocks
  475. !
  476. ! In use: r0, r2, r4, r5
  477. ! Scratch: r1, r3, r6, r7
  478. !
  479. ! We could do this with the four scratch registers, but if src
  480. ! and dest hit the same cache line, this will thrash, so make
  481. ! use of additional registers.
  482. !
  483. ! We also need r0 as a temporary (for movca), so 'undo' the invariant:
  484. ! r5: src (was r0+r5)
  485. ! r1: dest (was r0)
  486. ! this can be reversed at the end, so we don't need to save any extra
  487. ! state.
  488. !
  489. 1: mov.l r8, @-r15 ! 30 LS
  490. add r0, r5 ! 49 EX
  491. mov.l r9, @-r15 ! 30 LS
  492. mov r0, r1 ! 5 MT (latency=0)
  493. mov.l r10, @-r15 ! 30 LS
  494. add #-0x1c, r5 ! 50 EX
  495. mov.l r11, @-r15 ! 30 LS
  496. ! 16 cycles, 32 bytes per iteration
  497. 2: mov.l @(0x00,r5),r0 ! 18 LS (latency=2)
  498. add #-0x20, r1 ! 50 EX
  499. mov.l @(0x04,r5),r3 ! 18 LS (latency=2)
  500. mov.l @(0x08,r5),r6 ! 18 LS (latency=2)
  501. mov.l @(0x0c,r5),r7 ! 18 LS (latency=2)
  502. mov.l @(0x10,r5),r8 ! 18 LS (latency=2)
  503. mov.l @(0x14,r5),r9 ! 18 LS (latency=2)
  504. mov.l @(0x18,r5),r10 ! 18 LS (latency=2)
  505. mov.l @(0x1c,r5),r11 ! 18 LS (latency=2)
  506. movca.l r0,@r1 ! 40 LS (latency=3-7)
  507. mov.l r3,@(0x04,r1) ! 33 LS
  508. mov.l r6,@(0x08,r1) ! 33 LS
  509. mov.l r7,@(0x0c,r1) ! 33 LS
  510. mov.l r8,@(0x10,r1) ! 33 LS
  511. add #-0x20, r5 ! 50 EX
  512. mov.l r9,@(0x14,r1) ! 33 LS
  513. cmp/eq r2,r1 ! 54 MT
  514. mov.l r10,@(0x18,r1) ! 33 LS
  515. bf/s 2b ! 109 BR
  516. mov.l r11,@(0x1c,r1) ! 33 LS
  517. mov r1, r0 ! 5 MT (latency=0)
  518. mov.l @r15+, r11 ! 15 LS
  519. sub r1, r5 ! 75 EX
  520. mov.l @r15+, r10 ! 15 LS
  521. cmp/eq r4, r0 ! 54 MT
  522. bf/s 1f ! 109 BR
  523. mov.l @r15+, r9 ! 15 LS
  524. rts
  525. 1: mov.l @r15+, r8 ! 15 LS
  526. #endif
  527. sub r4, r1 ! 75 EX (len remaining)
  528. ! number of trailing bytes is non-zero
  529. !
  530. ! invariants restored (r5 already decremented by 4)
  531. ! also r1=num bytes remaining
  532. mov #4, r2 ! 6 EX
  533. mov r4, r7 ! 5 MT (latency=0)
  534. add #0x1c, r5 ! 50 EX (back to -4)
  535. cmp/hs r2, r1 ! 58 MT
  536. bf/s 5f ! 108 BR
  537. add #11, r7 ! 50 EX
  538. mov.l @(r0, r5), r6 ! 21 LS (latency=2)
  539. tst r2, r1 ! 86 MT
  540. mov r5, r3 ! 5 MT (latency=0)
  541. bt/s 4f ! 111 BR
  542. add #-4, r3 ! 50 EX
  543. cmp/hs r2, r1 ! 58 MT
  544. bt/s 5f ! 111 BR
  545. mov.l r6,@-r0 ! 30 LS
  546. ! 4 cycles, 2 long words per iteration
  547. 3: mov.l @(r0, r5), r6 ! 21 LS (latency=2)
  548. 4: mov.l @(r0, r3), r2 ! 21 LS (latency=2)
  549. cmp/hi r7, r0
  550. mov.l r6, @-r0 ! 30 LS
  551. bt/s 3b ! 109 BR
  552. mov.l r2, @-r0 ! 30 LS
  553. ! Copy the final 0-3 bytes
  554. 5: cmp/eq r0, r4 ! 54 MT
  555. add #-10, r7 ! 50 EX
  556. bt 9f ! 110 BR
  557. add #3,r5 ! 50 EX
  558. ! 3 cycles, 1 byte per iteration
  559. 1: mov.b @(r0,r5),r1 ! 19 LS
  560. cmp/hi r7,r0 ! 57 MT
  561. bt/s 1b ! 111 BR
  562. mov.b r1,@-r0 ! 28 LS
  563. 9: rts
  564. nop
  565. !
  566. ! GHIJ KLMN OPQR --> ..GH IJKL MNOP QR..
  567. !
  568. .balign 32
  569. .Lcase2:
  570. ! Size is 16 or greater and less then 64, but may have trailing bytes
  571. 2: mov r5, r6 ! 5 MT (latency=0)
  572. add #-2,r5 ! 50 EX
  573. mov r4,r2 ! 5 MT (latency=0)
  574. add #-4,r6 ! 50 EX
  575. add #7,r2 ! 50 EX
  576. 3: mov.w @(r0,r5),r1 ! 20 LS (latency=2)
  577. mov.w @(r0,r6),r3 ! 20 LS (latency=2)
  578. cmp/hi r2,r0 ! 57 MT
  579. mov.w r1,@-r0 ! 29 LS
  580. bt/s 3b ! 111 BR
  581. mov.w r3,@-r0 ! 29 LS
  582. bra 10f
  583. nop
  584. .balign 32
  585. .Lcase2b:
  586. ! Size is at least 64 bytes, so will be going round the big loop at least once.
  587. !
  588. ! r2 = rounded up r4
  589. ! r3 = rounded down r0
  590. mov r0, r3 ! 5 MT (latency=0)
  591. mov #(~0x1f), r1 ! 6 EX
  592. and r1, r3 ! 78 EX
  593. mov r4, r2 ! 5 MT (latency=0)
  594. cmp/eq r3, r0 ! 54 MT
  595. add #0x1f, r2 ! 50 EX
  596. add #-2, r5 ! 50 EX
  597. bt/s 1f ! 110 BR
  598. and r1, r2 ! 78 EX
  599. ! Copy a short word one at a time until we are cache line aligned
  600. ! Normal values: r0, r2, r3, r4
  601. ! Unused: r1, r6, r7
  602. ! Mod: r5 (=r5-2)
  603. !
  604. add #2, r3 ! 50 EX
  605. 2: mov.w @(r0,r5),r1 ! 20 LS (latency=2)
  606. cmp/eq r3,r0 ! 54 MT
  607. bf/s 2b ! 111 BR
  608. mov.w r1,@-r0 ! 29 LS
  609. ! Copy the cache line aligned blocks
  610. !
  611. ! In use: r0, r2, r4, r5 (=r5-2)
  612. ! Scratch: r1, r3, r6, r7
  613. !
  614. ! We could do this with the four scratch registers, but if src
  615. ! and dest hit the same cache line, this will thrash, so make
  616. ! use of additional registers.
  617. !
  618. ! We also need r0 as a temporary (for movca), so 'undo' the invariant:
  619. ! r5: src (was r0+r5)
  620. ! r1: dest (was r0)
  621. ! this can be reversed at the end, so we don't need to save any extra
  622. ! state.
  623. !
  624. 1: mov.l r8, @-r15 ! 30 LS
  625. add r0, r5 ! 49 EX
  626. mov.l r9, @-r15 ! 30 LS
  627. mov r0, r1 ! 5 MT (latency=0)
  628. mov.l r10, @-r15 ! 30 LS
  629. add #-0x1e, r5 ! 50 EX
  630. mov.l r11, @-r15 ! 30 LS
  631. mov.l r12, @-r15 ! 30 LS
  632. ! 17 cycles, 32 bytes per iteration
  633. #ifdef __LITTLE_ENDIAN__
  634. 2: mov.w @r5+, r0 ! 14 LS (latency=2) ..JI
  635. add #-0x20, r1 ! 50 EX
  636. mov.l @r5+, r3 ! 15 LS (latency=2) NMLK
  637. mov.l @r5+, r6 ! 15 LS (latency=2) RQPO
  638. shll16 r0 ! 103 EX JI..
  639. mov.l @r5+, r7 ! 15 LS (latency=2)
  640. xtrct r3, r0 ! 48 EX LKJI
  641. mov.l @r5+, r8 ! 15 LS (latency=2)
  642. xtrct r6, r3 ! 48 EX PONM
  643. mov.l @r5+, r9 ! 15 LS (latency=2)
  644. xtrct r7, r6 ! 48 EX
  645. mov.l @r5+, r10 ! 15 LS (latency=2)
  646. xtrct r8, r7 ! 48 EX
  647. mov.l @r5+, r11 ! 15 LS (latency=2)
  648. xtrct r9, r8 ! 48 EX
  649. mov.w @r5+, r12 ! 15 LS (latency=2)
  650. xtrct r10, r9 ! 48 EX
  651. movca.l r0,@r1 ! 40 LS (latency=3-7)
  652. xtrct r11, r10 ! 48 EX
  653. mov.l r3, @(0x04,r1) ! 33 LS
  654. xtrct r12, r11 ! 48 EX
  655. mov.l r6, @(0x08,r1) ! 33 LS
  656. mov.l r7, @(0x0c,r1) ! 33 LS
  657. mov.l r8, @(0x10,r1) ! 33 LS
  658. add #-0x40, r5 ! 50 EX
  659. mov.l r9, @(0x14,r1) ! 33 LS
  660. cmp/eq r2,r1 ! 54 MT
  661. mov.l r10, @(0x18,r1) ! 33 LS
  662. bf/s 2b ! 109 BR
  663. mov.l r11, @(0x1c,r1) ! 33 LS
  664. #else
  665. 2: mov.w @(0x1e,r5), r0 ! 17 LS (latency=2)
  666. add #-2, r5 ! 50 EX
  667. mov.l @(0x1c,r5), r3 ! 18 LS (latency=2)
  668. add #-4, r1 ! 50 EX
  669. mov.l @(0x18,r5), r6 ! 18 LS (latency=2)
  670. shll16 r0 ! 103 EX
  671. mov.l @(0x14,r5), r7 ! 18 LS (latency=2)
  672. xtrct r3, r0 ! 48 EX
  673. mov.l @(0x10,r5), r8 ! 18 LS (latency=2)
  674. xtrct r6, r3 ! 48 EX
  675. mov.l @(0x0c,r5), r9 ! 18 LS (latency=2)
  676. xtrct r7, r6 ! 48 EX
  677. mov.l @(0x08,r5), r10 ! 18 LS (latency=2)
  678. xtrct r8, r7 ! 48 EX
  679. mov.l @(0x04,r5), r11 ! 18 LS (latency=2)
  680. xtrct r9, r8 ! 48 EX
  681. mov.l @(0x00,r5), r12 ! 18 LS (latency=2)
  682. xtrct r10, r9 ! 48 EX
  683. movca.l r0,@r1 ! 40 LS (latency=3-7)
  684. add #-0x1c, r1 ! 50 EX
  685. mov.l r3, @(0x18,r1) ! 33 LS
  686. xtrct r11, r10 ! 48 EX
  687. mov.l r6, @(0x14,r1) ! 33 LS
  688. xtrct r12, r11 ! 48 EX
  689. mov.l r7, @(0x10,r1) ! 33 LS
  690. mov.l r8, @(0x0c,r1) ! 33 LS
  691. add #-0x1e, r5 ! 50 EX
  692. mov.l r9, @(0x08,r1) ! 33 LS
  693. cmp/eq r2,r1 ! 54 MT
  694. mov.l r10, @(0x04,r1) ! 33 LS
  695. bf/s 2b ! 109 BR
  696. mov.l r11, @(0x00,r1) ! 33 LS
  697. #endif
  698. mov.l @r15+, r12
  699. mov r1, r0 ! 5 MT (latency=0)
  700. mov.l @r15+, r11 ! 15 LS
  701. sub r1, r5 ! 75 EX
  702. mov.l @r15+, r10 ! 15 LS
  703. cmp/eq r4, r0 ! 54 MT
  704. bf/s 1f ! 109 BR
  705. mov.l @r15+, r9 ! 15 LS
  706. rts
  707. 1: mov.l @r15+, r8 ! 15 LS
  708. add #0x1e, r5 ! 50 EX
  709. ! Finish off a short word at a time
  710. ! r5 must be invariant - 2
  711. 10: mov r4,r2 ! 5 MT (latency=0)
  712. add #1,r2 ! 50 EX
  713. cmp/hi r2, r0 ! 57 MT
  714. bf/s 1f ! 109 BR
  715. add #2, r2 ! 50 EX
  716. 3: mov.w @(r0,r5),r1 ! 20 LS
  717. cmp/hi r2,r0 ! 57 MT
  718. bt/s 3b ! 109 BR
  719. mov.w r1,@-r0 ! 29 LS
  720. 1:
  721. !
  722. ! Finally, copy the last byte if necessary
  723. cmp/eq r4,r0 ! 54 MT
  724. bt/s 9b
  725. add #1,r5
  726. mov.b @(r0,r5),r1
  727. rts
  728. mov.b r1,@-r0
  729. END(memcpy)
  730. libc_hidden_def (memcpy)