1 /* Optimized memset implementation for PowerPC64.
2 Copyright (C) 1997, 1999, 2000, 2002, 2003, 2007
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
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.
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
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
24 /* __ptr_t [r3] memset (__ptr_t s [r3], int c [r4], size_t n [r5]));
27 The memset is done in three sizes: byte (8 bits), word (32 bits),
28 cache line (256 bits). There is a special case for setting cache lines
29 to 0, to take advantage of the dcbz instruction. */
32 EALIGN (BP_SYM (memset), 5, 0)
36 #define rRTN r3 /* Initial value of 1st argument. */
37 #if __BOUNDED_POINTERS__
38 # define rMEMP0 r4 /* Original value of 1st arg. */
39 # define rCHR r5 /* Char to set in each byte. */
40 # define rLEN r6 /* Length of region to set. */
41 # define rMEMP r10 /* Address at which we are storing. */
43 # define rMEMP0 r3 /* Original value of 1st arg. */
44 # define rCHR r4 /* Char to set in each byte. */
45 # define rLEN r5 /* Length of region to set. */
46 # 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 #if __BOUNDED_POINTERS__
57 CHECK_BOUNDS_BOTH_WIDE (rMEMP0, rTMP, rTMP2, rLEN)
59 STORE_RETURN_VALUE (rMEMP0)
60 STORE_RETURN_BOUNDS (rTMP, rTMP2)
63 /* Take care of case for size <= 4. */
65 andi. rALIGN, rMEMP0, 7
69 /* Align to doubleword boundary. */
71 rlwimi rCHR, rCHR, 8, 16, 23 /* Replicate byte to halfword. */
74 subfic rALIGN, rALIGN, 8
75 cror 28,30,31 /* Detect odd word aligned. */
76 add rMEMP, rMEMP, rALIGN
77 sub rLEN, rLEN, rALIGN
78 rlwimi rCHR, rCHR, 16, 0, 15 /* Replicate halfword to word. */
80 /* Process the even word of doubleword. */
89 /* Process the odd word of doubleword. */
91 bf 28, L(g4x) /* If false, word aligned on odd word. */
98 /* Handle the case of size < 31. */
100 rlwimi rCHR, rCHR, 16, 0, 15 /* Replicate halfword to word. */
104 /* Align to 32-byte boundary. */
105 andi. rALIGN, rMEMP, 0x18
106 subfic rALIGN, rALIGN, 0x20
107 insrdi rCHR,rCHR,32,0 /* Replicate word to double word. */
110 add rMEMP, rMEMP, rALIGN
111 sub rLEN, rLEN, rALIGN
112 cmplwi cr1, rALIGN, 0x10
115 stdu rCHR, -8(rMEMP2)
116 L(a1): blt cr1, L(a2)
118 stdu rCHR, -16(rMEMP2)
121 /* Now aligned to a 32 byte boundary. */
124 clrrdi. rALIGN, rLEN, 5
126 beq cr1, L(zloopstart) /* Special case for clearing memory using dcbz. */
130 beq L(medium) /* We may not actually get to do a full line. */
131 clrldi. rLEN, rLEN, 59
132 add rMEMP, rMEMP, rALIGN
136 L(c3): dcbtst rNEG64, rMEMP
140 stdu rCHR, -32(rMEMP)
147 stdu rCHR, -32(rMEMP)
149 add rMEMP, rMEMP, rALIGN
153 /* Clear lines of memory in 128-byte chunks. */
155 /* If the remaining length is less the 32 bytes, don't bother getting
156 the cache line size. */
158 li rCLS,128 /* cache line size is 128 */
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. */
167 blt cr1,L(handletail32)
177 /* Now we are aligned to the cache line and can use dcbz. */
180 blt cr1,L(handletail32)
186 /* We are here because the cache line size was set and was not 32-bytes
187 and the remainder (rLEN) is less than the actual cache line size.
188 So set up the preconditions for L(nondcbz) and go there. */
190 clrrwi. rALIGN, rLEN, 5
195 /* Memset of 8 bytes or less. */
218 /* Memset of 0-31 bytes. */
221 insrdi rCHR,rCHR,32,0 /* Replicate word to double word. */
224 add rMEMP, rMEMP, rLEN
226 bt- 31, L(medium_31t)
227 bt- 30, L(medium_30t)
229 bt- 29, L(medium_29t)
231 bge- cr1, L(medium_27t)
238 bf- 30, L(medium_30f)
241 bf- 29, L(medium_29f)
244 blt- cr1, L(medium_27f)
247 stdu rCHR, -16(rMEMP)
253 END_GEN_TB (BP_SYM (memset),TB_TOCLESS)
254 libc_hidden_builtin_def (memset)
256 /* Copied from bzero.S to prevent the linker from inserting a stub
257 between bzero and memset. */
258 ENTRY (BP_SYM (__bzero))
260 #if __BOUNDED_POINTERS__
264 /* Tell memset that we don't want a return value. */
272 END_GEN_TB (BP_SYM (__bzero),TB_TOCLESS)
274 weak_alias (BP_SYM (__bzero), BP_SYM (bzero))