memcpy.S 11 KB

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