memcpy.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Extracted from GLIBC memcpy.c and memcopy.h, which is:
  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, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <linux/types.h>
  17. /* Type to use for aligned memory operations.
  18. This should normally be the biggest type supported by a single load
  19. and store. */
  20. #define op_t unsigned long int
  21. #define OPSIZ (sizeof(op_t))
  22. /* Optimal type for storing bytes in registers. */
  23. #define reg_char char
  24. #define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
  25. /* Copy exactly NBYTES bytes from SRC_BP to DST_BP,
  26. without any assumptions about alignment of the pointers. */
  27. #define BYTE_COPY_FWD(dst_bp, src_bp, nbytes) \
  28. do { \
  29. size_t __nbytes = (nbytes); \
  30. while (__nbytes > 0) { \
  31. unsigned char __x = ((unsigned char *) src_bp)[0]; \
  32. src_bp += 1; \
  33. __nbytes -= 1; \
  34. ((unsigned char *) dst_bp)[0] = __x; \
  35. dst_bp += 1; \
  36. } \
  37. } while (0)
  38. /* Copy *up to* NBYTES bytes from SRC_BP to DST_BP, with
  39. the assumption that DST_BP is aligned on an OPSIZ multiple. If
  40. not all bytes could be easily copied, store remaining number of bytes
  41. in NBYTES_LEFT, otherwise store 0. */
  42. /* extern void _wordcopy_fwd_aligned __P ((long int, long int, size_t)); */
  43. /* extern void _wordcopy_fwd_dest_aligned __P ((long int, long int, size_t)); */
  44. #define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes) \
  45. do { \
  46. if (src_bp % OPSIZ == 0) \
  47. _wordcopy_fwd_aligned(dst_bp, src_bp, (nbytes) / OPSIZ);\
  48. else \
  49. _wordcopy_fwd_dest_aligned(dst_bp, src_bp, (nbytes) / OPSIZ);\
  50. src_bp += (nbytes) & -OPSIZ; \
  51. dst_bp += (nbytes) & -OPSIZ; \
  52. (nbytes_left) = (nbytes) % OPSIZ; \
  53. } while (0)
  54. /* Threshold value for when to enter the unrolled loops. */
  55. #define OP_T_THRES 16
  56. /* _wordcopy_fwd_aligned -- Copy block beginning at SRCP to
  57. block beginning at DSTP with LEN `op_t' words (not LEN bytes!).
  58. Both SRCP and DSTP should be aligned for memory operations on `op_t's. */
  59. /* stream-lined (read x8 + write x8) */
  60. static void _wordcopy_fwd_aligned(long int dstp, long int srcp, size_t len)
  61. {
  62. while (len > 7) {
  63. register op_t a0, a1, a2, a3, a4, a5, a6, a7;
  64. a0 = ((op_t *) srcp)[0];
  65. a1 = ((op_t *) srcp)[1];
  66. a2 = ((op_t *) srcp)[2];
  67. a3 = ((op_t *) srcp)[3];
  68. a4 = ((op_t *) srcp)[4];
  69. a5 = ((op_t *) srcp)[5];
  70. a6 = ((op_t *) srcp)[6];
  71. a7 = ((op_t *) srcp)[7];
  72. ((op_t *) dstp)[0] = a0;
  73. ((op_t *) dstp)[1] = a1;
  74. ((op_t *) dstp)[2] = a2;
  75. ((op_t *) dstp)[3] = a3;
  76. ((op_t *) dstp)[4] = a4;
  77. ((op_t *) dstp)[5] = a5;
  78. ((op_t *) dstp)[6] = a6;
  79. ((op_t *) dstp)[7] = a7;
  80. srcp += 8 * OPSIZ;
  81. dstp += 8 * OPSIZ;
  82. len -= 8;
  83. }
  84. while (len > 0) {
  85. *(op_t *)dstp = *(op_t *)srcp;
  86. srcp += OPSIZ;
  87. dstp += OPSIZ;
  88. len -= 1;
  89. }
  90. }
  91. /* _wordcopy_fwd_dest_aligned -- Copy block beginning at SRCP to
  92. block beginning at DSTP with LEN `op_t' words (not LEN bytes!).
  93. DSTP should be aligned for memory operations on `op_t's, but SRCP must
  94. *not* be aligned. */
  95. /* stream-lined (read x4 + write x4) */
  96. static void _wordcopy_fwd_dest_aligned(long int dstp, long int srcp,
  97. size_t len)
  98. {
  99. op_t ap;
  100. int sh_1, sh_2;
  101. /* Calculate how to shift a word read at the memory operation
  102. aligned srcp to make it aligned for copy. */
  103. sh_1 = 8 * (srcp % OPSIZ);
  104. sh_2 = 8 * OPSIZ - sh_1;
  105. /* Make SRCP aligned by rounding it down to the beginning of the `op_t'
  106. it points in the middle of. */
  107. srcp &= -OPSIZ;
  108. ap = ((op_t *) srcp)[0];
  109. srcp += OPSIZ;
  110. while (len > 3) {
  111. op_t a0, a1, a2, a3;
  112. a0 = ((op_t *) srcp)[0];
  113. a1 = ((op_t *) srcp)[1];
  114. a2 = ((op_t *) srcp)[2];
  115. a3 = ((op_t *) srcp)[3];
  116. ((op_t *) dstp)[0] = MERGE(ap, sh_1, a0, sh_2);
  117. ((op_t *) dstp)[1] = MERGE(a0, sh_1, a1, sh_2);
  118. ((op_t *) dstp)[2] = MERGE(a1, sh_1, a2, sh_2);
  119. ((op_t *) dstp)[3] = MERGE(a2, sh_1, a3, sh_2);
  120. ap = a3;
  121. srcp += 4 * OPSIZ;
  122. dstp += 4 * OPSIZ;
  123. len -= 4;
  124. }
  125. while (len > 0) {
  126. register op_t a0;
  127. a0 = ((op_t *) srcp)[0];
  128. ((op_t *) dstp)[0] = MERGE(ap, sh_1, a0, sh_2);
  129. ap = a0;
  130. srcp += OPSIZ;
  131. dstp += OPSIZ;
  132. len -= 1;
  133. }
  134. }
  135. void *memcpy(void *dstpp, const void *srcpp, size_t len)
  136. {
  137. unsigned long int dstp = (long int) dstpp;
  138. unsigned long int srcp = (long int) srcpp;
  139. /* Copy from the beginning to the end. */
  140. /* If there not too few bytes to copy, use word copy. */
  141. if (len >= OP_T_THRES) {
  142. /* Copy just a few bytes to make DSTP aligned. */
  143. len -= (-dstp) % OPSIZ;
  144. BYTE_COPY_FWD(dstp, srcp, (-dstp) % OPSIZ);
  145. /* Copy whole pages from SRCP to DSTP by virtual address
  146. manipulation, as much as possible. */
  147. /* PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len); */
  148. /* Copy from SRCP to DSTP taking advantage of the known
  149. alignment of DSTP. Number of bytes remaining is put in the
  150. third argument, i.e. in LEN. This number may vary from
  151. machine to machine. */
  152. WORD_COPY_FWD(dstp, srcp, len, len);
  153. /* Fall out and copy the tail. */
  154. }
  155. /* There are just a few bytes to copy. Use byte memory operations. */
  156. BYTE_COPY_FWD(dstp, srcp, len);
  157. return dstpp;
  158. }
  159. void *memcpyb(void *dstpp, const void *srcpp, unsigned len)
  160. {
  161. unsigned long int dstp = (long int) dstpp;
  162. unsigned long int srcp = (long int) srcpp;
  163. BYTE_COPY_FWD(dstp, srcp, len);
  164. return dstpp;
  165. }