memcpy.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /* memcpy.S: Sparc optimized memcpy and memmove code
  2. * Hand optimized from GNU libc's memcpy and memmove
  3. * Copyright (C) 1991,1996 Free Software Foundation
  4. * Copyright (C) 1995 Linus Torvalds (Linus.Torvalds@helsinki.fi)
  5. * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
  6. * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
  7. * Copyright (C) 1996 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  8. */
  9. #include <asm/export.h>
  10. #define FUNC(x) \
  11. .globl x; \
  12. .type x,@function; \
  13. .align 4; \
  14. x:
  15. /* Both these macros have to start with exactly the same insn */
  16. #define MOVE_BIGCHUNK(src, dst, offset, t0, t1, t2, t3, t4, t5, t6, t7) \
  17. ldd [%src + (offset) + 0x00], %t0; \
  18. ldd [%src + (offset) + 0x08], %t2; \
  19. ldd [%src + (offset) + 0x10], %t4; \
  20. ldd [%src + (offset) + 0x18], %t6; \
  21. st %t0, [%dst + (offset) + 0x00]; \
  22. st %t1, [%dst + (offset) + 0x04]; \
  23. st %t2, [%dst + (offset) + 0x08]; \
  24. st %t3, [%dst + (offset) + 0x0c]; \
  25. st %t4, [%dst + (offset) + 0x10]; \
  26. st %t5, [%dst + (offset) + 0x14]; \
  27. st %t6, [%dst + (offset) + 0x18]; \
  28. st %t7, [%dst + (offset) + 0x1c];
  29. #define MOVE_BIGALIGNCHUNK(src, dst, offset, t0, t1, t2, t3, t4, t5, t6, t7) \
  30. ldd [%src + (offset) + 0x00], %t0; \
  31. ldd [%src + (offset) + 0x08], %t2; \
  32. ldd [%src + (offset) + 0x10], %t4; \
  33. ldd [%src + (offset) + 0x18], %t6; \
  34. std %t0, [%dst + (offset) + 0x00]; \
  35. std %t2, [%dst + (offset) + 0x08]; \
  36. std %t4, [%dst + (offset) + 0x10]; \
  37. std %t6, [%dst + (offset) + 0x18];
  38. #define MOVE_LASTCHUNK(src, dst, offset, t0, t1, t2, t3) \
  39. ldd [%src - (offset) - 0x10], %t0; \
  40. ldd [%src - (offset) - 0x08], %t2; \
  41. st %t0, [%dst - (offset) - 0x10]; \
  42. st %t1, [%dst - (offset) - 0x0c]; \
  43. st %t2, [%dst - (offset) - 0x08]; \
  44. st %t3, [%dst - (offset) - 0x04];
  45. #define MOVE_LASTALIGNCHUNK(src, dst, offset, t0, t1, t2, t3) \
  46. ldd [%src - (offset) - 0x10], %t0; \
  47. ldd [%src - (offset) - 0x08], %t2; \
  48. std %t0, [%dst - (offset) - 0x10]; \
  49. std %t2, [%dst - (offset) - 0x08];
  50. #define MOVE_SHORTCHUNK(src, dst, offset, t0, t1) \
  51. ldub [%src - (offset) - 0x02], %t0; \
  52. ldub [%src - (offset) - 0x01], %t1; \
  53. stb %t0, [%dst - (offset) - 0x02]; \
  54. stb %t1, [%dst - (offset) - 0x01];
  55. /* Both these macros have to start with exactly the same insn */
  56. #define RMOVE_BIGCHUNK(src, dst, offset, t0, t1, t2, t3, t4, t5, t6, t7) \
  57. ldd [%src - (offset) - 0x20], %t0; \
  58. ldd [%src - (offset) - 0x18], %t2; \
  59. ldd [%src - (offset) - 0x10], %t4; \
  60. ldd [%src - (offset) - 0x08], %t6; \
  61. st %t0, [%dst - (offset) - 0x20]; \
  62. st %t1, [%dst - (offset) - 0x1c]; \
  63. st %t2, [%dst - (offset) - 0x18]; \
  64. st %t3, [%dst - (offset) - 0x14]; \
  65. st %t4, [%dst - (offset) - 0x10]; \
  66. st %t5, [%dst - (offset) - 0x0c]; \
  67. st %t6, [%dst - (offset) - 0x08]; \
  68. st %t7, [%dst - (offset) - 0x04];
  69. #define RMOVE_BIGALIGNCHUNK(src, dst, offset, t0, t1, t2, t3, t4, t5, t6, t7) \
  70. ldd [%src - (offset) - 0x20], %t0; \
  71. ldd [%src - (offset) - 0x18], %t2; \
  72. ldd [%src - (offset) - 0x10], %t4; \
  73. ldd [%src - (offset) - 0x08], %t6; \
  74. std %t0, [%dst - (offset) - 0x20]; \
  75. std %t2, [%dst - (offset) - 0x18]; \
  76. std %t4, [%dst - (offset) - 0x10]; \
  77. std %t6, [%dst - (offset) - 0x08];
  78. #define RMOVE_LASTCHUNK(src, dst, offset, t0, t1, t2, t3) \
  79. ldd [%src + (offset) + 0x00], %t0; \
  80. ldd [%src + (offset) + 0x08], %t2; \
  81. st %t0, [%dst + (offset) + 0x00]; \
  82. st %t1, [%dst + (offset) + 0x04]; \
  83. st %t2, [%dst + (offset) + 0x08]; \
  84. st %t3, [%dst + (offset) + 0x0c];
  85. #define RMOVE_SHORTCHUNK(src, dst, offset, t0, t1) \
  86. ldub [%src + (offset) + 0x00], %t0; \
  87. ldub [%src + (offset) + 0x01], %t1; \
  88. stb %t0, [%dst + (offset) + 0x00]; \
  89. stb %t1, [%dst + (offset) + 0x01];
  90. #define SMOVE_CHUNK(src, dst, offset, t0, t1, t2, t3, t4, t5, t6, prev, shil, shir, offset2) \
  91. ldd [%src + (offset) + 0x00], %t0; \
  92. ldd [%src + (offset) + 0x08], %t2; \
  93. srl %t0, shir, %t5; \
  94. srl %t1, shir, %t6; \
  95. sll %t0, shil, %t0; \
  96. or %t5, %prev, %t5; \
  97. sll %t1, shil, %prev; \
  98. or %t6, %t0, %t0; \
  99. srl %t2, shir, %t1; \
  100. srl %t3, shir, %t6; \
  101. sll %t2, shil, %t2; \
  102. or %t1, %prev, %t1; \
  103. std %t4, [%dst + (offset) + (offset2) - 0x04]; \
  104. std %t0, [%dst + (offset) + (offset2) + 0x04]; \
  105. sll %t3, shil, %prev; \
  106. or %t6, %t2, %t4;
  107. #define SMOVE_ALIGNCHUNK(src, dst, offset, t0, t1, t2, t3, t4, t5, t6, prev, shil, shir, offset2) \
  108. ldd [%src + (offset) + 0x00], %t0; \
  109. ldd [%src + (offset) + 0x08], %t2; \
  110. srl %t0, shir, %t4; \
  111. srl %t1, shir, %t5; \
  112. sll %t0, shil, %t6; \
  113. or %t4, %prev, %t0; \
  114. sll %t1, shil, %prev; \
  115. or %t5, %t6, %t1; \
  116. srl %t2, shir, %t4; \
  117. srl %t3, shir, %t5; \
  118. sll %t2, shil, %t6; \
  119. or %t4, %prev, %t2; \
  120. sll %t3, shil, %prev; \
  121. or %t5, %t6, %t3; \
  122. std %t0, [%dst + (offset) + (offset2) + 0x00]; \
  123. std %t2, [%dst + (offset) + (offset2) + 0x08];
  124. .text
  125. .align 4
  126. 0:
  127. retl
  128. nop ! Only bcopy returns here and it retuns void...
  129. #ifdef __KERNEL__
  130. FUNC(amemmove)
  131. FUNC(__memmove)
  132. EXPORT_SYMBOL(__memmove)
  133. #endif
  134. FUNC(memmove)
  135. EXPORT_SYMBOL(memmove)
  136. cmp %o0, %o1
  137. mov %o0, %g7
  138. bleu 9f
  139. sub %o0, %o1, %o4
  140. add %o1, %o2, %o3
  141. cmp %o3, %o0
  142. bleu 0f
  143. andcc %o4, 3, %o5
  144. add %o1, %o2, %o1
  145. add %o0, %o2, %o0
  146. sub %o1, 1, %o1
  147. sub %o0, 1, %o0
  148. 1: /* reverse_bytes */
  149. ldub [%o1], %o4
  150. subcc %o2, 1, %o2
  151. stb %o4, [%o0]
  152. sub %o1, 1, %o1
  153. bne 1b
  154. sub %o0, 1, %o0
  155. retl
  156. mov %g7, %o0
  157. /* NOTE: This code is executed just for the cases,
  158. where %src (=%o1) & 3 is != 0.
  159. We need to align it to 4. So, for (%src & 3)
  160. 1 we need to do ldub,lduh
  161. 2 lduh
  162. 3 just ldub
  163. so even if it looks weird, the branches
  164. are correct here. -jj
  165. */
  166. 78: /* dword_align */
  167. andcc %o1, 1, %g0
  168. be 4f
  169. andcc %o1, 2, %g0
  170. ldub [%o1], %g2
  171. add %o1, 1, %o1
  172. stb %g2, [%o0]
  173. sub %o2, 1, %o2
  174. bne 3f
  175. add %o0, 1, %o0
  176. 4:
  177. lduh [%o1], %g2
  178. add %o1, 2, %o1
  179. sth %g2, [%o0]
  180. sub %o2, 2, %o2
  181. b 3f
  182. add %o0, 2, %o0
  183. FUNC(memcpy) /* %o0=dst %o1=src %o2=len */
  184. EXPORT_SYMBOL(memcpy)
  185. sub %o0, %o1, %o4
  186. mov %o0, %g7
  187. 9:
  188. andcc %o4, 3, %o5
  189. 0:
  190. bne 86f
  191. cmp %o2, 15
  192. bleu 90f
  193. andcc %o1, 3, %g0
  194. bne 78b
  195. 3:
  196. andcc %o1, 4, %g0
  197. be 2f
  198. mov %o2, %g1
  199. ld [%o1], %o4
  200. sub %g1, 4, %g1
  201. st %o4, [%o0]
  202. add %o1, 4, %o1
  203. add %o0, 4, %o0
  204. 2:
  205. andcc %g1, 0xffffff80, %g0
  206. be 3f
  207. andcc %o0, 4, %g0
  208. be 82f + 4
  209. 5:
  210. MOVE_BIGCHUNK(o1, o0, 0x00, o2, o3, o4, o5, g2, g3, g4, g5)
  211. MOVE_BIGCHUNK(o1, o0, 0x20, o2, o3, o4, o5, g2, g3, g4, g5)
  212. MOVE_BIGCHUNK(o1, o0, 0x40, o2, o3, o4, o5, g2, g3, g4, g5)
  213. MOVE_BIGCHUNK(o1, o0, 0x60, o2, o3, o4, o5, g2, g3, g4, g5)
  214. sub %g1, 128, %g1
  215. add %o1, 128, %o1
  216. cmp %g1, 128
  217. bge 5b
  218. add %o0, 128, %o0
  219. 3:
  220. andcc %g1, 0x70, %g4
  221. be 80f
  222. andcc %g1, 8, %g0
  223. sethi %hi(80f), %o5
  224. srl %g4, 1, %o4
  225. add %g4, %o4, %o4
  226. add %o1, %g4, %o1
  227. sub %o5, %o4, %o5
  228. jmpl %o5 + %lo(80f), %g0
  229. add %o0, %g4, %o0
  230. 79: /* memcpy_table */
  231. MOVE_LASTCHUNK(o1, o0, 0x60, g2, g3, g4, g5)
  232. MOVE_LASTCHUNK(o1, o0, 0x50, g2, g3, g4, g5)
  233. MOVE_LASTCHUNK(o1, o0, 0x40, g2, g3, g4, g5)
  234. MOVE_LASTCHUNK(o1, o0, 0x30, g2, g3, g4, g5)
  235. MOVE_LASTCHUNK(o1, o0, 0x20, g2, g3, g4, g5)
  236. MOVE_LASTCHUNK(o1, o0, 0x10, g2, g3, g4, g5)
  237. MOVE_LASTCHUNK(o1, o0, 0x00, g2, g3, g4, g5)
  238. 80: /* memcpy_table_end */
  239. be 81f
  240. andcc %g1, 4, %g0
  241. ldd [%o1], %g2
  242. add %o0, 8, %o0
  243. st %g2, [%o0 - 0x08]
  244. add %o1, 8, %o1
  245. st %g3, [%o0 - 0x04]
  246. 81: /* memcpy_last7 */
  247. be 1f
  248. andcc %g1, 2, %g0
  249. ld [%o1], %g2
  250. add %o1, 4, %o1
  251. st %g2, [%o0]
  252. add %o0, 4, %o0
  253. 1:
  254. be 1f
  255. andcc %g1, 1, %g0
  256. lduh [%o1], %g2
  257. add %o1, 2, %o1
  258. sth %g2, [%o0]
  259. add %o0, 2, %o0
  260. 1:
  261. be 1f
  262. nop
  263. ldub [%o1], %g2
  264. stb %g2, [%o0]
  265. 1:
  266. retl
  267. mov %g7, %o0
  268. 82: /* ldd_std */
  269. MOVE_BIGALIGNCHUNK(o1, o0, 0x00, o2, o3, o4, o5, g2, g3, g4, g5)
  270. MOVE_BIGALIGNCHUNK(o1, o0, 0x20, o2, o3, o4, o5, g2, g3, g4, g5)
  271. MOVE_BIGALIGNCHUNK(o1, o0, 0x40, o2, o3, o4, o5, g2, g3, g4, g5)
  272. MOVE_BIGALIGNCHUNK(o1, o0, 0x60, o2, o3, o4, o5, g2, g3, g4, g5)
  273. subcc %g1, 128, %g1
  274. add %o1, 128, %o1
  275. cmp %g1, 128
  276. bge 82b
  277. add %o0, 128, %o0
  278. andcc %g1, 0x70, %g4
  279. be 84f
  280. andcc %g1, 8, %g0
  281. sethi %hi(84f), %o5
  282. add %o1, %g4, %o1
  283. sub %o5, %g4, %o5
  284. jmpl %o5 + %lo(84f), %g0
  285. add %o0, %g4, %o0
  286. 83: /* amemcpy_table */
  287. MOVE_LASTALIGNCHUNK(o1, o0, 0x60, g2, g3, g4, g5)
  288. MOVE_LASTALIGNCHUNK(o1, o0, 0x50, g2, g3, g4, g5)
  289. MOVE_LASTALIGNCHUNK(o1, o0, 0x40, g2, g3, g4, g5)
  290. MOVE_LASTALIGNCHUNK(o1, o0, 0x30, g2, g3, g4, g5)
  291. MOVE_LASTALIGNCHUNK(o1, o0, 0x20, g2, g3, g4, g5)
  292. MOVE_LASTALIGNCHUNK(o1, o0, 0x10, g2, g3, g4, g5)
  293. MOVE_LASTALIGNCHUNK(o1, o0, 0x00, g2, g3, g4, g5)
  294. 84: /* amemcpy_table_end */
  295. be 85f
  296. andcc %g1, 4, %g0
  297. ldd [%o1], %g2
  298. add %o0, 8, %o0
  299. std %g2, [%o0 - 0x08]
  300. add %o1, 8, %o1
  301. 85: /* amemcpy_last7 */
  302. be 1f
  303. andcc %g1, 2, %g0
  304. ld [%o1], %g2
  305. add %o1, 4, %o1
  306. st %g2, [%o0]
  307. add %o0, 4, %o0
  308. 1:
  309. be 1f
  310. andcc %g1, 1, %g0
  311. lduh [%o1], %g2
  312. add %o1, 2, %o1
  313. sth %g2, [%o0]
  314. add %o0, 2, %o0
  315. 1:
  316. be 1f
  317. nop
  318. ldub [%o1], %g2
  319. stb %g2, [%o0]
  320. 1:
  321. retl
  322. mov %g7, %o0
  323. 86: /* non_aligned */
  324. cmp %o2, 6
  325. bleu 88f
  326. nop
  327. save %sp, -96, %sp
  328. andcc %i0, 3, %g0
  329. be 61f
  330. andcc %i0, 1, %g0
  331. be 60f
  332. andcc %i0, 2, %g0
  333. ldub [%i1], %g5
  334. add %i1, 1, %i1
  335. stb %g5, [%i0]
  336. sub %i2, 1, %i2
  337. bne 61f
  338. add %i0, 1, %i0
  339. 60:
  340. ldub [%i1], %g3
  341. add %i1, 2, %i1
  342. stb %g3, [%i0]
  343. sub %i2, 2, %i2
  344. ldub [%i1 - 1], %g3
  345. add %i0, 2, %i0
  346. stb %g3, [%i0 - 1]
  347. 61:
  348. and %i1, 3, %g2
  349. and %i2, 0xc, %g3
  350. and %i1, -4, %i1
  351. cmp %g3, 4
  352. sll %g2, 3, %g4
  353. mov 32, %g2
  354. be 4f
  355. sub %g2, %g4, %l0
  356. blu 3f
  357. cmp %g3, 0x8
  358. be 2f
  359. srl %i2, 2, %g3
  360. ld [%i1], %i3
  361. add %i0, -8, %i0
  362. ld [%i1 + 4], %i4
  363. b 8f
  364. add %g3, 1, %g3
  365. 2:
  366. ld [%i1], %i4
  367. add %i0, -12, %i0
  368. ld [%i1 + 4], %i5
  369. add %g3, 2, %g3
  370. b 9f
  371. add %i1, -4, %i1
  372. 3:
  373. ld [%i1], %g1
  374. add %i0, -4, %i0
  375. ld [%i1 + 4], %i3
  376. srl %i2, 2, %g3
  377. b 7f
  378. add %i1, 4, %i1
  379. 4:
  380. ld [%i1], %i5
  381. cmp %i2, 7
  382. ld [%i1 + 4], %g1
  383. srl %i2, 2, %g3
  384. bleu 10f
  385. add %i1, 8, %i1
  386. ld [%i1], %i3
  387. add %g3, -1, %g3
  388. 5:
  389. sll %i5, %g4, %g2
  390. srl %g1, %l0, %g5
  391. or %g2, %g5, %g2
  392. st %g2, [%i0]
  393. 7:
  394. ld [%i1 + 4], %i4
  395. sll %g1, %g4, %g2
  396. srl %i3, %l0, %g5
  397. or %g2, %g5, %g2
  398. st %g2, [%i0 + 4]
  399. 8:
  400. ld [%i1 + 8], %i5
  401. sll %i3, %g4, %g2
  402. srl %i4, %l0, %g5
  403. or %g2, %g5, %g2
  404. st %g2, [%i0 + 8]
  405. 9:
  406. ld [%i1 + 12], %g1
  407. sll %i4, %g4, %g2
  408. srl %i5, %l0, %g5
  409. addcc %g3, -4, %g3
  410. or %g2, %g5, %g2
  411. add %i1, 16, %i1
  412. st %g2, [%i0 + 12]
  413. add %i0, 16, %i0
  414. bne,a 5b
  415. ld [%i1], %i3
  416. 10:
  417. sll %i5, %g4, %g2
  418. srl %g1, %l0, %g5
  419. srl %l0, 3, %g3
  420. or %g2, %g5, %g2
  421. sub %i1, %g3, %i1
  422. andcc %i2, 2, %g0
  423. st %g2, [%i0]
  424. be 1f
  425. andcc %i2, 1, %g0
  426. ldub [%i1], %g2
  427. add %i1, 2, %i1
  428. stb %g2, [%i0 + 4]
  429. add %i0, 2, %i0
  430. ldub [%i1 - 1], %g2
  431. stb %g2, [%i0 + 3]
  432. 1:
  433. be 1f
  434. nop
  435. ldub [%i1], %g2
  436. stb %g2, [%i0 + 4]
  437. 1:
  438. ret
  439. restore %g7, %g0, %o0
  440. 88: /* short_end */
  441. and %o2, 0xe, %o3
  442. 20:
  443. sethi %hi(89f), %o5
  444. sll %o3, 3, %o4
  445. add %o0, %o3, %o0
  446. sub %o5, %o4, %o5
  447. add %o1, %o3, %o1
  448. jmpl %o5 + %lo(89f), %g0
  449. andcc %o2, 1, %g0
  450. MOVE_SHORTCHUNK(o1, o0, 0x0c, g2, g3)
  451. MOVE_SHORTCHUNK(o1, o0, 0x0a, g2, g3)
  452. MOVE_SHORTCHUNK(o1, o0, 0x08, g2, g3)
  453. MOVE_SHORTCHUNK(o1, o0, 0x06, g2, g3)
  454. MOVE_SHORTCHUNK(o1, o0, 0x04, g2, g3)
  455. MOVE_SHORTCHUNK(o1, o0, 0x02, g2, g3)
  456. MOVE_SHORTCHUNK(o1, o0, 0x00, g2, g3)
  457. 89: /* short_table_end */
  458. be 1f
  459. nop
  460. ldub [%o1], %g2
  461. stb %g2, [%o0]
  462. 1:
  463. retl
  464. mov %g7, %o0
  465. 90: /* short_aligned_end */
  466. bne 88b
  467. andcc %o2, 8, %g0
  468. be 1f
  469. andcc %o2, 4, %g0
  470. ld [%o1 + 0x00], %g2
  471. ld [%o1 + 0x04], %g3
  472. add %o1, 8, %o1
  473. st %g2, [%o0 + 0x00]
  474. st %g3, [%o0 + 0x04]
  475. add %o0, 8, %o0
  476. 1:
  477. b 81b
  478. mov %o2, %g1