powerpc: refactor strchr, strchrnul, and strrchr IFUNC.
[glibc.git] / sysdeps / powerpc / powerpc64 / power7 / memcpy.S
blobae2db216f3d3f79e47bbdf1b67c60125db0eca51
1 /* Optimized memcpy implementation for PowerPC64/POWER7.
2    Copyright (C) 2010-2017 Free Software Foundation, Inc.
3    Contributed by Luis Machado <luisgpm@br.ibm.com>.
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>
23 /* __ptr_t [r3] memcpy (__ptr_t dst [r3], __ptr_t src [r4], size_t len [r5]);
24    Returns 'dst'.  */
26 #define dst 11          /* Use r11 so r3 kept unchanged.  */
27 #define src 4
28 #define cnt 5
30         .machine power7
31 EALIGN (memcpy, 5, 0)
32         CALL_MCOUNT 3
34         cmpldi  cr1,cnt,31
35         neg     0,3
36         ble     cr1, L(copy_LT_32)  /* If move < 32 bytes use short move
37                                     code.  */
39 /* Align copies using VSX instructions to quadword. It is to avoid alignment
40    traps when memcpy is used on non-cacheable memory (for instance, memory
41    mapped I/O).  */
42         andi.   10,3,15
43         clrldi  11,4,60
44         cmpld   cr6,10,11       /* SRC and DST alignments match?  */
46         mr      dst,3
47         bne     cr6,L(copy_GE_32_unaligned)
48         beq     L(aligned_copy)
50         mtocrf  0x01,0
51         clrldi  0,0,60
53 /* Get the DST and SRC aligned to 16 bytes.  */
55         bf      31,2f
56         lbz     6,0(src)
57         addi    src,src,1
58         stb     6,0(dst)
59         addi    dst,dst,1
61         bf      30,4f
62         lhz     6,0(src)
63         addi    src,src,2
64         sth     6,0(dst)
65         addi    dst,dst,2
67         bf      29,8f
68         lwz     6,0(src)
69         addi    src,src,4
70         stw     6,0(dst)
71         addi    dst,dst,4
73         bf      28,16f
74         ld      6,0(src)
75         addi    src,src,8
76         std     6,0(dst)
77         addi    dst,dst,8
78 16:
79         subf    cnt,0,cnt
81 /* Main aligned copy loop. Copies 128 bytes at a time. */
82 L(aligned_copy):
83         li      6,16
84         li      7,32
85         li      8,48
86         mtocrf  0x02,cnt
87         srdi    12,cnt,7
88         cmpdi   12,0
89         beq     L(aligned_tail)
90         lxvd2x  6,0,src
91         lxvd2x  7,src,6
92         mtctr   12
93         b       L(aligned_128loop)
95         .align  4
96 L(aligned_128head):
97         /* for the 2nd + iteration of this loop. */
98         lxvd2x  6,0,src
99         lxvd2x  7,src,6
100 L(aligned_128loop):
101         lxvd2x  8,src,7
102         lxvd2x  9,src,8
103         stxvd2x 6,0,dst
104         addi    src,src,64
105         stxvd2x 7,dst,6
106         stxvd2x 8,dst,7
107         stxvd2x 9,dst,8
108         lxvd2x  6,0,src
109         lxvd2x  7,src,6
110         addi    dst,dst,64
111         lxvd2x  8,src,7
112         lxvd2x  9,src,8
113         addi    src,src,64
114         stxvd2x 6,0,dst
115         stxvd2x 7,dst,6
116         stxvd2x 8,dst,7
117         stxvd2x 9,dst,8
118         addi    dst,dst,64
119         bdnz    L(aligned_128head)
121 L(aligned_tail):
122         mtocrf  0x01,cnt
123         bf      25,32f
124         lxvd2x  6,0,src
125         lxvd2x  7,src,6
126         lxvd2x  8,src,7
127         lxvd2x  9,src,8
128         addi    src,src,64
129         stxvd2x 6,0,dst
130         stxvd2x 7,dst,6
131         stxvd2x 8,dst,7
132         stxvd2x 9,dst,8
133         addi    dst,dst,64
135         bf      26,16f
136         lxvd2x  6,0,src
137         lxvd2x  7,src,6
138         addi    src,src,32
139         stxvd2x 6,0,dst
140         stxvd2x 7,dst,6
141         addi    dst,dst,32
143         bf      27,8f
144         lxvd2x  6,0,src
145         addi    src,src,16
146         stxvd2x 6,0,dst
147         addi    dst,dst,16
149         bf      28,4f
150         ld      6,0(src)
151         addi    src,src,8
152         std     6,0(dst)
153         addi    dst,dst,8
154 4:      /* Copies 4~7 bytes.  */
155         bf      29,L(tail2)
156         lwz     6,0(src)
157         stw     6,0(dst)
158         bf      30,L(tail5)
159         lhz     7,4(src)
160         sth     7,4(dst)
161         bflr    31
162         lbz     8,6(src)
163         stb     8,6(dst)
164         /* Return original DST pointer.  */
165         blr
168 /* Handle copies of 0~31 bytes.  */
169         .align  4
170 L(copy_LT_32):
171         mr      dst,3
172         cmpldi  cr6,cnt,8
173         mtocrf  0x01,cnt
174         ble     cr6,L(copy_LE_8)
176         /* At least 9 bytes to go.  */
177         neg     8,4
178         andi.   0,8,3
179         cmpldi  cr1,cnt,16
180         beq     L(copy_LT_32_aligned)
182         /* Force 4-byte alignment for SRC.  */
183         mtocrf  0x01,0
184         subf    cnt,0,cnt
186         bf      30,1f
187         lhz     6,0(src)
188         addi    src,src,2
189         sth     6,0(dst)
190         addi    dst,dst,2
192         bf      31,L(end_4bytes_alignment)
193         lbz     6,0(src)
194         addi    src,src,1
195         stb     6,0(dst)
196         addi    dst,dst,1
198         .align  4
199 L(end_4bytes_alignment):
200         cmpldi  cr1,cnt,16
201         mtocrf  0x01,cnt
203 L(copy_LT_32_aligned):
204         /* At least 6 bytes to go, and SRC is word-aligned.  */
205         blt     cr1,8f
207         /* Copy 16 bytes.  */
208         lwz     6,0(src)
209         lwz     7,4(src)
210         stw     6,0(dst)
211         lwz     8,8(src)
212         stw     7,4(dst)
213         lwz     6,12(src)
214         addi    src,src,16
215         stw     8,8(dst)
216         stw     6,12(dst)
217         addi    dst,dst,16
218 8:      /* Copy 8 bytes.  */
219         bf      28,L(tail4)
220         lwz     6,0(src)
221         lwz     7,4(src)
222         addi    src,src,8
223         stw     6,0(dst)
224         stw     7,4(dst)
225         addi    dst,dst,8
227         .align  4
228 /* Copies 4~7 bytes.  */
229 L(tail4):
230         bf      29,L(tail2)
231         lwz     6,0(src)
232         stw     6,0(dst)
233         bf      30,L(tail5)
234         lhz     7,4(src)
235         sth     7,4(dst)
236         bflr    31
237         lbz     8,6(src)
238         stb     8,6(dst)
239         /* Return original DST pointer.  */
240         blr
242         .align  4
243 /* Copies 2~3 bytes.  */
244 L(tail2):
245         bf      30,1f
246         lhz     6,0(src)
247         sth     6,0(dst)
248         bflr    31
249         lbz     7,2(src)
250         stb     7,2(dst)
251         blr
253         .align  4
254 L(tail5):
255         bflr    31
256         lbz     6,4(src)
257         stb     6,4(dst)
258         blr
260         .align  4
262         bflr    31
263         lbz     6,0(src)
264         stb     6,0(dst)
265         /* Return original DST pointer.  */
266         blr
269 /* Handles copies of 0~8 bytes.  */
270         .align  4
271 L(copy_LE_8):
272         bne     cr6,L(tail4)
274         /* Though we could've used ld/std here, they are still
275         slow for unaligned cases.  */
277         lwz     6,0(src)
278         lwz     7,4(src)
279         stw     6,0(dst)
280         stw     7,4(dst)
281         blr
284 /* Handle copies of 32+ bytes where DST is aligned (to quadword) but
285    SRC is not.  Use aligned quadword loads from SRC, shifted to realign
286    the data, allowing for aligned DST stores.  */
287         .align  4
288 L(copy_GE_32_unaligned):
289         clrldi  0,0,60        /* Number of bytes until the 1st dst quadword.  */
290         srdi    9,cnt,4       /* Number of full quadwords remaining.  */
292         beq     L(copy_GE_32_unaligned_cont)
294         /* DST is not quadword aligned, get it aligned.  */
296         mtocrf  0x01,0
297         subf    cnt,0,cnt
299         /* Vector instructions work best when proper alignment (16-bytes)
300         is present.  Move 0~15 bytes as needed to get DST quadword-aligned.  */
302         bf      31,2f
303         lbz     6,0(src)
304         addi    src,src,1
305         stb     6,0(dst)
306         addi    dst,dst,1
308         bf      30,4f
309         lhz     6,0(src)
310         addi    src,src,2
311         sth     6,0(dst)
312         addi    dst,dst,2
314         bf      29,8f
315         lwz     6,0(src)
316         addi    src,src,4
317         stw     6,0(dst)
318         addi    dst,dst,4
320         bf      28,0f
321         ld      6,0(src)
322         addi    src,src,8
323         std     6,0(dst)
324         addi    dst,dst,8
326         srdi    9,cnt,4       /* Number of full quadwords remaining.  */
328         /* The proper alignment is present, it is OK to copy the bytes now.  */
329 L(copy_GE_32_unaligned_cont):
331         /* Setup two indexes to speed up the indexed vector operations.  */
332         clrldi  10,cnt,60
333         li      6,16          /* Index for 16-bytes offsets.  */
334         li      7,32          /* Index for 32-bytes offsets.  */
335         cmpldi  cr1,10,0
336         srdi    8,cnt,5       /* Setup the loop counter.  */
337         mtocrf  0x01,9
338         cmpldi  cr6,9,1
339 #ifdef __LITTLE_ENDIAN__
340         lvsr    5,0,src
341 #else
342         lvsl    5,0,src
343 #endif
344         lvx     3,0,src
345         li      0,0
346         bf      31,L(setup_unaligned_loop)
348         /* Copy another 16 bytes to align to 32-bytes due to the loop.  */
349         lvx     4,src,6
350 #ifdef __LITTLE_ENDIAN__
351         vperm   6,4,3,5
352 #else
353         vperm   6,3,4,5
354 #endif
355         addi    src,src,16
356         stvx    6,0,dst
357         addi    dst,dst,16
358         vor     3,4,4
359         clrrdi  0,src,60
361 L(setup_unaligned_loop):
362         mtctr   8
363         ble     cr6,L(end_unaligned_loop)
365         /* Copy 32 bytes at a time using vector instructions.  */
366         .align  4
367 L(unaligned_loop):
369         /* Note: vr6/vr10 may contain data that was already copied,
370         but in order to get proper alignment, we may have to copy
371         some portions again. This is faster than having unaligned
372         vector instructions though.  */
374         lvx     4,src,6
375 #ifdef __LITTLE_ENDIAN__
376         vperm   6,4,3,5
377 #else
378         vperm   6,3,4,5
379 #endif
380         lvx     3,src,7
381 #ifdef __LITTLE_ENDIAN__
382         vperm   10,3,4,5
383 #else
384         vperm   10,4,3,5
385 #endif
386         addi    src,src,32
387         stvx    6,0,dst
388         stvx    10,dst,6
389         addi    dst,dst,32
390         bdnz    L(unaligned_loop)
392         clrrdi  0,src,60
394         .align  4
395 L(end_unaligned_loop):
397         /* Check for tail bytes.  */
398         mtocrf  0x01,cnt
399         beqlr   cr1
401         add     src,src,0
403         /*  We have 1~15 tail bytes to copy, and DST is quadword aligned.  */
404         /* Copy 8 bytes.  */
405         bf      28,4f
406         lwz     6,0(src)
407         lwz     7,4(src)
408         addi    src,src,8
409         stw     6,0(dst)
410         stw     7,4(dst)
411         addi    dst,dst,8
412 4:      /* Copy 4~7 bytes.  */
413         bf      29,L(tail2)
414         lwz     6,0(src)
415         stw     6,0(dst)
416         bf      30,L(tail5)
417         lhz     7,4(src)
418         sth     7,4(dst)
419         bflr    31
420         lbz     8,6(src)
421         stb     8,6(dst)
422         /* Return original DST pointer.  */
423         blr
425 END_GEN_TB (memcpy,TB_TOCLESS)
426 libc_hidden_builtin_def (memcpy)