alpha: Add hidden alias for fegetenv.
[glibc-ports.git] / sysdeps / m68k / strchrnul.S
blob9d13ec16a0d30e14aa91acd5595eba47acaffdd6
1 /* strchrnul (str, ch) -- Return pointer to first occurrence of CH in STR
2    or the final NUL byte.
3    For Motorola 68000.
4    Copyright (C) 1999 Free Software Foundation, Inc.
5    This file is part of the GNU C Library.
6    Contributed by Andreas Schwab <schwab@gnu.org>.
8    The GNU C Library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public
10    License as published by the Free Software Foundation; either
11    version 2.1 of the License, or (at your option) any later version.
13    The GNU C Library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Lesser General Public License for more details.
18    You should have received a copy of the GNU Lesser General Public
19    License along with the GNU C Library; if not, write to the Free
20    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21    02111-1307 USA.  */
23 #include <sysdep.h>
24 #include "asm-syntax.h"
26         TEXT
27 ENTRY(__strchrnul)
28         /* Save the callee-saved registers we use.  */
29         movel   R(d2),MEM_PREDEC(sp)
30         movel   R(d3),MEM_PREDEC(sp)
32         /* Get string pointer and character.  */
33         movel   MEM_DISP(sp,12),R(a0)
34         moveb   MEM_DISP(sp,19),R(d0)
36         /* Distribute the character to all bytes of a longword.  */
37         movel   R(d0),R(d1)
38         lsll    #8,R(d1)
39         moveb   R(d0),R(d1)
40         movel   R(d1),R(d0)
41         swap    R(d0)
42         movew   R(d1),R(d0)
44         /* First search for the character one byte at a time until the
45            pointer is aligned to a longword boundary.  */
46         movel   R(a0),R(d1)
47 #ifdef __mcoldfire__
48         andl    #3,R(d1)
49 #else
50         andw    #3,R(d1)
51 #endif
52         beq     L(L1)
53         moveb   MEM(a0),R(d2)
54         cmpb    R(d0),R(d2)
55         beq     L(L9)
56         tstb    R(d2)
57         beq     L(L9)
58         addql   #1,R(a0)
60 #ifdef __mcoldfire__
61         subql   #3,R(d1)
62 #else
63         subqw   #3,R(d1)
64 #endif
65         beq     L(L1)
66         moveb   MEM(a0),R(d2)
67         cmpb    R(d0),R(d2)
68         beq     L(L9)
69         tstb    R(d2)
70         beq     L(L9)
71         addql   #1,R(a0)
73 #ifdef __mcoldfire__
74         addql   #1,R(d1)
75 #else
76         addqw   #1,R(d1)
77 #endif
78         beq     L(L1)
79         moveb   MEM(a0),R(d2)
80         cmpb    R(d0),R(d2)
81         beq     L(L9)
82         tstb    R(d2)
83         beq     L(L9)
84         addql   #1,R(a0)
86 L(L1:)
87         /* Load the magic bits.  Unlike the generic implementation we can
88            use the carry bit as the fourth hole.  */
89         movel   #0xfefefeff,R(d3)
91       /* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
92          change any of the hole bits of LONGWORD.
94          1) Is this safe?  Will it catch all the zero bytes?
95          Suppose there is a byte with all zeros.  Any carry bits
96          propagating from its left will fall into the hole at its
97          least significant bit and stop.  Since there will be no
98          carry from its most significant bit, the LSB of the
99          byte to the left will be unchanged, and the zero will be
100          detected.
102          2) Is this worthwhile?  Will it ignore everything except
103          zero bytes?  Suppose every byte of LONGWORD has a bit set
104          somewhere.  There will be a carry into bit 8.  If bit 8
105          is set, this will carry into bit 16.  If bit 8 is clear,
106          one of bits 9-15 must be set, so there will be a carry
107          into bit 16.  Similarly, there will be a carry into bit
108          24.  If one of bits 24-31 is set, there will be a carry
109          into bit 32 (=carry flag), so all of the hole bits will
110          be changed.
112          3) But wait!  Aren't we looking for C, not zero?
113          Good point.  So what we do is XOR LONGWORD with a longword,
114          each of whose bytes is C.  This turns each byte that is C
115          into a zero.  */
117 L(L2:)
118         /* Get the longword in question.  */
119         movel   MEM_POSTINC(a0),R(d1)
120         /* XOR with the byte we search for.  */
121         eorl    R(d0),R(d1)
123         /* Add the magic value.  We get carry bits reported for each byte
124            which is not C.  */
125         movel   R(d3),R(d2)
126         addl    R(d1),R(d2)
128         /* Check the fourth carry bit before it is clobbered by the next
129            XOR.  If it is not set we have a hit.  */
130         bcc     L(L8)
132         /* We are only interested in carry bits that change due to the
133            previous add, so remove original bits.  */
134         eorl    R(d1),R(d2)
136         /* Now test for the other three overflow bits.
137            Set all non-carry bits.  */
138         orl     R(d3),R(d2)
139         /* Add 1 to get zero if all carry bits were set.  */
140         addql   #1,R(d2)
142         /* If we don't get zero then at least one byte of the word equals
143            C.  */
144         bne     L(L8)
146         /* Next look for a NUL byte.
147            Restore original longword without reload.  */
148         eorl    R(d0),R(d1)
149         /* Add the magic value.  We get carry bits reported for each byte
150            which is not NUL.  */
151         movel   R(d3),R(d2)
152         addl    R(d1),R(d2)
154         /* Check the fourth carry bit before it is clobbered by the next
155            XOR.  If it is not set we have a hit.  */
156         bcc     L(L8)
158         /* We are only interested in carry bits that change due to the
159            previous add, so remove original bits.  */
160         eorl    R(d1),R(d2)
162         /* Now test for the other three overflow bits.
163            Set all non-carry bits.  */
164         orl     R(d3),R(d2)
165         /* Add 1 to get zero if all carry bits were set.  */
166         addql   #1,R(d2)
168         /* If we don't get zero then at least one byte of the word was
169            NUL.  Otherwise continue with the next longword.  */
170         bne     L(L8)
172         /* Get the longword in question.  */
173         movel   MEM_POSTINC(a0),R(d1)
174         /* XOR with the byte we search for.  */
175         eorl    R(d0),R(d1)
177         /* Add the magic value.  We get carry bits reported for each byte
178            which is not C.  */
179         movel   R(d3),R(d2)
180         addl    R(d1),R(d2)
182         /* Check the fourth carry bit before it is clobbered by the next
183            XOR.  If it is not set we have a hit.  */
184         bcc     L(L8)
186         /* We are only interested in carry bits that change due to the
187            previous add, so remove original bits */
188         eorl    R(d1),R(d2)
190         /* Now test for the other three overflow bits.
191            Set all non-carry bits.  */
192         orl     R(d3),R(d2)
193         /* Add 1 to get zero if all carry bits were set.  */
194         addql   #1,R(d2)
196         /* If we don't get zero then at least one byte of the word equals
197            C.  */
198         bne     L(L8)
200         /* Next look for a NUL byte.
201            Restore original longword without reload.  */
202         eorl    R(d0),R(d1)
203         /* Add the magic value.  We get carry bits reported for each byte
204            which is not NUL.  */
205         movel   R(d3),R(d2)
206         addl    R(d1),R(d2)
208         /* Check the fourth carry bit before it is clobbered by the next
209            XOR.  If it is not set we have a hit.  */
210         bcc     L(L8)
212         /* We are only interested in carry bits that change due to the
213            previous add, so remove original bits */
214         eorl    R(d1),R(d2)
216         /* Now test for the other three overflow bits.
217            Set all non-carry bits.  */
218         orl     R(d3),R(d2)
219         /* Add 1 to get zero if all carry bits were set.  */
220         addql   #1,R(d2)
222         /* If we don't get zero then at least one byte of the word was
223            NUL.  Otherwise continue with the next longword.  */
224         beq     L(L2)
226 L(L8:)
227         /* We have a hit.  Check to see which byte it was.  First
228            compensate for the autoincrement in the loop.  */
229         subql   #4,R(a0)
231         moveb   MEM(a0),R(d1)
232         cmpb    R(d0),R(d1)
233         beq     L(L9)
234         tstb    R(d1)
235         beq     L(L9)
236         addql   #1,R(a0)
238         moveb   MEM(a0),R(d1)
239         cmpb    R(d0),R(d1)
240         beq     L(L9)
241         tstb    R(d1)
242         beq     L(L9)
243         addql   #1,R(a0)
245         moveb   MEM(a0),R(d1)
246         cmpb    R(d0),R(d1)
247         beq     L(L9)
248         tstb    R(d1)
249         beq     L(L9)
250         addql   #1,R(a0)
252         /* Otherwise the fourth byte must equal C or be NUL.  */
253 L(L9:)
254         movel   R(a0),R(d0)
255         movel   MEM_POSTINC(sp),R(d3)
256         movel   MEM_POSTINC(sp),R(d2)
257         rts
258 END(__strchrnul)
260 weak_alias (__strchrnul, strchrnul)