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