memcpy.S 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. .text
  56. .align 4
  57. FUNC(memmove)
  58. EXPORT_SYMBOL(memmove)
  59. cmp %o0, %o1
  60. mov %o0, %g7
  61. bleu 9f
  62. sub %o0, %o1, %o4
  63. add %o1, %o2, %o3
  64. cmp %o3, %o0
  65. bleu 0f
  66. andcc %o4, 3, %o5
  67. add %o1, %o2, %o1
  68. add %o0, %o2, %o0
  69. sub %o1, 1, %o1
  70. sub %o0, 1, %o0
  71. 1: /* reverse_bytes */
  72. ldub [%o1], %o4
  73. subcc %o2, 1, %o2
  74. stb %o4, [%o0]
  75. sub %o1, 1, %o1
  76. bne 1b
  77. sub %o0, 1, %o0
  78. retl
  79. mov %g7, %o0
  80. /* NOTE: This code is executed just for the cases,
  81. where %src (=%o1) & 3 is != 0.
  82. We need to align it to 4. So, for (%src & 3)
  83. 1 we need to do ldub,lduh
  84. 2 lduh
  85. 3 just ldub
  86. so even if it looks weird, the branches
  87. are correct here. -jj
  88. */
  89. 78: /* dword_align */
  90. andcc %o1, 1, %g0
  91. be 4f
  92. andcc %o1, 2, %g0
  93. ldub [%o1], %g2
  94. add %o1, 1, %o1
  95. stb %g2, [%o0]
  96. sub %o2, 1, %o2
  97. bne 3f
  98. add %o0, 1, %o0
  99. 4:
  100. lduh [%o1], %g2
  101. add %o1, 2, %o1
  102. sth %g2, [%o0]
  103. sub %o2, 2, %o2
  104. b 3f
  105. add %o0, 2, %o0
  106. FUNC(memcpy) /* %o0=dst %o1=src %o2=len */
  107. EXPORT_SYMBOL(memcpy)
  108. sub %o0, %o1, %o4
  109. mov %o0, %g7
  110. 9:
  111. andcc %o4, 3, %o5
  112. 0:
  113. bne 86f
  114. cmp %o2, 15
  115. bleu 90f
  116. andcc %o1, 3, %g0
  117. bne 78b
  118. 3:
  119. andcc %o1, 4, %g0
  120. be 2f
  121. mov %o2, %g1
  122. ld [%o1], %o4
  123. sub %g1, 4, %g1
  124. st %o4, [%o0]
  125. add %o1, 4, %o1
  126. add %o0, 4, %o0
  127. 2:
  128. andcc %g1, 0xffffff80, %g0
  129. be 3f
  130. andcc %o0, 4, %g0
  131. be 82f + 4
  132. 5:
  133. MOVE_BIGCHUNK(o1, o0, 0x00, o2, o3, o4, o5, g2, g3, g4, g5)
  134. MOVE_BIGCHUNK(o1, o0, 0x20, o2, o3, o4, o5, g2, g3, g4, g5)
  135. MOVE_BIGCHUNK(o1, o0, 0x40, o2, o3, o4, o5, g2, g3, g4, g5)
  136. MOVE_BIGCHUNK(o1, o0, 0x60, o2, o3, o4, o5, g2, g3, g4, g5)
  137. sub %g1, 128, %g1
  138. add %o1, 128, %o1
  139. cmp %g1, 128
  140. bge 5b
  141. add %o0, 128, %o0
  142. 3:
  143. andcc %g1, 0x70, %g4
  144. be 80f
  145. andcc %g1, 8, %g0
  146. sethi %hi(80f), %o5
  147. srl %g4, 1, %o4
  148. add %g4, %o4, %o4
  149. add %o1, %g4, %o1
  150. sub %o5, %o4, %o5
  151. jmpl %o5 + %lo(80f), %g0
  152. add %o0, %g4, %o0
  153. 79: /* memcpy_table */
  154. MOVE_LASTCHUNK(o1, o0, 0x60, g2, g3, g4, g5)
  155. MOVE_LASTCHUNK(o1, o0, 0x50, g2, g3, g4, g5)
  156. MOVE_LASTCHUNK(o1, o0, 0x40, g2, g3, g4, g5)
  157. MOVE_LASTCHUNK(o1, o0, 0x30, g2, g3, g4, g5)
  158. MOVE_LASTCHUNK(o1, o0, 0x20, g2, g3, g4, g5)
  159. MOVE_LASTCHUNK(o1, o0, 0x10, g2, g3, g4, g5)
  160. MOVE_LASTCHUNK(o1, o0, 0x00, g2, g3, g4, g5)
  161. 80: /* memcpy_table_end */
  162. be 81f
  163. andcc %g1, 4, %g0
  164. ldd [%o1], %g2
  165. add %o0, 8, %o0
  166. st %g2, [%o0 - 0x08]
  167. add %o1, 8, %o1
  168. st %g3, [%o0 - 0x04]
  169. 81: /* memcpy_last7 */
  170. be 1f
  171. andcc %g1, 2, %g0
  172. ld [%o1], %g2
  173. add %o1, 4, %o1
  174. st %g2, [%o0]
  175. add %o0, 4, %o0
  176. 1:
  177. be 1f
  178. andcc %g1, 1, %g0
  179. lduh [%o1], %g2
  180. add %o1, 2, %o1
  181. sth %g2, [%o0]
  182. add %o0, 2, %o0
  183. 1:
  184. be 1f
  185. nop
  186. ldub [%o1], %g2
  187. stb %g2, [%o0]
  188. 1:
  189. retl
  190. mov %g7, %o0
  191. 82: /* ldd_std */
  192. MOVE_BIGALIGNCHUNK(o1, o0, 0x00, o2, o3, o4, o5, g2, g3, g4, g5)
  193. MOVE_BIGALIGNCHUNK(o1, o0, 0x20, o2, o3, o4, o5, g2, g3, g4, g5)
  194. MOVE_BIGALIGNCHUNK(o1, o0, 0x40, o2, o3, o4, o5, g2, g3, g4, g5)
  195. MOVE_BIGALIGNCHUNK(o1, o0, 0x60, o2, o3, o4, o5, g2, g3, g4, g5)
  196. subcc %g1, 128, %g1
  197. add %o1, 128, %o1
  198. cmp %g1, 128
  199. bge 82b
  200. add %o0, 128, %o0
  201. andcc %g1, 0x70, %g4
  202. be 84f
  203. andcc %g1, 8, %g0
  204. sethi %hi(84f), %o5
  205. add %o1, %g4, %o1
  206. sub %o5, %g4, %o5
  207. jmpl %o5 + %lo(84f), %g0
  208. add %o0, %g4, %o0
  209. 83: /* amemcpy_table */
  210. MOVE_LASTALIGNCHUNK(o1, o0, 0x60, g2, g3, g4, g5)
  211. MOVE_LASTALIGNCHUNK(o1, o0, 0x50, g2, g3, g4, g5)
  212. MOVE_LASTALIGNCHUNK(o1, o0, 0x40, g2, g3, g4, g5)
  213. MOVE_LASTALIGNCHUNK(o1, o0, 0x30, g2, g3, g4, g5)
  214. MOVE_LASTALIGNCHUNK(o1, o0, 0x20, g2, g3, g4, g5)
  215. MOVE_LASTALIGNCHUNK(o1, o0, 0x10, g2, g3, g4, g5)
  216. MOVE_LASTALIGNCHUNK(o1, o0, 0x00, g2, g3, g4, g5)
  217. 84: /* amemcpy_table_end */
  218. be 85f
  219. andcc %g1, 4, %g0
  220. ldd [%o1], %g2
  221. add %o0, 8, %o0
  222. std %g2, [%o0 - 0x08]
  223. add %o1, 8, %o1
  224. 85: /* amemcpy_last7 */
  225. be 1f
  226. andcc %g1, 2, %g0
  227. ld [%o1], %g2
  228. add %o1, 4, %o1
  229. st %g2, [%o0]
  230. add %o0, 4, %o0
  231. 1:
  232. be 1f
  233. andcc %g1, 1, %g0
  234. lduh [%o1], %g2
  235. add %o1, 2, %o1
  236. sth %g2, [%o0]
  237. add %o0, 2, %o0
  238. 1:
  239. be 1f
  240. nop
  241. ldub [%o1], %g2
  242. stb %g2, [%o0]
  243. 1:
  244. retl
  245. mov %g7, %o0
  246. 86: /* non_aligned */
  247. cmp %o2, 6
  248. bleu 88f
  249. nop
  250. save %sp, -96, %sp
  251. andcc %i0, 3, %g0
  252. be 61f
  253. andcc %i0, 1, %g0
  254. be 60f
  255. andcc %i0, 2, %g0
  256. ldub [%i1], %g5
  257. add %i1, 1, %i1
  258. stb %g5, [%i0]
  259. sub %i2, 1, %i2
  260. bne 61f
  261. add %i0, 1, %i0
  262. 60:
  263. ldub [%i1], %g3
  264. add %i1, 2, %i1
  265. stb %g3, [%i0]
  266. sub %i2, 2, %i2
  267. ldub [%i1 - 1], %g3
  268. add %i0, 2, %i0
  269. stb %g3, [%i0 - 1]
  270. 61:
  271. and %i1, 3, %g2
  272. and %i2, 0xc, %g3
  273. and %i1, -4, %i1
  274. cmp %g3, 4
  275. sll %g2, 3, %g4
  276. mov 32, %g2
  277. be 4f
  278. sub %g2, %g4, %l0
  279. blu 3f
  280. cmp %g3, 0x8
  281. be 2f
  282. srl %i2, 2, %g3
  283. ld [%i1], %i3
  284. add %i0, -8, %i0
  285. ld [%i1 + 4], %i4
  286. b 8f
  287. add %g3, 1, %g3
  288. 2:
  289. ld [%i1], %i4
  290. add %i0, -12, %i0
  291. ld [%i1 + 4], %i5
  292. add %g3, 2, %g3
  293. b 9f
  294. add %i1, -4, %i1
  295. 3:
  296. ld [%i1], %g1
  297. add %i0, -4, %i0
  298. ld [%i1 + 4], %i3
  299. srl %i2, 2, %g3
  300. b 7f
  301. add %i1, 4, %i1
  302. 4:
  303. ld [%i1], %i5
  304. cmp %i2, 7
  305. ld [%i1 + 4], %g1
  306. srl %i2, 2, %g3
  307. bleu 10f
  308. add %i1, 8, %i1
  309. ld [%i1], %i3
  310. add %g3, -1, %g3
  311. 5:
  312. sll %i5, %g4, %g2
  313. srl %g1, %l0, %g5
  314. or %g2, %g5, %g2
  315. st %g2, [%i0]
  316. 7:
  317. ld [%i1 + 4], %i4
  318. sll %g1, %g4, %g2
  319. srl %i3, %l0, %g5
  320. or %g2, %g5, %g2
  321. st %g2, [%i0 + 4]
  322. 8:
  323. ld [%i1 + 8], %i5
  324. sll %i3, %g4, %g2
  325. srl %i4, %l0, %g5
  326. or %g2, %g5, %g2
  327. st %g2, [%i0 + 8]
  328. 9:
  329. ld [%i1 + 12], %g1
  330. sll %i4, %g4, %g2
  331. srl %i5, %l0, %g5
  332. addcc %g3, -4, %g3
  333. or %g2, %g5, %g2
  334. add %i1, 16, %i1
  335. st %g2, [%i0 + 12]
  336. add %i0, 16, %i0
  337. bne,a 5b
  338. ld [%i1], %i3
  339. 10:
  340. sll %i5, %g4, %g2
  341. srl %g1, %l0, %g5
  342. srl %l0, 3, %g3
  343. or %g2, %g5, %g2
  344. sub %i1, %g3, %i1
  345. andcc %i2, 2, %g0
  346. st %g2, [%i0]
  347. be 1f
  348. andcc %i2, 1, %g0
  349. ldub [%i1], %g2
  350. add %i1, 2, %i1
  351. stb %g2, [%i0 + 4]
  352. add %i0, 2, %i0
  353. ldub [%i1 - 1], %g2
  354. stb %g2, [%i0 + 3]
  355. 1:
  356. be 1f
  357. nop
  358. ldub [%i1], %g2
  359. stb %g2, [%i0 + 4]
  360. 1:
  361. ret
  362. restore %g7, %g0, %o0
  363. 88: /* short_end */
  364. and %o2, 0xe, %o3
  365. 20:
  366. sethi %hi(89f), %o5
  367. sll %o3, 3, %o4
  368. add %o0, %o3, %o0
  369. sub %o5, %o4, %o5
  370. add %o1, %o3, %o1
  371. jmpl %o5 + %lo(89f), %g0
  372. andcc %o2, 1, %g0
  373. MOVE_SHORTCHUNK(o1, o0, 0x0c, g2, g3)
  374. MOVE_SHORTCHUNK(o1, o0, 0x0a, g2, g3)
  375. MOVE_SHORTCHUNK(o1, o0, 0x08, g2, g3)
  376. MOVE_SHORTCHUNK(o1, o0, 0x06, g2, g3)
  377. MOVE_SHORTCHUNK(o1, o0, 0x04, g2, g3)
  378. MOVE_SHORTCHUNK(o1, o0, 0x02, g2, g3)
  379. MOVE_SHORTCHUNK(o1, o0, 0x00, g2, g3)
  380. 89: /* short_table_end */
  381. be 1f
  382. nop
  383. ldub [%o1], %g2
  384. stb %g2, [%o0]
  385. 1:
  386. retl
  387. mov %g7, %o0
  388. 90: /* short_aligned_end */
  389. bne 88b
  390. andcc %o2, 8, %g0
  391. be 1f
  392. andcc %o2, 4, %g0
  393. ld [%o1 + 0x00], %g2
  394. ld [%o1 + 0x04], %g3
  395. add %o1, 8, %o1
  396. st %g2, [%o0 + 0x00]
  397. st %g3, [%o0 + 0x04]
  398. add %o0, 8, %o0
  399. 1:
  400. b 81b
  401. mov %o2, %g1