strchrnul.S 7.1 KB

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