Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / generic / memcopy.h
blob49e53630dd8029aebb7504243c8db2e144cd687e
1 /* memcopy.h -- definitions for memory copy functions. Generic C version.
2 Copyright (C) 1991-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Torbjorn Granlund (tege@sics.se).
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 #ifndef _MEMCOPY_H
21 #define _MEMCOPY_H 1
23 /* The strategy of the memory functions is:
25 1. Copy bytes until the destination pointer is aligned.
27 2. Copy words in unrolled loops. If the source and destination
28 are not aligned in the same way, use word memory operations,
29 but shift and merge two read words before writing.
31 3. Copy the few remaining bytes.
33 This is fast on processors that have at least 10 registers for
34 allocation by GCC, and that can access memory at reg+const in one
35 instruction.
37 I made an "exhaustive" test of this memmove when I wrote it,
38 exhaustive in the sense that I tried all alignment and length
39 combinations, with and without overlap. */
41 #include <sys/cdefs.h>
42 #include <endian.h>
44 /* The macros defined in this file are:
46 BYTE_COPY_FWD(dst_beg_ptr, src_beg_ptr, nbytes_to_copy)
48 BYTE_COPY_BWD(dst_end_ptr, src_end_ptr, nbytes_to_copy)
50 WORD_COPY_FWD(dst_beg_ptr, src_beg_ptr, nbytes_remaining, nbytes_to_copy)
52 WORD_COPY_BWD(dst_end_ptr, src_end_ptr, nbytes_remaining, nbytes_to_copy)
54 MERGE(old_word, sh_1, new_word, sh_2)
55 [I fail to understand. I feel stupid. --roland]
58 /* Type to use for aligned memory operations.
59 This should normally be the biggest type supported by a single load
60 and store. */
61 #define op_t unsigned long int
62 #define OPSIZ (sizeof(op_t))
64 /* Type to use for unaligned operations. */
65 typedef unsigned char byte;
67 #if __BYTE_ORDER == __LITTLE_ENDIAN
68 #define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
69 #endif
70 #if __BYTE_ORDER == __BIG_ENDIAN
71 #define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
72 #endif
74 /* Copy exactly NBYTES bytes from SRC_BP to DST_BP,
75 without any assumptions about alignment of the pointers. */
76 #define BYTE_COPY_FWD(dst_bp, src_bp, nbytes) \
77 do \
78 { \
79 size_t __nbytes = (nbytes); \
80 while (__nbytes > 0) \
81 { \
82 byte __x = ((byte *) src_bp)[0]; \
83 src_bp += 1; \
84 __nbytes -= 1; \
85 ((byte *) dst_bp)[0] = __x; \
86 dst_bp += 1; \
87 } \
88 } while (0)
90 /* Copy exactly NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
91 beginning at the bytes right before the pointers and continuing towards
92 smaller addresses. Don't assume anything about alignment of the
93 pointers. */
94 #define BYTE_COPY_BWD(dst_ep, src_ep, nbytes) \
95 do \
96 { \
97 size_t __nbytes = (nbytes); \
98 while (__nbytes > 0) \
99 { \
100 byte __x; \
101 src_ep -= 1; \
102 __x = ((byte *) src_ep)[0]; \
103 dst_ep -= 1; \
104 __nbytes -= 1; \
105 ((byte *) dst_ep)[0] = __x; \
107 } while (0)
109 /* Copy *up to* NBYTES bytes from SRC_BP to DST_BP, with
110 the assumption that DST_BP is aligned on an OPSIZ multiple. If
111 not all bytes could be easily copied, store remaining number of bytes
112 in NBYTES_LEFT, otherwise store 0. */
113 extern void _wordcopy_fwd_aligned (long int, long int, size_t) __THROW;
114 extern void _wordcopy_fwd_dest_aligned (long int, long int, size_t) __THROW;
115 #define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes) \
116 do \
118 if (src_bp % OPSIZ == 0) \
119 _wordcopy_fwd_aligned (dst_bp, src_bp, (nbytes) / OPSIZ); \
120 else \
121 _wordcopy_fwd_dest_aligned (dst_bp, src_bp, (nbytes) / OPSIZ); \
122 src_bp += (nbytes) & -OPSIZ; \
123 dst_bp += (nbytes) & -OPSIZ; \
124 (nbytes_left) = (nbytes) % OPSIZ; \
125 } while (0)
127 /* Copy *up to* NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
128 beginning at the words (of type op_t) right before the pointers and
129 continuing towards smaller addresses. May take advantage of that
130 DST_END_PTR is aligned on an OPSIZ multiple. If not all bytes could be
131 easily copied, store remaining number of bytes in NBYTES_REMAINING,
132 otherwise store 0. */
133 extern void _wordcopy_bwd_aligned (long int, long int, size_t) __THROW;
134 extern void _wordcopy_bwd_dest_aligned (long int, long int, size_t) __THROW;
135 #define WORD_COPY_BWD(dst_ep, src_ep, nbytes_left, nbytes) \
136 do \
138 if (src_ep % OPSIZ == 0) \
139 _wordcopy_bwd_aligned (dst_ep, src_ep, (nbytes) / OPSIZ); \
140 else \
141 _wordcopy_bwd_dest_aligned (dst_ep, src_ep, (nbytes) / OPSIZ); \
142 src_ep -= (nbytes) & -OPSIZ; \
143 dst_ep -= (nbytes) & -OPSIZ; \
144 (nbytes_left) = (nbytes) % OPSIZ; \
145 } while (0)
148 /* Threshold value for when to enter the unrolled loops. */
149 #define OP_T_THRES 16
151 #endif /* memcopy.h */