]>
Commit | Line | Data |
---|---|---|
5bfc4252 UD |
1 | /* Optimized version of the memccpy() function. |
2 | This file is part of the GNU C Library. | |
e352f0d8 | 3 | Copyright (C) 2000, 2001, 2003 Free Software Foundation, Inc. |
5bfc4252 UD |
4 | Contributed by Dan Pop <Dan.Pop@cern.ch>. |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
5bfc4252 UD |
10 | |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
5bfc4252 | 15 | |
41bdb6e2 AJ |
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 | |
19 | 02111-1307 USA. */ | |
5bfc4252 UD |
20 | |
21 | /* Return: a pointer to the next byte after char in dest or NULL | |
22 | ||
23 | Inputs: | |
24 | in0: dest | |
25 | in1: src | |
26 | in2: char | |
27 | in3: byte count | |
a1ce647f | 28 | |
5bfc4252 | 29 | This implementation assumes little endian mode (UM.be = 0). |
a1ce647f | 30 | |
5bfc4252 UD |
31 | This implementation assumes that it is safe to do read ahead |
32 | in the src block, without getting beyond its limit. */ | |
33 | ||
34 | #include <sysdep.h> | |
35 | #undef ret | |
36 | ||
37 | #define OP_T_THRES 16 | |
38 | #define OPSIZ 8 | |
39 | ||
5bfc4252 UD |
40 | #define saved_pr r17 |
41 | #define saved_lc r18 | |
42 | #define dest r19 | |
43 | #define src r20 | |
44 | #define len r21 | |
45 | #define asrc r22 | |
46 | #define tmp r23 | |
47 | #define char r24 | |
48 | #define charx8 r25 | |
e352f0d8 | 49 | #define saved_ec r26 |
5bfc4252 UD |
50 | #define sh2 r28 |
51 | #define sh1 r29 | |
52 | #define loopcnt r30 | |
53 | #define value r31 | |
54 | ||
55 | ENTRY(memccpy) | |
8ee35515 UD |
56 | .prologue |
57 | alloc r2 = ar.pfs, 4, 40 - 4, 0, 40 | |
5bfc4252 UD |
58 | |
59 | #include "softpipe.h" | |
e352f0d8 | 60 | .rotr r[MEMLAT + 7], tmp1[4], tmp2[4], val[4], tmp3[2], pos0[2] |
5bfc4252 UD |
61 | .rotp p[MEMLAT + 6 + 1] |
62 | ||
63 | mov ret0 = r0 // return NULL if no match | |
8ee35515 | 64 | .save pr, saved_pr |
5bfc4252 | 65 | mov saved_pr = pr // save the predicate registers |
e352f0d8 | 66 | mov dest = in0 // dest |
8ee35515 | 67 | .save ar.lc, saved_lc |
5bfc4252 | 68 | mov saved_lc = ar.lc // save the loop counter |
e352f0d8 | 69 | mov saved_ec = ar.ec // save the loop counter |
8ee35515 | 70 | .body |
5bfc4252 UD |
71 | mov src = in1 // src |
72 | extr.u char = in2, 0, 8 // char | |
73 | mov len = in3 // len | |
74 | sub tmp = r0, in0 // tmp = -dest | |
75 | cmp.ne p7, p0 = r0, r0 // clear p7 | |
76 | ;; | |
e352f0d8 | 77 | and loopcnt = 7, tmp // loopcnt = -dest % 8 |
5bfc4252 | 78 | cmp.ge p6, p0 = OP_T_THRES, len // is len <= OP_T_THRES |
e352f0d8 UD |
79 | mov ar.ec = 0 // ec not guaranteed zero on entry |
80 | (p6) br.cond.spnt .cpyfew // copy byte by byte | |
5bfc4252 UD |
81 | ;; |
82 | cmp.eq p6, p0 = loopcnt, r0 | |
83 | mux1 charx8 = char, @brcst | |
84 | (p6) br.cond.sptk .dest_aligned | |
85 | sub len = len, loopcnt // len -= -dest % 8 | |
86 | adds loopcnt = -1, loopcnt // --loopcnt | |
87 | ;; | |
88 | mov ar.lc = loopcnt | |
89 | .l1: // copy -dest % 8 bytes | |
90 | ld1 value = [src], 1 // value = *src++ | |
91 | ;; | |
a1ce647f | 92 | st1 [dest] = value, 1 // *dest++ = value |
5bfc4252 UD |
93 | cmp.eq p6, p0 = value, char |
94 | (p6) br.cond.spnt .foundit | |
a1ce647f | 95 | br.cloop.dptk .l1 |
5bfc4252 UD |
96 | .dest_aligned: |
97 | and sh1 = 7, src // sh1 = src % 8 | |
98 | and tmp = -8, len // tmp = len & -OPSIZ | |
99 | and asrc = -8, src // asrc = src & -OPSIZ -- align src | |
100 | shr.u loopcnt = len, 3 // loopcnt = len / 8 | |
101 | and len = 7, len ;; // len = len % 8 | |
102 | shl sh1 = sh1, 3 // sh1 = 8 * (src % 8) | |
103 | adds loopcnt = -1, loopcnt // --loopcnt | |
104 | mov pr.rot = 1 << 16 ;; // set rotating predicates | |
105 | sub sh2 = 64, sh1 // sh2 = 64 - sh1 | |
106 | mov ar.lc = loopcnt // set LC | |
107 | cmp.eq p6, p0 = sh1, r0 // is the src aligned? | |
108 | (p6) br.cond.sptk .src_aligned ;; | |
109 | add src = src, tmp // src += len & -OPSIZ | |
110 | mov ar.ec = MEMLAT + 6 + 1 // six more passes needed | |
111 | ld8 r[1] = [asrc], 8 // r[1] = w0 | |
112 | cmp.ne p6, p0 = r0, r0 ;; // clear p6 | |
113 | .align 32 | |
a1ce647f | 114 | .l2: |
e352f0d8 | 115 | (p[0]) ld8.s r[0] = [asrc], 8 // r[0] = w1 |
5bfc4252 UD |
116 | (p[MEMLAT]) shr.u tmp1[0] = r[1 + MEMLAT], sh1 // tmp1 = w0 >> sh1 |
117 | (p[MEMLAT]) shl tmp2[0] = r[0 + MEMLAT], sh2 // tmp2 = w1 << sh2 | |
118 | (p[MEMLAT+4]) xor tmp3[0] = val[1], charx8 | |
119 | (p[MEMLAT+5]) czx1.r pos0[0] = tmp3[1] | |
e352f0d8 UD |
120 | (p[MEMLAT+6]) chk.s r[6 + MEMLAT], .recovery1 // our data isn't |
121 | // valid - rollback! | |
5bfc4252 UD |
122 | (p[MEMLAT+6]) cmp.ne p6, p0 = 8, pos0[1] |
123 | (p6) br.cond.spnt .gotit | |
124 | (p[MEMLAT+6]) st8 [dest] = val[3], 8 // store val to dest | |
125 | (p[MEMLAT+3]) or val[0] = tmp1[3], tmp2[3] // val = tmp1 | tmp2 | |
126 | br.ctop.sptk .l2 | |
127 | br.cond.sptk .cpyfew | |
e352f0d8 | 128 | |
5bfc4252 UD |
129 | .src_aligned: |
130 | cmp.ne p6, p0 = r0, r0 // clear p6 | |
131 | mov ar.ec = MEMLAT + 2 + 1 ;; // set EC | |
132 | .l3: | |
e352f0d8 | 133 | (p[0]) ld8.s r[0] = [src], 8 |
5bfc4252 UD |
134 | (p[MEMLAT]) xor tmp3[0] = r[MEMLAT], charx8 |
135 | (p[MEMLAT+1]) czx1.r pos0[0] = tmp3[1] | |
136 | (p[MEMLAT+2]) cmp.ne p7, p0 = 8, pos0[1] | |
e352f0d8 | 137 | (p[MEMLAT+2]) chk.s r[MEMLAT+2], .recovery2 |
5bfc4252 | 138 | (p7) br.cond.spnt .gotit |
e352f0d8 | 139 | .back2: |
5bfc4252 UD |
140 | (p[MEMLAT+2]) st8 [dest] = r[MEMLAT+2], 8 |
141 | br.ctop.dptk .l3 | |
142 | .cpyfew: | |
143 | cmp.eq p6, p0 = len, r0 // is len == 0 ? | |
144 | adds len = -1, len // --len; | |
145 | (p6) br.cond.spnt .restore_and_exit ;; | |
146 | mov ar.lc = len | |
147 | .l4: | |
148 | ld1 value = [src], 1 | |
149 | ;; | |
150 | st1 [dest] = value, 1 | |
151 | cmp.eq p6, p0 = value, char | |
152 | (p6) br.cond.spnt .foundit | |
153 | br.cloop.dptk .l4 ;; | |
154 | .foundit: | |
155 | (p6) mov ret0 = dest | |
156 | .restore_and_exit: | |
5bfc4252 UD |
157 | mov pr = saved_pr, -1 // restore the predicate registers |
158 | mov ar.lc = saved_lc // restore the loop counter | |
e352f0d8 | 159 | mov ar.ec = saved_ec ;; // restore the epilog counter |
5bfc4252 UD |
160 | br.ret.sptk.many b0 |
161 | .gotit: | |
a1ce647f | 162 | .pred.rel "mutex" p6, p7 |
5bfc4252 UD |
163 | (p6) mov value = val[3] // if coming from l2 |
164 | (p7) mov value = r[MEMLAT+2] // if coming from l3 | |
165 | mov ar.lc = pos0[1] ;; | |
166 | .l5: | |
167 | extr.u tmp = value, 0, 8 ;; | |
168 | st1 [dest] = tmp, 1 | |
169 | shr.u value = value, 8 | |
170 | br.cloop.sptk .l5 ;; | |
171 | mov ret0 = dest | |
5bfc4252 UD |
172 | mov pr = saved_pr, -1 |
173 | mov ar.lc = saved_lc | |
a1ce647f | 174 | br.ret.sptk.many b0 |
e352f0d8 UD |
175 | |
176 | .recovery1: | |
177 | adds src = -(MEMLAT + 6 + 1) * 8, asrc | |
178 | mov loopcnt = ar.lc | |
179 | mov tmp = ar.ec ;; | |
180 | sub sh1 = (MEMLAT + 6 + 1), tmp | |
181 | shr.u sh2 = sh2, 3 | |
182 | ;; | |
183 | shl loopcnt = loopcnt, 3 | |
184 | sub src = src, sh2 | |
185 | shl sh1 = sh1, 3 | |
186 | shl tmp = tmp, 3 | |
187 | ;; | |
188 | add len = len, loopcnt | |
189 | add src = sh1, src ;; | |
190 | add len = tmp, len | |
191 | .back1: | |
192 | br.cond.sptk .cpyfew | |
193 | ||
194 | .recovery2: | |
195 | add tmp = -(MEMLAT + 3) * 8, src | |
196 | (p7) br.cond.spnt .gotit | |
197 | ;; | |
198 | ld8 r[MEMLAT+2] = [tmp] ;; | |
199 | xor pos0[1] = r[MEMLAT+2], charx8 ;; | |
200 | czx1.r pos0[1] = pos0[1] ;; | |
201 | cmp.ne p7, p6 = 8, pos0[1] | |
202 | (p7) br.cond.spnt .gotit | |
203 | br.cond.sptk .back2 | |
5bfc4252 | 204 | END(memccpy) |