memcopy.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. { \
  50. int __nbytes = (nbytes); \
  51. while (__nbytes > 0) \
  52. { \
  53. byte __x = ((byte *) src_bp)[0]; \
  54. src_bp += 1; \
  55. __nbytes -= 1; \
  56. ((byte *) dst_bp)[0] = __x; \
  57. dst_bp += 1; \
  58. } \
  59. } while (0)
  60. /* Copy exactly NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
  61. beginning at the bytes right before the pointers and continuing towards
  62. smaller addresses. Don't assume anything about alignment of the
  63. pointers. */
  64. #define BYTE_COPY_BWD(dst_ep, src_ep, nbytes) \
  65. do \
  66. { \
  67. int __nbytes = (nbytes); \
  68. while (__nbytes > 0) \
  69. { \
  70. byte __x; \
  71. src_ep -= 1; \
  72. __x = ((byte *) src_ep)[0]; \
  73. dst_ep -= 1; \
  74. __nbytes -= 1; \
  75. ((byte *) dst_ep)[0] = __x; \
  76. } \
  77. } while (0)
  78. /* Copy *up to* NBYTES bytes from SRC_BP to DST_BP, with
  79. the assumption that DST_BP is aligned on an OPSIZ multiple. If
  80. not all bytes could be easily copied, store remaining number of bytes
  81. in NBYTES_LEFT, otherwise store 0. */
  82. extern void _wordcopy_fwd_aligned (long int, long int, int);
  83. extern void _wordcopy_fwd_dest_aligned (long int, long int, int);
  84. #define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes) \
  85. do \
  86. { \
  87. if (src_bp % OPSIZ == 0) \
  88. _wordcopy_fwd_aligned (dst_bp, src_bp, (nbytes) / OPSIZ); \
  89. else \
  90. _wordcopy_fwd_dest_aligned (dst_bp, src_bp, (nbytes) / OPSIZ); \
  91. src_bp += (nbytes) & -OPSIZ; \
  92. dst_bp += (nbytes) & -OPSIZ; \
  93. (nbytes_left) = (nbytes) % OPSIZ; \
  94. } while (0)
  95. /* Copy *up to* NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
  96. beginning at the words (of type op_t) right before the pointers and
  97. continuing towards smaller addresses. May take advantage of that
  98. DST_END_PTR is aligned on an OPSIZ multiple. If not all bytes could be
  99. easily copied, store remaining number of bytes in NBYTES_REMAINING,
  100. otherwise store 0. */
  101. extern void _wordcopy_bwd_aligned (long int, long int, int);
  102. extern void _wordcopy_bwd_dest_aligned (long int, long int, int);
  103. #define WORD_COPY_BWD(dst_ep, src_ep, nbytes_left, nbytes) \
  104. do \
  105. { \
  106. if (src_ep % OPSIZ == 0) \
  107. _wordcopy_bwd_aligned (dst_ep, src_ep, (nbytes) / OPSIZ); \
  108. else \
  109. _wordcopy_bwd_dest_aligned (dst_ep, src_ep, (nbytes) / OPSIZ); \
  110. src_ep -= (nbytes) & -OPSIZ; \
  111. dst_ep -= (nbytes) & -OPSIZ; \
  112. (nbytes_left) = (nbytes) % OPSIZ; \
  113. } while (0)
  114. /* Threshold value for when to enter the unrolled loops. */
  115. #define OP_T_THRES 16