memcopy.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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, 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 <sys/cdefs.h>
  30. #include <endian.h>
  31. /* The macros defined in this file are:
  32. BYTE_COPY_FWD(dst_beg_ptr, src_beg_ptr, nbytes_to_copy)
  33. BYTE_COPY_BWD(dst_end_ptr, src_end_ptr, nbytes_to_copy)
  34. WORD_COPY_FWD(dst_beg_ptr, src_beg_ptr, nbytes_remaining, nbytes_to_copy)
  35. WORD_COPY_BWD(dst_end_ptr, src_end_ptr, nbytes_remaining, nbytes_to_copy)
  36. MERGE(old_word, sh_1, new_word, sh_2)
  37. [I fail to understand. I feel stupid. --roland]
  38. */
  39. /* Type to use for aligned memory operations.
  40. This should normally be the biggest type supported by a single load
  41. and store. */
  42. #define op_t unsigned long int
  43. #define OPSIZ (sizeof(op_t))
  44. /* Type to use for unaligned operations. */
  45. typedef unsigned char byte;
  46. /* Optimal type for storing bytes in registers. */
  47. #define reg_char char
  48. #if __BYTE_ORDER == __LITTLE_ENDIAN
  49. #define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
  50. #endif
  51. #if __BYTE_ORDER == __BIG_ENDIAN
  52. #define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
  53. #endif
  54. /* Copy exactly NBYTES bytes from SRC_BP to DST_BP,
  55. without any assumptions about alignment of the pointers. */
  56. #define BYTE_COPY_FWD(dst_bp, src_bp, nbytes) \
  57. do \
  58. { \
  59. size_t __nbytes = (nbytes); \
  60. while (__nbytes > 0) \
  61. { \
  62. byte __x = ((byte *) src_bp)[0]; \
  63. src_bp += 1; \
  64. __nbytes -= 1; \
  65. ((byte *) dst_bp)[0] = __x; \
  66. dst_bp += 1; \
  67. } \
  68. } while (0)
  69. /* Copy exactly NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
  70. beginning at the bytes right before the pointers and continuing towards
  71. smaller addresses. Don't assume anything about alignment of the
  72. pointers. */
  73. #define BYTE_COPY_BWD(dst_ep, src_ep, nbytes) \
  74. do \
  75. { \
  76. size_t __nbytes = (nbytes); \
  77. while (__nbytes > 0) \
  78. { \
  79. byte __x; \
  80. src_ep -= 1; \
  81. __x = ((byte *) src_ep)[0]; \
  82. dst_ep -= 1; \
  83. __nbytes -= 1; \
  84. ((byte *) dst_ep)[0] = __x; \
  85. } \
  86. } while (0)
  87. /* Copy *up to* NBYTES bytes from SRC_BP to DST_BP, with
  88. the assumption that DST_BP is aligned on an OPSIZ multiple. If
  89. not all bytes could be easily copied, store remaining number of bytes
  90. in NBYTES_LEFT, otherwise store 0. */
  91. /* extern void _wordcopy_fwd_aligned __P ((long int, long int, size_t)); */
  92. /* extern void _wordcopy_fwd_dest_aligned __P ((long int, long int, size_t)); */
  93. #define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes) \
  94. do \
  95. { \
  96. if (src_bp % OPSIZ == 0) \
  97. _wordcopy_fwd_aligned (dst_bp, src_bp, (nbytes) / OPSIZ); \
  98. else \
  99. _wordcopy_fwd_dest_aligned (dst_bp, src_bp, (nbytes) / OPSIZ); \
  100. src_bp += (nbytes) & -OPSIZ; \
  101. dst_bp += (nbytes) & -OPSIZ; \
  102. (nbytes_left) = (nbytes) % OPSIZ; \
  103. } while (0)
  104. /* Copy *up to* NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
  105. beginning at the words (of type op_t) right before the pointers and
  106. continuing towards smaller addresses. May take advantage of that
  107. DST_END_PTR is aligned on an OPSIZ multiple. If not all bytes could be
  108. easily copied, store remaining number of bytes in NBYTES_REMAINING,
  109. otherwise store 0. */
  110. /* extern void _wordcopy_bwd_aligned __P ((long int, long int, size_t)); */
  111. /* extern void _wordcopy_bwd_dest_aligned __P ((long int, long int, size_t)); */
  112. #define WORD_COPY_BWD(dst_ep, src_ep, nbytes_left, nbytes) \
  113. do \
  114. { \
  115. if (src_ep % OPSIZ == 0) \
  116. _wordcopy_bwd_aligned (dst_ep, src_ep, (nbytes) / OPSIZ); \
  117. else \
  118. _wordcopy_bwd_dest_aligned (dst_ep, src_ep, (nbytes) / OPSIZ); \
  119. src_ep -= (nbytes) & -OPSIZ; \
  120. dst_ep -= (nbytes) & -OPSIZ; \
  121. (nbytes_left) = (nbytes) % OPSIZ; \
  122. } while (0)
  123. /* Threshold value for when to enter the unrolled loops. */
  124. #define OP_T_THRES 16