memmove.S 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* Optimized version of the standard memmove() function.
  2. This file is part of the GNU C Library.
  3. Copyright (C) 2000, 2001, 2003 Free Software Foundation, Inc.
  4. Contributed by Dan Pop <Dan.Pop@cern.ch>.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. /* Return: dest
  17. Inputs:
  18. in0: dest
  19. in1: src
  20. in2: byte count
  21. The core of the function is the memcpy implementation used in memcpy.S.
  22. When bytes have to be copied backwards, only the easy case, when
  23. all arguments are multiples of 8, is optimised.
  24. In this form, it assumes little endian mode. For big endian mode,
  25. sh1 must be computed using an extra instruction: sub sh1 = 64, sh1
  26. or the UM.be bit should be cleared at the beginning and set at the end. */
  27. #include <sysdep.h>
  28. #undef ret
  29. #define OP_T_THRES 16
  30. #define OPSIZ 8
  31. #define adest r15
  32. #define saved_pr r17
  33. #define saved_lc r18
  34. #define dest r19
  35. #define src r20
  36. #define len r21
  37. #define asrc r22
  38. #define tmp2 r23
  39. #define tmp3 r24
  40. #define tmp4 r25
  41. #define ptable r26
  42. #define ploop56 r27
  43. #define loopaddr r28
  44. #define sh1 r29
  45. #define loopcnt r30
  46. #define value r31
  47. #ifdef GAS_ALIGN_BREAKS_UNWIND_INFO
  48. # define ALIGN(n) { nop 0 }
  49. #else
  50. # define ALIGN(n) .align n
  51. #endif
  52. #define LOOP(shift) \
  53. ALIGN(32); \
  54. .loop##shift : \
  55. (p[0]) ld8 r[0] = [asrc], 8 ; /* w1 */ \
  56. (p[MEMLAT+1]) st8 [dest] = value, 8 ; \
  57. (p[MEMLAT]) shrp value = r[MEMLAT], r[MEMLAT+1], shift ; \
  58. nop.b 0 ; \
  59. nop.b 0 ; \
  60. br.ctop.sptk .loop##shift ; \
  61. br.cond.sptk .cpyfew ; /* deal with the remaining bytes */
  62. #define MEMLAT 21
  63. #define Nrot (((2*MEMLAT+3) + 7) & ~7)
  64. ENTRY(memmove)
  65. .prologue
  66. alloc r2 = ar.pfs, 3, Nrot - 3, 0, Nrot
  67. .rotr r[MEMLAT + 2], q[MEMLAT + 1]
  68. .rotp p[MEMLAT + 2]
  69. mov ret0 = in0 /* return value = dest */
  70. .save pr, saved_pr
  71. mov saved_pr = pr /* save the predicate registers */
  72. .save ar.lc, saved_lc
  73. mov saved_lc = ar.lc /* save the loop counter */
  74. .body
  75. or tmp3 = in0, in1 ;; /* tmp3 = dest | src */
  76. or tmp3 = tmp3, in2 /* tmp3 = dest | src | len */
  77. mov dest = in0 /* dest */
  78. mov src = in1 /* src */
  79. mov len = in2 /* len */
  80. sub tmp2 = r0, in0 /* tmp2 = -dest */
  81. cmp.eq p6, p0 = in2, r0 /* if (len == 0) */
  82. (p6) br.cond.spnt .restore_and_exit;;/* return dest; */
  83. and tmp4 = 7, tmp3 /* tmp4 = (dest | src | len) & 7 */
  84. cmp.le p6, p0 = dest, src /* if dest <= src it's always safe */
  85. (p6) br.cond.spnt .forward /* to copy forward */
  86. add tmp3 = src, len;;
  87. cmp.lt p6, p0 = dest, tmp3 /* if dest > src && dest < src + len */
  88. (p6) br.cond.spnt .backward /* we have to copy backward */
  89. .forward:
  90. shr.u loopcnt = len, 4 ;; /* loopcnt = len / 16 */
  91. cmp.ne p6, p0 = tmp4, r0 /* if ((dest | src | len) & 7 != 0) */
  92. (p6) br.cond.sptk .next /* goto next; */
  93. /* The optimal case, when dest, src and len are all multiples of 8 */
  94. and tmp3 = 0xf, len
  95. mov pr.rot = 1 << 16 /* set rotating predicates */
  96. mov ar.ec = MEMLAT + 1 ;; /* set the epilog counter */
  97. cmp.ne p6, p0 = tmp3, r0 /* do we have to copy an extra word? */
  98. adds loopcnt = -1, loopcnt;; /* --loopcnt */
  99. (p6) ld8 value = [src], 8;;
  100. (p6) st8 [dest] = value, 8 /* copy the "odd" word */
  101. mov ar.lc = loopcnt /* set the loop counter */
  102. cmp.eq p6, p0 = 8, len
  103. (p6) br.cond.spnt .restore_and_exit;;/* the one-word special case */
  104. adds adest = 8, dest /* set adest one word ahead of dest */
  105. adds asrc = 8, src ;; /* set asrc one word ahead of src */
  106. nop.b 0 /* get the "golden" alignment for */
  107. nop.b 0 /* the next loop */
  108. .l0:
  109. (p[0]) ld8 r[0] = [src], 16
  110. (p[0]) ld8 q[0] = [asrc], 16
  111. (p[MEMLAT]) st8 [dest] = r[MEMLAT], 16
  112. (p[MEMLAT]) st8 [adest] = q[MEMLAT], 16
  113. br.ctop.dptk .l0 ;;
  114. mov pr = saved_pr, -1 /* restore the predicate registers */
  115. mov ar.lc = saved_lc /* restore the loop counter */
  116. br.ret.sptk.many b0
  117. .next:
  118. cmp.ge p6, p0 = OP_T_THRES, len /* is len <= OP_T_THRES */
  119. and loopcnt = 7, tmp2 /* loopcnt = -dest % 8 */
  120. (p6) br.cond.spnt .cpyfew /* copy byte by byte */
  121. ;;
  122. cmp.eq p6, p0 = loopcnt, r0
  123. (p6) br.cond.sptk .dest_aligned
  124. sub len = len, loopcnt /* len -= -dest % 8 */
  125. adds loopcnt = -1, loopcnt /* --loopcnt */
  126. ;;
  127. mov ar.lc = loopcnt
  128. .l1: /* copy -dest % 8 bytes */
  129. ld1 value = [src], 1 /* value = *src++ */
  130. ;;
  131. st1 [dest] = value, 1 /* *dest++ = value */
  132. br.cloop.dptk .l1
  133. .dest_aligned:
  134. and sh1 = 7, src /* sh1 = src % 8 */
  135. and tmp2 = -8, len /* tmp2 = len & -OPSIZ */
  136. and asrc = -8, src /* asrc = src & -OPSIZ -- align src */
  137. shr.u loopcnt = len, 3 /* loopcnt = len / 8 */
  138. and len = 7, len;; /* len = len % 8 */
  139. adds loopcnt = -1, loopcnt /* --loopcnt */
  140. addl tmp4 = @ltoff(.table), gp
  141. addl tmp3 = @ltoff(.loop56), gp
  142. mov ar.ec = MEMLAT + 1 /* set EC */
  143. mov pr.rot = 1 << 16;; /* set rotating predicates */
  144. mov ar.lc = loopcnt /* set LC */
  145. cmp.eq p6, p0 = sh1, r0 /* is the src aligned? */
  146. (p6) br.cond.sptk .src_aligned
  147. add src = src, tmp2 /* src += len & -OPSIZ */
  148. shl sh1 = sh1, 3 /* sh1 = 8 * (src % 8) */
  149. ld8 ploop56 = [tmp3] /* ploop56 = &loop56 */
  150. ld8 ptable = [tmp4];; /* ptable = &table */
  151. add tmp3 = ptable, sh1;; /* tmp3 = &table + sh1 */
  152. mov ar.ec = MEMLAT + 1 + 1 /* one more pass needed */
  153. ld8 tmp4 = [tmp3];; /* tmp4 = loop offset */
  154. sub loopaddr = ploop56,tmp4 /* loopadd = &loop56 - loop offset */
  155. ld8 r[1] = [asrc], 8;; /* w0 */
  156. mov b6 = loopaddr;;
  157. br b6 /* jump to the appropriate loop */
  158. LOOP(8)
  159. LOOP(16)
  160. LOOP(24)
  161. LOOP(32)
  162. LOOP(40)
  163. LOOP(48)
  164. LOOP(56)
  165. .src_aligned:
  166. .l3:
  167. (p[0]) ld8 r[0] = [src], 8
  168. (p[MEMLAT]) st8 [dest] = r[MEMLAT], 8
  169. br.ctop.dptk .l3
  170. .cpyfew:
  171. cmp.eq p6, p0 = len, r0 /* is len == 0 ? */
  172. adds len = -1, len /* --len; */
  173. (p6) br.cond.spnt .restore_and_exit ;;
  174. mov ar.lc = len
  175. .l4:
  176. ld1 value = [src], 1
  177. ;;
  178. st1 [dest] = value, 1
  179. br.cloop.dptk .l4 ;;
  180. .restore_and_exit:
  181. mov pr = saved_pr, -1 /* restore the predicate registers */
  182. mov ar.lc = saved_lc /* restore the loop counter */
  183. br.ret.sptk.many b0
  184. /* In the case of a backward copy, optimise only the case when everything
  185. is a multiple of 8, otherwise copy byte by byte. The backward copy is
  186. used only when the blocks are overlapping and dest > src.
  187. */
  188. .backward:
  189. shr.u loopcnt = len, 3 /* loopcnt = len / 8 */
  190. add src = src, len /* src points one byte past the end */
  191. add dest = dest, len ;; /* dest points one byte past the end */
  192. mov ar.ec = MEMLAT + 1 /* set the epilog counter */
  193. mov pr.rot = 1 << 16 /* set rotating predicates */
  194. adds loopcnt = -1, loopcnt /* --loopcnt */
  195. cmp.ne p6, p0 = tmp4, r0 /* if ((dest | src | len) & 7 != 0) */
  196. (p6) br.cond.sptk .bytecopy ;; /* copy byte by byte backward */
  197. adds src = -8, src /* src points to the last word */
  198. adds dest = -8, dest /* dest points to the last word */
  199. mov ar.lc = loopcnt;; /* set the loop counter */
  200. .l5:
  201. (p[0]) ld8 r[0] = [src], -8
  202. (p[MEMLAT]) st8 [dest] = r[MEMLAT], -8
  203. br.ctop.dptk .l5
  204. br.cond.sptk .restore_and_exit
  205. .bytecopy:
  206. adds src = -1, src /* src points to the last byte */
  207. adds dest = -1, dest /* dest points to the last byte */
  208. adds loopcnt = -1, len;; /* loopcnt = len - 1 */
  209. mov ar.lc = loopcnt;; /* set the loop counter */
  210. .l6:
  211. (p[0]) ld1 r[0] = [src], -1
  212. (p[MEMLAT]) st1 [dest] = r[MEMLAT], -1
  213. br.ctop.dptk .l6
  214. br.cond.sptk .restore_and_exit
  215. END(memmove)
  216. .rodata
  217. .align 8
  218. .table:
  219. data8 0 /* dummy entry */
  220. data8 .loop56 - .loop8
  221. data8 .loop56 - .loop16
  222. data8 .loop56 - .loop24
  223. data8 .loop56 - .loop32
  224. data8 .loop56 - .loop40
  225. data8 .loop56 - .loop48
  226. data8 .loop56 - .loop56
  227. libc_hidden_def (memmove)