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