1 /* Optimized 64-bit memset implementation for POWER6.
2 Copyright (C) 1997-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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/>. */
21 /* void * [r3] memset (void *s [r3], int c [r4], size_t n [r5]));
24 The memset is done in three sizes: byte (8 bits), word (32 bits),
25 cache line (256 bits). There is a special case for setting cache lines
26 to 0, to take advantage of the dcbz instruction. */
29 # define MEMSET memset
32 ENTRY_TOCLESS (MEMSET, 7)
36 #define rRTN r3 /* Initial value of 1st argument. */
37 #define rMEMP0 r3 /* Original value of 1st arg. */
38 #define rCHR r4 /* Char to set in each byte. */
39 #define rLEN r5 /* Length of region to set. */
40 #define rMEMP r6 /* Address at which we are storing. */
41 #define rALIGN r7 /* Number of bytes we are setting now (when aligning). */
43 #define rMEMP3 r9 /* Alt mem pointer. */
45 /* Take care of case for size <= 4. */
47 andi. rALIGN, rMEMP0, 7
51 /* Align to doubleword boundary. */
53 insrdi rCHR, rCHR, 8, 48 /* Replicate byte to halfword. */
56 subfic rALIGN, rALIGN, 8
57 cror 28,30,31 /* Detect odd word aligned. */
58 add rMEMP, rMEMP, rALIGN
59 sub rLEN, rLEN, rALIGN
60 insrdi rCHR, rCHR, 16, 32 /* Replicate halfword to word. */
62 /* Process the even word of doubleword. */
71 /* Process the odd word of doubleword. */
73 bf 28, L(g4x) /* If false, word aligned on odd word. */
80 /* Handle the case of size < 31. */
82 insrdi rCHR, rCHR, 16, 32 /* Replicate halfword to word. */
86 /* Align to 32-byte boundary. */
87 andi. rALIGN, rMEMP, 0x18
88 subfic rALIGN, rALIGN, 0x20
89 insrdi rCHR, rCHR, 32, 0 /* Replicate word to double word. */
92 add rMEMP, rMEMP, rALIGN
93 sub rLEN, rLEN, rALIGN
94 cmplwi cr1, rALIGN, 0x10
100 stdu rCHR, -16(rMEMP2)
103 /* Now aligned to a 32 byte boundary. */
107 clrrdi. rALIGN, rLEN, 5
109 beq cr1, L(zloopstart) /* Special case for clearing memory using dcbz. */
110 beq L(medium) /* We may not actually get to do a full line. */
112 /* Storing a non-zero "c" value. We are aligned at a sector (32-byte)
113 boundary may not be at cache line (128-byte) boundary. */
115 /* memset in 32-byte chunks until we get to a cache line boundary.
116 If rLEN is less than the distance to the next cache-line boundary use
117 cacheAligned1 code to finish the tail. */
121 blt cr1,L(cacheAligned1)
123 beq L(nzCacheAligned)
129 andi. rTMP,rMEMP3,127
132 beq L(nzCacheAligned)
141 beq L(nzCacheAligned)
151 blt cr1,L(cacheAligned1)
152 b L(nzCacheAligned128)
154 /* Now we are aligned to the cache line and can use dcbtst. */
158 blt cr1,L(cacheAligned1)
159 b L(nzCacheAligned128)
161 L(nzCacheAligned128):
182 bge cr1,L(nzCacheAligned128)
186 /* Storing a zero "c" value. We are aligned at a sector (32-byte)
187 boundary but may not be at cache line (128-byte) boundary. If the
188 remaining length spans a full cache line we can use the Data cache
189 block zero instruction. */
191 /* memset in 32-byte chunks until we get to a cache line boundary.
192 If rLEN is less than the distance to the next cache-line boundary use
193 cacheAligned1 code to finish the tail. */
199 blt cr1,L(cacheAligned1)
207 andi. rTMP,rMEMP3,127
229 blt cr1,L(cacheAligned1)
230 blt cr6,L(cacheAligned128)
233 /* Now we are aligned to the cache line and can use dcbz. */
238 blt cr1,L(cacheAligned1)
242 blt cr6,L(cacheAligned128)
243 bgt cr5,L(cacheAligned512)
250 blt cr1,L(cacheAligned1)
251 blt cr6,L(cacheAligned128)
254 /* A simple loop for the longer (>640 bytes) lengths. This form limits
255 the branch miss-predicted to exactly 1 at loop exit.*/
258 blt cr1,L(cacheAligned1)
274 bge cr6,L(cacheAligned256)
276 blt cr1,L(cacheAligned1)
285 blt cr1,L(handletail32)
295 blt cr1,L(handletail32)
305 blt cr1,L(handletail32)
313 /* We are here because the length or remainder (rLEN) is less than the
314 cache line/sector size and does not justify aggressive loop unrolling.
315 So set up the preconditions for L(medium) and go there. */
324 /* Memset of 8 bytes or less. */
347 /* Memset of 0-31 bytes. */
350 insrdi rCHR, rCHR, 32, 0 /* Replicate word to double word. */
353 add rMEMP, rMEMP, rLEN
355 bt- 31, L(medium_31t)
356 bt- 30, L(medium_30t)
360 bge cr1, L(medium_27t)
367 bf- 30, L(medium_30f)
370 bf- 29, L(medium_29f)
373 blt cr1, L(medium_27f)
376 stdu rCHR, -16(rMEMP)
382 END_GEN_TB (MEMSET,TB_TOCLESS)
383 libc_hidden_builtin_def (memset)