memcopy.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* memcopy.h -- definitions for memory copy functions. Generic C version.
  2. Copyright (C) 1991, 1992, 1993, 1997 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, see
  15. <http://www.gnu.org/licenses/>. */
  16. /* The strategy of the memory functions is:
  17. 1. Copy bytes until the destination pointer is aligned.
  18. 2. Copy words in unrolled loops. If the source and destination
  19. are not aligned in the same way, use word memory operations,
  20. but shift and merge two read words before writing.
  21. 3. Copy the few remaining bytes.
  22. This is fast on processors that have at least 10 registers for
  23. allocation by GCC, and that can access memory at reg+const in one
  24. instruction.
  25. I made an "exhaustive" test of this memmove when I wrote it,
  26. exhaustive in the sense that I tried all alignment and length
  27. combinations, with and without overlap. */
  28. #include <sys/cdefs.h>
  29. #include <endian.h>
  30. /* The macros defined in this file are:
  31. BYTE_COPY_FWD(dst_beg_ptr, src_beg_ptr, nbytes_to_copy)
  32. BYTE_COPY_BWD(dst_end_ptr, src_end_ptr, nbytes_to_copy)
  33. WORD_COPY_FWD(dst_beg_ptr, src_beg_ptr, nbytes_remaining, nbytes_to_copy)
  34. WORD_COPY_BWD(dst_end_ptr, src_end_ptr, nbytes_remaining, nbytes_to_copy)
  35. MERGE(old_word, sh_1, new_word, sh_2)
  36. [I fail to understand. I feel stupid. --roland]
  37. */
  38. /* Type to use for aligned memory operations.
  39. This should normally be the biggest type supported by a single load
  40. and store. */
  41. #define op_t unsigned long int
  42. #define OPSIZ (sizeof(op_t))
  43. /* Type to use for unaligned operations. */
  44. typedef unsigned char byte;
  45. /* Optimal type for storing bytes in registers. */
  46. #define reg_char char
  47. #if __BYTE_ORDER == __LITTLE_ENDIAN
  48. #define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
  49. #endif
  50. #if __BYTE_ORDER == __BIG_ENDIAN
  51. #define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
  52. #endif
  53. /* Copy exactly NBYTES bytes from SRC_BP to DST_BP,
  54. without any assumptions about alignment of the pointers. */
  55. #define BYTE_COPY_FWD(dst_bp, src_bp, nbytes) \
  56. do \
  57. { \
  58. size_t __nbytes = (nbytes); \
  59. while (__nbytes > 0) \
  60. { \
  61. byte __x = ((byte *) src_bp)[0]; \
  62. src_bp += 1; \
  63. __nbytes -= 1; \
  64. ((byte *) dst_bp)[0] = __x; \
  65. dst_bp += 1; \
  66. } \
  67. } while (0)
  68. /* Copy exactly NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
  69. beginning at the bytes right before the pointers and continuing towards
  70. smaller addresses. Don't assume anything about alignment of the
  71. pointers. */
  72. #define BYTE_COPY_BWD(dst_ep, src_ep, nbytes) \
  73. do \
  74. { \
  75. size_t __nbytes = (nbytes); \
  76. while (__nbytes > 0) \
  77. { \
  78. byte __x; \
  79. src_ep -= 1; \
  80. __x = ((byte *) src_ep)[0]; \
  81. dst_ep -= 1; \
  82. __nbytes -= 1; \
  83. ((byte *) dst_ep)[0] = __x; \
  84. } \
  85. } while (0)
  86. /* Copy *up to* NBYTES bytes from SRC_BP to DST_BP, with
  87. the assumption that DST_BP is aligned on an OPSIZ multiple. If
  88. not all bytes could be easily copied, store remaining number of bytes
  89. in NBYTES_LEFT, otherwise store 0. */
  90. /* extern void _wordcopy_fwd_aligned __P ((long int, long int, size_t)); */
  91. /* extern void _wordcopy_fwd_dest_aligned __P ((long int, long int, size_t)); */
  92. #define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes) \
  93. do \
  94. { \
  95. if (src_bp % OPSIZ == 0) \
  96. _wordcopy_fwd_aligned (dst_bp, src_bp, (nbytes) / OPSIZ); \
  97. else \
  98. _wordcopy_fwd_dest_aligned (dst_bp, src_bp, (nbytes) / OPSIZ); \
  99. src_bp += (nbytes) & -OPSIZ; \
  100. dst_bp += (nbytes) & -OPSIZ; \
  101. (nbytes_left) = (nbytes) % OPSIZ; \
  102. } while (0)
  103. /* Copy *up to* NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
  104. beginning at the words (of type op_t) right before the pointers and
  105. continuing towards smaller addresses. May take advantage of that
  106. DST_END_PTR is aligned on an OPSIZ multiple. If not all bytes could be
  107. easily copied, store remaining number of bytes in NBYTES_REMAINING,
  108. otherwise store 0. */
  109. /* extern void _wordcopy_bwd_aligned __P ((long int, long int, size_t)); */
  110. /* extern void _wordcopy_bwd_dest_aligned __P ((long int, long int, size_t)); */
  111. #define WORD_COPY_BWD(dst_ep, src_ep, nbytes_left, nbytes) \
  112. do \
  113. { \
  114. if (src_ep % OPSIZ == 0) \
  115. _wordcopy_bwd_aligned (dst_ep, src_ep, (nbytes) / OPSIZ); \
  116. else \
  117. _wordcopy_bwd_dest_aligned (dst_ep, src_ep, (nbytes) / OPSIZ); \
  118. src_ep -= (nbytes) & -OPSIZ; \
  119. dst_ep -= (nbytes) & -OPSIZ; \
  120. (nbytes_left) = (nbytes) % OPSIZ; \
  121. } while (0)
  122. /* Threshold value for when to enter the unrolled loops. */
  123. #define OP_T_THRES 16