1 /* Optimized version of the standard memchr() function.
2 This file is part of the GNU C Library.
3 Copyright (C) 2000, 2001, 2003, 2010 Free Software Foundation, Inc.
4 Contributed by Dan Pop <Dan.Pop@cern.ch>.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 /* Return: the address of the first occurence of chr in str or NULL
28 This implementation assumes little endian mode. For big endian mode,
29 the instruction czx1.r should be replaced by czx1.l.
31 The algorithm is fairly straightforward: search byte by byte until we
32 we get to a word aligned address, then search word by word as much as
33 possible; the remaining few bytes are searched one at a time.
35 The word by word search is performed by xor-ing the word with a word
36 containing chr in every byte. If there is a hit, the result will
37 contain a zero byte in the corresponding position. The presence and
38 position of that zero byte is detected with a czx instruction.
40 All the loops in this function could have had the internal branch removed
41 if br.ctop and br.cloop could be predicated :-(. */
60 alloc r2 = ar.pfs, 3, 0, 29, 32
62 .rotr value[MEMLAT+1], addr[MEMLAT+3], aux[2], poschr[2]
65 mov saved_lc = ar.lc // save the loop counter
67 mov saved_pr = pr // save the predicates
70 add last = str, in2 // last byte
71 and tmp = 7, str // tmp = str % 8
72 cmp.ne p7, p0 = r0, r0 // clear p7
73 extr.u chr = in1, 0, 8 // chr = (unsigned char) in1
75 cmp.gtu p6, p0 = 16, in2 // use a simple loop for short
76 (p6) br.cond.spnt .srchfew ;; // searches
77 sub loopcnt = 8, tmp // loopcnt = 8 - tmp
78 cmp.eq p6, p0 = tmp, r0
79 (p6) br.cond.sptk .str_aligned;;
80 sub len = len, loopcnt
81 adds loopcnt = -1, loopcnt;;
86 cmp.eq p6, p0 = val, chr
87 (p6) br.cond.spnt .foundit
90 cmp.ne p6, p0 = r0, r0 // clear p6
91 shr.u loopcnt = len, 3 // loopcnt = len / 8
92 and len = 7, len ;; // remaining len = len & 7
93 adds loopcnt = -1, loopcnt
94 mov ar.ec = MEMLAT + 3
95 mux1 chrx8 = chr, @brcst ;; // get a word full of chr
97 mov pr.rot = 1 << 16 ;;
99 (p[0]) mov addr[0] = ret0
100 (p[0]) ld8.s value[0] = [ret0], 8 // speculative load
101 (p[MEMLAT]) chk.s value[MEMLAT], .recovery // check and recovery
102 (p[MEMLAT]) xor aux[0] = value[MEMLAT], chrx8
103 (p[MEMLAT+1]) czx1.r poschr[0] = aux[1]
104 (p[MEMLAT+2]) cmp.ne p7, p0 = 8, poschr[1]
105 (p7) br.cond.dpnt .foundit
108 adds loopcnt = -1, len
109 cmp.eq p6, p0 = len, r0
110 (p6) br.cond.spnt .notfound ;;
115 cmp.eq p6, p0 = val, chr
116 (p6) br.cond.dpnt .foundit
119 cmp.ne p6, p0 = r0, r0 // clear p6 (p7 was already 0 when we got here)
120 mov ret0 = r0 ;; // return NULL
122 .pred.rel "mutex" p6, p7
123 (p6) adds ret0 = -1, ret0 // if we got here from l1 or l3
124 (p7) add ret0 = addr[MEMLAT+2], poschr[1] // if we got here from l2
125 mov pr = saved_pr, -1
131 # error "MEMLAT must be 6!"
133 (p[MEMLAT-6]) add ret0 = -8, ret0;;
134 (p[MEMLAT-5]) add ret0 = -8, ret0;;
135 (p[MEMLAT-4]) add ret0 = -8, ret0;;
136 (p[MEMLAT-3]) add ret0 = -8, ret0;;
137 (p[MEMLAT-2]) add ret0 = -8, ret0;;
138 (p[MEMLAT-1]) add ret0 = -8, ret0;;
139 (p[MEMLAT]) add ret0 = -8, ret0;;
140 (p[MEMLAT+1]) add ret0 = -8, ret0;;
141 (p[MEMLAT+2]) add ret0 = -8, ret0;;
143 mov addr[MEMLAT+2] = ret0
144 ld8 tmp = [ret0];; // load the first unchecked 8byte
145 xor aux[1] = tmp, chrx8;;
146 czx1.r poschr[1] = aux[1];;
147 cmp.ne p7, p0 = 8, poschr[1];;
148 (p7) add ret0 = addr[MEMLAT+2], poschr[1];;
149 (p7) cmp.geu p6, p7 = ret0, last // don't go over the last byte
150 (p6) br.cond.spnt .notfound;;
151 (p7) br.cond.spnt .foundit;;
152 adds ret0 = 8, ret0 // load the next unchecked 8byte
157 weak_alias (__memchr, memchr)
158 #if !__BOUNDED_POINTERS__
159 weak_alias (__memchr, __ubp_memchr)
161 libc_hidden_builtin_def (memchr)