strchr.S 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* strchr (str, ch) -- Return pointer to first occurrence of CH in STR.
  2. For Motorola 68000.
  3. Copyright (C) 1999-2019 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Andreas Schwab <schwab@gnu.org>.
  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. #include <sysdep.h>
  18. #include "asm-syntax.h"
  19. TEXT
  20. ENTRY(strchr)
  21. /* Save the callee-saved registers we use. */
  22. movel R(d2),MEM_PREDEC(sp)
  23. cfi_adjust_cfa_offset (4)
  24. movel R(d3),MEM_PREDEC(sp)
  25. cfi_adjust_cfa_offset (4)
  26. cfi_rel_offset (R(d2),4)
  27. cfi_rel_offset (R(d3),0)
  28. /* Get string pointer and character. */
  29. movel MEM_DISP(sp,12),R(a0)
  30. moveb MEM_DISP(sp,19),R(d0)
  31. /* Distribute the character to all bytes of a longword. */
  32. movel R(d0),R(d1)
  33. lsll #8,R(d1)
  34. moveb R(d0),R(d1)
  35. movel R(d1),R(d0)
  36. swap R(d0)
  37. movew R(d1),R(d0)
  38. /* First search for the character one byte at a time until the
  39. pointer is aligned to a longword boundary. */
  40. movel R(a0),R(d1)
  41. #ifdef __mcoldfire__
  42. andl #3,R(d1)
  43. #else
  44. andw #3,R(d1)
  45. #endif
  46. beq L(L1)
  47. moveb MEM(a0),R(d2)
  48. cmpb R(d0),R(d2)
  49. beq L(L9)
  50. tstb R(d2)
  51. beq L(L3)
  52. addql #1,R(a0)
  53. #ifdef __mcoldfire__
  54. subql #3,R(d1)
  55. #else
  56. subqw #3,R(d1)
  57. #endif
  58. beq L(L1)
  59. moveb MEM(a0),R(d2)
  60. cmpb R(d0),R(d2)
  61. beq L(L9)
  62. tstb R(d2)
  63. beq L(L3)
  64. addql #1,R(a0)
  65. #ifdef __mcoldfire__
  66. addql #1,R(d1)
  67. #else
  68. addqw #1,R(d1)
  69. #endif
  70. beq L(L1)
  71. moveb MEM(a0),R(d2)
  72. cmpb R(d0),R(d2)
  73. beq L(L9)
  74. tstb R(d2)
  75. beq L(L3)
  76. addql #1,R(a0)
  77. L(L1:)
  78. /* Load the magic bits. Unlike the generic implementation we can
  79. use the carry bit as the fourth hole. */
  80. movel #0xfefefeff,R(d3)
  81. /* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
  82. change any of the hole bits of LONGWORD.
  83. 1) Is this safe? Will it catch all the zero bytes?
  84. Suppose there is a byte with all zeros. Any carry bits
  85. propagating from its left will fall into the hole at its
  86. least significant bit and stop. Since there will be no
  87. carry from its most significant bit, the LSB of the
  88. byte to the left will be unchanged, and the zero will be
  89. detected.
  90. 2) Is this worthwhile? Will it ignore everything except
  91. zero bytes? Suppose every byte of LONGWORD has a bit set
  92. somewhere. There will be a carry into bit 8. If bit 8
  93. is set, this will carry into bit 16. If bit 8 is clear,
  94. one of bits 9-15 must be set, so there will be a carry
  95. into bit 16. Similarly, there will be a carry into bit
  96. 24. If one of bits 24-31 is set, there will be a carry
  97. into bit 32 (=carry flag), so all of the hole bits will
  98. be changed.
  99. 3) But wait! Aren't we looking for C, not zero?
  100. Good point. So what we do is XOR LONGWORD with a longword,
  101. each of whose bytes is C. This turns each byte that is C
  102. into a zero. */
  103. L(L2:)
  104. /* Get the longword in question. */
  105. movel MEM_POSTINC(a0),R(d1)
  106. /* XOR with the byte we search for. */
  107. eorl R(d0),R(d1)
  108. /* Add the magic value. We get carry bits reported for each byte
  109. which is not C. */
  110. movel R(d3),R(d2)
  111. addl R(d1),R(d2)
  112. /* Check the fourth carry bit before it is clobbered by the next
  113. XOR. If it is not set we have a hit. */
  114. bcc L(L8)
  115. /* We are only interested in carry bits that change due to the
  116. previous add, so remove original bits. */
  117. eorl R(d1),R(d2)
  118. /* Now test for the other three overflow bits.
  119. Set all non-carry bits. */
  120. orl R(d3),R(d2)
  121. /* Add 1 to get zero if all carry bits were set. */
  122. addql #1,R(d2)
  123. /* If we don't get zero then at least one byte of the word equals
  124. C. */
  125. bne L(L8)
  126. /* Next look for a NUL byte.
  127. Restore original longword without reload. */
  128. eorl R(d0),R(d1)
  129. /* Add the magic value. We get carry bits reported for each byte
  130. which is not NUL. */
  131. movel R(d3),R(d2)
  132. addl R(d1),R(d2)
  133. /* Check the fourth carry bit before it is clobbered by the next
  134. XOR. If it is not set we have a hit, and return NULL. */
  135. bcc L(L3)
  136. /* We are only interested in carry bits that change due to the
  137. previous add, so remove original bits. */
  138. eorl R(d1),R(d2)
  139. /* Now test for the other three overflow bits.
  140. Set all non-carry bits. */
  141. orl R(d3),R(d2)
  142. /* Add 1 to get zero if all carry bits were set. */
  143. addql #1,R(d2)
  144. /* If we don't get zero then at least one byte of the word was NUL
  145. and we return NULL. Otherwise continue with the next longword. */
  146. bne L(L3)
  147. /* Get the longword in question. */
  148. movel MEM_POSTINC(a0),R(d1)
  149. /* XOR with the byte we search for. */
  150. eorl R(d0),R(d1)
  151. /* Add the magic value. We get carry bits reported for each byte
  152. which is not C. */
  153. movel R(d3),R(d2)
  154. addl R(d1),R(d2)
  155. /* Check the fourth carry bit before it is clobbered by the next
  156. XOR. If it is not set we have a hit. */
  157. bcc L(L8)
  158. /* We are only interested in carry bits that change due to the
  159. previous add, so remove original bits */
  160. eorl R(d1),R(d2)
  161. /* Now test for the other three overflow bits.
  162. Set all non-carry bits. */
  163. orl R(d3),R(d2)
  164. /* Add 1 to get zero if all carry bits were set. */
  165. addql #1,R(d2)
  166. /* If we don't get zero then at least one byte of the word equals
  167. C. */
  168. bne L(L8)
  169. /* Next look for a NUL byte.
  170. Restore original longword without reload. */
  171. eorl R(d0),R(d1)
  172. /* Add the magic value. We get carry bits reported for each byte
  173. which is not NUL. */
  174. movel R(d3),R(d2)
  175. addl R(d1),R(d2)
  176. /* Check the fourth carry bit before it is clobbered by the next
  177. XOR. If it is not set we have a hit, and return NULL. */
  178. bcc L(L3)
  179. /* We are only interested in carry bits that change due to the
  180. previous add, so remove original bits */
  181. eorl R(d1),R(d2)
  182. /* Now test for the other three overflow bits.
  183. Set all non-carry bits. */
  184. orl R(d3),R(d2)
  185. /* Add 1 to get zero if all carry bits were set. */
  186. addql #1,R(d2)
  187. /* If we don't get zero then at least one byte of the word was NUL
  188. and we return NULL. Otherwise continue with the next longword. */
  189. beq L(L2)
  190. L(L3:)
  191. /* Return NULL. */
  192. clrl R(d0)
  193. movel R(d0),R(a0)
  194. movel MEM_POSTINC(sp),R(d3)
  195. cfi_remember_state
  196. cfi_adjust_cfa_offset (-4)
  197. cfi_restore (R(d3))
  198. movel MEM_POSTINC(sp),R(d2)
  199. cfi_adjust_cfa_offset (-4)
  200. cfi_restore (R(d2))
  201. rts
  202. cfi_restore_state
  203. L(L8:)
  204. /* We have a hit. Check to see which byte it was. First
  205. compensate for the autoincrement in the loop. */
  206. subql #4,R(a0)
  207. moveb MEM(a0),R(d1)
  208. cmpb R(d0),R(d1)
  209. beq L(L9)
  210. tstb R(d1)
  211. beq L(L3)
  212. addql #1,R(a0)
  213. moveb MEM(a0),R(d1)
  214. cmpb R(d0),R(d1)
  215. beq L(L9)
  216. tstb R(d1)
  217. beq L(L3)
  218. addql #1,R(a0)
  219. moveb MEM(a0),R(d1)
  220. cmpb R(d0),R(d1)
  221. beq L(L9)
  222. tstb R(d1)
  223. beq L(L3)
  224. addql #1,R(a0)
  225. /* Otherwise the fourth byte must equal C. */
  226. L(L9:)
  227. movel R(a0),R(d0)
  228. movel MEM_POSTINC(sp),R(d3)
  229. cfi_adjust_cfa_offset (-4)
  230. cfi_restore (R(d3))
  231. movel MEM_POSTINC(sp),R(d2)
  232. cfi_adjust_cfa_offset (-4)
  233. cfi_restore (R(d2))
  234. rts
  235. END(strchr)
  236. weak_alias (strchr, index)
  237. libc_hidden_builtin_def (strchr)