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