2.5-18.1
[glibc.git] / sysdeps / powerpc / powerpc64 / memset.S
blob09c79fccd72374f7c2d40d872d58f80109a5d1a1
1 /* Optimized memset implementation for PowerPC64.
2    Copyright (C) 1997, 1999, 2000, 2002, 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.  */
28         .globl __cache_line_size
29         .lcomm __cache_line_size,4,4
30         .section        ".toc","aw"
31 .LC0:
32         .tc __cache_line_size[TC],__cache_line_size
33         .section        ".text"
34         .align 2
36 /* __ptr_t [r3] memset (__ptr_t s [r3], int c [r4], size_t n [r5]));
37    Returns 's'.
39    The memset is done in three sizes: byte (8 bits), word (32 bits),
40    cache line (256 bits). There is a special case for setting cache lines
41    to 0, to take advantage of the dcbz instruction.  */
43 EALIGN (BP_SYM (memset), 5, 0)
44         CALL_MCOUNT 3
46 #define rTMP    r0
47 #define rRTN    r3      /* Initial value of 1st argument.  */
48 #if __BOUNDED_POINTERS__
49 # define rMEMP0 r4      /* Original value of 1st arg.  */
50 # define rCHR   r5      /* Char to set in each byte.  */
51 # define rLEN   r6      /* Length of region to set.  */
52 # define rMEMP  r10     /* Address at which we are storing.  */
53 #else
54 # define rMEMP0 r3      /* Original value of 1st arg.  */
55 # define rCHR   r4      /* Char to set in each byte.  */
56 # define rLEN   r5      /* Length of region to set.  */
57 # define rMEMP  r6      /* Address at which we are storing.  */
58 #endif
59 #define rALIGN  r7      /* Number of bytes we are setting now (when aligning). */
60 #define rMEMP2  r8
62 #define rNEG64  r8      /* Constant -64 for clearing with dcbz.  */
63 #define rCLS    r8      /* Cache line size obtained from static.  */
64 #define rCLM    r9      /* Cache line size mask to check for cache alignment.  */
65 L(_memset):
66 #if __BOUNDED_POINTERS__
67         cmpldi  cr1, rRTN, 0
68         CHECK_BOUNDS_BOTH_WIDE (rMEMP0, rTMP, rTMP2, rLEN)
69         beq     cr1, L(b0)
70         STORE_RETURN_VALUE (rMEMP0)
71         STORE_RETURN_BOUNDS (rTMP, rTMP2)
72 L(b0):
73 #endif
74 /* Take care of case for size <= 4.  */
75         cmpldi  cr1, rLEN, 8
76         andi.   rALIGN, rMEMP0, 7
77         mr      rMEMP, rMEMP0
78         ble-    cr1, L(small)
80 /* Align to doubleword boundary.  */
81         cmpldi  cr5, rLEN, 31
82         rlwimi  rCHR, rCHR, 8, 16, 23 /* Replicate byte to halfword.  */
83         beq+    L(aligned2)
84         mtcrf   0x01, rMEMP0
85         subfic  rALIGN, rALIGN, 8
86         cror    28,30,31                /* Detect odd word aligned.  */
87         add     rMEMP, rMEMP, rALIGN
88         sub     rLEN, rLEN, rALIGN
89         rlwimi  rCHR, rCHR, 16, 0, 15 /* Replicate halfword to word.  */
90         bt      29, L(g4)
91 /* Process the even word of doubleword.  */
92         bf+     31, L(g2)
93         stb     rCHR, 0(rMEMP0)
94         bt      30, L(g4x)
95 L(g2):
96         sth     rCHR, -6(rMEMP)
97 L(g4x):
98         stw     rCHR, -4(rMEMP)
99         b       L(aligned)
100 /* Process the odd word of doubleword.  */
101 L(g4):
102         bf      28, L(g4x) /* If false, word aligned on odd word.  */
103         bf+     31, L(g0)
104         stb     rCHR, 0(rMEMP0)
105         bt      30, L(aligned)
106 L(g0):
107         sth     rCHR, -2(rMEMP)
109 /* Handle the case of size < 31.  */
110 L(aligned2):
111         rlwimi  rCHR, rCHR, 16, 0, 15 /* Replicate halfword to word.  */
112 L(aligned):
113         mtcrf   0x01, rLEN
114         ble     cr5, L(medium)
115 /* Align to 32-byte boundary.  */
116         andi.   rALIGN, rMEMP, 0x18
117         subfic  rALIGN, rALIGN, 0x20
118         insrdi  rCHR,rCHR,32,0 /* Replicate word to double word. */
119         beq     L(caligned)
120         mtcrf   0x01, rALIGN
121         add     rMEMP, rMEMP, rALIGN
122         sub     rLEN, rLEN, rALIGN
123         cmplwi  cr1, rALIGN, 0x10
124         mr      rMEMP2, rMEMP
125         bf      28, L(a1)
126         stdu    rCHR, -8(rMEMP2)
127 L(a1):  blt     cr1, L(a2)
128         std     rCHR, -8(rMEMP2)
129         stdu    rCHR, -16(rMEMP2)
130 L(a2):
132 /* Now aligned to a 32 byte boundary.  */
133 L(caligned):
134         cmpldi  cr1, rCHR, 0
135         clrrdi. rALIGN, rLEN, 5
136         mtcrf   0x01, rLEN
137         beq     cr1, L(zloopstart) /* Special case for clearing memory using dcbz.  */
138 L(nondcbz):
139         srdi    rTMP, rALIGN, 5
140         mtctr   rTMP
141         beq     L(medium)       /* We may not actually get to do a full line.  */
142         clrldi. rLEN, rLEN, 59
143         add     rMEMP, rMEMP, rALIGN
144         li      rNEG64, -0x40
145         bdz     L(cloopdone)
147 L(c3):  dcbtst  rNEG64, rMEMP
148         std     rCHR, -8(rMEMP)
149         std     rCHR, -16(rMEMP)
150         std     rCHR, -24(rMEMP)
151         stdu    rCHR, -32(rMEMP)
152         bdnz    L(c3)
153 L(cloopdone):
154         std     rCHR, -8(rMEMP)
155         std     rCHR, -16(rMEMP)
156         cmpldi  cr1, rLEN, 16
157         std     rCHR, -24(rMEMP)
158         stdu    rCHR, -32(rMEMP)
159         beqlr
160         add     rMEMP, rMEMP, rALIGN
161         b       L(medium_tail2)
163         .align 5
164 /* Clear lines of memory in 128-byte chunks.  */
165 L(zloopstart):
166 /* If the remaining length is less the 32 bytes, don't bother getting
167          the cache line size.  */
168         beq     L(medium)
169         ld      rCLS,.LC0@toc(r2)
170         lwz     rCLS,0(rCLS)
171 /* If the cache line size was not set just goto to L(nondcbz) which is
172          safe for any cache line size.  */
173         cmpldi  cr1,rCLS,0
174         beq             cr1,L(nondcbz)
177 /* Now we know the cache line size, and it is not 32-bytes, but
178          we may not yet be aligned to the cache line. May have a partial
179          line to fill, so touch it 1st.  */
180         dcbt    0,rMEMP
181         addi    rCLM,rCLS,-1
182 L(getCacheAligned):
183         cmpldi  cr1,rLEN,32
184         and.    rTMP,rCLM,rMEMP
185         blt             cr1,L(handletail32)
186         beq             L(cacheAligned)
187         addi    rMEMP,rMEMP,32
188         addi    rLEN,rLEN,-32
189         std             rCHR,-32(rMEMP)
190         std             rCHR,-24(rMEMP)
191         std             rCHR,-16(rMEMP)
192         std             rCHR,-8(rMEMP)
193         b               L(getCacheAligned)
195 /* Now we are aligned to the cache line and can use dcbz.  */
196 L(cacheAligned):
197         cmpld   cr1,rLEN,rCLS
198         blt             cr1,L(handletail32)
199         dcbz    0,rMEMP
200         subf    rLEN,rCLS,rLEN
201         add             rMEMP,rMEMP,rCLS
202         b               L(cacheAligned)
204 /* We are here because the cache line size was set and was not 32-bytes
205    and the remainder (rLEN) is less than the actual cache line size.
206    So set up the preconditions for L(nondcbz) and go there.  */
207 L(handletail32):
208         clrrwi. rALIGN, rLEN, 5
209         b               L(nondcbz)
211         .align 5
212 L(small):
213 /* Memset of 8 bytes or less.  */
214         cmpldi  cr6, rLEN, 4
215         cmpldi  cr5, rLEN, 1
216         ble     cr6,L(le4)
217         subi    rLEN, rLEN, 4
218         stb     rCHR,0(rMEMP)
219         stb     rCHR,1(rMEMP)
220         stb     rCHR,2(rMEMP)
221         stb     rCHR,3(rMEMP)
222         addi    rMEMP,rMEMP, 4
223         cmpldi  cr5, rLEN, 1
224 L(le4):
225         cmpldi  cr1, rLEN, 3
226         bltlr   cr5
227         stb     rCHR, 0(rMEMP)
228         beqlr   cr5
229         stb     rCHR, 1(rMEMP)
230         bltlr   cr1
231         stb     rCHR, 2(rMEMP)
232         beqlr   cr1
233         stb     rCHR, 3(rMEMP)
234         blr
236 /* Memset of 0-31 bytes.  */
237         .align 5
238 L(medium):
239         insrdi  rCHR,rCHR,32,0 /* Replicate word to double word.  */
240         cmpldi  cr1, rLEN, 16
241 L(medium_tail2):
242         add     rMEMP, rMEMP, rLEN
243 L(medium_tail):
244         bt-     31, L(medium_31t)
245         bt-     30, L(medium_30t)
246 L(medium_30f):
247         bt-     29, L(medium_29t)
248 L(medium_29f):
249         bge-    cr1, L(medium_27t)
250         bflr-   28
251         std     rCHR, -8(rMEMP)
252         blr
254 L(medium_31t):
255         stbu    rCHR, -1(rMEMP)
256         bf-     30, L(medium_30f)
257 L(medium_30t):
258         sthu    rCHR, -2(rMEMP)
259         bf-     29, L(medium_29f)
260 L(medium_29t):
261         stwu    rCHR, -4(rMEMP)
262         blt-    cr1, L(medium_27f)
263 L(medium_27t):
264         std     rCHR, -8(rMEMP)
265         stdu    rCHR, -16(rMEMP)
266 L(medium_27f):
267         bflr-   28
268 L(medium_28t):
269         std     rCHR, -8(rMEMP)
270         blr
271 END_GEN_TB (BP_SYM (memset),TB_TOCLESS)
272 libc_hidden_builtin_def (memset)
274 /* Copied from bzero.S to prevent the linker from inserting a stub
275    between bzero and memset.  */
276 ENTRY (BP_SYM (__bzero))
277         CALL_MCOUNT 3
278 #if __BOUNDED_POINTERS__
279         mr      r6,r4
280         li      r5,0
281         mr      r4,r3
282         /* Tell memset that we don't want a return value.  */
283         li      r3,0
284         b       L(_memset)
285 #else
286         mr      r5,r4
287         li      r4,0
288         b       L(_memset)
289 #endif
290 END_GEN_TB (BP_SYM (__bzero),TB_TOCLESS)
292 weak_alias (BP_SYM (__bzero), BP_SYM (bzero))