You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

memcpy_test.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * memcpy() self-tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <stdint.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <ipxe/test.h>
  35. #include <ipxe/profile.h>
  36. /** Number of sample iterations for profiling */
  37. #define PROFILE_COUNT 16
  38. /* Provide global functions to allow inspection of generated code */
  39. void memcpy_0 ( void *dest, void *src ) { memcpy ( dest, src, 0 ); }
  40. void memcpy_1 ( void *dest, void *src ) { memcpy ( dest, src, 1 ); }
  41. void memcpy_2 ( void *dest, void *src ) { memcpy ( dest, src, 2 ); }
  42. void memcpy_3 ( void *dest, void *src ) { memcpy ( dest, src, 3 ); }
  43. void memcpy_4 ( void *dest, void *src ) { memcpy ( dest, src, 4 ); }
  44. void memcpy_5 ( void *dest, void *src ) { memcpy ( dest, src, 5 ); }
  45. void memcpy_6 ( void *dest, void *src ) { memcpy ( dest, src, 6 ); }
  46. void memcpy_7 ( void *dest, void *src ) { memcpy ( dest, src, 7 ); }
  47. void memcpy_8 ( void *dest, void *src ) { memcpy ( dest, src, 8 ); }
  48. void memcpy_9 ( void *dest, void *src ) { memcpy ( dest, src, 9 ); }
  49. void memcpy_10 ( void *dest, void *src ) { memcpy ( dest, src, 10 ); }
  50. void memcpy_11 ( void *dest, void *src ) { memcpy ( dest, src, 11 ); }
  51. void memcpy_12 ( void *dest, void *src ) { memcpy ( dest, src, 12 ); }
  52. void memcpy_13 ( void *dest, void *src ) { memcpy ( dest, src, 13 ); }
  53. void memcpy_14 ( void *dest, void *src ) { memcpy ( dest, src, 14 ); }
  54. void memcpy_15 ( void *dest, void *src ) { memcpy ( dest, src, 15 ); }
  55. void memcpy_16 ( void *dest, void *src ) { memcpy ( dest, src, 16 ); }
  56. void memcpy_17 ( void *dest, void *src ) { memcpy ( dest, src, 17 ); }
  57. void memcpy_18 ( void *dest, void *src ) { memcpy ( dest, src, 18 ); }
  58. void memcpy_19 ( void *dest, void *src ) { memcpy ( dest, src, 19 ); }
  59. void memcpy_20 ( void *dest, void *src ) { memcpy ( dest, src, 20 ); }
  60. void memcpy_21 ( void *dest, void *src ) { memcpy ( dest, src, 21 ); }
  61. void memcpy_22 ( void *dest, void *src ) { memcpy ( dest, src, 22 ); }
  62. void memcpy_23 ( void *dest, void *src ) { memcpy ( dest, src, 23 ); }
  63. void memcpy_24 ( void *dest, void *src ) { memcpy ( dest, src, 24 ); }
  64. void memcpy_25 ( void *dest, void *src ) { memcpy ( dest, src, 25 ); }
  65. void memcpy_26 ( void *dest, void *src ) { memcpy ( dest, src, 26 ); }
  66. void memcpy_27 ( void *dest, void *src ) { memcpy ( dest, src, 27 ); }
  67. void memcpy_28 ( void *dest, void *src ) { memcpy ( dest, src, 28 ); }
  68. void memcpy_29 ( void *dest, void *src ) { memcpy ( dest, src, 29 ); }
  69. void memcpy_30 ( void *dest, void *src ) { memcpy ( dest, src, 30 ); }
  70. void memcpy_31 ( void *dest, void *src ) { memcpy ( dest, src, 31 ); }
  71. /**
  72. * Force a call to the variable-length implementation of memcpy()
  73. *
  74. * @v dest Destination address
  75. * @v src Source address
  76. * @v len Length of data
  77. * @ret dest Destination address
  78. */
  79. __attribute__ (( noinline )) void * memcpy_var ( void *dest, const void *src,
  80. size_t len ) {
  81. return memcpy ( dest, src, len );
  82. }
  83. /**
  84. * Perform a constant-length memcpy() test
  85. *
  86. * ... Data to copy
  87. */
  88. #define MEMCPY_TEST_CONSTANT( ... ) do { \
  89. static const uint8_t src[] = { __VA_ARGS__ }; \
  90. uint8_t dest_const[ 1 + sizeof ( src ) + 1 ]; \
  91. uint8_t dest_var[ 1 + sizeof ( src ) + 1 ]; \
  92. \
  93. dest_const[0] = 0x33; \
  94. dest_const[ sizeof ( dest_const ) - 1 ] = 0x44; \
  95. memcpy ( ( dest_const + 1 ), src, \
  96. ( sizeof ( dest_const ) - 2 ) ); \
  97. ok ( dest_const[0] == 0x33 ); \
  98. ok ( dest_const[ sizeof ( dest_const ) - 1 ] == 0x44 ); \
  99. ok ( memcmp ( ( dest_const + 1 ), src, \
  100. ( sizeof ( dest_const ) - 2 ) ) == 0 ); \
  101. \
  102. dest_var[0] = 0x55; \
  103. dest_var[ sizeof ( dest_var ) - 1 ] = 0x66; \
  104. memcpy_var ( ( dest_var + 1 ), src, \
  105. ( sizeof ( dest_var ) - 2 ) ); \
  106. ok ( dest_var[0] == 0x55 ); \
  107. ok ( dest_var[ sizeof ( dest_var ) - 1 ] == 0x66 ); \
  108. ok ( memcmp ( ( dest_var + 1 ), src, \
  109. ( sizeof ( dest_var ) - 2 ) ) == 0 ); \
  110. } while ( 0 )
  111. /**
  112. * Test memcpy() speed
  113. *
  114. * @v dest_offset Destination alignment offset
  115. * @v src_offset Source alignment offset
  116. * @v len Length of data to copy
  117. */
  118. static void memcpy_test_speed ( unsigned int dest_offset,
  119. unsigned int src_offset, size_t len ) {
  120. struct profiler profiler;
  121. uint8_t *dest;
  122. uint8_t *src;
  123. unsigned int i;
  124. /* Allocate blocks */
  125. dest = malloc ( len + dest_offset );
  126. assert ( dest != NULL );
  127. src = malloc ( len + src_offset );
  128. assert ( src != NULL );
  129. /* Generate random source data */
  130. for ( i = 0 ; i < len ; i++ )
  131. src[ src_offset + i ] = random();
  132. /* Check correctness of copied data */
  133. memcpy ( ( dest + dest_offset ), ( src + src_offset ), len );
  134. ok ( memcmp ( ( dest + dest_offset ), ( src + src_offset ),
  135. len ) == 0 );
  136. /* Profile memcpy() */
  137. memset ( &profiler, 0, sizeof ( profiler ) );
  138. for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
  139. profile_start ( &profiler );
  140. memcpy ( ( dest + dest_offset ), ( src + src_offset ), len );
  141. profile_stop ( &profiler );
  142. }
  143. /* Free blocks */
  144. free ( dest );
  145. free ( src );
  146. DBG ( "MEMCPY copied %zd bytes (+%d => +%d) in %ld +/- %ld ticks\n",
  147. len, src_offset, dest_offset, profile_mean ( &profiler ),
  148. profile_stddev ( &profiler ) );
  149. }
  150. /**
  151. * Perform memcpy() self-tests
  152. *
  153. */
  154. static void memcpy_test_exec ( void ) {
  155. unsigned int dest_offset;
  156. unsigned int src_offset;
  157. /* Constant-length tests */
  158. MEMCPY_TEST_CONSTANT ( );
  159. MEMCPY_TEST_CONSTANT ( 0x86 );
  160. MEMCPY_TEST_CONSTANT ( 0x8c, 0xd3 );
  161. MEMCPY_TEST_CONSTANT ( 0x4e, 0x08, 0xed );
  162. MEMCPY_TEST_CONSTANT ( 0xcc, 0x61, 0x8f, 0x70 );
  163. MEMCPY_TEST_CONSTANT ( 0x6d, 0x28, 0xe0, 0x9e, 0x6d );
  164. MEMCPY_TEST_CONSTANT ( 0x7d, 0x13, 0x4f, 0xef, 0x17, 0xb3 );
  165. MEMCPY_TEST_CONSTANT ( 0x38, 0xa7, 0xd4, 0x8d, 0x44, 0x01, 0xfd );
  166. MEMCPY_TEST_CONSTANT ( 0x45, 0x9f, 0xf4, 0xf9, 0xf3, 0x0f, 0x99, 0x43 );
  167. MEMCPY_TEST_CONSTANT ( 0x69, 0x8c, 0xf6, 0x12, 0x79, 0x70, 0xd8, 0x1e,
  168. 0x9d );
  169. MEMCPY_TEST_CONSTANT ( 0xbe, 0x53, 0xb4, 0xb7, 0xdd, 0xe6, 0x35, 0x10,
  170. 0x3c, 0xe7 );
  171. MEMCPY_TEST_CONSTANT ( 0xaf, 0x41, 0x8a, 0x88, 0xb1, 0x4e, 0x52, 0xd4,
  172. 0xe6, 0xc3, 0x76 );
  173. MEMCPY_TEST_CONSTANT ( 0xdf, 0x43, 0xe4, 0x5d, 0xad, 0x17, 0x35, 0x38,
  174. 0x1a, 0x1d, 0x57, 0x58 );
  175. MEMCPY_TEST_CONSTANT ( 0x20, 0x52, 0x83, 0x92, 0xb9, 0x85, 0xa4, 0x06,
  176. 0x94, 0xe0, 0x3d, 0x57, 0xd4 );
  177. MEMCPY_TEST_CONSTANT ( 0xf1, 0x67, 0x31, 0x9e, 0x32, 0x98, 0x27, 0xe9,
  178. 0x8e, 0x62, 0xb4, 0x82, 0x7e, 0x02 );
  179. MEMCPY_TEST_CONSTANT ( 0x93, 0xc1, 0x55, 0xe3, 0x60, 0xce, 0xac, 0x1e,
  180. 0xae, 0x9d, 0xca, 0xec, 0x92, 0xb3, 0x38 );
  181. MEMCPY_TEST_CONSTANT ( 0xb3, 0xc1, 0xfa, 0xe7, 0x8a, 0x1c, 0xe4, 0xce,
  182. 0x85, 0xe6, 0x3c, 0xab, 0x1c, 0xa2, 0xaf, 0x7a );
  183. MEMCPY_TEST_CONSTANT ( 0x9b, 0x6e, 0x1c, 0x48, 0x82, 0xd3, 0x6e, 0x58,
  184. 0xa7, 0xb0, 0xe6, 0xea, 0x6d, 0xee, 0xc8, 0xf8,
  185. 0xaf );
  186. MEMCPY_TEST_CONSTANT ( 0x86, 0x6d, 0xb0, 0xf5, 0xf2, 0xc9, 0xcd, 0xfe,
  187. 0xfb, 0x38, 0x67, 0xbc, 0x51, 0x9d, 0x25, 0xbc,
  188. 0x09, 0x88 );
  189. MEMCPY_TEST_CONSTANT ( 0x58, 0xa4, 0x96, 0x9e, 0x98, 0x36, 0xdb, 0xae,
  190. 0x8a, 0x08, 0x7c, 0x64, 0xf9, 0xfb, 0x25, 0xb4,
  191. 0x8e, 0xf3, 0xed );
  192. MEMCPY_TEST_CONSTANT ( 0xc6, 0x3b, 0x84, 0x3c, 0x76, 0x24, 0x8e, 0x42,
  193. 0x11, 0x1f, 0x09, 0x2e, 0x24, 0xbb, 0x67, 0x71,
  194. 0x3a, 0xca, 0x60, 0xdd );
  195. MEMCPY_TEST_CONSTANT ( 0x8e, 0x2d, 0xa9, 0x58, 0x87, 0xe2, 0xac, 0x4b,
  196. 0xc8, 0xbf, 0xa2, 0x4e, 0xee, 0x3a, 0xa6, 0x71,
  197. 0x76, 0xee, 0x42, 0x05, 0x6e );
  198. MEMCPY_TEST_CONSTANT ( 0x8a, 0xda, 0xdf, 0x7b, 0x55, 0x41, 0x8c, 0xcd,
  199. 0x42, 0x40, 0x18, 0xe2, 0x60, 0xc4, 0x7d, 0x64,
  200. 0x00, 0xd5, 0xef, 0xa1, 0x7b, 0x31 );
  201. MEMCPY_TEST_CONSTANT ( 0xd9, 0x25, 0xcb, 0xbb, 0x9c, 0x1d, 0xdd, 0xcd,
  202. 0xde, 0x96, 0xd9, 0x74, 0x13, 0x95, 0xfe, 0x68,
  203. 0x0b, 0x3d, 0x30, 0x8d, 0x0c, 0x1e, 0x6d );
  204. MEMCPY_TEST_CONSTANT ( 0x2d, 0x0d, 0x02, 0x33, 0xd6, 0xbe, 0x6c, 0xa6,
  205. 0x0a, 0xab, 0xe5, 0xda, 0xe2, 0xab, 0x78, 0x3c,
  206. 0xd3, 0xdd, 0xea, 0xfa, 0x1a, 0xe4, 0xf4, 0xb3 );
  207. MEMCPY_TEST_CONSTANT ( 0x6a, 0x34, 0x39, 0xea, 0x29, 0x5f, 0xa6, 0x18,
  208. 0xc1, 0x53, 0x39, 0x78, 0xdb, 0x40, 0xf2, 0x98,
  209. 0x78, 0xcf, 0xee, 0xfd, 0xcd, 0xf8, 0x56, 0xf8,
  210. 0x30 );
  211. MEMCPY_TEST_CONSTANT ( 0xe4, 0xe5, 0x5a, 0x8d, 0xcf, 0x04, 0x29, 0x7c,
  212. 0xa7, 0xd8, 0x43, 0xbf, 0x0b, 0xbf, 0xe7, 0x68,
  213. 0xf7, 0x8c, 0x81, 0xf9, 0x3f, 0xad, 0xa4, 0x40,
  214. 0x38, 0x82 );
  215. MEMCPY_TEST_CONSTANT ( 0x71, 0xcd, 0x3d, 0x26, 0xde, 0x11, 0x23, 0xd5,
  216. 0x42, 0x6e, 0x63, 0x72, 0x53, 0xfc, 0x28, 0x06,
  217. 0x4b, 0xe0, 0x2c, 0x07, 0x6b, 0xe8, 0xd9, 0x5f,
  218. 0xf8, 0x74, 0xed );
  219. MEMCPY_TEST_CONSTANT ( 0x05, 0xb2, 0xae, 0x81, 0x91, 0xc9, 0xa2, 0x5f,
  220. 0xa9, 0x1b, 0x25, 0x7f, 0x32, 0x0c, 0x04, 0x00,
  221. 0xf1, 0x46, 0xab, 0x77, 0x1e, 0x12, 0x27, 0xe7,
  222. 0xf6, 0x1e, 0x0c, 0x29 );
  223. MEMCPY_TEST_CONSTANT ( 0x0e, 0xca, 0xa5, 0x56, 0x3d, 0x99, 0x99, 0xf9,
  224. 0x6e, 0xdd, 0x93, 0x98, 0xec, 0x8b, 0x5c, 0x71,
  225. 0x0c, 0xb0, 0xe6, 0x12, 0xf2, 0x10, 0x1a, 0xbe,
  226. 0x4a, 0xe0, 0xe3, 0x00, 0xf8 );
  227. MEMCPY_TEST_CONSTANT ( 0x40, 0xa8, 0x28, 0x5b, 0x12, 0x0d, 0x80, 0x8e,
  228. 0x8a, 0xd9, 0x92, 0x7a, 0x6e, 0x48, 0x8d, 0x14,
  229. 0x4b, 0xc6, 0xce, 0x21, 0x2f, 0x0e, 0x47, 0xbd,
  230. 0xf1, 0xca, 0x0e, 0x1f, 0x65, 0xc4 );
  231. MEMCPY_TEST_CONSTANT ( 0x84, 0x83, 0x44, 0xe8, 0x1c, 0xbf, 0x23, 0x05,
  232. 0xdf, 0xed, 0x3b, 0xb7, 0x0b, 0x4a, 0x05, 0xec,
  233. 0xb7, 0x6f, 0x1c, 0xfe, 0x05, 0x05, 0x4e, 0xd1,
  234. 0x50, 0x88, 0x81, 0x87, 0x68, 0xf6, 0x66 );
  235. MEMCPY_TEST_CONSTANT ( 0x0d, 0x1d, 0xcf, 0x3e, 0x7c, 0xf8, 0x12, 0x1b,
  236. 0x96, 0x7f, 0xff, 0x27, 0xca, 0xfe, 0xd3, 0x8b,
  237. 0x10, 0xb9, 0x5d, 0x05, 0xad, 0x50, 0xed, 0x35,
  238. 0x32, 0x9c, 0xe6, 0x3b, 0x73, 0xe0, 0x7d );
  239. /* Speed tests */
  240. memcpy_test_speed ( 0, 0, 64 );
  241. memcpy_test_speed ( 0, 0, 128 );
  242. memcpy_test_speed ( 0, 0, 256 );
  243. for ( dest_offset = 0 ; dest_offset < 4 ; dest_offset++ ) {
  244. for ( src_offset = 0 ; src_offset < 4 ; src_offset++ ) {
  245. memcpy_test_speed ( dest_offset, src_offset, 4096 );
  246. }
  247. }
  248. }
  249. /** memcpy() self-test */
  250. struct self_test memcpy_test __self_test = {
  251. .name = "memcpy",
  252. .exec = memcpy_test_exec,
  253. };