memcpy.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /********************************************************************
  2. ** File: memcpy.c
  3. **
  4. ** Copyright (C) 1999-2010 Daniel Vik
  5. **
  6. ** This software is provided 'as-is', without any express or implied
  7. ** warranty. In no event will the authors be held liable for any
  8. ** damages arising from the use of this software.
  9. ** Permission is granted to anyone to use this software for any
  10. ** purpose, including commercial applications, and to alter it and
  11. ** redistribute it freely, subject to the following restrictions:
  12. **
  13. ** 1. The origin of this software must not be misrepresented; you
  14. ** must not claim that you wrote the original software. If you
  15. ** use this software in a product, an acknowledgment in the
  16. ** use this software in a product, an acknowledgment in the
  17. ** product documentation would be appreciated but is not
  18. ** required.
  19. **
  20. ** 2. Altered source versions must be plainly marked as such, and
  21. ** must not be misrepresented as being the original software.
  22. **
  23. ** 3. This notice may not be removed or altered from any source
  24. ** distribution.
  25. **
  26. **
  27. ** Description: Implementation of the standard library function memcpy.
  28. ** This implementation of memcpy() is ANSI-C89 compatible.
  29. **
  30. ** The following configuration options can be set:
  31. **
  32. ** LITTLE_ENDIAN - Uses processor with little endian
  33. ** addressing. Default is big endian.
  34. **
  35. ** PRE_INC_PTRS - Use pre increment of pointers.
  36. ** Default is post increment of
  37. ** pointers.
  38. **
  39. ** INDEXED_COPY - Copying data using array indexing.
  40. ** Using this option, disables the
  41. ** PRE_INC_PTRS option.
  42. **
  43. ** MEMCPY_64BIT - Compiles memcpy for 64 bit
  44. ** architectures
  45. **
  46. **
  47. ** Best Settings:
  48. **
  49. ** Intel x86: LITTLE_ENDIAN and INDEXED_COPY
  50. **
  51. *******************************************************************/
  52. /********************************************************************
  53. ** Configuration definitions.
  54. *******************************************************************/
  55. #define LITTLE_ENDIAN
  56. #define INDEXED_COPY
  57. /********************************************************************
  58. ** Includes for size_t definition
  59. *******************************************************************/
  60. #include <stddef.h>
  61. /********************************************************************
  62. ** Typedefs
  63. *******************************************************************/
  64. typedef unsigned char UInt8;
  65. typedef unsigned short UInt16;
  66. typedef unsigned int UInt32;
  67. #ifdef _WIN32
  68. typedef unsigned __int64 UInt64;
  69. #else
  70. typedef unsigned long long UInt64;
  71. #endif
  72. #ifdef MEMCPY_64BIT
  73. typedef UInt64 UIntN;
  74. #define TYPE_WIDTH 8L
  75. #else
  76. typedef UInt32 UIntN;
  77. #define TYPE_WIDTH 4L
  78. #endif
  79. /********************************************************************
  80. ** Remove definitions when INDEXED_COPY is defined.
  81. *******************************************************************/
  82. #if defined (INDEXED_COPY)
  83. #if defined (PRE_INC_PTRS)
  84. #undef PRE_INC_PTRS
  85. #endif /*PRE_INC_PTRS*/
  86. #endif /*INDEXED_COPY*/
  87. /********************************************************************
  88. ** Definitions for pre and post increment of pointers.
  89. *******************************************************************/
  90. #if defined (PRE_INC_PTRS)
  91. #define START_VAL(x) (x)--
  92. #define INC_VAL(x) *++(x)
  93. #define CAST_TO_U8(p, o) ((UInt8*)p + o + TYPE_WIDTH)
  94. #define WHILE_DEST_BREAK (TYPE_WIDTH - 1)
  95. #define PRE_LOOP_ADJUST - (TYPE_WIDTH - 1)
  96. #define PRE_SWITCH_ADJUST + 1
  97. #else /*PRE_INC_PTRS*/
  98. #define START_VAL(x)
  99. #define INC_VAL(x) *(x)++
  100. #define CAST_TO_U8(p, o) ((UInt8*)p + o)
  101. #define WHILE_DEST_BREAK 0
  102. #define PRE_LOOP_ADJUST
  103. #define PRE_SWITCH_ADJUST
  104. #endif /*PRE_INC_PTRS*/
  105. /********************************************************************
  106. ** Definitions for endians
  107. *******************************************************************/
  108. #if defined (LITTLE_ENDIAN)
  109. #define SHL >>
  110. #define SHR <<
  111. #else /* LITTLE_ENDIAN */
  112. #define SHL <<
  113. #define SHR >>
  114. #endif /* LITTLE_ENDIAN */
  115. /********************************************************************
  116. ** Macros for copying words of different alignment.
  117. ** Uses incremening pointers.
  118. *******************************************************************/
  119. #define CP_INCR() { \
  120. INC_VAL(dstN) = INC_VAL(srcN); \
  121. }
  122. #define CP_INCR_SH(shl, shr) { \
  123. dstWord = srcWord SHL shl; \
  124. srcWord = INC_VAL(srcN); \
  125. dstWord |= srcWord SHR shr; \
  126. INC_VAL(dstN) = dstWord; \
  127. }
  128. /********************************************************************
  129. ** Macros for copying words of different alignment.
  130. ** Uses array indexes.
  131. *******************************************************************/
  132. #define CP_INDEX(idx) { \
  133. dstN[idx] = srcN[idx]; \
  134. }
  135. #define CP_INDEX_SH(x, shl, shr) { \
  136. dstWord = srcWord SHL shl; \
  137. srcWord = srcN[x]; \
  138. dstWord |= srcWord SHR shr; \
  139. dstN[x] = dstWord; \
  140. }
  141. /********************************************************************
  142. ** Macros for copying words of different alignment.
  143. ** Uses incremening pointers or array indexes depending on
  144. ** configuration.
  145. *******************************************************************/
  146. #if defined (INDEXED_COPY)
  147. #define CP(idx) CP_INDEX(idx)
  148. #define CP_SH(idx, shl, shr) CP_INDEX_SH(idx, shl, shr)
  149. #define INC_INDEX(p, o) ((p) += (o))
  150. #else /* INDEXED_COPY */
  151. #define CP(idx) CP_INCR()
  152. #define CP_SH(idx, shl, shr) CP_INCR_SH(shl, shr)
  153. #define INC_INDEX(p, o)
  154. #endif /* INDEXED_COPY */
  155. #define COPY_REMAINING(count) { \
  156. START_VAL(dst8); \
  157. START_VAL(src8); \
  158. \
  159. switch (count) { \
  160. case 7: INC_VAL(dst8) = INC_VAL(src8); \
  161. case 6: INC_VAL(dst8) = INC_VAL(src8); \
  162. case 5: INC_VAL(dst8) = INC_VAL(src8); \
  163. case 4: INC_VAL(dst8) = INC_VAL(src8); \
  164. case 3: INC_VAL(dst8) = INC_VAL(src8); \
  165. case 2: INC_VAL(dst8) = INC_VAL(src8); \
  166. case 1: INC_VAL(dst8) = INC_VAL(src8); \
  167. case 0: \
  168. default: break; \
  169. } \
  170. }
  171. #define COPY_NO_SHIFT() { \
  172. UIntN* dstN = (UIntN*)(dst8 PRE_LOOP_ADJUST); \
  173. UIntN* srcN = (UIntN*)(src8 PRE_LOOP_ADJUST); \
  174. size_t length = count / TYPE_WIDTH; \
  175. \
  176. while (length & 7) { \
  177. CP_INCR(); \
  178. length--; \
  179. } \
  180. \
  181. length /= 8; \
  182. \
  183. while (length--) { \
  184. CP(0); \
  185. CP(1); \
  186. CP(2); \
  187. CP(3); \
  188. CP(4); \
  189. CP(5); \
  190. CP(6); \
  191. CP(7); \
  192. \
  193. INC_INDEX(dstN, 8); \
  194. INC_INDEX(srcN, 8); \
  195. } \
  196. \
  197. src8 = CAST_TO_U8(srcN, 0); \
  198. dst8 = CAST_TO_U8(dstN, 0); \
  199. \
  200. COPY_REMAINING(count & (TYPE_WIDTH - 1)); \
  201. \
  202. return dest; \
  203. }
  204. #define COPY_SHIFT(shift) { \
  205. UIntN* dstN = (UIntN*)((((UIntN)dst8) PRE_LOOP_ADJUST) & \
  206. ~(TYPE_WIDTH - 1)); \
  207. UIntN* srcN = (UIntN*)((((UIntN)src8) PRE_LOOP_ADJUST) & \
  208. ~(TYPE_WIDTH - 1)); \
  209. size_t length = count / TYPE_WIDTH; \
  210. UIntN srcWord = INC_VAL(srcN); \
  211. UIntN dstWord; \
  212. \
  213. while (length & 7) { \
  214. CP_INCR_SH(8 * shift, 8 * (TYPE_WIDTH - shift)); \
  215. length--; \
  216. } \
  217. \
  218. length /= 8; \
  219. \
  220. while (length--) { \
  221. CP_SH(0, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  222. CP_SH(1, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  223. CP_SH(2, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  224. CP_SH(3, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  225. CP_SH(4, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  226. CP_SH(5, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  227. CP_SH(6, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  228. CP_SH(7, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  229. \
  230. INC_INDEX(dstN, 8); \
  231. INC_INDEX(srcN, 8); \
  232. } \
  233. \
  234. src8 = CAST_TO_U8(srcN, (shift - TYPE_WIDTH)); \
  235. dst8 = CAST_TO_U8(dstN, 0); \
  236. \
  237. COPY_REMAINING(count & (TYPE_WIDTH - 1)); \
  238. \
  239. return dest; \
  240. }
  241. /********************************************************************
  242. **
  243. ** void *memcpy(void *dest, const void *src, size_t count)
  244. **
  245. ** Args: dest - pointer to destination buffer
  246. ** src - pointer to source buffer
  247. ** count - number of bytes to copy
  248. **
  249. ** Return: A pointer to destination buffer
  250. **
  251. ** Purpose: Copies count bytes from src to dest.
  252. ** No overlap check is performed.
  253. **
  254. *******************************************************************/
  255. void *fast_memcpy(void *dest, const void *src, size_t count)
  256. {
  257. UInt8* dst8 = (UInt8*)dest;
  258. UInt8* src8 = (UInt8*)src;
  259. if (count < 8) {
  260. COPY_REMAINING(count);
  261. return dest;
  262. }
  263. START_VAL(dst8);
  264. START_VAL(src8);
  265. while (((UIntN)dst8 & (TYPE_WIDTH - 1)) != WHILE_DEST_BREAK) {
  266. INC_VAL(dst8) = INC_VAL(src8);
  267. count--;
  268. }
  269. switch ((((UIntN)src8) PRE_SWITCH_ADJUST) & (TYPE_WIDTH - 1)) {
  270. case 0: COPY_NO_SHIFT(); break;
  271. case 1: COPY_SHIFT(1); break;
  272. case 2: COPY_SHIFT(2); break;
  273. case 3: COPY_SHIFT(3); break;
  274. #if TYPE_WIDTH > 4
  275. case 4: COPY_SHIFT(4); break;
  276. case 5: COPY_SHIFT(5); break;
  277. case 6: COPY_SHIFT(6); break;
  278. case 7: COPY_SHIFT(7); break;
  279. #endif
  280. }
  281. }