memchr.S 7.2 KB

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