rawmemchr.S 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* rawmemchr (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(__rawmemchr)
  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. cmpb MEM(a0),R(d0)
  48. beq L(L9)
  49. addql #1,R(a0)
  50. #ifdef __mcoldfire__
  51. subql #3,R(d1)
  52. #else
  53. subqw #3,R(d1)
  54. #endif
  55. beq L(L1)
  56. cmpb MEM(a0),R(d0)
  57. beq L(L9)
  58. addql #1,R(a0)
  59. #ifdef __mcoldfire__
  60. addql #1,R(d1)
  61. #else
  62. addqw #1,R(d1)
  63. #endif
  64. beq L(L1)
  65. cmpb MEM(a0),R(d0)
  66. beq L(L9)
  67. addql #1,R(a0)
  68. L(L1:)
  69. /* Load the magic bits. Unlike the generic implementation we can
  70. use the carry bit as the fourth hole. */
  71. movel #0xfefefeff,R(d3)
  72. /* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
  73. change any of the hole bits of LONGWORD.
  74. 1) Is this safe? Will it catch all the zero bytes?
  75. Suppose there is a byte with all zeros. Any carry bits
  76. propagating from its left will fall into the hole at its
  77. least significant bit and stop. Since there will be no
  78. carry from its most significant bit, the LSB of the
  79. byte to the left will be unchanged, and the zero will be
  80. detected.
  81. 2) Is this worthwhile? Will it ignore everything except
  82. zero bytes? Suppose every byte of LONGWORD has a bit set
  83. somewhere. There will be a carry into bit 8. If bit 8
  84. is set, this will carry into bit 16. If bit 8 is clear,
  85. one of bits 9-15 must be set, so there will be a carry
  86. into bit 16. Similarly, there will be a carry into bit
  87. 24. If one of bits 24-31 is set, there will be a carry
  88. into bit 32 (=carry flag), so all of the hole bits will
  89. be changed.
  90. 3) But wait! Aren't we looking for C, not zero?
  91. Good point. So what we do is XOR LONGWORD with a longword,
  92. each of whose bytes is C. This turns each byte that is C
  93. into a zero. */
  94. L(L2:)
  95. /* Get the longword in question. */
  96. movel MEM_POSTINC(a0),R(d1)
  97. /* XOR with the byte we search for. */
  98. eorl R(d0),R(d1)
  99. /* Add the magic value. We get carry bits reported for each byte
  100. which is not C. */
  101. movel R(d3),R(d2)
  102. addl R(d1),R(d2)
  103. /* Check the fourth carry bit before it is clobbered by the next
  104. XOR. If it is not set we have a hit. */
  105. bcc L(L8)
  106. /* We are only interested in carry bits that change due to the
  107. previous add, so remove original bits. */
  108. eorl R(d1),R(d2)
  109. /* Now test for the other three overflow bits.
  110. Set all non-carry bits. */
  111. orl R(d3),R(d2)
  112. /* Add 1 to get zero if all carry bits were set. */
  113. addql #1,R(d2)
  114. /* If we don't get zero then at least one byte of the word equals
  115. C. */
  116. bne L(L8)
  117. /* Get the longword in question. */
  118. movel MEM_POSTINC(a0),R(d1)
  119. /* XOR with the byte we search for. */
  120. eorl R(d0),R(d1)
  121. /* Add the magic value. We get carry bits reported for each byte
  122. which is not C. */
  123. movel R(d3),R(d2)
  124. addl R(d1),R(d2)
  125. /* Check the fourth carry bit before it is clobbered by the next
  126. XOR. If it is not set we have a hit. */
  127. bcc L(L8)
  128. /* We are only interested in carry bits that change due to the
  129. previous add, so remove original bits */
  130. eorl R(d1),R(d2)
  131. /* Now test for the other three overflow bits.
  132. Set all non-carry bits. */
  133. orl R(d3),R(d2)
  134. /* Add 1 to get zero if all carry bits were set. */
  135. addql #1,R(d2)
  136. /* If we don't get zero then at least one byte of the word equals
  137. C. */
  138. beq L(L2)
  139. L(L8:)
  140. /* We have a hit. Check to see which byte it was. First
  141. compensate for the autoincrement in the loop. */
  142. subql #4,R(a0)
  143. cmpb MEM(a0),R(d0)
  144. beq L(L9)
  145. addql #1,R(a0)
  146. cmpb MEM(a0),R(d0)
  147. beq L(L9)
  148. addql #1,R(a0)
  149. cmpb MEM(a0),R(d0)
  150. beq L(L9)
  151. addql #1,R(a0)
  152. /* Otherwise the fourth byte must equal C. */
  153. L(L9:)
  154. movel R(a0),R(d0)
  155. movel MEM_POSTINC(sp),R(d3)
  156. cfi_adjust_cfa_offset (-4)
  157. cfi_restore (R(d3))
  158. movel MEM_POSTINC(sp),R(d2)
  159. cfi_adjust_cfa_offset (-4)
  160. cfi_restore (R(d2))
  161. rts
  162. END(__rawmemchr)
  163. libc_hidden_def (__rawmemchr)
  164. weak_alias (__rawmemchr, rawmemchr)