strncpy.S 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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, write to the Free
  16. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. 02111-1307 USA. */
  18. /* Return: dest
  19. Inputs:
  20. in0: dest
  21. in1: src
  22. in2: len
  23. In this form, it assumes little endian mode.
  24. */
  25. #include "sysdep.h"
  26. #undef ret
  27. #define saved_lc r15
  28. #define saved_pr r16
  29. #define thresh r17
  30. #define dest r18
  31. #define dest2 r19
  32. #define src r20
  33. #define len r21
  34. #define asrc r22
  35. #define tmp r23
  36. #define pos r24
  37. #define w0 r25
  38. #define w1 r26
  39. #define c r27
  40. #define sh2 r28
  41. #define sh1 r29
  42. #define loopcnt r30
  43. #define value r31
  44. ENTRY(strncpy)
  45. .prologue
  46. alloc r2 = ar.pfs, 3, 0, 29, 32
  47. #define MEMLAT 2
  48. .rotr r[MEMLAT + 2]
  49. .rotp p[MEMLAT + 1]
  50. mov ret0 = in0 // return value = dest
  51. .save pr, saved_pr
  52. mov saved_pr = pr // save the predicate registers
  53. .save ar.lc, saved_lc
  54. mov saved_lc = ar.lc // save the loop counter
  55. mov ar.ec = 0 // ec is not guaranteed to
  56. // be zero upon function entry
  57. .body
  58. cmp.geu p6, p5 = 24, in2
  59. (p6) br.cond.spnt .short_len
  60. sub tmp = r0, in0 ;; // tmp = -dest
  61. mov len = in2 // len
  62. mov dest = in0 // dest
  63. mov src = in1 // src
  64. and tmp = 7, tmp ;; // loopcnt = -dest % 8
  65. cmp.eq p6, p7 = tmp, r0
  66. adds loopcnt = -1, tmp // --loopcnt
  67. (p6) br.cond.sptk .dest_aligned ;;
  68. sub len = len, tmp // len -= -dest % 8
  69. mov ar.lc = loopcnt
  70. .l1: // copy -dest % 8 bytes
  71. (p5) ld1 c = [src], 1 // c = *src++
  72. ;;
  73. st1 [dest] = c, 1 // *dest++ = c
  74. cmp.ne p5, p7 = c, r0
  75. br.cloop.dptk .l1 ;;
  76. (p7) br.cond.dpnt .found0_align
  77. .dest_aligned: // p7 should be cleared here
  78. shr.u c = len, 3 // c = len / 8
  79. and sh1 = 7, src // sh1 = src % 8
  80. and asrc = -8, src ;; // asrc = src & -OPSIZ -- align src
  81. adds c = (MEMLAT-1), c // c = (len / 8) + MEMLAT - 1
  82. sub thresh = 8, sh1
  83. mov pr.rot = 1 << 16 // set rotating predicates
  84. shl sh1 = sh1, 3 ;; // sh1 = 8 * (src % 8)
  85. mov ar.lc = c // "infinite" loop
  86. sub sh2 = 64, sh1 // sh2 = 64 - sh1
  87. cmp.eq p6, p0 = sh1, r0 // is the src aligned?
  88. (p6) br.cond.sptk .src_aligned
  89. adds c = -(MEMLAT-1), c ;; // c = (len / 8)
  90. ld8 r[1] = [asrc],8
  91. mov ar.lc = c ;;
  92. .align 32
  93. .l2:
  94. (p6) st8 [dest] = value, 8 // store val to dest
  95. ld8.s r[0] = [asrc], 8
  96. shr.u value = r[1], sh1 ;; // value = w0 >> sh1
  97. czx1.r pos = value ;; // do we have an "early" zero
  98. cmp.lt p7, p0 = pos, thresh // in w0 >> sh1?
  99. adds len = -8, len // len -= 8
  100. (p7) br.cond.dpnt .nonalign_found0
  101. chk.s r[0], .recovery2 // it is safe to do that only
  102. .back2: // after the previous test
  103. shl tmp = r[0], sh2 // tmp = w1 << sh2
  104. ;;
  105. or value = value, tmp ;; // value |= tmp
  106. czx1.r pos = value ;;
  107. cmp.ne p7, p6 = 8, pos
  108. (p7) br.cond.dpnt .nonalign_found0
  109. br.ctop.dptk .l2 ;;
  110. adds len = 8, len
  111. br.cond.sptk .not_found0 ;;
  112. .nonalign_found0:
  113. cmp.gtu p6, p0 = -8, len
  114. (p6) br.cond.dptk .found0
  115. adds len = 8, len
  116. br.cond.sptk .not_found0 ;;
  117. .align 32
  118. .src_aligned:
  119. .l3:
  120. (p[0]) ld8.s r[0] = [src], 8
  121. (p[MEMLAT]) chk.s r[MEMLAT], .recovery3
  122. .back3:
  123. (p[MEMLAT]) mov value = r[MEMLAT]
  124. (p[MEMLAT]) czx1.r pos = r[MEMLAT] ;;
  125. (p[MEMLAT]) cmp.ne p7, p0 = 8, pos
  126. (p[MEMLAT]) adds len = -8, len // len -= 8
  127. (p7) br.cond.dpnt .found0
  128. (p[MEMLAT]) st8 [dest] = r[MEMLAT], 8
  129. br.ctop.dptk .l3 ;;
  130. chk.s r[MEMLAT-1], .recovery4
  131. .back4:
  132. mov value = r[MEMLAT-1]
  133. .not_found0:
  134. cmp.eq p5, p6 = len, r0
  135. adds len = -1, len
  136. (p5) br.cond.dptk .restore_and_exit ;;
  137. mov ar.lc = len
  138. .l4:
  139. (p6) extr.u c = value, 0, 8 // c = value & 0xff
  140. (p6) shr.u value = value, 8 ;;
  141. st1 [dest] = c, 1
  142. cmp.ne p6, p0 = c, r0
  143. br.cloop.dptk .l4
  144. br.cond.sptk .restore_and_exit
  145. .found0_align:
  146. mov pos = 0
  147. adds len = -8, len
  148. mov value = 0 ;;
  149. .found0:
  150. shl tmp = pos, 3
  151. shr.u loopcnt = len, 4 // loopcnt = len / 16
  152. mov c = -1 ;;
  153. cmp.eq p6, p0 = loopcnt, r0
  154. adds loopcnt = -1, loopcnt
  155. shl c = c, tmp ;;
  156. and len = 0xf, len
  157. andcm value = value, c
  158. mov ar.lc = loopcnt ;;
  159. cmp.le p7, p0 = 8, len
  160. adds dest2 = 16, dest
  161. st8 [dest] = value, 8
  162. and len = 0x7, len
  163. (p6) br.cond.dpnt .l6 ;;
  164. .l5:
  165. st8 [dest] = r0, 16
  166. st8 [dest2] = r0, 16
  167. br.cloop.dptk .l5 ;;
  168. .l6:
  169. (p7) st8 [dest] = r0, 8
  170. cmp.eq p5, p0 = len, r0
  171. adds len = -1, len
  172. (p5) br.cond.dptk .restore_and_exit ;;
  173. mov ar.lc = len ;;
  174. .l7:
  175. st1 [dest] = r0, 1
  176. br.cloop.dptk .l7 ;;
  177. .restore_and_exit:
  178. mov ar.lc = saved_lc // restore the loop counter
  179. mov pr = saved_pr, -1 // restore the predicate registers
  180. br.ret.sptk.many b0
  181. .short_len:
  182. cmp.eq p5, p0 = in2, r0
  183. adds loopcnt = -1, in2
  184. (p5) br.cond.spnt .restore_and_exit ;;
  185. mov ar.lc = loopcnt // p6 should be set when we get here
  186. .l8:
  187. (p6) ld1 c = [in1], 1 // c = *src++
  188. ;;
  189. st1 [in0] = c, 1 // *dest++ = c
  190. (p6) cmp.ne p6, p0 = c, r0
  191. br.cloop.dptk .l8
  192. ;;
  193. mov ar.lc = saved_lc // restore the loop counter
  194. mov pr = saved_pr, -1 // restore the predicate registers
  195. br.ret.sptk.many b0
  196. .recovery2:
  197. add c = 8, len
  198. add tmp = -8, asrc ;;
  199. cmp.gtu p8, p5 = c, thresh ;;
  200. (p8) ld8 r[0] = [tmp]
  201. (p5) mov r[0] = r0
  202. br.cond.sptk .back2
  203. .recovery3:
  204. add tmp = -(MEMLAT + 1) * 8, src ;;
  205. ld8 r[MEMLAT] = [tmp]
  206. br.cond.sptk .back3
  207. .recovery4:
  208. cmp.eq p5, p6 = len, r0
  209. add tmp = -MEMLAT * 8, src ;;
  210. (p6) ld8 r[MEMLAT - 1] = [tmp]
  211. (p5) mov r[MEMLAT - 1] = r0
  212. br.cond.sptk .back4
  213. END(strncpy)
  214. libc_hidden_def (strncpy)