memcpy.S 11 KB

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