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