memccpy.S 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* Optimized version of the memccpy() 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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. /* Return: a pointer to the next byte after char in dest or NULL
  18. Inputs:
  19. in0: dest
  20. in1: src
  21. in2: char
  22. in3: byte count
  23. This implementation assumes little endian mode (UM.be = 0).
  24. This implementation assumes that it is safe to do read ahead
  25. in the src block, without getting beyond its limit. */
  26. #include "sysdep.h"
  27. #undef ret
  28. #define OP_T_THRES 16
  29. #define OPSIZ 8
  30. #define saved_pr r17
  31. #define saved_lc r18
  32. #define dest r19
  33. #define src r20
  34. #define len r21
  35. #define asrc r22
  36. #define tmp r23
  37. #define char r24
  38. #define charx8 r25
  39. #define saved_ec r26
  40. #define sh2 r28
  41. #define sh1 r29
  42. #define loopcnt r30
  43. #define value r31
  44. #ifdef GAS_ALIGN_BREAKS_UNWIND_INFO
  45. /* Manually force proper loop-alignment. Note: be sure to
  46. double-check the code-layout after making any changes to
  47. this routine! */
  48. # define ALIGN(n) { nop 0 }
  49. #else
  50. # define ALIGN(n) .align n
  51. #endif
  52. ENTRY(memccpy)
  53. .prologue
  54. alloc r2 = ar.pfs, 4, 40 - 4, 0, 40
  55. #include "softpipe.h"
  56. .rotr r[MEMLAT + 7], tmp1[4], tmp2[4], val[4], tmp3[2], pos0[2]
  57. .rotp p[MEMLAT + 6 + 1]
  58. mov ret0 = r0 /* return NULL if no match */
  59. .save pr, saved_pr
  60. mov saved_pr = pr /* save the predicate registers */
  61. mov dest = in0 /* dest */
  62. .save ar.lc, saved_lc
  63. mov saved_lc = ar.lc /* save the loop counter */
  64. mov saved_ec = ar.ec /* save the loop counter */
  65. .body
  66. mov src = in1 /* src */
  67. extr.u char = in2, 0, 8 /* char */
  68. mov len = in3 /* len */
  69. sub tmp = r0, in0 /* tmp = -dest */
  70. cmp.ne p7, p0 = r0, r0 /* clear p7 */
  71. ;;
  72. and loopcnt = 7, tmp /* loopcnt = -dest % 8 */
  73. cmp.ge p6, p0 = OP_T_THRES, len /* is len <= OP_T_THRES */
  74. mov ar.ec = 0 /* ec not guaranteed zero on entry */
  75. (p6) br.cond.spnt .cpyfew /* copy byte by byte */
  76. ;;
  77. cmp.eq p6, p0 = loopcnt, r0
  78. mux1 charx8 = char, @brcst
  79. (p6) br.cond.sptk .dest_aligned
  80. sub len = len, loopcnt /* len -= -dest % 8 */
  81. adds loopcnt = -1, loopcnt /* --loopcnt */
  82. ;;
  83. mov ar.lc = loopcnt
  84. .l1: /* copy -dest % 8 bytes */
  85. ld1 value = [src], 1 /* value = *src++ */
  86. ;;
  87. st1 [dest] = value, 1 /* *dest++ = value */
  88. cmp.eq p6, p0 = value, char
  89. (p6) br.cond.spnt .foundit
  90. br.cloop.dptk .l1
  91. .dest_aligned:
  92. and sh1 = 7, src /* sh1 = src % 8 */
  93. and tmp = -8, len /* tmp = len & -OPSIZ */
  94. and asrc = -8, src /* asrc = src & -OPSIZ -- align src */
  95. shr.u loopcnt = len, 3 /* loopcnt = len / 8 */
  96. and len = 7, len ;; /* len = len % 8 */
  97. shl sh1 = sh1, 3 /* sh1 = 8 * (src % 8) */
  98. adds loopcnt = -1, loopcnt /* --loopcnt */
  99. mov pr.rot = 1 << 16 ;; /* set rotating predicates */
  100. sub sh2 = 64, sh1 /* sh2 = 64 - sh1 */
  101. mov ar.lc = loopcnt /* set LC */
  102. cmp.eq p6, p0 = sh1, r0 /* is the src aligned? */
  103. (p6) br.cond.sptk .src_aligned ;;
  104. add src = src, tmp /* src += len & -OPSIZ */
  105. mov ar.ec = MEMLAT + 6 + 1 /* six more passes needed */
  106. ld8 r[1] = [asrc], 8 /* r[1] = w0 */
  107. cmp.ne p6, p0 = r0, r0 ;; /* clear p6 */
  108. ALIGN(32)
  109. .l2:
  110. (p[0]) ld8.s r[0] = [asrc], 8 /* r[0] = w1 */
  111. (p[MEMLAT]) shr.u tmp1[0] = r[1 + MEMLAT], sh1 /* tmp1 = w0 >> sh1 */
  112. (p[MEMLAT]) shl tmp2[0] = r[0 + MEMLAT], sh2 /* tmp2 = w1 << sh2 */
  113. (p[MEMLAT+4]) xor tmp3[0] = val[1], charx8
  114. (p[MEMLAT+5]) czx1.r pos0[0] = tmp3[1]
  115. (p[MEMLAT+6]) chk.s r[6 + MEMLAT], .recovery1 /* our data isn't */
  116. /* valid - rollback! */
  117. (p[MEMLAT+6]) cmp.ne p6, p0 = 8, pos0[1]
  118. (p6) br.cond.spnt .gotit
  119. (p[MEMLAT+6]) st8 [dest] = val[3], 8 /* store val to dest */
  120. (p[MEMLAT+3]) or val[0] = tmp1[3], tmp2[3] /* val = tmp1 | tmp2 */
  121. br.ctop.sptk .l2
  122. br.cond.sptk .cpyfew
  123. .src_aligned:
  124. cmp.ne p6, p0 = r0, r0 /* clear p6 */
  125. mov ar.ec = MEMLAT + 2 + 1 ;; /* set EC */
  126. .l3:
  127. (p[0]) ld8.s r[0] = [src], 8
  128. (p[MEMLAT]) xor tmp3[0] = r[MEMLAT], charx8
  129. (p[MEMLAT+1]) czx1.r pos0[0] = tmp3[1]
  130. (p[MEMLAT+2]) cmp.ne p7, p0 = 8, pos0[1]
  131. (p[MEMLAT+2]) chk.s r[MEMLAT+2], .recovery2
  132. (p7) br.cond.spnt .gotit
  133. .back2:
  134. (p[MEMLAT+2]) st8 [dest] = r[MEMLAT+2], 8
  135. br.ctop.dptk .l3
  136. .cpyfew:
  137. cmp.eq p6, p0 = len, r0 /* is len == 0 ? */
  138. adds len = -1, len /* --len; */
  139. (p6) br.cond.spnt .restore_and_exit ;;
  140. mov ar.lc = len
  141. .l4:
  142. ld1 value = [src], 1
  143. ;;
  144. st1 [dest] = value, 1
  145. cmp.eq p6, p0 = value, char
  146. (p6) br.cond.spnt .foundit
  147. br.cloop.dptk .l4 ;;
  148. .foundit:
  149. (p6) mov ret0 = dest
  150. .restore_and_exit:
  151. mov pr = saved_pr, -1 /* restore the predicate registers */
  152. mov ar.lc = saved_lc /* restore the loop counter */
  153. mov ar.ec = saved_ec ;; /* restore the epilog counter */
  154. br.ret.sptk.many b0
  155. .gotit:
  156. .pred.rel "mutex" p6, p7
  157. (p6) mov value = val[3] /* if coming from l2 */
  158. (p7) mov value = r[MEMLAT+2] /* if coming from l3 */
  159. mov ar.lc = pos0[1] ;;
  160. .l5:
  161. extr.u tmp = value, 0, 8 ;;
  162. st1 [dest] = tmp, 1
  163. shr.u value = value, 8
  164. br.cloop.sptk .l5 ;;
  165. mov ret0 = dest
  166. mov pr = saved_pr, -1
  167. mov ar.lc = saved_lc
  168. br.ret.sptk.many b0
  169. .recovery1:
  170. adds src = -(MEMLAT + 6 + 1) * 8, asrc
  171. mov loopcnt = ar.lc
  172. mov tmp = ar.ec ;;
  173. sub sh1 = (MEMLAT + 6 + 1), tmp
  174. shr.u sh2 = sh2, 3
  175. ;;
  176. shl loopcnt = loopcnt, 3
  177. sub src = src, sh2
  178. shl sh1 = sh1, 3
  179. shl tmp = tmp, 3
  180. ;;
  181. add len = len, loopcnt
  182. add src = sh1, src ;;
  183. add len = tmp, len
  184. .back1:
  185. br.cond.sptk .cpyfew
  186. .recovery2:
  187. add tmp = -(MEMLAT + 3) * 8, src
  188. (p7) br.cond.spnt .gotit
  189. ;;
  190. ld8 r[MEMLAT+2] = [tmp] ;;
  191. xor pos0[1] = r[MEMLAT+2], charx8 ;;
  192. czx1.r pos0[1] = pos0[1] ;;
  193. cmp.ne p7, p6 = 8, pos0[1]
  194. (p7) br.cond.spnt .gotit
  195. br.cond.sptk .back2
  196. END(memccpy)