1 /* Optimized memset implementation for PowerPC64.
2 Copyright (C) 1997-2022 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/>. */
20 #include <rtld-global-offsets.h>
23 __GLRO_DEF(dl_cache_line_size)
28 /* void * [r3] memset (void *s [r3], int c [r4], size_t n [r5]));
31 The memset is done in three sizes: byte (8 bits), word (32 bits),
32 cache line (256 bits). There is a special case for setting cache lines
33 to 0, to take advantage of the dcbz instruction. */
36 # define MEMSET memset
43 #define rRTN r3 /* Initial value of 1st argument. */
44 #define rMEMP0 r3 /* Original value of 1st arg. */
45 #define rCHR r4 /* Char to set in each byte. */
46 #define rLEN r5 /* Length of region to set. */
47 #define rMEMP r6 /* Address at which we are storing. */
48 #define rALIGN r7 /* Number of bytes we are setting now (when aligning). */
51 #define rNEG64 r8 /* Constant -64 for clearing with dcbz. */
52 #define rCLS r8 /* Cache line size obtained from static. */
53 #define rCLM r9 /* Cache line size mask to check for cache alignment. */
55 /* Take care of case for size <= 4. */
57 andi. rALIGN, rMEMP0, 7
61 /* Align to doubleword boundary. */
63 insrdi rCHR, rCHR, 8, 48 /* Replicate byte to halfword. */
66 subfic rALIGN, rALIGN, 8
67 cror 28,30,31 /* Detect odd word aligned. */
68 add rMEMP, rMEMP, rALIGN
69 sub rLEN, rLEN, rALIGN
70 insrdi rCHR, rCHR, 16, 32 /* Replicate halfword to word. */
72 /* Process the even word of doubleword. */
81 /* Process the odd word of doubleword. */
83 bf 28, L(g4x) /* If false, word aligned on odd word. */
90 /* Handle the case of size < 31. */
92 insrdi rCHR, rCHR, 16, 32 /* Replicate halfword to word. */
96 /* Align to 32-byte boundary. */
97 andi. rALIGN, rMEMP, 0x18
98 subfic rALIGN, rALIGN, 0x20
99 insrdi rCHR, rCHR, 32, 0 /* Replicate word to double word. */
102 add rMEMP, rMEMP, rALIGN
103 sub rLEN, rLEN, rALIGN
104 cmplwi cr1, rALIGN, 0x10
107 stdu rCHR, -8(rMEMP2)
108 L(a1): blt cr1, L(a2)
110 stdu rCHR, -16(rMEMP2)
113 /* Now aligned to a 32 byte boundary. */
116 clrrdi. rALIGN, rLEN, 5
118 beq cr1, L(zloopstart) /* Special case for clearing memory using dcbz. */
122 beq L(medium) /* We may not actually get to do a full line. */
123 clrldi. rLEN, rLEN, 59
124 add rMEMP, rMEMP, rALIGN
128 L(c3): dcbtst rNEG64, rMEMP
132 stdu rCHR, -32(rMEMP)
139 stdu rCHR, -32(rMEMP)
141 add rMEMP, rMEMP, rALIGN
145 /* Clear lines of memory in 128-byte chunks. */
147 /* If the remaining length is less the 32 bytes, don't bother getting
148 the cache line size. */
150 /* Read the cache line size. */
151 __GLRO (rCLS, dl_cache_line_size,
152 RTLD_GLOBAL_RO_DL_CACHE_LINE_SIZE_OFFSET)
154 /* If the cache line size was not set just goto to L(nondcbz) which is
155 safe for any cache line size. */
160 /* Now we know the cache line size, and it is not 32-bytes, but
161 we may not yet be aligned to the cache line. May have a partial
162 line to fill, so touch it 1st. */
168 blt cr1,L(handletail32)
178 /* Now we are aligned to the cache line and can use dcbz. */
181 blt cr1,L(handletail32)
187 /* We are here because the cache line size was set and was not 32-bytes
188 and the remainder (rLEN) is less than the actual cache line size.
189 So set up the preconditions for L(nondcbz) and go there. */
191 clrrwi. rALIGN, rLEN, 5
196 /* Memset of 8 bytes or less. */
219 /* Memset of 0-31 bytes. */
222 insrdi rCHR, rCHR, 32, 0 /* Replicate word to double word. */
225 add rMEMP, rMEMP, rLEN
227 bt- 31, L(medium_31t)
228 bt- 30, L(medium_30t)
230 bt- 29, L(medium_29t)
232 bge- cr1, L(medium_27t)
239 bf- 30, L(medium_30f)
242 bf- 29, L(medium_29f)
245 blt- cr1, L(medium_27f)
248 stdu rCHR, -16(rMEMP)
254 END_GEN_TB (MEMSET,TB_TOCLESS)
255 libc_hidden_builtin_def (memset)
257 #ifndef NO_BZERO_IMPL
258 /* Copied from bzero.S to prevent the linker from inserting a stub
259 between bzero and memset. */
265 END_GEN_TB (__bzero,TB_TOCLESS)
267 weak_alias (__bzero, bzero)