1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Optimized version of the standard memset() function.
4 Copyright (c) 2002 Hewlett-Packard Co/CERN
5 Sverre Jarp <Sverre.Jarp@cern.ch>
14 The algorithm is fairly straightforward: set byte by byte until we
15 we get to a 16B-aligned address, then loop on 128 B chunks using an
16 early store as prefetching, then loop on 32B chucks, then clear remaining
17 words, finally clear remaining bytes.
18 Since a stf.spill f0 can store 16B in one go, we use this instruction
19 to get peak speed when value = 0. */
21 #include <asm/asmmacro.h>
22 #include <asm/export.h>
42 // This routine uses only scratch predicate registers (p6 - p15)
43 #define p_scr p6 // default register for same-cycle branches
55 #define LSIZE_SH 7 // shift amount
61 alloc tmp = ar.pfs, 3, 0, 0, 0
67 mov ret0 = dest // return value
68 cmp.ne p_nz, p_zr = value, r0 // use stf.spill if value is zero
69 cmp.eq p_scr, p0 = cnt, r0
72 and ptr2 = -(MIN1+1), dest // aligned address
73 and tmp = MIN1, dest // prepare to check for correct alignment
74 tbit.nz p_y, p_n = dest, 0 // Do we have an odd address? (M_B_U)
77 mux1 value = value, @brcst // create 8 identical bytes in word
78 (p_scr) br.ret.dpnt.many rp // return immediately if count = 0
81 cmp.ne p_unalgn, p0 = tmp, r0 //
83 sub bytecnt = (MIN1+1), tmp // NB: # of bytes to move is 1 higher than loopcnt
84 cmp.gt p_scr, p0 = 16, cnt // is it a minimalistic task?
85 (p_scr) br.cond.dptk.many .move_bytes_unaligned // go move just a few (M_B_U)
88 (p_unalgn) add ptr1 = (MIN1+1), ptr2 // after alignment
89 (p_unalgn) add ptr2 = MIN1P1HALF, ptr2 // after alignment
90 (p_unalgn) tbit.nz.unc p_y, p_n = bytecnt, 3 // should we do a st8 ?
93 (p_y) add cnt = -8, cnt //
94 (p_unalgn) tbit.nz.unc p_yy, p_nn = bytecnt, 2 // should we do a st4 ?
96 (p_y) st8 [ptr2] = value,-4 //
97 (p_n) add ptr2 = 4, ptr2 //
100 (p_yy) add cnt = -4, cnt //
101 (p_unalgn) tbit.nz.unc p_y, p_n = bytecnt, 1 // should we do a st2 ?
103 (p_yy) st4 [ptr2] = value,-2 //
104 (p_nn) add ptr2 = 2, ptr2 //
107 mov tmp = LINE_SIZE+1 // for compare
108 (p_y) add cnt = -2, cnt //
109 (p_unalgn) tbit.nz.unc p_yy, p_nn = bytecnt, 0 // should we do a st1 ?
111 setf.sig fvalue=value // transfer value to FLP side
112 (p_y) st2 [ptr2] = value,-1 //
113 (p_n) add ptr2 = 1, ptr2 //
117 (p_yy) st1 [ptr2] = value //
118 cmp.gt p_scr, p0 = tmp, cnt // is it a minimalistic task?
120 (p_yy) add cnt = -1, cnt //
121 (p_scr) br.cond.dpnt.many .fraction_of_line // go move just a few
126 shr.u linecnt = cnt, LSIZE_SH
127 (p_zr) br.cond.dptk.many .l1b // Jump to use stf.spill
130 TEXT_ALIGN(32) // --------------------- // L1A: store ahead into cache lines; fill later
132 and tmp = -(LINE_SIZE), cnt // compute end of range
133 mov ptr9 = ptr1 // used for prefetching
134 and cnt = (LINE_SIZE-1), cnt // remainder
136 mov loopcnt = PREF_AHEAD-1 // default prefetch loop
137 cmp.gt p_scr, p0 = PREF_AHEAD, linecnt // check against actual value
140 (p_scr) add loopcnt = -1, linecnt //
141 add ptr2 = 8, ptr1 // start of stores (beyond prefetch stores)
142 add ptr1 = tmp, ptr1 // first address beyond total range
145 add tmp = -1, linecnt // next loop count
146 mov.i ar.lc = loopcnt //
150 stf8 [ptr9] = fvalue, 128 // Do stores one cache line apart
152 br.cloop.dptk.few .pref_l1a
155 add ptr0 = 16, ptr2 // Two stores in parallel
160 stf8 [ptr2] = fvalue, 8
161 stf8 [ptr0] = fvalue, 8
164 stf8 [ptr2] = fvalue, 24
165 stf8 [ptr0] = fvalue, 24
168 stf8 [ptr2] = fvalue, 8
169 stf8 [ptr0] = fvalue, 8
172 stf8 [ptr2] = fvalue, 24
173 stf8 [ptr0] = fvalue, 24
176 stf8 [ptr2] = fvalue, 8
177 stf8 [ptr0] = fvalue, 8
180 stf8 [ptr2] = fvalue, 24
181 stf8 [ptr0] = fvalue, 24
184 stf8 [ptr2] = fvalue, 8
185 stf8 [ptr0] = fvalue, 32
186 cmp.lt p_scr, p0 = ptr9, ptr1 // do we need more prefetching?
189 stf8 [ptr2] = fvalue, 24
190 (p_scr) stf8 [ptr9] = fvalue, 128
191 br.cloop.dptk.few .l1ax
194 cmp.le p_scr, p0 = 8, cnt // just a few bytes left ?
195 (p_scr) br.cond.dpnt.many .fraction_of_line // Branch no. 2
196 br.cond.dpnt.many .move_bytes_from_alignment // Branch no. 3
200 .l1b: // ------------------------------------ // L1B: store ahead into cache lines; fill later
202 and tmp = -(LINE_SIZE), cnt // compute end of range
203 mov ptr9 = ptr1 // used for prefetching
204 and cnt = (LINE_SIZE-1), cnt // remainder
206 mov loopcnt = PREF_AHEAD-1 // default prefetch loop
207 cmp.gt p_scr, p0 = PREF_AHEAD, linecnt // check against actual value
210 (p_scr) add loopcnt = -1, linecnt
211 add ptr2 = 16, ptr1 // start of stores (beyond prefetch stores)
212 add ptr1 = tmp, ptr1 // first address beyond total range
215 add tmp = -1, linecnt // next loop count
216 mov.i ar.lc = loopcnt
220 stf.spill [ptr9] = f0, 128 // Do stores one cache line apart
222 br.cloop.dptk.few .pref_l1b
225 add ptr0 = 16, ptr2 // Two stores in parallel
230 stf.spill [ptr2] = f0, 32
231 stf.spill [ptr0] = f0, 32
234 stf.spill [ptr2] = f0, 32
235 stf.spill [ptr0] = f0, 32
238 stf.spill [ptr2] = f0, 32
239 stf.spill [ptr0] = f0, 64
240 cmp.lt p_scr, p0 = ptr9, ptr1 // do we need more prefetching?
243 stf.spill [ptr2] = f0, 32
244 (p_scr) stf.spill [ptr9] = f0, 128
245 br.cloop.dptk.few .l1bx
248 cmp.gt p_scr, p0 = 8, cnt // just a few bytes left ?
249 (p_scr) br.cond.dpnt.many .move_bytes_from_alignment //
255 shr.u loopcnt = cnt, 5 // loopcnt = cnt / 32
258 cmp.eq p_scr, p0 = loopcnt, r0
259 add loopcnt = -1, loopcnt
260 (p_scr) br.cond.dpnt.many .store_words
263 and cnt = 0x1f, cnt // compute the remaining cnt
264 mov.i ar.lc = loopcnt
267 .l2: // ------------------------------------ // L2A: store 32B in 2 cycles
269 stf8 [ptr1] = fvalue, 8
270 stf8 [ptr2] = fvalue, 8
272 stf8 [ptr1] = fvalue, 24
273 stf8 [ptr2] = fvalue, 24
274 br.cloop.dptk.many .l2
278 cmp.gt p_scr, p0 = 8, cnt // just a few bytes left ?
279 (p_scr) br.cond.dpnt.many .move_bytes_from_alignment // Branch
283 stf8 [ptr1] = fvalue, 8 // store
284 cmp.le p_y, p_n = 16, cnt
285 add cnt = -8, cnt // subtract
288 (p_y) stf8 [ptr1] = fvalue, 8 // store
289 (p_y) cmp.le.unc p_yy, p_nn = 16, cnt
290 (p_y) add cnt = -8, cnt // subtract
293 (p_yy) stf8 [ptr1] = fvalue, 8
294 (p_yy) add cnt = -8, cnt // subtract
297 .move_bytes_from_alignment:
299 cmp.eq p_scr, p0 = cnt, r0
300 tbit.nz.unc p_y, p0 = cnt, 2 // should we terminate with a st4 ?
301 (p_scr) br.cond.dpnt.few .restore_and_exit
304 (p_y) st4 [ptr1] = value,4
305 tbit.nz.unc p_yy, p0 = cnt, 1 // should we terminate with a st2 ?
308 (p_yy) st2 [ptr1] = value,2
309 tbit.nz.unc p_y, p0 = cnt, 0 // should we terminate with a st1 ?
313 (p_y) st1 [ptr1] = value
318 mov.i ar.lc = save_lc
322 .move_bytes_unaligned:
324 .pred.rel "mutex",p_y, p_n
325 .pred.rel "mutex",p_yy, p_nn
326 (p_n) cmp.le p_yy, p_nn = 4, cnt
327 (p_y) cmp.le p_yy, p_nn = 5, cnt
328 (p_n) add ptr2 = 2, ptr1
330 (p_y) add ptr2 = 3, ptr1
331 (p_y) st1 [ptr1] = value, 1 // fill 1 (odd-aligned) byte [15, 14 (or less) left]
332 (p_y) add cnt = -1, cnt
335 (p_yy) cmp.le.unc p_y, p0 = 8, cnt
336 add ptr3 = ptr1, cnt // prepare last store
337 mov.i ar.lc = save_lc
339 (p_yy) st2 [ptr1] = value, 4 // fill 2 (aligned) bytes
340 (p_yy) st2 [ptr2] = value, 4 // fill 2 (aligned) bytes [11, 10 (o less) left]
341 (p_yy) add cnt = -4, cnt
344 (p_y) cmp.le.unc p_yy, p0 = 8, cnt
345 add ptr3 = -1, ptr3 // last store
346 tbit.nz p_scr, p0 = cnt, 1 // will there be a st2 at the end ?
348 (p_y) st2 [ptr1] = value, 4 // fill 2 (aligned) bytes
349 (p_y) st2 [ptr2] = value, 4 // fill 2 (aligned) bytes [7, 6 (or less) left]
350 (p_y) add cnt = -4, cnt
353 (p_yy) st2 [ptr1] = value, 4 // fill 2 (aligned) bytes
354 (p_yy) st2 [ptr2] = value, 4 // fill 2 (aligned) bytes [3, 2 (or less) left]
355 tbit.nz p_y, p0 = cnt, 0 // will there be a st1 at the end ?
357 (p_yy) add cnt = -4, cnt
360 (p_scr) st2 [ptr1] = value // fill 2 (aligned) bytes
361 (p_y) st1 [ptr3] = value // fill last byte (using ptr3)
365 EXPORT_SYMBOL(memset)