(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / m68k / rawmemchr.S
blobacd8f76e444b7f621368c44fcb153eb15461e3a2
1 /* rawmemchr (str, ch) -- Return pointer to first occurrence of CH in STR.
2    For Motorola 68000.
3    Copyright (C) 1999, 2002 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5    Contributed by Andreas Schwab <schwab@gnu.org>.
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.
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.
17    You should have received a copy of the GNU Lesser General Public
18    License along with the GNU C Library; if not, write to the Free
19    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20    02111-1307 USA.  */
22 #include <sysdep.h>
23 #include "asm-syntax.h"
25         TEXT
26 ENTRY(__rawmemchr)
27         /* Save the callee-saved registers we use.  */
28         movel   R(d2),MEM_PREDEC(sp)
29         movel   R(d3),MEM_PREDEC(sp)
31         /* Get string pointer and character.  */
32         movel   MEM_DISP(sp,12),R(a0)
33         moveb   MEM_DISP(sp,19),R(d0)
35         /* Distribute the character to all bytes of a longword.  */
36         movel   R(d0),R(d1)
37         lsll    #8,R(d1)
38         moveb   R(d0),R(d1)
39         movel   R(d1),R(d0)
40         swap    R(d0)
41         movew   R(d1),R(d0)
43         /* First search for the character one byte at a time until the
44            pointer is aligned to a longword boundary.  */
45         movel   R(a0),R(d1)
46         andw    #3,R(d1)
47         beq     L(L1)
48         cmpb    MEM(a0),R(d0)
49         beq     L(L9)
50         addql   #1,R(a0)
52         movel   R(a0),R(d1)
53         andw    #3,R(d1)
54         beq     L(L1)
55         cmpb    MEM(a0),R(d0)
56         beq     L(L9)
57         addql   #1,R(a0)
59         movel   R(a0),R(d1)
60         andw    #3,R(d1)
61         beq     L(L1)
62         cmpb    MEM(a0),R(d0)
63         beq     L(L9)
64         addql   #1,R(a0)
66 L(L1:)
67         /* Load the magic bits.  Unlike the generic implementation we can
68            use the carry bit as the fourth hole.  */
69         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.
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.
82          2) Is this worthwhile?  Will it ignore everything except
83          zero bytes?  Suppose every byte of LONGWORD has a bit set
84          somewhere.  There will be a carry into bit 8.  If bit 8
85          is set, this will carry into bit 16.  If bit 8 is clear,
86          one of bits 9-15 must be set, so there will be a carry
87          into bit 16.  Similarly, there will be a carry into bit
88          24.  If one of bits 24-31 is set, there will be a carry
89          into bit 32 (=carry flag), so all of the hole bits will
90          be changed.
92          3) But wait!  Aren't we looking for C, not zero?
93          Good point.  So what we do is XOR LONGWORD with a longword,
94          each of whose bytes is C.  This turns each byte that is C
95          into a zero.  */
97 L(L2:)
98         /* Get the longword in question.  */
99         movel   MEM_POSTINC(a0),R(d1)
100         /* XOR with the byte we search for.  */
101         eorl    R(d0),R(d1)
103         /* Add the magic value.  We get carry bits reported for each byte
104            which is not C.  */
105         movel   R(d3),R(d2)
106         addl    R(d1),R(d2)
108         /* Check the fourth carry bit before it is clobbered by the next
109            XOR.  If it is not set we have a hit.  */
110         bcc     L(L8)
112         /* We are only interested in carry bits that change due to the
113            previous add, so remove original bits.  */
114         eorl    R(d1),R(d2)
116         /* Now test for the other three overflow bits.
117            Set all non-carry bits.  */
118         orl     R(d3),R(d2)
119         /* Add 1 to get zero if all carry bits were set.  */
120         addql   #1,R(d2)
122         /* If we don't get zero then at least one byte of the word equals
123            C.  */
124         bne     L(L8)
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)
131         /* Add the magic value.  We get carry bits reported for each byte
132            which is not C.  */
133         movel   R(d3),R(d2)
134         addl    R(d1),R(d2)
136         /* Check the fourth carry bit before it is clobbered by the next
137            XOR.  If it is not set we have a hit.  */
138         bcc     L(L8)
140         /* We are only interested in carry bits that change due to the
141            previous add, so remove original bits */
142         eorl    R(d1),R(d2)
144         /* Now test for the other three overflow bits.
145            Set all non-carry bits.  */
146         orl     R(d3),R(d2)
147         /* Add 1 to get zero if all carry bits were set.  */
148         addql   #1,R(d2)
150         /* If we don't get zero then at least one byte of the word equals
151            C.  */
152         beq     L(L2)
154 L(L8:)
155         /* We have a hit.  Check to see which byte it was.  First
156            compensate for the autoincrement in the loop.  */
157         subql   #4,R(a0)
159         cmpb    MEM(a0),R(d0)
160         beq     L(L9)
161         addql   #1,R(a0)
163         cmpb    MEM(a0),R(d0)
164         beq     L(L9)
165         addql   #1,R(a0)
167         cmpb    MEM(a0),R(d0)
168         beq     L(L9)
169         addql   #1,R(a0)
171         /* Otherwise the fourth byte must equal C.  */
172 L(L9:)
173         movel   R(a0),R(d0)
174         movel   MEM_POSTINC(sp),R(d3)
175         movel   MEM_POSTINC(sp),R(d2)
176         rts
177 END(__rawmemchr)
179 libc_hidden_def (__rawmemchr)
180 weak_alias (__rawmemchr, rawmemchr)