strncpy.S 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* Optimized strcpy for Xtensa.
  2. Copyright (C) 2001, 2007 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. #include "../../sysdeps/linux/xtensa/sysdep.h"
  17. #include <bits/xtensa-config.h>
  18. #ifdef __XTENSA_EB__
  19. #define MASK0 0xff000000
  20. #define MASK1 0x00ff0000
  21. #define MASK2 0x0000ff00
  22. #define MASK3 0x000000ff
  23. #else
  24. #define MASK0 0x000000ff
  25. #define MASK1 0x0000ff00
  26. #define MASK2 0x00ff0000
  27. #define MASK3 0xff000000
  28. #endif
  29. /* Do not use .literal_position in the ENTRY macro. */
  30. #undef LITERAL_POSITION
  31. #define LITERAL_POSITION
  32. .text
  33. .align 4
  34. .literal_position
  35. __strncpy_aux:
  36. .Lsrc1mod2: /* src address is odd */
  37. l8ui a8, a3, 0 /* get byte 0 */
  38. addi a3, a3, 1 /* advance src pointer */
  39. s8i a8, a10, 0 /* store byte 0 */
  40. addi a4, a4, -1 /* decrement n */
  41. beqz a4, .Lret /* if n is zero */
  42. addi a10, a10, 1 /* advance dst pointer */
  43. beqz a8, .Lfill /* if byte 0 is zero */
  44. bbci.l a3, 1, .Lsrcaligned /* if src is now word-aligned */
  45. .Lsrc2mod4: /* src address is 2 mod 4 */
  46. l8ui a8, a3, 0 /* get byte 0 */
  47. addi a4, a4, -1 /* decrement n */
  48. s8i a8, a10, 0 /* store byte 0 */
  49. beqz a4, .Lret /* if n is zero */
  50. addi a10, a10, 1 /* advance dst pointer */
  51. beqz a8, .Lfill /* if byte 0 is zero */
  52. l8ui a8, a3, 1 /* get byte 0 */
  53. addi a3, a3, 2 /* advance src pointer */
  54. s8i a8, a10, 0 /* store byte 0 */
  55. addi a4, a4, -1 /* decrement n */
  56. beqz a4, .Lret /* if n is zero */
  57. addi a10, a10, 1 /* advance dst pointer */
  58. bnez a8, .Lsrcaligned
  59. j .Lfill
  60. .Lret:
  61. retw
  62. ENTRY (strncpy)
  63. /* a2 = dst, a3 = src */
  64. mov a10, a2 /* leave dst in return value register */
  65. beqz a4, .Lret /* if n is zero */
  66. movi a11, MASK0
  67. movi a5, MASK1
  68. movi a6, MASK2
  69. movi a7, MASK3
  70. bbsi.l a3, 0, .Lsrc1mod2
  71. bbsi.l a3, 1, .Lsrc2mod4
  72. .Lsrcaligned:
  73. /* Check if the destination is aligned. */
  74. movi a8, 3
  75. bnone a10, a8, .Laligned
  76. j .Ldstunaligned
  77. /* Fill the dst with zeros -- n is at least 1. */
  78. .Lfill:
  79. movi a9, 0
  80. bbsi.l a10, 0, .Lfill1mod2
  81. bbsi.l a10, 1, .Lfill2mod4
  82. .Lfillaligned:
  83. blti a4, 4, .Lfillcleanup
  84. /* Loop filling complete words with zero. */
  85. #if XCHAL_HAVE_LOOPS
  86. srai a8, a4, 2
  87. loop a8, 1f
  88. s32i a9, a10, 0
  89. addi a10, a10, 4
  90. 1: slli a8, a8, 2
  91. sub a4, a4, a8
  92. #else /* !XCHAL_HAVE_LOOPS */
  93. 1: s32i a9, a10, 0
  94. addi a10, a10, 4
  95. addi a4, a4, -4
  96. bgei a4, 4, 1b
  97. #endif /* !XCHAL_HAVE_LOOPS */
  98. beqz a4, 2f
  99. .Lfillcleanup:
  100. /* Fill leftover (1 to 3) bytes with zero. */
  101. s8i a9, a10, 0 /* store byte 0 */
  102. addi a4, a4, -1 /* decrement n */
  103. addi a10, a10, 1
  104. bnez a4, .Lfillcleanup
  105. 2: retw
  106. .Lfill1mod2: /* dst address is odd */
  107. s8i a9, a10, 0 /* store byte 0 */
  108. addi a4, a4, -1 /* decrement n */
  109. beqz a4, 2b /* if n is zero */
  110. addi a10, a10, 1 /* advance dst pointer */
  111. bbci.l a10, 1, .Lfillaligned /* if dst is now word-aligned */
  112. .Lfill2mod4: /* dst address is 2 mod 4 */
  113. s8i a9, a10, 0 /* store byte 0 */
  114. addi a4, a4, -1 /* decrement n */
  115. beqz a4, 2b /* if n is zero */
  116. s8i a9, a10, 1 /* store byte 1 */
  117. addi a4, a4, -1 /* decrement n */
  118. beqz a4, 2b /* if n is zero */
  119. addi a10, a10, 2 /* advance dst pointer */
  120. j .Lfillaligned
  121. /* dst is word-aligned; src is word-aligned; n is at least 1. */
  122. .align 4
  123. /* (2 mod 4) alignment for loop instruction */
  124. .Laligned:
  125. #if XCHAL_HAVE_LOOPS
  126. _movi.n a8, 0 /* set up for the maximum loop count */
  127. loop a8, 1f /* loop forever (almost anyway) */
  128. blti a4, 5, .Ldstunaligned /* n is near limit; do one at a time */
  129. l32i a8, a3, 0 /* get word from src */
  130. addi a3, a3, 4 /* advance src pointer */
  131. bnone a8, a11, .Lz0 /* if byte 0 is zero */
  132. bnone a8, a5, .Lz1 /* if byte 1 is zero */
  133. bnone a8, a6, .Lz2 /* if byte 2 is zero */
  134. s32i a8, a10, 0 /* store word to dst */
  135. addi a4, a4, -4 /* decrement n */
  136. addi a10, a10, 4 /* advance dst pointer */
  137. bnone a8, a7, .Lfill /* if byte 3 is zero */
  138. 1:
  139. #else /* !XCHAL_HAVE_LOOPS */
  140. 1: blti a4, 5, .Ldstunaligned /* n is near limit; do one at a time */
  141. l32i a8, a3, 0 /* get word from src */
  142. addi a3, a3, 4 /* advance src pointer */
  143. bnone a8, a11, .Lz0 /* if byte 0 is zero */
  144. bnone a8, a5, .Lz1 /* if byte 1 is zero */
  145. bnone a8, a6, .Lz2 /* if byte 2 is zero */
  146. s32i a8, a10, 0 /* store word to dst */
  147. addi a4, a4, -4 /* decrement n */
  148. addi a10, a10, 4 /* advance dst pointer */
  149. bany a8, a7, 1b /* no zeroes */
  150. #endif /* !XCHAL_HAVE_LOOPS */
  151. j .Lfill
  152. .Lz0: /* Byte 0 is zero. */
  153. #ifdef __XTENSA_EB__
  154. movi a8, 0
  155. #endif
  156. s8i a8, a10, 0
  157. addi a4, a4, -1 /* decrement n */
  158. addi a10, a10, 1 /* advance dst pointer */
  159. j .Lfill
  160. .Lz1: /* Byte 1 is zero. */
  161. #ifdef __XTENSA_EB__
  162. extui a8, a8, 16, 16
  163. #endif
  164. s16i a8, a10, 0
  165. addi a4, a4, -2 /* decrement n */
  166. addi a10, a10, 2 /* advance dst pointer */
  167. j .Lfill
  168. .Lz2: /* Byte 2 is zero. */
  169. #ifdef __XTENSA_EB__
  170. extui a8, a8, 16, 16
  171. #endif
  172. s16i a8, a10, 0
  173. movi a8, 0
  174. s8i a8, a10, 2
  175. addi a4, a4, -3 /* decrement n */
  176. addi a10, a10, 3 /* advance dst pointer */
  177. j .Lfill
  178. .align 4
  179. /* (2 mod 4) alignment for loop instruction */
  180. .Ldstunaligned:
  181. #if XCHAL_HAVE_LOOPS
  182. _movi.n a8, 0 /* set up for the maximum loop count */
  183. loop a8, 2f /* loop forever (almost anyway) */
  184. #endif
  185. 1: l8ui a8, a3, 0
  186. addi a3, a3, 1
  187. s8i a8, a10, 0
  188. addi a4, a4, -1
  189. beqz a4, 3f
  190. addi a10, a10, 1
  191. #if XCHAL_HAVE_LOOPS
  192. beqz a8, 2f
  193. #else
  194. bnez a8, 1b
  195. #endif
  196. 2: j .Lfill
  197. 3: retw
  198. libc_hidden_def (strncpy)