strncpy.S 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* Optimized version of the standard strncpy() function.
  2. This file is part of the GNU C Library.
  3. Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
  4. Contributed by Dan Pop <Dan.Pop@cern.ch>
  5. and Jakub Jelinek <jakub@redhat.com>.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, see
  16. <http://www.gnu.org/licenses/>. */
  17. /* Return: dest
  18. Inputs:
  19. in0: dest
  20. in1: src
  21. in2: len
  22. In this form, it assumes little endian mode.
  23. */
  24. #include <sysdep.h>
  25. #undef ret
  26. #define saved_lc r15
  27. #define saved_pr r16
  28. #define thresh r17
  29. #define dest r18
  30. #define dest2 r19
  31. #define src r20
  32. #define len r21
  33. #define asrc r22
  34. #define tmp r23
  35. #define pos r24
  36. #define w0 r25
  37. #define w1 r26
  38. #define c r27
  39. #define sh2 r28
  40. #define sh1 r29
  41. #define loopcnt r30
  42. #define value r31
  43. ENTRY(strncpy)
  44. .prologue
  45. alloc r2 = ar.pfs, 3, 0, 29, 32
  46. #define MEMLAT 2
  47. .rotr r[MEMLAT + 2]
  48. .rotp p[MEMLAT + 1]
  49. mov ret0 = in0 /* return value = dest */
  50. .save pr, saved_pr
  51. mov saved_pr = pr /* save the predicate registers */
  52. .save ar.lc, saved_lc
  53. mov saved_lc = ar.lc /* save the loop counter */
  54. mov ar.ec = 0 /* ec is not guaranteed to */
  55. /* be zero upon function entry */
  56. .body
  57. cmp.geu p6, p5 = 24, in2
  58. (p6) br.cond.spnt .short_len
  59. sub tmp = r0, in0 ;; /* tmp = -dest */
  60. mov len = in2 /* len */
  61. mov dest = in0 /* dest */
  62. mov src = in1 /* src */
  63. and tmp = 7, tmp ;; /* loopcnt = -dest % 8 */
  64. cmp.eq p6, p7 = tmp, r0
  65. adds loopcnt = -1, tmp /* --loopcnt */
  66. (p6) br.cond.sptk .dest_aligned ;;
  67. sub len = len, tmp /* len -= -dest % 8 */
  68. mov ar.lc = loopcnt
  69. .l1: /* copy -dest % 8 bytes */
  70. (p5) ld1 c = [src], 1 /* c = *src++ */
  71. ;;
  72. st1 [dest] = c, 1 /* *dest++ = c */
  73. cmp.ne p5, p7 = c, r0
  74. br.cloop.dptk .l1 ;;
  75. (p7) br.cond.dpnt .found0_align
  76. .dest_aligned: /* p7 should be cleared here */
  77. shr.u c = len, 3 /* c = len / 8 */
  78. and sh1 = 7, src /* sh1 = src % 8 */
  79. and asrc = -8, src ;; /* asrc = src & -OPSIZ -- align src */
  80. adds c = (MEMLAT-1), c /* c = (len / 8) + MEMLAT - 1 */
  81. sub thresh = 8, sh1
  82. mov pr.rot = 1 << 16 /* set rotating predicates */
  83. shl sh1 = sh1, 3 ;; /* sh1 = 8 * (src % 8) */
  84. mov ar.lc = c /* "infinite" loop */
  85. sub sh2 = 64, sh1 /* sh2 = 64 - sh1 */
  86. cmp.eq p6, p0 = sh1, r0 /* is the src aligned? */
  87. (p6) br.cond.sptk .src_aligned
  88. adds c = -(MEMLAT-1), c ;; /* c = (len / 8) */
  89. ld8 r[1] = [asrc],8
  90. mov ar.lc = c ;;
  91. .align 32
  92. .l2:
  93. (p6) st8 [dest] = value, 8 /* store val to dest */
  94. ld8.s r[0] = [asrc], 8
  95. shr.u value = r[1], sh1 ;; /* value = w0 >> sh1 */
  96. czx1.r pos = value ;; /* do we have an "early" zero */
  97. cmp.lt p7, p0 = pos, thresh /* in w0 >> sh1? */
  98. adds len = -8, len /* len -= 8 */
  99. (p7) br.cond.dpnt .nonalign_found0
  100. chk.s r[0], .recovery2 /* it is safe to do that only */
  101. .back2: /* after the previous test */
  102. shl tmp = r[0], sh2 /* tmp = w1 << sh2 */
  103. ;;
  104. or value = value, tmp ;; /* value |= tmp */
  105. czx1.r pos = value ;;
  106. cmp.ne p7, p6 = 8, pos
  107. (p7) br.cond.dpnt .nonalign_found0
  108. br.ctop.dptk .l2 ;;
  109. adds len = 8, len
  110. br.cond.sptk .not_found0 ;;
  111. .nonalign_found0:
  112. cmp.gtu p6, p0 = -8, len
  113. (p6) br.cond.dptk .found0
  114. adds len = 8, len
  115. br.cond.sptk .not_found0 ;;
  116. .align 32
  117. .src_aligned:
  118. .l3:
  119. (p[0]) ld8.s r[0] = [src], 8
  120. (p[MEMLAT]) chk.s r[MEMLAT], .recovery3
  121. .back3:
  122. (p[MEMLAT]) mov value = r[MEMLAT]
  123. (p[MEMLAT]) czx1.r pos = r[MEMLAT] ;;
  124. (p[MEMLAT]) cmp.ne p7, p0 = 8, pos
  125. (p[MEMLAT]) adds len = -8, len /* len -= 8 */
  126. (p7) br.cond.dpnt .found0
  127. (p[MEMLAT]) st8 [dest] = r[MEMLAT], 8
  128. br.ctop.dptk .l3 ;;
  129. chk.s r[MEMLAT-1], .recovery4
  130. .back4:
  131. mov value = r[MEMLAT-1]
  132. .not_found0:
  133. cmp.eq p5, p6 = len, r0
  134. adds len = -1, len
  135. (p5) br.cond.dptk .restore_and_exit ;;
  136. mov ar.lc = len
  137. .l4:
  138. (p6) extr.u c = value, 0, 8 /* c = value & 0xff */
  139. (p6) shr.u value = value, 8 ;;
  140. st1 [dest] = c, 1
  141. cmp.ne p6, p0 = c, r0
  142. br.cloop.dptk .l4
  143. br.cond.sptk .restore_and_exit
  144. .found0_align:
  145. mov pos = 0
  146. adds len = -8, len
  147. mov value = 0 ;;
  148. .found0:
  149. shl tmp = pos, 3
  150. shr.u loopcnt = len, 4 /* loopcnt = len / 16 */
  151. mov c = -1 ;;
  152. cmp.eq p6, p0 = loopcnt, r0
  153. adds loopcnt = -1, loopcnt
  154. shl c = c, tmp ;;
  155. and len = 0xf, len
  156. andcm value = value, c
  157. mov ar.lc = loopcnt ;;
  158. cmp.le p7, p0 = 8, len
  159. adds dest2 = 16, dest
  160. st8 [dest] = value, 8
  161. and len = 0x7, len
  162. (p6) br.cond.dpnt .l6 ;;
  163. .l5:
  164. st8 [dest] = r0, 16
  165. st8 [dest2] = r0, 16
  166. br.cloop.dptk .l5 ;;
  167. .l6:
  168. (p7) st8 [dest] = r0, 8
  169. cmp.eq p5, p0 = len, r0
  170. adds len = -1, len
  171. (p5) br.cond.dptk .restore_and_exit ;;
  172. mov ar.lc = len ;;
  173. .l7:
  174. st1 [dest] = r0, 1
  175. br.cloop.dptk .l7 ;;
  176. .restore_and_exit:
  177. mov ar.lc = saved_lc /* restore the loop counter */
  178. mov pr = saved_pr, -1 /* restore the predicate registers */
  179. br.ret.sptk.many b0
  180. .short_len:
  181. cmp.eq p5, p0 = in2, r0
  182. adds loopcnt = -1, in2
  183. (p5) br.cond.spnt .restore_and_exit ;;
  184. mov ar.lc = loopcnt /* p6 should be set when we get here */
  185. .l8:
  186. (p6) ld1 c = [in1], 1 /* c = *src++ */
  187. ;;
  188. st1 [in0] = c, 1 /* *dest++ = c */
  189. (p6) cmp.ne p6, p0 = c, r0
  190. br.cloop.dptk .l8
  191. ;;
  192. mov ar.lc = saved_lc /* restore the loop counter */
  193. mov pr = saved_pr, -1 /* restore the predicate registers */
  194. br.ret.sptk.many b0
  195. .recovery2:
  196. add c = 8, len
  197. add tmp = -8, asrc ;;
  198. cmp.gtu p8, p5 = c, thresh ;;
  199. (p8) ld8 r[0] = [tmp]
  200. (p5) mov r[0] = r0
  201. br.cond.sptk .back2
  202. .recovery3:
  203. add tmp = -(MEMLAT + 1) * 8, src ;;
  204. ld8 r[MEMLAT] = [tmp]
  205. br.cond.sptk .back3
  206. .recovery4:
  207. cmp.eq p5, p6 = len, r0
  208. add tmp = -MEMLAT * 8, src ;;
  209. (p6) ld8 r[MEMLAT - 1] = [tmp]
  210. (p5) mov r[MEMLAT - 1] = r0
  211. br.cond.sptk .back4
  212. END(strncpy)
  213. libc_hidden_def (strncpy)