Back to index

glibc  2.9
memcopy.h
Go to the documentation of this file.
00001 /* memcopy.h -- definitions for memory copy functions.  Generic C version.
00002    Copyright (C) 1991, 1992, 1993, 1997, 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Torbjorn Granlund (tege@sics.se).
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 /* The strategy of the memory functions is:
00022 
00023      1. Copy bytes until the destination pointer is aligned.
00024 
00025      2. Copy words in unrolled loops.  If the source and destination
00026      are not aligned in the same way, use word memory operations,
00027      but shift and merge two read words before writing.
00028 
00029      3. Copy the few remaining bytes.
00030 
00031    This is fast on processors that have at least 10 registers for
00032    allocation by GCC, and that can access memory at reg+const in one
00033    instruction.
00034 
00035    I made an "exhaustive" test of this memmove when I wrote it,
00036    exhaustive in the sense that I tried all alignment and length
00037    combinations, with and without overlap.  */
00038 
00039 #include <sys/cdefs.h>
00040 #include <endian.h>
00041 
00042 /* The macros defined in this file are:
00043 
00044    BYTE_COPY_FWD(dst_beg_ptr, src_beg_ptr, nbytes_to_copy)
00045 
00046    BYTE_COPY_BWD(dst_end_ptr, src_end_ptr, nbytes_to_copy)
00047 
00048    WORD_COPY_FWD(dst_beg_ptr, src_beg_ptr, nbytes_remaining, nbytes_to_copy)
00049 
00050    WORD_COPY_BWD(dst_end_ptr, src_end_ptr, nbytes_remaining, nbytes_to_copy)
00051 
00052    MERGE(old_word, sh_1, new_word, sh_2)
00053      [I fail to understand.  I feel stupid.  --roland]
00054 */
00055 
00056 /* Type to use for aligned memory operations.
00057    This should normally be the biggest type supported by a single load
00058    and store.  */
00059 #define       op_t   unsigned long int
00060 #define OPSIZ (sizeof(op_t))
00061 
00062 /* Type to use for unaligned operations.  */
00063 typedef unsigned char byte;
00064 
00065 /* Optimal type for storing bytes in registers.  */
00066 #define       reg_char      char
00067 
00068 #if __BYTE_ORDER == __LITTLE_ENDIAN
00069 #define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
00070 #endif
00071 #if __BYTE_ORDER == __BIG_ENDIAN
00072 #define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
00073 #endif
00074 
00075 /* Copy exactly NBYTES bytes from SRC_BP to DST_BP,
00076    without any assumptions about alignment of the pointers.  */
00077 #define BYTE_COPY_FWD(dst_bp, src_bp, nbytes)                               \
00078   do                                                                 \
00079     {                                                                \
00080       size_t __nbytes = (nbytes);                                    \
00081       while (__nbytes > 0)                                           \
00082        {                                                             \
00083          byte __x = ((byte *) src_bp)[0];                            \
00084          src_bp += 1;                                                       \
00085          __nbytes -= 1;                                              \
00086          ((byte *) dst_bp)[0] = __x;                                        \
00087          dst_bp += 1;                                                       \
00088        }                                                             \
00089     } while (0)
00090 
00091 /* Copy exactly NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
00092    beginning at the bytes right before the pointers and continuing towards
00093    smaller addresses.  Don't assume anything about alignment of the
00094    pointers.  */
00095 #define BYTE_COPY_BWD(dst_ep, src_ep, nbytes)                               \
00096   do                                                                 \
00097     {                                                                \
00098       size_t __nbytes = (nbytes);                                    \
00099       while (__nbytes > 0)                                           \
00100        {                                                             \
00101          byte __x;                                                   \
00102          src_ep -= 1;                                                       \
00103          __x = ((byte *) src_ep)[0];                                        \
00104          dst_ep -= 1;                                                       \
00105          __nbytes -= 1;                                              \
00106          ((byte *) dst_ep)[0] = __x;                                        \
00107        }                                                             \
00108     } while (0)
00109 
00110 /* Copy *up to* NBYTES bytes from SRC_BP to DST_BP, with
00111    the assumption that DST_BP is aligned on an OPSIZ multiple.  If
00112    not all bytes could be easily copied, store remaining number of bytes
00113    in NBYTES_LEFT, otherwise store 0.  */
00114 extern void _wordcopy_fwd_aligned (long int, long int, size_t) __THROW;
00115 extern void _wordcopy_fwd_dest_aligned (long int, long int, size_t) __THROW;
00116 #define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes)                  \
00117   do                                                                 \
00118     {                                                                \
00119       if (src_bp % OPSIZ == 0)                                              \
00120        _wordcopy_fwd_aligned (dst_bp, src_bp, (nbytes) / OPSIZ);            \
00121       else                                                           \
00122        _wordcopy_fwd_dest_aligned (dst_bp, src_bp, (nbytes) / OPSIZ);       \
00123       src_bp += (nbytes) & -OPSIZ;                                   \
00124       dst_bp += (nbytes) & -OPSIZ;                                   \
00125       (nbytes_left) = (nbytes) % OPSIZ;                                     \
00126     } while (0)
00127 
00128 /* Copy *up to* NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
00129    beginning at the words (of type op_t) right before the pointers and
00130    continuing towards smaller addresses.  May take advantage of that
00131    DST_END_PTR is aligned on an OPSIZ multiple.  If not all bytes could be
00132    easily copied, store remaining number of bytes in NBYTES_REMAINING,
00133    otherwise store 0.  */
00134 extern void _wordcopy_bwd_aligned (long int, long int, size_t) __THROW;
00135 extern void _wordcopy_bwd_dest_aligned (long int, long int, size_t) __THROW;
00136 #define WORD_COPY_BWD(dst_ep, src_ep, nbytes_left, nbytes)                  \
00137   do                                                                 \
00138     {                                                                \
00139       if (src_ep % OPSIZ == 0)                                              \
00140        _wordcopy_bwd_aligned (dst_ep, src_ep, (nbytes) / OPSIZ);            \
00141       else                                                           \
00142        _wordcopy_bwd_dest_aligned (dst_ep, src_ep, (nbytes) / OPSIZ);       \
00143       src_ep -= (nbytes) & -OPSIZ;                                   \
00144       dst_ep -= (nbytes) & -OPSIZ;                                   \
00145       (nbytes_left) = (nbytes) % OPSIZ;                                     \
00146     } while (0)
00147 
00148 
00149 /* Threshold value for when to enter the unrolled loops.  */
00150 #define       OP_T_THRES    16