(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / powerpc / powerpc32 / memset.S
blob53f11433202bf348490778bc93488460cbb9b1a6
1 /* Optimized memset implementation for PowerPC.
2    Copyright (C) 1997, 1999, 2000, 2003 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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
20 #include <sysdep.h>
21 #include <bp-sym.h>
22 #include <bp-asm.h>
24 /* Define a global static that can hold the cache line size.  The
25    assumption is that startup code will access the "aux vector" to
26    to obtain the value set by the kernel and store it into this
27    variable.  */
29         .globl __cache_line_size
30         .lcomm __cache_line_size,4,4
32 /* __ptr_t [r3] memset (__ptr_t s [r3], int c [r4], size_t n [r5]));
33    Returns 's'.
35    The memset is done in four sizes: byte (8 bits), word (32 bits),
36    32-byte blocks (256 bits) and __cache_line_size (128, 256, 1024 bits).
37    There is a special case for setting whole cache lines to 0, which
38    takes advantage of the dcbz instruction.  */
40         .section        ".text"
41 EALIGN (BP_SYM (memset), 5, 1)
43 #define rTMP    r0
44 #define rRTN    r3      /* initial value of 1st argument */
45 #if __BOUNDED_POINTERS__
46 # define rMEMP0 r4      /* original value of 1st arg */
47 # define rCHR   r5      /* char to set in each byte */
48 # define rLEN   r6      /* length of region to set */
49 # define rMEMP  r10     /* address at which we are storing */
50 #else
51 # define rMEMP0 r3      /* original value of 1st arg */
52 # define rCHR   r4      /* char to set in each byte */
53 # define rLEN   r5      /* length of region to set */
54 # define rMEMP  r6      /* address at which we are storing */
55 #endif
56 #define rALIGN  r7      /* number of bytes we are setting now (when aligning) */
57 #define rMEMP2  r8
59 #define rPOS32  r7      /* constant +32 for clearing with dcbz */
60 #define rNEG64  r8      /* constant -64 for clearing with dcbz */
61 #define rNEG32  r9      /* constant -32 for clearing with dcbz */
63 #define rGOT    r9      /* Address of the Global Offset Table.  */
64 #define rCLS    r8      /* Cache line size obtained from static.  */
65 #define rCLM    r9      /* Cache line size mask to check for cache alignment.  */
67 #if __BOUNDED_POINTERS__
68         cmplwi  cr1, rRTN, 0
69         CHECK_BOUNDS_BOTH_WIDE (rMEMP0, rTMP, rTMP2, rLEN)
70         beq     cr1, L(b0)
71         STORE_RETURN_VALUE (rMEMP0)
72         STORE_RETURN_BOUNDS (rTMP, rTMP2)
73 L(b0):
74 #endif
76 /* take care of case for size <= 4  */
77         cmplwi  cr1, rLEN, 4
78         andi.   rALIGN, rMEMP0, 3
79         mr      rMEMP, rMEMP0
80         ble-    cr1, L(small)
81 /* align to word boundary  */
82         cmplwi  cr5, rLEN, 31
83         rlwimi  rCHR, rCHR, 8, 16, 23
84         beq+    L(aligned)      /* 8th instruction from .align */
85         mtcrf   0x01, rMEMP0
86         subfic  rALIGN, rALIGN, 4
87         add     rMEMP, rMEMP, rALIGN
88         sub     rLEN, rLEN, rALIGN
89         bf+     31, L(g0)
90         stb     rCHR, 0(rMEMP0)
91         bt      30, L(aligned)
92 L(g0):  sth     rCHR, -2(rMEMP) /* 16th instruction from .align */
93 /* take care of case for size < 31 */
94 L(aligned):
95         mtcrf   0x01, rLEN
96         rlwimi  rCHR, rCHR, 16, 0, 15
97         ble     cr5, L(medium)
98 /* align to cache line boundary...  */
99         andi.   rALIGN, rMEMP, 0x1C
100         subfic  rALIGN, rALIGN, 0x20
101         beq     L(caligned)
102         mtcrf   0x01, rALIGN
103         add     rMEMP, rMEMP, rALIGN
104         sub     rLEN, rLEN, rALIGN
105         cmplwi  cr1, rALIGN, 0x10
106         mr      rMEMP2, rMEMP
107         bf      28, L(a1)
108         stw     rCHR, -4(rMEMP2)
109         stwu    rCHR, -8(rMEMP2)
110 L(a1):  blt     cr1, L(a2)
111         stw     rCHR, -4(rMEMP2) /* 32nd instruction from .align */
112         stw     rCHR, -8(rMEMP2)
113         stw     rCHR, -12(rMEMP2)
114         stwu    rCHR, -16(rMEMP2)
115 L(a2):  bf      29, L(caligned)
116         stw     rCHR, -4(rMEMP2)
117 /* now aligned to a cache line.  */
118 L(caligned):
119         cmplwi  cr1, rCHR, 0
120         clrrwi. rALIGN, rLEN, 5
121         mtcrf   0x01, rLEN      /* 40th instruction from .align */
123 /* Check if we can use the special case for clearing memory using dcbz.
124    This requires that we know the correct cache line size for this
125    processor.  Getting the __cache_line_size may require establishing GOT
126    addressability, so branch out of line to set this up.  */
127         beq     cr1, L(checklinesize)
129 /* Store blocks of 32-bytes (256-bits) starting on a 32-byte boundary.
130    Can't assume that rCHR is zero or that the cache line size is either
131    32-bytes or even known.  */
132 L(nondcbz):
133         srwi    rTMP, rALIGN, 5
134         mtctr   rTMP
135         beq     L(medium)       /* we may not actually get to do a full line */
136         clrlwi. rLEN, rLEN, 27
137         add     rMEMP, rMEMP, rALIGN
138         li      rNEG64, -0x40
139         bdz     L(cloopdone)    /* 48th instruction from .align */
141 /* We can't use dcbz here as we don't know the cache line size.  We can
142    use "data cache block touch for store", which is safe.  */
143 L(c3):  dcbtst rNEG64, rMEMP
144         stw     rCHR, -4(rMEMP)
145         stw     rCHR, -8(rMEMP)
146         stw     rCHR, -12(rMEMP)
147         stw     rCHR, -16(rMEMP)
148         nop                     /* let 601 fetch last 4 instructions of loop */
149         stw     rCHR, -20(rMEMP)
150         stw     rCHR, -24(rMEMP) /* 56th instruction from .align */
151         nop                     /* let 601 fetch first 8 instructions of loop */
152         stw     rCHR, -28(rMEMP)
153         stwu    rCHR, -32(rMEMP)
154         bdnz    L(c3)
155 L(cloopdone):
156         stw     rCHR, -4(rMEMP)
157         stw     rCHR, -8(rMEMP)
158         stw     rCHR, -12(rMEMP)
159         stw     rCHR, -16(rMEMP) /* 64th instruction from .align */
160         stw     rCHR, -20(rMEMP)
161         cmplwi  cr1, rLEN, 16
162         stw     rCHR, -24(rMEMP)
163         stw     rCHR, -28(rMEMP)
164         stwu    rCHR, -32(rMEMP)
165         beqlr
166         add     rMEMP, rMEMP, rALIGN
167         b       L(medium_tail2) /* 72nd instruction from .align */
169         .align 5
170         nop
171 /* Clear cache lines of memory in 128-byte chunks.
172    This code is optimized for processors with 32-byte cache lines.
173    It is further optimized for the 601 processor, which requires
174    some care in how the code is aligned in the i-cache.  */
175 L(zloopstart):
176         clrlwi  rLEN, rLEN, 27
177         mtcrf   0x02, rALIGN
178         srwi.   rTMP, rALIGN, 7
179         mtctr   rTMP
180         li      rPOS32, 0x20
181         li      rNEG64, -0x40
182         cmplwi  cr1, rLEN, 16   /* 8 */
183         bf      26, L(z0)
184         dcbz    0, rMEMP
185         addi    rMEMP, rMEMP, 0x20
186 L(z0):  li      rNEG32, -0x20
187         bf      25, L(z1)
188         dcbz    0, rMEMP
189         dcbz    rPOS32, rMEMP
190         addi    rMEMP, rMEMP, 0x40 /* 16 */
191 L(z1):  cmplwi  cr5, rLEN, 0
192         beq     L(medium)
193 L(zloop):
194         dcbz    0, rMEMP
195         dcbz    rPOS32, rMEMP
196         addi    rMEMP, rMEMP, 0x80
197         dcbz    rNEG64, rMEMP
198         dcbz    rNEG32, rMEMP
199         bdnz    L(zloop)
200         beqlr   cr5
201         b       L(medium_tail2)
203         .align 5
204 L(small):
205 /* Memset of 4 bytes or less.  */
206         cmplwi  cr5, rLEN, 1
207         cmplwi  cr1, rLEN, 3
208         bltlr   cr5
209         stb     rCHR, 0(rMEMP)
210         beqlr   cr5
211         nop
212         stb     rCHR, 1(rMEMP)
213         bltlr   cr1
214         stb     rCHR, 2(rMEMP)
215         beqlr   cr1
216         nop
217         stb     rCHR, 3(rMEMP)
218         blr
220 /* Memset of 0-31 bytes.  */
221         .align 5
222 L(medium):
223         cmplwi  cr1, rLEN, 16
224 L(medium_tail2):
225         add     rMEMP, rMEMP, rLEN
226 L(medium_tail):
227         bt-     31, L(medium_31t)
228         bt-     30, L(medium_30t)
229 L(medium_30f):
230         bt-     29, L(medium_29t)
231 L(medium_29f):
232         bge-    cr1, L(medium_27t)
233         bflr-   28
234         stw     rCHR, -4(rMEMP) /* 8th instruction from .align */
235         stw     rCHR, -8(rMEMP)
236         blr
238 L(medium_31t):
239         stbu    rCHR, -1(rMEMP)
240         bf-     30, L(medium_30f)
241 L(medium_30t):
242         sthu    rCHR, -2(rMEMP)
243         bf-     29, L(medium_29f)
244 L(medium_29t):
245         stwu    rCHR, -4(rMEMP)
246         blt-    cr1, L(medium_27f) /* 16th instruction from .align */
247 L(medium_27t):
248         stw     rCHR, -4(rMEMP)
249         stw     rCHR, -8(rMEMP)
250         stw     rCHR, -12(rMEMP)
251         stwu    rCHR, -16(rMEMP)
252 L(medium_27f):
253         bflr-   28
254 L(medium_28t):
255         stw     rCHR, -4(rMEMP)
256         stw     rCHR, -8(rMEMP)
257         blr
259 L(checklinesize):
260 #ifdef SHARED
261         mflr rTMP
262 /* If the remaining length is less the 32 bytes then don't bother getting
263          the cache line size.  */
264         beq     L(medium)
265 /* Establishes GOT addressability so we can load __cache_line_size
266    from static. This value was set from the aux vector during startup.  */
267         bl   _GLOBAL_OFFSET_TABLE_@local-4
268         mflr rGOT
269         lwz      rGOT,__cache_line_size@got(rGOT)
270         lwz      rCLS,0(rGOT)
271         mtlr rTMP
272 #else
273 /* Load __cache_line_size from static. This value was set from the
274    aux vector during startup.  */
275         lis      rCLS,__cache_line_size@ha
276 /* If the remaining length is less the 32 bytes then don't bother getting
277          the cache line size.  */
278         beq     L(medium)
279         lwz  rCLS,__cache_line_size@l(rCLS)
280 #endif
282 /*If the cache line size was not set then goto to L(nondcbz), which is
283         safe for any cache line size.  */
284         cmplwi cr1,rCLS,0
285         beq     cr1,L(nondcbz)
287 /* If the cache line size is 32 bytes then goto to L(zloopstart),
288          which is coded specificly for 32-byte lines (and 601).  */
289         cmplwi cr1,rCLS,32
290         beq     cr1,L(zloopstart)
292 /* Now we know the cache line size and it is not 32-bytes.  However
293          we may not yet be aligned to the cache line and may have a partial
294          line to fill.  Touch it 1st to fetch the cache line.  */
295         dcbtst 0,rMEMP
297         addi rCLM,rCLS,-1
298 L(getCacheAligned):
299         cmplwi cr1,rLEN,32
300         and. rTMP,rCLM,rMEMP
301         blt      cr1,L(handletail32)
302         beq      L(cacheAligned)
303 /* We are not aligned to start of a cache line yet.  Store 32-byte
304    of data and test again.  */
305         addi rMEMP,rMEMP,32
306         addi rLEN,rLEN,-32
307         stw      rCHR,-32(rMEMP)
308         stw      rCHR,-28(rMEMP)
309         stw      rCHR,-24(rMEMP)
310         stw      rCHR,-20(rMEMP)
311         stw      rCHR,-16(rMEMP)
312         stw      rCHR,-12(rMEMP)
313         stw      rCHR,-8(rMEMP)
314         stw      rCHR,-4(rMEMP)
315         b        L(getCacheAligned)
317 /* Now we are aligned to the cache line and can use dcbz.  */
318 L(cacheAligned):
319         cmplw cr1,rLEN,rCLS
320         blt      cr1,L(handletail32)
321         dcbz 0,rMEMP
322         subf rLEN,rCLS,rLEN
323         add      rMEMP,rMEMP,rCLS
324         b        L(cacheAligned)
326 /* We are here because; the cache line size was set, it was not
327    32-bytes, and the remainder (rLEN) is now less than the actual cache
328    line size.  Set up the preconditions for L(nondcbz) and go there to
329    store the remaining bytes.  */
330 L(handletail32):
331         clrrwi. rALIGN, rLEN, 5
332         b               L(nondcbz)
334 END (BP_SYM (memset))
335 libc_hidden_builtin_def (memset)