memcpy.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. ********************************************************************************
  3. **
  4. ** \file ./boot/startup/src/memcpy.c
  5. **
  6. ** \version $Id: memcpy.c 7112 2011-08-15 15:24:45Z dkless $
  7. **
  8. ** \brief ARM1176 function retargeting.
  9. **
  10. ** This files implements an optimized memcpy().
  11. **
  12. ** \attention THIS SAMPLE CODE IS PROVIDED AS IS. FUJITSU SEMICONDUCTOR
  13. ** ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
  14. ** OMMISSIONS.
  15. **
  16. ** (C) Copyright 2006-2010 by Fujitsu Microelectronics Europe GmbH
  17. **
  18. *****************************************************************************
  19. */
  20. /********************************************************************
  21. ** File: memcpy.c
  22. **
  23. ** Copyright (C) 1999-2010 Daniel Vik
  24. **
  25. ** This software is provided 'as-is', without any express or implied
  26. ** warranty. In no event will the authors be held liable for any
  27. ** damages arising from the use of this software.
  28. ** Permission is granted to anyone to use this software for any
  29. ** purpose, including commercial applications, and to alter it and
  30. ** redistribute it freely, subject to the following restrictions:
  31. **
  32. ** 1. The origin of this software must not be misrepresented; you
  33. ** must not claim that you wrote the original software. If you
  34. ** use this software in a product, an acknowledgment in the
  35. ** product documentation would be appreciated but is not
  36. ** required.
  37. **
  38. ** 2. Altered source versions must be plainly marked as such, and
  39. ** must not be misrepresented as being the original software.
  40. **
  41. ** 3. This notice may not be removed or altered from any source
  42. ** distribution.
  43. **
  44. **
  45. ** Description: Implementation of the standard library function memcpy.
  46. ** This implementation of memcpy() is ANSI-C89 compatible.
  47. **
  48. ** The following configuration options can be set:
  49. **
  50. ** LITTLE_ENDIAN - Uses processor with little endian
  51. ** addressing. Default is big endian.
  52. **
  53. ** PRE_INC_PTRS - Use pre increment of pointers.
  54. ** Default is post increment of
  55. ** pointers.
  56. **
  57. ** INDEXED_COPY - Copying data using array indexing.
  58. ** Using this option, disables the
  59. ** PRE_INC_PTRS option.
  60. **
  61. ** MEMCPY_64BIT - Compiles memcpy for 64 bit
  62. ** architectures
  63. **
  64. **
  65. ** Best Settings:
  66. **
  67. ** Intel x86: LITTLE_ENDIAN and INDEXED_COPY
  68. **
  69. *******************************************************************/
  70. /*lint -save -e* */
  71. /********************************************************************
  72. ** Configuration definitions.
  73. *******************************************************************/
  74. #define LITTLE_ENDIAN
  75. #define INDEXED_COPY
  76. #define MEMCPY_64BIT
  77. /********************************************************************
  78. ** Includes for size_t definition
  79. *******************************************************************/
  80. #include <stddef.h>
  81. #include <stdint.h>
  82. /********************************************************************
  83. ** Typedefs
  84. *******************************************************************/
  85. /*lint -save -e751 */
  86. typedef unsigned char UInt8;
  87. typedef unsigned short UInt16;
  88. typedef unsigned int UInt32;
  89. #ifdef _WIN32
  90. typedef unsigned __int64 UInt64;
  91. #else
  92. typedef unsigned long long UInt64;
  93. #endif
  94. #ifdef MEMCPY_64BIT
  95. typedef UInt64 UIntN;
  96. #define TYPE_WIDTH 8L
  97. #else
  98. typedef UInt32 UIntN;
  99. #define TYPE_WIDTH 4L
  100. #endif
  101. /********************************************************************
  102. ** Remove definitions when INDEXED_COPY is defined.
  103. *******************************************************************/
  104. #if defined (INDEXED_COPY)
  105. #if defined (PRE_INC_PTRS)
  106. #undef PRE_INC_PTRS
  107. #endif /*PRE_INC_PTRS*/
  108. #endif /*INDEXED_COPY*/
  109. /********************************************************************
  110. ** Definitions for pre and post increment of pointers.
  111. *******************************************************************/
  112. #if defined (PRE_INC_PTRS)
  113. #define START_VAL(x) (x)--
  114. #define INC_VAL(x) *++(x)
  115. #define CAST_TO_U8(p, o) ((UInt8*)p + o + TYPE_WIDTH)
  116. #define WHILE_DEST_BREAK (TYPE_WIDTH - 1)
  117. #define PRE_LOOP_ADJUST - (TYPE_WIDTH - 1)
  118. #define PRE_SWITCH_ADJUST + 1
  119. #else /*PRE_INC_PTRS*/
  120. #define START_VAL(x)
  121. #define INC_VAL(x) *(x)++
  122. #define CAST_TO_U8(p, o) ((UInt8*)p + o)
  123. #define WHILE_DEST_BREAK 0
  124. #define PRE_LOOP_ADJUST
  125. #define PRE_SWITCH_ADJUST
  126. #endif /*PRE_INC_PTRS*/
  127. /********************************************************************
  128. ** Definitions for endians
  129. *******************************************************************/
  130. #if defined (LITTLE_ENDIAN)
  131. #define SHL >>
  132. #define SHR <<
  133. #else /* LITTLE_ENDIAN */
  134. #define SHL <<
  135. #define SHR >>
  136. #endif /* LITTLE_ENDIAN */
  137. /********************************************************************
  138. ** Macros for copying words of different alignment.
  139. ** Uses incremening pointers.
  140. *******************************************************************/
  141. #define CP_INCR() { \
  142. INC_VAL(dstN) = INC_VAL(srcN); \
  143. }
  144. #define CP_INCR_SH(shl, shr) { \
  145. dstWord = srcWord SHL shl; \
  146. srcWord = INC_VAL(srcN); \
  147. dstWord |= srcWord SHR shr; \
  148. INC_VAL(dstN) = dstWord; \
  149. }
  150. /********************************************************************
  151. ** Macros for copying words of different alignment.
  152. ** Uses array indexes.
  153. *******************************************************************/
  154. #define CP_INDEX(idx) { \
  155. dstN[idx] = srcN[idx]; \
  156. }
  157. #define CP_INDEX_SH(x, shl, shr) { \
  158. dstWord = srcWord SHL shl; \
  159. srcWord = srcN[x]; \
  160. dstWord |= srcWord SHR shr; \
  161. dstN[x] = dstWord; \
  162. }
  163. /********************************************************************
  164. ** Macros for copying words of different alignment.
  165. ** Uses incremening pointers or array indexes depending on
  166. ** configuration.
  167. *******************************************************************/
  168. #if defined (INDEXED_COPY)
  169. #define CP(idx) CP_INDEX(idx)
  170. #define CP_SH(idx, shl, shr) CP_INDEX_SH(idx, shl, shr)
  171. #define INC_INDEX(p, o) ((p) += (o))
  172. #else /* INDEXED_COPY */
  173. #define CP(idx) CP_INCR()
  174. #define CP_SH(idx, shl, shr) CP_INCR_SH(shl, shr)
  175. #define INC_INDEX(p, o)
  176. #endif /* INDEXED_COPY */
  177. #define COPY_REMAINING(count) { \
  178. START_VAL(dst8); \
  179. START_VAL(src8); \
  180. \
  181. switch (count) { \
  182. case 7: INC_VAL(dst8) = INC_VAL(src8); \
  183. case 6: INC_VAL(dst8) = INC_VAL(src8); \
  184. case 5: INC_VAL(dst8) = INC_VAL(src8); \
  185. case 4: INC_VAL(dst8) = INC_VAL(src8); \
  186. case 3: INC_VAL(dst8) = INC_VAL(src8); \
  187. case 2: INC_VAL(dst8) = INC_VAL(src8); \
  188. case 1: INC_VAL(dst8) = INC_VAL(src8); \
  189. case 0: \
  190. default: break; \
  191. } \
  192. }
  193. #define COPY_NO_SHIFT() { \
  194. UIntN* dstN = (UIntN*)(dst8 PRE_LOOP_ADJUST); \
  195. UIntN* srcN = (UIntN*)(src8 PRE_LOOP_ADJUST); \
  196. size_t length = count / TYPE_WIDTH; \
  197. \
  198. while (length & 7) { \
  199. CP_INCR(); \
  200. length--; \
  201. } \
  202. \
  203. length /= 8; \
  204. \
  205. while (length--) { \
  206. CP(0); \
  207. CP(1); \
  208. CP(2); \
  209. CP(3); \
  210. CP(4); \
  211. CP(5); \
  212. CP(6); \
  213. CP(7); \
  214. \
  215. INC_INDEX(dstN, 8); \
  216. INC_INDEX(srcN, 8); \
  217. } \
  218. \
  219. src8 = CAST_TO_U8(srcN, 0); \
  220. dst8 = CAST_TO_U8(dstN, 0); \
  221. \
  222. COPY_REMAINING(count & (TYPE_WIDTH - 1)); \
  223. \
  224. return dest; \
  225. }
  226. #define COPY_SHIFT(shift) { \
  227. UIntN* dstN = (UIntN*)(intptr_t)((((UIntN)(intptr_t)dst8) PRE_LOOP_ADJUST) & \
  228. ~(TYPE_WIDTH - 1)); \
  229. UIntN* srcN = (UIntN*)(intptr_t)((((UIntN)(intptr_t)src8) PRE_LOOP_ADJUST) & \
  230. ~(TYPE_WIDTH - 1)); \
  231. size_t length = count / TYPE_WIDTH; \
  232. UIntN srcWord = INC_VAL(srcN); \
  233. UIntN dstWord; \
  234. \
  235. while (length & 7) { \
  236. CP_INCR_SH(8 * shift, 8 * (TYPE_WIDTH - shift)); \
  237. length--; \
  238. } \
  239. \
  240. length /= 8; \
  241. \
  242. while (length--) { \
  243. CP_SH(0, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  244. CP_SH(1, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  245. CP_SH(2, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  246. CP_SH(3, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  247. CP_SH(4, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  248. CP_SH(5, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  249. CP_SH(6, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  250. CP_SH(7, 8 * shift, 8 * (TYPE_WIDTH - shift)); \
  251. \
  252. INC_INDEX(dstN, 8); \
  253. INC_INDEX(srcN, 8); \
  254. } \
  255. \
  256. src8 = CAST_TO_U8(srcN, (shift - TYPE_WIDTH)); \
  257. dst8 = CAST_TO_U8(dstN, 0); \
  258. \
  259. COPY_REMAINING(count & (TYPE_WIDTH - 1)); \
  260. \
  261. return dest; \
  262. }
  263. /********************************************************************
  264. **
  265. ** void *memcpy(void *dest, const void *src, size_t count)
  266. **
  267. ** Args: dest - pointer to destination buffer
  268. ** src - pointer to source buffer
  269. ** count - number of bytes to copy
  270. **
  271. ** Return: A pointer to destination buffer
  272. **
  273. ** Purpose: Copies count bytes from src to dest.
  274. ** No overlap check is performed.
  275. **
  276. *******************************************************************/
  277. #if 0//use libc 's api to improve performance.
  278. void *memcpy(void *dest, const void *src, size_t count)
  279. {
  280. UInt8* dst8 = (UInt8*)dest;
  281. UInt8* src8 = (UInt8*)src;
  282. if (count < 8) {
  283. COPY_REMAINING(count);
  284. return dest;
  285. }
  286. START_VAL(dst8);
  287. START_VAL(src8);
  288. while (((UIntN)(intptr_t)dst8 & (TYPE_WIDTH - 1)) != WHILE_DEST_BREAK) {
  289. INC_VAL(dst8) = INC_VAL(src8);
  290. count--;
  291. }
  292. switch ((((UIntN)(intptr_t)src8) PRE_SWITCH_ADJUST) & (TYPE_WIDTH - 1)) {
  293. case 0: COPY_NO_SHIFT();
  294. case 1: COPY_SHIFT(1);
  295. case 2: COPY_SHIFT(2);
  296. case 3: COPY_SHIFT(3);
  297. #if TYPE_WIDTH >= 4
  298. case 4: COPY_SHIFT(4);
  299. case 5: COPY_SHIFT(5);
  300. case 6: COPY_SHIFT(6);
  301. case 7: COPY_SHIFT(7);
  302. #endif
  303. }
  304. return 0;
  305. }
  306. /**
  307. * memset - Fill a region of memory with the given value
  308. * @s: Pointer to the start of the area.
  309. * @c: The byte to fill the area with
  310. * @count: The size of the area.
  311. *
  312. * Do not use memset() to access IO space, use memset_io() instead.
  313. */
  314. void * memset(void * s,int c,size_t count)
  315. {
  316. unsigned long *sl = (unsigned long *) s;
  317. unsigned long cl = 0;
  318. char *s8;
  319. int i;
  320. /* do it one word at a time (32 bits or 64 bits) while possible */
  321. if ( ((unsigned long)s & (sizeof(*sl) - 1)) == 0) {
  322. for (i = 0; i < sizeof(*sl); i++) {
  323. cl <<= 8;
  324. cl |= c & 0xff;
  325. }
  326. while (count >= sizeof(*sl)) {
  327. *sl++ = cl;
  328. count -= sizeof(*sl);
  329. }
  330. }
  331. /* fill 8 bits at a time */
  332. s8 = (char *)sl;
  333. while (count--)
  334. *s8++ = c;
  335. return s;
  336. }
  337. /**
  338. * memmove - Copy one area of memory to another
  339. * @dest: Where to copy to
  340. * @src: Where to copy from
  341. * @count: The size of the area.
  342. *
  343. * Unlike memcpy(), memmove() copes with overlapping areas.
  344. */
  345. void * memmove(void * dest,const void *src,size_t count)
  346. {
  347. char *tmp, *s;
  348. if (src == dest)
  349. return dest;
  350. if (dest <= src) {
  351. tmp = (char *) dest;
  352. s = (char *) src;
  353. while (count--)
  354. *tmp++ = *s++;
  355. }
  356. else {
  357. tmp = (char *) dest + count;
  358. s = (char *) src + count;
  359. while (count--)
  360. *--tmp = *--s;
  361. }
  362. return dest;
  363. }
  364. /**
  365. * memcmp - Compare two areas of memory
  366. * @cs: One area of memory
  367. * @ct: Another area of memory
  368. * @count: The size of the area.
  369. */
  370. int memcmp(const void * cs,const void * ct,size_t count)
  371. {
  372. const unsigned char *su1, *su2;
  373. int res = 0;
  374. for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  375. if ((res = *su1 - *su2) != 0)
  376. break;
  377. return res;
  378. }
  379. #endif
  380. /*lint -restore */