strchr.S 7.3 KB

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