1 /* memcopy.h -- definitions for memory copy functions. Generic C version. 2 Copyright (C) 1991-2022 Free Software Foundation, Inc. 3 This file is part of the GNU C Library. 4 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. 9 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. 14 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/>. */ 18 19 /* The strategy of the memory functions is: 20 21 1. Copy bytes until the destination pointer is aligned. 22 23 2. Copy words in unrolled loops. If the source and destination 24 are not aligned in the same way, use word memory operations, 25 but shift and merge two read words before writing. 26 27 3. Copy the few remaining bytes. 28 29 This is fast on processors that have at least 10 registers for 30 allocation by GCC, and that can access memory at reg+const in one 31 instruction. 32 33 I made an "exhaustive" test of this memmove when I wrote it, 34 exhaustive in the sense that I tried all alignment and length 35 combinations, with and without overlap. */ 36 37 #include <sysdeps/generic/memcopy.h> 38 39 /* The macros defined in this file are: 40 41 BYTE_COPY_FWD(dst_beg_ptr, src_beg_ptr, nbytes_to_copy) 42 43 BYTE_COPY_BWD(dst_end_ptr, src_end_ptr, nbytes_to_copy) 44 45 WORD_COPY_FWD(dst_beg_ptr, src_beg_ptr, nbytes_remaining, nbytes_to_copy) 46 47 WORD_COPY_BWD(dst_end_ptr, src_end_ptr, nbytes_remaining, nbytes_to_copy) 48 49 MERGE(old_word, sh_1, new_word, sh_2) 50 [I fail to understand. I feel stupid. --roland] 51 */ 52 53 54 /* Threshold value for when to enter the unrolled loops. */ 55 #undef OP_T_THRES 56 #define OP_T_THRES 16 57 58 /* Copy exactly NBYTES bytes from SRC_BP to DST_BP, 59 without any assumptions about alignment of the pointers. */ 60 #undef BYTE_COPY_FWD 61 #define BYTE_COPY_FWD(dst_bp, src_bp, nbytes) \ 62 do \ 63 { \ 64 size_t __nbytes = (nbytes); \ 65 if (__nbytes & 1) \ 66 { \ 67 ((byte *) dst_bp)[0] = ((byte *) src_bp)[0]; \ 68 src_bp += 1; \ 69 dst_bp += 1; \ 70 __nbytes -= 1; \ 71 } \ 72 while (__nbytes > 0) \ 73 { \ 74 byte __x = ((byte *) src_bp)[0]; \ 75 byte __y = ((byte *) src_bp)[1]; \ 76 src_bp += 2; \ 77 __nbytes -= 2; \ 78 ((byte *) dst_bp)[0] = __x; \ 79 ((byte *) dst_bp)[1] = __y; \ 80 dst_bp += 2; \ 81 } \ 82 } while (0) 83 84 /* Copy exactly NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR, 85 beginning at the bytes right before the pointers and continuing towards 86 smaller addresses. Don't assume anything about alignment of the 87 pointers. */ 88 #undef BYTE_COPY_BWD 89 #define BYTE_COPY_BWD(dst_ep, src_ep, nbytes) \ 90 do \ 91 { \ 92 size_t __nbytes = (nbytes); \ 93 if (__nbytes & 1) \ 94 { \ 95 src_ep -= 1; \ 96 dst_ep -= 1; \ 97 ((byte *) dst_ep)[0] = ((byte *) src_ep)[0]; \ 98 __nbytes -= 1; \ 99 } \ 100 while (__nbytes > 0) \ 101 { \ 102 byte __x, __y; \ 103 src_ep -= 2; \ 104 __y = ((byte *) src_ep)[1]; \ 105 __x = ((byte *) src_ep)[0]; \ 106 dst_ep -= 2; \ 107 __nbytes -= 2; \ 108 ((byte *) dst_ep)[1] = __y; \ 109 ((byte *) dst_ep)[0] = __x; \ 110 } \ 111 } while (0) 112 113 /* The powerpc memcpy implementation is safe to use for memmove. */ 114 #undef MEMCPY_OK_FOR_FWD_MEMMOVE 115 #define MEMCPY_OK_FOR_FWD_MEMMOVE 1 116