2.9
[glibc/nacl-glibc.git] / sysdeps / sparc / sparc32 / memchr.S
blobfcd98315db0f9314fb98484e8e837447614e50dc
1 /* memchr (str, ch, n) -- Return pointer to first occurrence of CH in STR less
2    than N.
3    For SPARC v7.
4    Copyright (C) 1996, 1999, 2000, 2003 Free Software Foundation, Inc.
5    This file is part of the GNU C Library.
6    Contributed by Jakub Jelinek <jj@ultra.linux.cz> and
7                   David S. Miller <davem@caip.rutgers.edu>.
8    This version is developed using the same algorithm as the fast C
9    version which carries the following introduction:
10    Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
11    with help from Dan Sahlin (dan@sics.se) and
12    commentary by Jim Blandy (jimb@ai.mit.edu);
13    adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
14    and implemented by Roland McGrath (roland@ai.mit.edu).
16    The GNU C Library is free software; you can redistribute it and/or
17    modify it under the terms of the GNU Lesser General Public
18    License as published by the Free Software Foundation; either
19    version 2.1 of the License, or (at your option) any later version.
21    The GNU C Library is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24    Lesser General Public License for more details.
26    You should have received a copy of the GNU Lesser General Public
27    License along with the GNU C Library; if not, write to the Free
28    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29    02111-1307 USA.  */
31 #include <sysdep.h>
33         .text
34         .align          4
35 ENTRY(__memchr)
36         andcc           %o1, 0xff, %o1
37         sll             %o1, 8, %g6
38         andcc           %o0, 3, %g0
39         or              %o1, %g6, %g6
40         sll             %g6, 16, %o3
41         be              10f
42          or             %o3, %g6, %g2
43         cmp             %o2, 0
44         be              9f
45          sethi          %hi(0x80808080), %o4
46         ldub            [%o0], %g4
47         cmp             %g4, %o1
48         be              1f
49          add            %o0, 1, %o0
50         subcc           %o2, 1, %o2
51         be              9f
52          andcc          %o0, 3, %g0
53         be              4f
54          or             %o4, %lo(0x80808080), %o3
55         ldub            [%o0], %g4
56         cmp             %g4, %o1
57         be              1f
58          add            %o0, 1, %o0
59         subcc           %o2, 1, %o2
60         be              9f
61          andcc          %o0, 3, %g0
62         be              5f
63          sethi          %hi(0x01010101), %o5
64         ldub            [%o0], %g4
65         cmp             %g4, %o1
66         be              1f
67          add            %o0, 1, %o0
68         subcc           %o2, 1, %o2
69         bne,a           7f
70          and            %o2, 3, %g1
71         retl
72          clr            %o0
73 1:      retl
74          sub            %o0, 1, %o0
75 10:     sethi           %hi(0x80808080), %o4
76         or              %o4, %lo(0x80808080), %o3
77 4:      sethi           %hi(0x01010101), %o5
78 5:      and             %o2, 3, %g1
79 7:      andcc           %o2, 0xfffffffc, %o2
80         be              0f
81          or             %o5, %lo(0x01010101), %g6
82         ld              [%o0], %g4
83 6:      xor             %g4, %g2, %g5
84         add             %o0, 4, %o0
85         sub             %g5, %g6, %g5
86         andcc           %g5, %o3, %g0
87         bne             8f
88          subcc          %o2, 4, %o2
89         bne,a           6b
90          ld             [%o0], %g4
91 0:      cmp             %g1, 0
92 1:      be              9f
93          add            %o0, 4, %o0
94         ldub            [%o0 - 4], %g4
95         cmp             %g4, %o1
96         be              4f
97          cmp            %g1, 1
98         be              9f
99          ldub           [%o0 - 3], %g4
100         cmp             %g4, %o1
101         be              3f
102          cmp            %g1, 2
103         be              9f
104          ldub           [%o0 - 2], %g4
105         cmp             %g4, %o1
106         be              2f
107          nop
108 9:      retl
109          clr            %o0
111         /* Check every byte. */
112 8:      srl             %g4, 24, %g5
113         and             %g5, 0xff, %g5
114         cmp             %g5, %o1
115         be              4f
116          srl            %g4, 16, %g5
117         and             %g5, 0xff, %g5
118         cmp             %g5, %o1
119         be              3f
120          srl            %g4, 8, %g5
121         and             %g5, 0xff, %g5
122         cmp             %g5, %o1
123         be              2f
124          and            %g4, 0xff, %g5
125         cmp             %g5, %o1
126         be              1f
127          cmp            %o2, 0
128         bne,a           6b
129          ld             [%o0], %g4
130         b               1b
131          cmp            %g1, 0
132 1:      retl
133          sub            %o0, 1, %o0
134 2:      retl
135          sub            %o0, 2, %o0
136 3:      retl
137          sub            %o0, 3, %o0
138 4:      retl
139          sub            %o0, 4, %o0
140 END(__memchr)
142 weak_alias (__memchr, memchr)
143 #if !__BOUNDED_POINTERS__
144 weak_alias (__memchr, __ubp_memchr)
145 #endif
146 libc_hidden_builtin_def (memchr)