1 /* Optimized memcpy implementation for PowerPC32/POWER7.
2 Copyright (C) 2010 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, write to the Free
18 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA
25 /* __ptr_t [r3] memcpy (__ptr_t dst [r3], __ptr_t src [r4], size_t len [r5]);
29 EALIGN (BP_SYM (memcpy), 5, 0)
33 cfi_adjust_cfa_offset(32)
35 cfi_offset(30,(20-32))
41 ble cr1, L(copy_LT_32) /* If move < 32 bytes use short move
44 andi. 11,3,7 /* Check alignment of DST. */
45 clrlwi 10,4,29 /* Check alignment of SRC. */
46 cmplw cr6,10,11 /* SRC and DST alignments match? */
49 bne cr6,L(copy_GE_32_unaligned)
51 srwi 9,5,3 /* Number of full quadwords remaining. */
53 beq L(copy_GE_32_aligned_cont)
59 /* Get the SRC aligned to 8 bytes. */
77 clrlwi 10,12,29 /* Check alignment of SRC again. */
78 srwi 9,31,3 /* Number of full doublewords remaining. */
80 L(copy_GE_32_aligned_cont):
90 /* Copy 1~3 doublewords so the main loop starts
91 at a multiple of 32 bytes. */
110 1: /* Copy 1 doubleword and set the counter. */
120 4: /* Main aligned copy loop. Copies 32-bytes at a time. */
135 /* Check for tail bytes. */
145 /* At this point we have a tail of 0-7 bytes and we know that the
146 destination is doubleword-aligned. */
147 4: /* Copy 4 bytes. */
154 2: /* Copy 2 bytes. */
161 1: /* Copy 1 byte. */
166 0: /* Return original DST pointer. */
173 /* Handle copies of 0~31 bytes. */
181 /* At least 9 bytes to go. */
187 beq L(copy_LT_32_aligned)
189 /* Force 4-bytes alignment for SRC. */
198 1: bf 31,L(end_4bytes_alignment)
206 L(end_4bytes_alignment):
210 L(copy_LT_32_aligned):
211 /* At least 6 bytes to go, and SRC is word-aligned. */
225 8: /* Copy 8 bytes. */
234 4: /* Copy 4 bytes. */
241 2: /* Copy 2-3 bytes. */
250 /* Return original DST pointer. */
257 1: /* Copy 1 byte. */
262 0: /* Return original DST pointer. */
268 /* Handles copies of 0~8 bytes. */
273 /* Though we could've used lfd/stfd here, they are still
274 slow for unaligned cases. */
281 /* Return original DST pointer. */
288 4: /* Copies 4~7 bytes. */
300 /* Return original DST pointer. */
307 5: /* Copy 1 byte. */
313 0: /* Return original DST pointer. */
319 /* Handle copies of 32+ bytes where DST is aligned (to quadword) but
320 SRC is not. Use aligned quadword loads from SRC, shifted to realign
321 the data, allowing for aligned DST stores. */
323 L(copy_GE_32_unaligned):
324 andi. 11,3,15 /* Check alignment of DST. */
325 clrlwi 0,0,28 /* Number of bytes until the 1st
327 srwi 9,5,4 /* Number of full quadwords remaining. */
329 beq L(copy_GE_32_unaligned_cont)
331 /* SRC is not quadword aligned, get it aligned. */
336 /* Vector instructions work best when proper alignment (16-bytes)
337 is present. Move 0~15 bytes as needed to get DST quadword-aligned. */
338 1: /* Copy 1 byte. */
345 2: /* Copy 2 bytes. */
352 4: /* Copy 4 bytes. */
359 8: /* Copy 8 bytes. */
367 clrlwi 10,12,28 /* Check alignment of SRC. */
368 srdi 9,31,4 /* Number of full quadwords remaining. */
370 /* The proper alignment is present, it is OK to copy the bytes now. */
371 L(copy_GE_32_unaligned_cont):
373 /* Setup two indexes to speed up the indexed vector operations. */
375 li 6,16 /* Index for 16-bytes offsets. */
376 li 7,32 /* Index for 32-bytes offsets. */
378 srdi 8,31,5 /* Setup the loop counter. */
385 bf 31,L(setup_unaligned_loop)
387 /* Copy another 16 bytes to align to 32-bytes due to the loop . */
395 L(setup_unaligned_loop):
397 ble cr6,L(end_unaligned_loop)
399 /* Copy 32 bytes at a time using vector instructions. */
403 /* Note: vr6/vr10 may contain data that was already copied,
404 but in order to get proper alignment, we may have to copy
405 some portions again. This is faster than having unaligned
406 vector instructions though. */
408 lvx 4,11,6 /* vr4 = r11+16. */
409 vperm 6,3,4,5 /* Merge the correctly-aligned portions
410 of vr3/vr4 into vr6. */
411 lvx 3,11,7 /* vr3 = r11+32. */
412 vperm 10,4,3,5 /* Merge the correctly-aligned portions
413 of vr3/vr4 into vr10. */
419 bdnz L(unaligned_loop)
422 L(end_unaligned_loop):
424 /* Check for tail bytes. */
432 /* We have 1~15 tail bytes to copy, and DST is quadword aligned. */
433 8: /* Copy 8 bytes. */
442 4: /* Copy 4 bytes. */
449 2: /* Copy 2~3 bytes. */
456 1: /* Copy 1 byte. */
461 0: /* Return original DST pointer. */
468 END (BP_SYM (memcpy))
469 libc_hidden_builtin_def (memcpy)