1 /* Optimized version of the standard memset() function.
2 This file is part of the GNU C Library.
3 Copyright (C) 2000-2024 Free Software Foundation, Inc.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
26 The algorithm is fairly straightforward: set byte by byte until we
27 we get to a 16B-aligned address, then loop on 128 B chunks using an
28 early store as prefetching, then loop on 32B chucks, then clear remaining
29 words, finally clear remaining bytes.
30 Since a stf.spill f0 can store 16B in one go, we use this instruction
31 to get peak speed when value = 0. */
53 // This routine uses only scratch predicate registers (p6 - p15)
54 #define p_scr p6 // default register for same-cycle branches
68 #define LSIZE_SH 7 // shift amount
75 #elif defined(USE_FLP)
84 alloc tmp = ar.pfs, 3, 0, 0, 0
90 mov ret0 = dest // return value
91 cmp.ne p_nz, p_zr = value, r0 // use stf.spill if value is zero
92 cmp.eq p_scr, p0 = cnt, r0
95 and ptr2 = -(MIN1+1), dest // aligned address
96 and tmp = MIN1, dest // prepare to check for alignment
97 tbit.nz p_y, p_n = dest, 0 // Do we have an odd address? (M_B_U)
100 mux1 value = value, @brcst // create 8 identical bytes in word
101 (p_scr) br.ret.dpnt.many rp // return immediately if count = 0
104 cmp.ne p_unalgn, p0 = tmp, r0
105 } { .mib // NB: # of bytes to move is 1 higher
106 sub bytecnt = (MIN1+1), tmp // than loopcnt
107 cmp.gt p_scr, p0 = 16, cnt // is it a minimalistic task?
108 (p_scr) br.cond.dptk.many .move_bytes_unaligned // go move just a few (M_B_U)
111 (p_unalgn) add ptr1 = (MIN1+1), ptr2 // after alignment
112 (p_unalgn) add ptr2 = MIN1P1HALF, ptr2 // after alignment
113 (p_unalgn) tbit.nz.unc p_y, p_n = bytecnt, 3 // should we do a st8 ?
116 (p_y) add cnt = -8, cnt
117 (p_unalgn) tbit.nz.unc p_yy, p_nn = bytecnt, 2 // should we do a st4 ?
119 (p_y) st8 [ptr2] = value, -4
120 (p_n) add ptr2 = 4, ptr2
123 (p_yy) add cnt = -4, cnt
124 (p_unalgn) tbit.nz.unc p_y, p_n = bytecnt, 1 // should we do a st2 ?
126 (p_yy) st4 [ptr2] = value, -2
127 (p_nn) add ptr2 = 2, ptr2
130 mov tmp = LINE_SIZE+1 // for compare
131 (p_y) add cnt = -2, cnt
132 (p_unalgn) tbit.nz.unc p_yy, p_nn = bytecnt, 0 // should we do a st1 ?
134 setf.sig fvalue=value // transfer value to FLP side
135 (p_y) st2 [ptr2] = value, -1
136 (p_n) add ptr2 = 1, ptr2
140 (p_yy) st1 [ptr2] = value
141 cmp.gt p_scr, p0 = tmp, cnt // is it a minimalistic task?
143 (p_yy) add cnt = -1, cnt
144 (p_scr) br.cond.dpnt.many .fraction_of_line // go move just a few
149 shr.u linecnt = cnt, LSIZE_SH
150 (p_zr) br.cond.dptk.many .l1b // Jump to use stf.spill
153 #ifndef GAS_ALIGN_BREAKS_UNWIND_INFO
154 .align 32 // -------- // L1A: store ahead into cache lines; fill later
157 and tmp = -(LINE_SIZE), cnt // compute end of range
158 mov ptr9 = ptr1 // used for prefetching
159 and cnt = (LINE_SIZE-1), cnt // remainder
161 mov loopcnt = PREF_AHEAD-1 // default prefetch loop
162 cmp.gt p_scr, p0 = PREF_AHEAD, linecnt // check against actual value
165 (p_scr) add loopcnt = -1, linecnt // start of stores
166 add ptr2 = 8, ptr1 // (beyond prefetch stores)
167 add ptr1 = tmp, ptr1 // first address beyond total
170 add tmp = -1, linecnt // next loop count
171 movi0 ar.lc = loopcnt
175 store [ptr9] = myval, 128 // Do stores one cache line apart
177 br.cloop.dptk.few .pref_l1a
180 add ptr0 = 16, ptr2 // Two stores in parallel
185 store [ptr2] = myval, 8
186 store [ptr0] = myval, 8
189 store [ptr2] = myval, 24
190 store [ptr0] = myval, 24
193 store [ptr2] = myval, 8
194 store [ptr0] = myval, 8
197 store [ptr2] = myval, 24
198 store [ptr0] = myval, 24
201 store [ptr2] = myval, 8
202 store [ptr0] = myval, 8
205 store [ptr2] = myval, 24
206 store [ptr0] = myval, 24
209 store [ptr2] = myval, 8
210 store [ptr0] = myval, 32
211 cmp.lt p_scr, p0 = ptr9, ptr1 // do we need more prefetching?
214 store [ptr2] = myval, 24
215 (p_scr) store [ptr9] = myval, 128
216 br.cloop.dptk.few .l1ax
219 cmp.le p_scr, p0 = 8, cnt // just a few bytes left ?
220 (p_scr) br.cond.dpnt.many .fraction_of_line // Branch no. 2
221 br.cond.dpnt.many .move_bytes_from_alignment // Branch no. 3
224 #ifdef GAS_ALIGN_BREAKS_UNWIND_INFO
229 .l1b: // ------------------ // L1B: store ahead into cache lines; fill later
231 and tmp = -(LINE_SIZE), cnt // compute end of range
232 mov ptr9 = ptr1 // used for prefetching
233 and cnt = (LINE_SIZE-1), cnt // remainder
235 mov loopcnt = PREF_AHEAD-1 // default prefetch loop
236 cmp.gt p_scr, p0 = PREF_AHEAD, linecnt // check against actual value
239 (p_scr) add loopcnt = -1, linecnt
240 add ptr2 = 16, ptr1 // start of stores (beyond prefetch stores)
241 add ptr1 = tmp, ptr1 // first address beyond total range
244 add tmp = -1, linecnt // next loop count
245 movi0 ar.lc = loopcnt
249 stf.spill [ptr9] = f0, 128 // Do stores one cache line apart
251 br.cloop.dptk.few .pref_l1b
254 add ptr0 = 16, ptr2 // Two stores in parallel
259 stf.spill [ptr2] = f0, 32
260 stf.spill [ptr0] = f0, 32
263 stf.spill [ptr2] = f0, 32
264 stf.spill [ptr0] = f0, 32
267 stf.spill [ptr2] = f0, 32
268 stf.spill [ptr0] = f0, 64
269 cmp.lt p_scr, p0 = ptr9, ptr1 // do we need more prefetching?
272 stf.spill [ptr2] = f0, 32
273 (p_scr) stf.spill [ptr9] = f0, 128
274 br.cloop.dptk.few .l1bx
277 cmp.gt p_scr, p0 = 8, cnt // just a few bytes left ?
278 (p_scr) br.cond.dpnt.many .move_bytes_from_alignment
284 shr.u loopcnt = cnt, 5 // loopcnt = cnt / 32
287 cmp.eq p_scr, p0 = loopcnt, r0
288 add loopcnt = -1, loopcnt
289 (p_scr) br.cond.dpnt.many store_words
292 and cnt = 0x1f, cnt // compute the remaining cnt
293 movi0 ar.lc = loopcnt
295 #ifndef GAS_ALIGN_BREAKS_UNWIND_INFO
298 .l2: // ---------------------------- // L2A: store 32B in 2 cycles
300 store [ptr1] = myval, 8
301 store [ptr2] = myval, 8
303 store [ptr1] = myval, 24
304 store [ptr2] = myval, 24
305 br.cloop.dptk.many .l2
309 cmp.gt p_scr, p0 = 8, cnt // just a few bytes left ?
310 (p_scr) br.cond.dpnt.many .move_bytes_from_alignment // Branch
314 store [ptr1] = myval, 8 // store
315 cmp.le p_y, p_n = 16, cnt //
316 add cnt = -8, cnt // subtract
319 (p_y) store [ptr1] = myval, 8 // store
320 (p_y) cmp.le.unc p_yy, p_nn = 16, cnt //
321 (p_y) add cnt = -8, cnt // subtract
324 (p_yy) store [ptr1] = myval, 8 //
325 (p_yy) add cnt = -8, cnt // subtract
328 .move_bytes_from_alignment:
330 cmp.eq p_scr, p0 = cnt, r0
331 tbit.nz.unc p_y, p0 = cnt, 2 // should we terminate with a st4 ?
332 (p_scr) br.cond.dpnt.few .restore_and_exit
335 (p_y) st4 [ptr1] = value, 4
336 tbit.nz.unc p_yy, p0 = cnt, 1 // should we terminate with a st2 ?
339 (p_yy) st2 [ptr1] = value, 2
340 tbit.nz.unc p_y, p0 = cnt, 0
344 (p_y) st1 [ptr1] = value
349 movi0 ar.lc = save_lc
353 .move_bytes_unaligned:
355 .pred.rel "mutex",p_y, p_n
356 .pred.rel "mutex",p_yy, p_nn
357 (p_n) cmp.le p_yy, p_nn = 4, cnt
358 (p_y) cmp.le p_yy, p_nn = 5, cnt
359 (p_n) add ptr2 = 2, ptr1
361 (p_y) add ptr2 = 3, ptr1
362 (p_y) st1 [ptr1] = value, 1 // fill 1 (odd-aligned) byte
363 (p_y) add cnt = -1, cnt // [15, 14 (or less) left]
366 (p_yy) cmp.le.unc p_y, p0 = 8, cnt
367 add ptr3 = ptr1, cnt // prepare last store
368 movi0 ar.lc = save_lc
370 (p_yy) st2 [ptr1] = value, 4 // fill 2 (aligned) bytes
371 (p_yy) st2 [ptr2] = value, 4 // fill 2 (aligned) bytes
372 (p_yy) add cnt = -4, cnt // [11, 10 (o less) left]
375 (p_y) cmp.le.unc p_yy, p0 = 8, cnt
376 add ptr3 = -1, ptr3 // last store
377 tbit.nz p_scr, p0 = cnt, 1 // will there be a st2 at the end ?
379 (p_y) st2 [ptr1] = value, 4 // fill 2 (aligned) bytes
380 (p_y) st2 [ptr2] = value, 4 // fill 2 (aligned) bytes
381 (p_y) add cnt = -4, cnt // [7, 6 (or less) left]
384 (p_yy) st2 [ptr1] = value, 4 // fill 2 (aligned) bytes
385 (p_yy) st2 [ptr2] = value, 4 // fill 2 (aligned) bytes
386 // [3, 2 (or less) left]
387 tbit.nz p_y, p0 = cnt, 0 // will there be a st1 at the end ?
389 (p_yy) add cnt = -4, cnt
392 (p_scr) st2 [ptr1] = value // fill 2 (aligned) bytes
393 (p_y) st1 [ptr3] = value // fill last byte (using ptr3)
397 libc_hidden_builtin_def (memset)