rawmemchr.S 5.2 KB

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