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