1 /* Optimized mempcpy implementation for POWER7.
2 Copyright (C) 2010-2024 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 <https://www.gnu.org/licenses/>. */
21 /* void * [r3] __mempcpy (void *dst [r3], void *src [r4], size_t len [r5]);
22 Returns 'dst' + 'len'. */
25 EALIGN (__mempcpy, 5, 0)
29 cfi_adjust_cfa_offset(32)
31 cfi_offset(30,(20-32))
37 ble cr1,L(copy_LT_32) /* If move < 32 bytes use short move
40 andi. 11,3,7 /* Check alignment of DST. */
41 clrlwi 10,4,29 /* Check alignment of SRC. */
42 cmplw cr6,10,11 /* SRC and DST alignments match? */
45 bne cr6,L(copy_GE_32_unaligned)
47 srwi 9,5,3 /* Number of full quadwords remaining. */
49 beq L(copy_GE_32_aligned_cont)
55 /* Get the SRC aligned to 8 bytes. */
73 clrlwi 10,12,29 /* Check alignment of SRC again. */
74 srwi 9,31,3 /* Number of full doublewords remaining. */
76 L(copy_GE_32_aligned_cont):
86 /* Copy 1~3 doublewords so the main loop starts
87 at a multiple of 32 bytes. */
106 1: /* Copy 1 doubleword and set the counter. */
116 4: /* Main aligned copy loop. Copies 32-bytes at a time. */
131 /* Check for tail bytes. */
141 /* At this point we have a tail of 0-7 bytes and we know that the
142 destination is doubleword-aligned. */
143 4: /* Copy 4 bytes. */
150 2: /* Copy 2 bytes. */
157 1: /* Copy 1 byte. */
162 0: /* Return DST + LEN pointer. */
169 /* Handle copies of 0~31 bytes. */
177 /* At least 9 bytes to go. */
183 beq L(copy_LT_32_aligned)
185 /* Force 4-bytes alignment for SRC. */
194 1: bf 31,L(end_4bytes_alignment)
202 L(end_4bytes_alignment):
206 L(copy_LT_32_aligned):
207 /* At least 6 bytes to go, and SRC is word-aligned. */
221 8: /* Copy 8 bytes. */
230 4: /* Copy 4 bytes. */
237 2: /* Copy 2-3 bytes. */
246 /* Return DST + LEN pointer. */
253 1: /* Copy 1 byte. */
258 0: /* Return DST + LEN pointer. */
264 /* Handles copies of 0~8 bytes. */
269 /* Though we could've used lfd/stfd here, they are still
270 slow for unaligned cases. */
277 /* Return DST + LEN pointer. */
284 4: /* Copies 4~7 bytes. */
296 /* Return DST + LEN pointer. */
303 5: /* Copy 1 byte. */
309 0: /* Return DST + LEN pointer. */
315 /* Handle copies of 32+ bytes where DST is aligned (to quadword) but
316 SRC is not. Use aligned quadword loads from SRC, shifted to realign
317 the data, allowing for aligned DST stores. */
319 L(copy_GE_32_unaligned):
320 andi. 11,3,15 /* Check alignment of DST. */
321 clrlwi 0,0,28 /* Number of bytes until the 1st
323 srwi 9,5,4 /* Number of full quadwords remaining. */
325 beq L(copy_GE_32_unaligned_cont)
327 /* DST is not quadword aligned, get it aligned. */
332 /* Vector instructions work best when proper alignment (16-bytes)
333 is present. Move 0~15 bytes as needed to get DST quadword-aligned. */
334 1: /* Copy 1 byte. */
341 2: /* Copy 2 bytes. */
348 4: /* Copy 4 bytes. */
355 8: /* Copy 8 bytes. */
363 clrlwi 10,12,28 /* Check alignment of SRC. */
364 srwi 9,31,4 /* Number of full quadwords remaining. */
366 /* The proper alignment is present, it is OK to copy the bytes now. */
367 L(copy_GE_32_unaligned_cont):
369 /* Setup two indexes to speed up the indexed vector operations. */
371 li 6,16 /* Index for 16-bytes offsets. */
372 li 7,32 /* Index for 32-bytes offsets. */
374 srwi 8,31,5 /* Setup the loop counter. */
379 #ifdef __LITTLE_ENDIAN__
385 bf 31,L(setup_unaligned_loop)
387 /* Copy another 16 bytes to align to 32-bytes due to the loop . */
389 #ifdef __LITTLE_ENDIAN__
399 L(setup_unaligned_loop):
401 ble cr6,L(end_unaligned_loop)
403 /* Copy 32 bytes at a time using vector instructions. */
407 /* Note: vr6/vr10 may contain data that was already copied,
408 but in order to get proper alignment, we may have to copy
409 some portions again. This is faster than having unaligned
410 vector instructions though. */
412 lvx 4,11,6 /* vr4 = r11+16. */
413 #ifdef __LITTLE_ENDIAN__
418 lvx 3,11,7 /* vr3 = r11+32. */
419 #ifdef __LITTLE_ENDIAN__
429 bdnz L(unaligned_loop)
432 L(end_unaligned_loop):
434 /* Check for tail bytes. */
442 /* We have 1~15 tail bytes to copy, and DST is quadword aligned. */
443 8: /* Copy 8 bytes. */
452 4: /* Copy 4 bytes. */
459 2: /* Copy 2~3 bytes. */
466 1: /* Copy 1 byte. */
471 0: /* Return DST + LEN pointer. */
479 libc_hidden_def (__mempcpy)
480 weak_alias (__mempcpy, mempcpy)
481 libc_hidden_builtin_def (mempcpy)