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