1 /* Optimized memset implementation for PowerPC.
2 Copyright (C) 1997-2013 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 /* __ptr_t [r3] memset (__ptr_t s [r3], int c [r4], size_t n [r5]));
26 The memset is done in four sizes: byte (8 bits), word (32 bits),
27 32-byte blocks (256 bits) and __cache_line_size (128, 256, 1024 bits).
28 There is a special case for setting whole cache lines to 0, which
29 takes advantage of the dcbz instruction. */
32 EALIGN (BP_SYM (memset), 5, 1)
35 #define rRTN r3 /* initial value of 1st argument */
36 #if __BOUNDED_POINTERS__
37 # define rMEMP0 r4 /* original value of 1st arg */
38 # define rCHR r5 /* char to set in each byte */
39 # define rLEN r6 /* length of region to set */
40 # define rMEMP r10 /* address at which we are storing */
42 # define rMEMP0 r3 /* original value of 1st arg */
43 # define rCHR r4 /* char to set in each byte */
44 # define rLEN r5 /* length of region to set */
45 # define rMEMP r6 /* address at which we are storing */
47 #define rALIGN r7 /* number of bytes we are setting now (when aligning) */
50 #define rPOS32 r7 /* constant +32 for clearing with dcbz */
51 #define rNEG64 r8 /* constant -64 for clearing with dcbz */
52 #define rNEG32 r9 /* constant -32 for clearing with dcbz */
54 #define rGOT r9 /* Address of the Global Offset Table. */
55 #define rCLS r8 /* Cache line size obtained from static. */
56 #define rCLM r9 /* Cache line size mask to check for cache alignment. */
58 #if __BOUNDED_POINTERS__
60 CHECK_BOUNDS_BOTH_WIDE (rMEMP0, rTMP, rTMP2, rLEN)
62 STORE_RETURN_VALUE (rMEMP0)
63 STORE_RETURN_BOUNDS (rTMP, rTMP2)
67 /* take care of case for size <= 4 */
69 andi. rALIGN, rMEMP0, 3
72 /* align to word boundary */
74 rlwimi rCHR, rCHR, 8, 16, 23
75 beq+ L(aligned) /* 8th instruction from .align */
77 subfic rALIGN, rALIGN, 4
78 add rMEMP, rMEMP, rALIGN
79 sub rLEN, rLEN, rALIGN
83 L(g0): sth rCHR, -2(rMEMP) /* 16th instruction from .align */
84 /* take care of case for size < 31 */
87 rlwimi rCHR, rCHR, 16, 0, 15
89 /* align to cache line boundary... */
90 andi. rALIGN, rMEMP, 0x1C
91 subfic rALIGN, rALIGN, 0x20
94 add rMEMP, rMEMP, rALIGN
95 sub rLEN, rLEN, rALIGN
96 cmplwi cr1, rALIGN, 0x10
100 stwu rCHR, -8(rMEMP2)
101 L(a1): blt cr1, L(a2)
102 stw rCHR, -4(rMEMP2) /* 32nd instruction from .align */
104 stw rCHR, -12(rMEMP2)
105 stwu rCHR, -16(rMEMP2)
106 L(a2): bf 29, L(caligned)
108 /* now aligned to a cache line. */
111 clrrwi. rALIGN, rLEN, 5
112 mtcrf 0x01, rLEN /* 40th instruction from .align */
114 /* Check if we can use the special case for clearing memory using dcbz.
115 This requires that we know the correct cache line size for this
116 processor. Getting the __cache_line_size may require establishing GOT
117 addressability, so branch out of line to set this up. */
118 beq cr1, L(checklinesize)
120 /* Store blocks of 32-bytes (256-bits) starting on a 32-byte boundary.
121 Can't assume that rCHR is zero or that the cache line size is either
122 32-bytes or even known. */
126 beq L(medium) /* we may not actually get to do a full line */
127 clrlwi. rLEN, rLEN, 27
128 add rMEMP, rMEMP, rALIGN
130 bdz L(cloopdone) /* 48th instruction from .align */
132 /* We can't use dcbz here as we don't know the cache line size. We can
133 use "data cache block touch for store", which is safe. */
134 L(c3): dcbtst rNEG64, rMEMP
139 nop /* let 601 fetch last 4 instructions of loop */
141 stw rCHR, -24(rMEMP) /* 56th instruction from .align */
142 nop /* let 601 fetch first 8 instructions of loop */
144 stwu rCHR, -32(rMEMP)
150 stw rCHR, -16(rMEMP) /* 64th instruction from .align */
155 stwu rCHR, -32(rMEMP)
157 add rMEMP, rMEMP, rALIGN
158 b L(medium_tail2) /* 72nd instruction from .align */
162 /* Clear cache lines of memory in 128-byte chunks.
163 This code is optimized for processors with 32-byte cache lines.
164 It is further optimized for the 601 processor, which requires
165 some care in how the code is aligned in the i-cache. */
167 clrlwi rLEN, rLEN, 27
169 srwi. rTMP, rALIGN, 7
173 cmplwi cr1, rLEN, 16 /* 8 */
176 addi rMEMP, rMEMP, 0x20
177 L(z0): li rNEG32, -0x20
181 addi rMEMP, rMEMP, 0x40 /* 16 */
182 L(z1): cmplwi cr5, rLEN, 0
187 addi rMEMP, rMEMP, 0x80
196 /* Memset of 4 bytes or less. */
211 /* Memset of 0-31 bytes. */
216 add rMEMP, rMEMP, rLEN
218 bt- 31, L(medium_31t)
219 bt- 30, L(medium_30t)
221 bt- 29, L(medium_29t)
223 bge- cr1, L(medium_27t)
225 stw rCHR, -4(rMEMP) /* 8th instruction from .align */
231 bf- 30, L(medium_30f)
234 bf- 29, L(medium_29f)
237 blt- cr1, L(medium_27f) /* 16th instruction from .align */
242 stwu rCHR, -16(rMEMP)
253 /* If the remaining length is less the 32 bytes then don't bother getting
254 the cache line size. */
256 /* Establishes GOT addressability so we can load __cache_line_size
257 from static. This value was set from the aux vector during startup. */
258 SETUP_GOT_ACCESS(rGOT,got_label)
259 addis rGOT,rGOT,__cache_line_size-got_label@ha
260 lwz rCLS,__cache_line_size-got_label@l(rGOT)
263 /* Load __cache_line_size from static. This value was set from the
264 aux vector during startup. */
265 lis rCLS,__cache_line_size@ha
266 /* If the remaining length is less the 32 bytes then don't bother getting
267 the cache line size. */
269 lwz rCLS,__cache_line_size@l(rCLS)
272 /* If the cache line size was not set then goto to L(nondcbz), which is
273 safe for any cache line size. */
277 /* If the cache line size is 32 bytes then goto to L(zloopstart),
278 which is coded specifically for 32-byte lines (and 601). */
280 beq cr1,L(zloopstart)
282 /* Now we know the cache line size and it is not 32-bytes. However
283 we may not yet be aligned to the cache line and may have a partial
284 line to fill. Touch it 1st to fetch the cache line. */
291 blt cr1,L(handletail32)
293 /* We are not aligned to start of a cache line yet. Store 32-byte
294 of data and test again. */
307 /* Now we are aligned to the cache line and can use dcbz. */
310 blt cr1,L(handletail32)
316 /* We are here because; the cache line size was set, it was not
317 32-bytes, and the remainder (rLEN) is now less than the actual cache
318 line size. Set up the preconditions for L(nondcbz) and go there to
319 store the remaining bytes. */
321 clrrwi. rALIGN, rLEN, 5
324 END (BP_SYM (memset))
325 libc_hidden_builtin_def (memset)