memcpy.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Copy memory to memory until the specified number of bytes
  2. has been copied. Overlap is NOT handled correctly.
  3. Copyright (C) 1991, 1997, 2003 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Torbjorn Granlund (tege@sics.se).
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, write to the Free
  16. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. 02111-1307 USA. */
  18. #include <string.h>
  19. #include "memcopy.h"
  20. #include "pagecopy.h"
  21. libc_hidden_proto(memcpy)
  22. /* _wordcopy_fwd_aligned -- Copy block beginning at SRCP to
  23. block beginning at DSTP with LEN `op_t' words (not LEN bytes!).
  24. Both SRCP and DSTP should be aligned for memory operations on `op_t's. */
  25. static void _wordcopy_fwd_aligned (long int dstp, long int srcp, size_t len)
  26. {
  27. op_t a0, a1;
  28. switch (len % 8)
  29. {
  30. case 2:
  31. a0 = ((op_t *) srcp)[0];
  32. srcp -= 6 * OPSIZ;
  33. dstp -= 7 * OPSIZ;
  34. len += 6;
  35. goto do1;
  36. case 3:
  37. a1 = ((op_t *) srcp)[0];
  38. srcp -= 5 * OPSIZ;
  39. dstp -= 6 * OPSIZ;
  40. len += 5;
  41. goto do2;
  42. case 4:
  43. a0 = ((op_t *) srcp)[0];
  44. srcp -= 4 * OPSIZ;
  45. dstp -= 5 * OPSIZ;
  46. len += 4;
  47. goto do3;
  48. case 5:
  49. a1 = ((op_t *) srcp)[0];
  50. srcp -= 3 * OPSIZ;
  51. dstp -= 4 * OPSIZ;
  52. len += 3;
  53. goto do4;
  54. case 6:
  55. a0 = ((op_t *) srcp)[0];
  56. srcp -= 2 * OPSIZ;
  57. dstp -= 3 * OPSIZ;
  58. len += 2;
  59. goto do5;
  60. case 7:
  61. a1 = ((op_t *) srcp)[0];
  62. srcp -= 1 * OPSIZ;
  63. dstp -= 2 * OPSIZ;
  64. len += 1;
  65. goto do6;
  66. case 0:
  67. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  68. return;
  69. a0 = ((op_t *) srcp)[0];
  70. srcp -= 0 * OPSIZ;
  71. dstp -= 1 * OPSIZ;
  72. goto do7;
  73. case 1:
  74. a1 = ((op_t *) srcp)[0];
  75. srcp -=-1 * OPSIZ;
  76. dstp -= 0 * OPSIZ;
  77. len -= 1;
  78. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  79. goto do0;
  80. goto do8; /* No-op. */
  81. }
  82. do
  83. {
  84. do8:
  85. a0 = ((op_t *) srcp)[0];
  86. ((op_t *) dstp)[0] = a1;
  87. do7:
  88. a1 = ((op_t *) srcp)[1];
  89. ((op_t *) dstp)[1] = a0;
  90. do6:
  91. a0 = ((op_t *) srcp)[2];
  92. ((op_t *) dstp)[2] = a1;
  93. do5:
  94. a1 = ((op_t *) srcp)[3];
  95. ((op_t *) dstp)[3] = a0;
  96. do4:
  97. a0 = ((op_t *) srcp)[4];
  98. ((op_t *) dstp)[4] = a1;
  99. do3:
  100. a1 = ((op_t *) srcp)[5];
  101. ((op_t *) dstp)[5] = a0;
  102. do2:
  103. a0 = ((op_t *) srcp)[6];
  104. ((op_t *) dstp)[6] = a1;
  105. do1:
  106. a1 = ((op_t *) srcp)[7];
  107. ((op_t *) dstp)[7] = a0;
  108. srcp += 8 * OPSIZ;
  109. dstp += 8 * OPSIZ;
  110. len -= 8;
  111. }
  112. while (len != 0);
  113. /* This is the right position for do0. Please don't move
  114. it into the loop. */
  115. do0:
  116. ((op_t *) dstp)[0] = a1;
  117. }
  118. /* _wordcopy_fwd_dest_aligned -- Copy block beginning at SRCP to
  119. block beginning at DSTP with LEN `op_t' words (not LEN bytes!).
  120. DSTP should be aligned for memory operations on `op_t's, but SRCP must
  121. *not* be aligned. */
  122. static void _wordcopy_fwd_dest_aligned (long int dstp, long int srcp, size_t len)
  123. {
  124. op_t a0, a1, a2, a3;
  125. int sh_1, sh_2;
  126. /* Calculate how to shift a word read at the memory operation
  127. aligned srcp to make it aligned for copy. */
  128. sh_1 = 8 * (srcp % OPSIZ);
  129. sh_2 = 8 * OPSIZ - sh_1;
  130. /* Make SRCP aligned by rounding it down to the beginning of the `op_t'
  131. it points in the middle of. */
  132. srcp &= -OPSIZ;
  133. switch (len % 4)
  134. {
  135. case 2:
  136. a1 = ((op_t *) srcp)[0];
  137. a2 = ((op_t *) srcp)[1];
  138. srcp -= 1 * OPSIZ;
  139. dstp -= 3 * OPSIZ;
  140. len += 2;
  141. goto do1;
  142. case 3:
  143. a0 = ((op_t *) srcp)[0];
  144. a1 = ((op_t *) srcp)[1];
  145. srcp -= 0 * OPSIZ;
  146. dstp -= 2 * OPSIZ;
  147. len += 1;
  148. goto do2;
  149. case 0:
  150. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  151. return;
  152. a3 = ((op_t *) srcp)[0];
  153. a0 = ((op_t *) srcp)[1];
  154. srcp -=-1 * OPSIZ;
  155. dstp -= 1 * OPSIZ;
  156. len += 0;
  157. goto do3;
  158. case 1:
  159. a2 = ((op_t *) srcp)[0];
  160. a3 = ((op_t *) srcp)[1];
  161. srcp -=-2 * OPSIZ;
  162. dstp -= 0 * OPSIZ;
  163. len -= 1;
  164. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  165. goto do0;
  166. goto do4; /* No-op. */
  167. }
  168. do
  169. {
  170. do4:
  171. a0 = ((op_t *) srcp)[0];
  172. ((op_t *) dstp)[0] = MERGE (a2, sh_1, a3, sh_2);
  173. do3:
  174. a1 = ((op_t *) srcp)[1];
  175. ((op_t *) dstp)[1] = MERGE (a3, sh_1, a0, sh_2);
  176. do2:
  177. a2 = ((op_t *) srcp)[2];
  178. ((op_t *) dstp)[2] = MERGE (a0, sh_1, a1, sh_2);
  179. do1:
  180. a3 = ((op_t *) srcp)[3];
  181. ((op_t *) dstp)[3] = MERGE (a1, sh_1, a2, sh_2);
  182. srcp += 4 * OPSIZ;
  183. dstp += 4 * OPSIZ;
  184. len -= 4;
  185. }
  186. while (len != 0);
  187. /* This is the right position for do0. Please don't move
  188. it into the loop. */
  189. do0:
  190. ((op_t *) dstp)[0] = MERGE (a2, sh_1, a3, sh_2);
  191. }
  192. void *memcpy (void *dstpp, const void *srcpp, size_t len)
  193. {
  194. unsigned long int dstp = (long int) dstpp;
  195. unsigned long int srcp = (long int) srcpp;
  196. /* Copy from the beginning to the end. */
  197. /* If there not too few bytes to copy, use word copy. */
  198. if (len >= OP_T_THRES)
  199. {
  200. /* Copy just a few bytes to make DSTP aligned. */
  201. len -= (-dstp) % OPSIZ;
  202. BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
  203. /* Copy whole pages from SRCP to DSTP by virtual address manipulation,
  204. as much as possible. */
  205. PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len);
  206. /* Copy from SRCP to DSTP taking advantage of the known alignment of
  207. DSTP. Number of bytes remaining is put in the third argument,
  208. i.e. in LEN. This number may vary from machine to machine. */
  209. WORD_COPY_FWD (dstp, srcp, len, len);
  210. /* Fall out and copy the tail. */
  211. }
  212. /* There are just a few bytes to copy. Use byte memory operations. */
  213. BYTE_COPY_FWD (dstp, srcp, len);
  214. return dstpp;
  215. }
  216. libc_hidden_def(memcpy)