memcpy.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. #include "memcpy.h"
  62. /********************************************************************
  63. ** Typedefs
  64. *******************************************************************/
  65. typedef unsigned char UInt8;
  66. typedef unsigned short UInt16;
  67. typedef unsigned int UInt32;
  68. #ifdef _WIN32
  69. typedef unsigned __int64 UInt64;
  70. #else
  71. typedef unsigned long long UInt64;
  72. #endif
  73. #ifdef MEMCPY_64BIT
  74. typedef UInt64 UIntN;
  75. #define TYPE_WIDTH 8L
  76. #else
  77. typedef UInt32 UIntN;
  78. #define TYPE_WIDTH 4L
  79. #endif
  80. /********************************************************************
  81. ** Remove definitions when INDEXED_COPY is defined.
  82. *******************************************************************/
  83. #if defined (INDEXED_COPY)
  84. #if defined (PRE_INC_PTRS)
  85. #undef PRE_INC_PTRS
  86. #endif /*PRE_INC_PTRS*/
  87. #endif /*INDEXED_COPY*/
  88. /********************************************************************
  89. ** Definitions for pre and post increment of pointers.
  90. *******************************************************************/
  91. #if defined (PRE_INC_PTRS)
  92. #define START_VAL(x) (x)--
  93. #define INC_VAL(x) *++(x)
  94. #define CAST_TO_U8(p, o) ((UInt8*)p + o + TYPE_WIDTH)
  95. #define WHILE_DEST_BREAK (TYPE_WIDTH - 1)
  96. #define PRE_LOOP_ADJUST - (TYPE_WIDTH - 1)
  97. #define PRE_SWITCH_ADJUST + 1
  98. #else /*PRE_INC_PTRS*/
  99. #define START_VAL(x)
  100. #define INC_VAL(x) *(x)++
  101. #define CAST_TO_U8(p, o) ((UInt8*)p + o)
  102. #define WHILE_DEST_BREAK 0
  103. #define PRE_LOOP_ADJUST
  104. #define PRE_SWITCH_ADJUST
  105. #endif /*PRE_INC_PTRS*/
  106. /********************************************************************
  107. ** Definitions for endians
  108. *******************************************************************/
  109. #if defined (LITTLE_ENDIAN)
  110. #define SHL >>
  111. #define SHR <<
  112. #else /* LITTLE_ENDIAN */
  113. #define SHL <<
  114. #define SHR >>
  115. #endif /* LITTLE_ENDIAN */
  116. /********************************************************************
  117. ** Macros for copying words of different alignment.
  118. ** Uses incremening pointers.
  119. *******************************************************************/
  120. #define CP_INCR() { \
  121. INC_VAL(dstN) = INC_VAL(srcN); \
  122. }
  123. #define CP_INCR_SH(shl, shr) { \
  124. dstWord = srcWord SHL shl; \
  125. srcWord = INC_VAL(srcN); \
  126. dstWord |= srcWord SHR shr; \
  127. INC_VAL(dstN) = dstWord; \
  128. }
  129. /********************************************************************
  130. ** Macros for copying words of different alignment.
  131. ** Uses array indexes.
  132. *******************************************************************/
  133. #define CP_INDEX(idx) { \
  134. dstN[idx] = srcN[idx]; \
  135. }
  136. #define CP_INDEX_SH(x, shl, shr) { \
  137. dstWord = srcWord SHL shl; \
  138. srcWord = srcN[x]; \
  139. dstWord |= srcWord SHR shr; \
  140. dstN[x] = dstWord; \
  141. }
  142. /********************************************************************
  143. ** Macros for copying words of different alignment.
  144. ** Uses incremening pointers or array indexes depending on
  145. ** configuration.
  146. *******************************************************************/
  147. #if defined (INDEXED_COPY)
  148. #define CP(idx) CP_INDEX(idx)
  149. #define CP_SH(idx, shl, shr) CP_INDEX_SH(idx, shl, shr)
  150. #define INC_INDEX(p, o) ((p) += (o))
  151. #else /* INDEXED_COPY */
  152. #define CP(idx) CP_INCR()
  153. #define CP_SH(idx, shl, shr) CP_INCR_SH(shl, shr)
  154. #define INC_INDEX(p, o)
  155. #endif /* INDEXED_COPY */
  156. #define COPY_REMAINING(count) { \
  157. START_VAL(dst8); \
  158. START_VAL(src8); \
  159. \
  160. switch (count) { \
  161. case 7: INC_VAL(dst8) = INC_VAL(src8); \
  162. case 6: INC_VAL(dst8) = INC_VAL(src8); \
  163. case 5: INC_VAL(dst8) = INC_VAL(src8); \
  164. case 4: INC_VAL(dst8) = INC_VAL(src8); \
  165. case 3: INC_VAL(dst8) = INC_VAL(src8); \
  166. case 2: INC_VAL(dst8) = INC_VAL(src8); \
  167. case 1: INC_VAL(dst8) = INC_VAL(src8); \
  168. case 0: \
  169. default: break; \
  170. } \
  171. }
  172. #define COPY_NO_SHIFT() { \
  173. UIntN* dstN = (UIntN*)(dst8 PRE_LOOP_ADJUST); \
  174. UIntN* srcN = (UIntN*)(src8 PRE_LOOP_ADJUST); \
  175. size_t length = count / TYPE_WIDTH; \
  176. \
  177. while (length & 7) { \
  178. CP_INCR(); \
  179. length--; \
  180. } \
  181. \
  182. length /= 8; \
  183. \
  184. while (length--) { \
  185. CP(0); \
  186. CP(1); \
  187. CP(2); \
  188. CP(3); \
  189. CP(4); \
  190. CP(5); \
  191. CP(6); \
  192. CP(7); \
  193. \
  194. INC_INDEX(dstN, 8); \
  195. INC_INDEX(srcN, 8); \
  196. } \
  197. \
  198. src8 = CAST_TO_U8(srcN, 0); \
  199. dst8 = CAST_TO_U8(dstN, 0); \
  200. \
  201. COPY_REMAINING(count & (TYPE_WIDTH - 1)); \
  202. \
  203. return dest; \
  204. }
  205. #define COPY_SHIFT(shift) { \
  206. UIntN* dstN = (UIntN*)((((UIntN)dst8) PRE_LOOP_ADJUST) & \
  207. ~(TYPE_WIDTH - 1)); \
  208. UIntN* srcN = (UIntN*)((((UIntN)src8) PRE_LOOP_ADJUST) & \
  209. ~(TYPE_WIDTH - 1)); \
  210. size_t length = count / TYPE_WIDTH; \
  211. UIntN srcWord = INC_VAL(srcN); \
  212. UIntN dstWord; \
  213. \
  214. while (length & 7) { \
  215. CP_INCR_SH(8 * shift, 8 * (TYPE_WIDTH - shift)); \
  216. length--; \
  217. } \
  218. \
  219. length /= 8; \
  220. \
  221. while (length--) { \
  222. CP_SH(0, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  223. CP_SH(1, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  224. CP_SH(2, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  225. CP_SH(3, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  226. CP_SH(4, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  227. CP_SH(5, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  228. CP_SH(6, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  229. CP_SH(7, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  230. \
  231. INC_INDEX(dstN, 8); \
  232. INC_INDEX(srcN, 8); \
  233. } \
  234. \
  235. src8 = CAST_TO_U8(srcN, (shift - TYPE_WIDTH)); \
  236. dst8 = CAST_TO_U8(dstN, 0); \
  237. \
  238. COPY_REMAINING(count & (TYPE_WIDTH - 1)); \
  239. \
  240. return dest; \
  241. }
  242. /********************************************************************
  243. **
  244. ** void *memcpy(void *dest, const void *src, size_t count)
  245. **
  246. ** Args: dest - pointer to destination buffer
  247. ** src - pointer to source buffer
  248. ** count - number of bytes to copy
  249. **
  250. ** Return: A pointer to destination buffer
  251. **
  252. ** Purpose: Copies count bytes from src to dest.
  253. ** No overlap check is performed.
  254. **
  255. *******************************************************************/
  256. void *memcpy(void *dest, const void *src, size_t count)
  257. {
  258. UInt8* dst8 = (UInt8*)dest;
  259. UInt8* src8 = (UInt8*)src;
  260. if (count < 8) {
  261. COPY_REMAINING(count);
  262. return dest;
  263. }
  264. START_VAL(dst8);
  265. START_VAL(src8);
  266. while (((UIntN)dst8 & (TYPE_WIDTH - 1)) != WHILE_DEST_BREAK) {
  267. INC_VAL(dst8) = INC_VAL(src8);
  268. count--;
  269. }
  270. switch ((((UIntN)src8) PRE_SWITCH_ADJUST) & (TYPE_WIDTH - 1)) {
  271. case 0: COPY_NO_SHIFT(); break;
  272. case 1: COPY_SHIFT(1); break;
  273. case 2: COPY_SHIFT(2); break;
  274. case 3: COPY_SHIFT(3); break;
  275. #if TYPE_WIDTH > 4
  276. case 4: COPY_SHIFT(4); break;
  277. case 5: COPY_SHIFT(5); break;
  278. case 6: COPY_SHIFT(6); break;
  279. case 7: COPY_SHIFT(7); break;
  280. #endif
  281. }
  282. return dest;
  283. }