1 /* Optimized memset implementation for PowerPC64.
2 Copyright (C) 1997-2017 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 <http://www.gnu.org/licenses/>. */
23 .tc __cache_line_size[TC],__cache_line_size
27 /* __ptr_t [r3] memset (__ptr_t s [r3], int c [r4], size_t n [r5]));
30 The memset is done in three sizes: byte (8 bits), word (32 bits),
31 cache line (256 bits). There is a special case for setting cache lines
32 to 0, to take advantage of the dcbz instruction. */
35 # define MEMSET memset
42 #define rRTN r3 /* Initial value of 1st argument. */
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. */
47 #define rALIGN r7 /* Number of bytes we are setting now (when aligning). */
50 #define rNEG64 r8 /* Constant -64 for clearing with dcbz. */
51 #define rCLS r8 /* Cache line size obtained from static. */
52 #define rCLM r9 /* Cache line size mask to check for cache alignment. */
54 /* Take care of case for size <= 4. */
56 andi. rALIGN, rMEMP0, 7
60 /* Align to doubleword boundary. */
62 insrdi rCHR, rCHR, 8, 48 /* Replicate byte to halfword. */
65 subfic rALIGN, rALIGN, 8
66 cror 28,30,31 /* Detect odd word aligned. */
67 add rMEMP, rMEMP, rALIGN
68 sub rLEN, rLEN, rALIGN
69 insrdi rCHR, rCHR, 16, 32 /* Replicate halfword to word. */
71 /* Process the even word of doubleword. */
80 /* Process the odd word of doubleword. */
82 bf 28, L(g4x) /* If false, word aligned on odd word. */
89 /* Handle the case of size < 31. */
91 insrdi rCHR, rCHR, 16, 32 /* Replicate halfword to word. */
95 /* Align to 32-byte boundary. */
96 andi. rALIGN, rMEMP, 0x18
97 subfic rALIGN, rALIGN, 0x20
98 insrdi rCHR, rCHR, 32, 0 /* Replicate word to double word. */
101 add rMEMP, rMEMP, rALIGN
102 sub rLEN, rLEN, rALIGN
103 cmplwi cr1, rALIGN, 0x10
106 stdu rCHR, -8(rMEMP2)
107 L(a1): blt cr1, L(a2)
109 stdu rCHR, -16(rMEMP2)
112 /* Now aligned to a 32 byte boundary. */
115 clrrdi. rALIGN, rLEN, 5
117 beq cr1, L(zloopstart) /* Special case for clearing memory using dcbz. */
121 beq L(medium) /* We may not actually get to do a full line. */
122 clrldi. rLEN, rLEN, 59
123 add rMEMP, rMEMP, rALIGN
127 L(c3): dcbtst rNEG64, rMEMP
131 stdu rCHR, -32(rMEMP)
138 stdu rCHR, -32(rMEMP)
140 add rMEMP, rMEMP, rALIGN
144 /* Clear lines of memory in 128-byte chunks. */
146 /* If the remaining length is less the 32 bytes, don't bother getting
147 the cache line size. */
151 /* If the cache line size was not set just goto to L(nondcbz) which is
152 safe for any cache line size. */
157 /* Now we know the cache line size, and it is not 32-bytes, but
158 we may not yet be aligned to the cache line. May have a partial
159 line to fill, so touch it 1st. */
165 blt cr1,L(handletail32)
175 /* Now we are aligned to the cache line and can use dcbz. */
178 blt cr1,L(handletail32)
184 /* We are here because the cache line size was set and was not 32-bytes
185 and the remainder (rLEN) is less than the actual cache line size.
186 So set up the preconditions for L(nondcbz) and go there. */
188 clrrwi. rALIGN, rLEN, 5
193 /* Memset of 8 bytes or less. */
216 /* Memset of 0-31 bytes. */
219 insrdi rCHR, rCHR, 32, 0 /* Replicate word to double word. */
222 add rMEMP, rMEMP, rLEN
224 bt- 31, L(medium_31t)
225 bt- 30, L(medium_30t)
227 bt- 29, L(medium_29t)
229 bge- cr1, L(medium_27t)
236 bf- 30, L(medium_30f)
239 bf- 29, L(medium_29f)
242 blt- cr1, L(medium_27f)
245 stdu rCHR, -16(rMEMP)
251 END_GEN_TB (MEMSET,TB_TOCLESS)
252 libc_hidden_builtin_def (memset)
254 #ifndef NO_BZERO_IMPL
255 /* Copied from bzero.S to prevent the linker from inserting a stub
256 between bzero and memset. */
262 END_GEN_TB (__bzero,TB_TOCLESS)
264 weak_alias (__bzero, bzero)