memcopy.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* memcopy.h -- definitions for memory copy functions. Generic C version.
  2. Copyright (C) 1991, 1992, 1993, 1997, 2004 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Torbjorn Granlund (tege@sics.se).
  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. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. /* The strategy of the memory functions is:
  18. 1. Copy bytes until the destination pointer is aligned.
  19. 2. Copy words in unrolled loops. If the source and destination
  20. are not aligned in the same way, use word memory operations,
  21. but shift and merge two read words before writing.
  22. 3. Copy the few remaining bytes.
  23. This is fast on processors that have at least 10 registers for
  24. allocation by GCC, and that can access memory at reg+const in one
  25. instruction.
  26. I made an "exhaustive" test of this memmove when I wrote it,
  27. exhaustive in the sense that I tried all alignment and length
  28. combinations, with and without overlap. */
  29. #include <host_endian.h>
  30. /* Type to use for aligned memory operations.
  31. This should normally be the biggest type supported by a single load
  32. and store. */
  33. #define op_t unsigned long int
  34. #define OPSIZ (sizeof(op_t))
  35. /* Type to use for unaligned operations. */
  36. typedef unsigned char byte;
  37. /* Optimal type for storing bytes in registers. */
  38. #define reg_char char
  39. #if __BYTE_ORDER == __LITTLE_ENDIAN
  40. #define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
  41. #endif
  42. #if __BYTE_ORDER == __BIG_ENDIAN
  43. #define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
  44. #endif
  45. /* Copy exactly NBYTES bytes from SRC_BP to DST_BP,
  46. without any assumptions about alignment of the pointers. */
  47. #define BYTE_COPY_FWD(dst_bp, src_bp, nbytes) \
  48. do { \
  49. int __nbytes = (nbytes); \
  50. while (__nbytes > 0) { \
  51. byte __x = ((byte*)src_bp)[0]; \
  52. src_bp += 1; \
  53. __nbytes -= 1; \
  54. ((byte*)dst_bp)[0] = __x; \
  55. dst_bp += 1; \
  56. } \
  57. } while (0)
  58. /* Copy exactly NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
  59. beginning at the bytes right before the pointers and continuing towards
  60. smaller addresses. Don't assume anything about alignment of the
  61. pointers. */
  62. #define BYTE_COPY_BWD(dst_ep, src_ep, nbytes) \
  63. do { \
  64. int __nbytes = (nbytes); \
  65. while (__nbytes > 0) { \
  66. byte __x; \
  67. src_ep -= 1; \
  68. __x = ((byte*)src_ep)[0]; \
  69. dst_ep -= 1; \
  70. __nbytes -= 1; \
  71. ((byte*)dst_ep)[0] = __x; \
  72. } \
  73. } while (0)
  74. /* Copy *up to* NBYTES bytes from SRC_BP to DST_BP, with
  75. the assumption that DST_BP is aligned on an OPSIZ multiple. If
  76. not all bytes could be easily copied, store remaining number of bytes
  77. in NBYTES_LEFT, otherwise store 0. */
  78. extern void _wordcopy_fwd_aligned(long int, long int, int);
  79. extern void _wordcopy_fwd_dest_aligned(long int, long int, int);
  80. #define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes) \
  81. do { \
  82. if (src_bp % OPSIZ == 0) \
  83. _wordcopy_fwd_aligned(dst_bp, src_bp, (nbytes) / OPSIZ); \
  84. else \
  85. _wordcopy_fwd_dest_aligned(dst_bp, src_bp, (nbytes) / OPSIZ); \
  86. src_bp += (nbytes) & -OPSIZ; \
  87. dst_bp += (nbytes) & -OPSIZ; \
  88. (nbytes_left) = (nbytes) % OPSIZ; \
  89. } while (0)
  90. /* Copy *up to* NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
  91. beginning at the words (of type op_t) right before the pointers and
  92. continuing towards smaller addresses. May take advantage of that
  93. DST_END_PTR is aligned on an OPSIZ multiple. If not all bytes could be
  94. easily copied, store remaining number of bytes in NBYTES_REMAINING,
  95. otherwise store 0. */
  96. extern void _wordcopy_bwd_aligned(long int, long int, int);
  97. extern void _wordcopy_bwd_dest_aligned(long int, long int, int);
  98. #define WORD_COPY_BWD(dst_ep, src_ep, nbytes_left, nbytes) \
  99. do { \
  100. if (src_ep % OPSIZ == 0) \
  101. _wordcopy_bwd_aligned(dst_ep, src_ep, (nbytes) / OPSIZ); \
  102. else \
  103. _wordcopy_bwd_dest_aligned(dst_ep, src_ep, (nbytes) / OPSIZ); \
  104. src_ep -= (nbytes) & -OPSIZ; \
  105. dst_ep -= (nbytes) & -OPSIZ; \
  106. (nbytes_left) = (nbytes) % OPSIZ; \
  107. } while (0)
  108. /* Threshold value for when to enter the unrolled loops. */
  109. #define OP_T_THRES 16