iPXE
memcpy_test.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * memcpy() self-tests
00029  *
00030  */
00031 
00032 /* Forcibly enable assertions */
00033 #undef NDEBUG
00034 
00035 #include <stdint.h>
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include <ipxe/test.h>
00039 #include <ipxe/profile.h>
00040 
00041 /** Number of sample iterations for profiling */
00042 #define PROFILE_COUNT 16
00043 
00044 /* Provide global functions to allow inspection of generated code */
00045 
00046 void memcpy_0 ( void *dest, void *src ) { memcpy ( dest, src, 0 ); }
00047 void memcpy_1 ( void *dest, void *src ) { memcpy ( dest, src, 1 ); }
00048 void memcpy_2 ( void *dest, void *src ) { memcpy ( dest, src, 2 ); }
00049 void memcpy_3 ( void *dest, void *src ) { memcpy ( dest, src, 3 ); }
00050 void memcpy_4 ( void *dest, void *src ) { memcpy ( dest, src, 4 ); }
00051 void memcpy_5 ( void *dest, void *src ) { memcpy ( dest, src, 5 ); }
00052 void memcpy_6 ( void *dest, void *src ) { memcpy ( dest, src, 6 ); }
00053 void memcpy_7 ( void *dest, void *src ) { memcpy ( dest, src, 7 ); }
00054 void memcpy_8 ( void *dest, void *src ) { memcpy ( dest, src, 8 ); }
00055 void memcpy_9 ( void *dest, void *src ) { memcpy ( dest, src, 9 ); }
00056 void memcpy_10 ( void *dest, void *src ) { memcpy ( dest, src, 10 ); }
00057 void memcpy_11 ( void *dest, void *src ) { memcpy ( dest, src, 11 ); }
00058 void memcpy_12 ( void *dest, void *src ) { memcpy ( dest, src, 12 ); }
00059 void memcpy_13 ( void *dest, void *src ) { memcpy ( dest, src, 13 ); }
00060 void memcpy_14 ( void *dest, void *src ) { memcpy ( dest, src, 14 ); }
00061 void memcpy_15 ( void *dest, void *src ) { memcpy ( dest, src, 15 ); }
00062 void memcpy_16 ( void *dest, void *src ) { memcpy ( dest, src, 16 ); }
00063 void memcpy_17 ( void *dest, void *src ) { memcpy ( dest, src, 17 ); }
00064 void memcpy_18 ( void *dest, void *src ) { memcpy ( dest, src, 18 ); }
00065 void memcpy_19 ( void *dest, void *src ) { memcpy ( dest, src, 19 ); }
00066 void memcpy_20 ( void *dest, void *src ) { memcpy ( dest, src, 20 ); }
00067 void memcpy_21 ( void *dest, void *src ) { memcpy ( dest, src, 21 ); }
00068 void memcpy_22 ( void *dest, void *src ) { memcpy ( dest, src, 22 ); }
00069 void memcpy_23 ( void *dest, void *src ) { memcpy ( dest, src, 23 ); }
00070 void memcpy_24 ( void *dest, void *src ) { memcpy ( dest, src, 24 ); }
00071 void memcpy_25 ( void *dest, void *src ) { memcpy ( dest, src, 25 ); }
00072 void memcpy_26 ( void *dest, void *src ) { memcpy ( dest, src, 26 ); }
00073 void memcpy_27 ( void *dest, void *src ) { memcpy ( dest, src, 27 ); }
00074 void memcpy_28 ( void *dest, void *src ) { memcpy ( dest, src, 28 ); }
00075 void memcpy_29 ( void *dest, void *src ) { memcpy ( dest, src, 29 ); }
00076 void memcpy_30 ( void *dest, void *src ) { memcpy ( dest, src, 30 ); }
00077 void memcpy_31 ( void *dest, void *src ) { memcpy ( dest, src, 31 ); }
00078 
00079 /**
00080  * Force a call to the variable-length implementation of memcpy()
00081  *
00082  * @v dest              Destination address
00083  * @v src               Source address
00084  * @v len               Length of data
00085  * @ret dest            Destination address
00086  */
00087 __attribute__ (( noinline )) void * memcpy_var ( void *dest, const void *src,
00088                                                  size_t len ) {
00089         return memcpy ( dest, src, len );
00090 }
00091 
00092 /**
00093  * Perform a constant-length memcpy() test
00094  *
00095  * ...                  Data to copy
00096  */
00097 #define MEMCPY_TEST_CONSTANT( ... ) do {                                \
00098                 static const uint8_t src[] = { __VA_ARGS__ };           \
00099                 uint8_t dest_const[ 1 + sizeof ( src ) + 1 ];           \
00100                 uint8_t dest_var[ 1 + sizeof ( src ) + 1 ];             \
00101                                                                         \
00102                 dest_const[0] = 0x33;                                   \
00103                 dest_const[ sizeof ( dest_const ) - 1 ] = 0x44;         \
00104                 memcpy ( ( dest_const + 1 ), src,                       \
00105                          ( sizeof ( dest_const ) - 2 ) );               \
00106                 ok ( dest_const[0] == 0x33 );                           \
00107                 ok ( dest_const[ sizeof ( dest_const ) - 1 ] == 0x44 ); \
00108                 ok ( memcmp ( ( dest_const + 1 ), src,                  \
00109                               ( sizeof ( dest_const ) - 2 ) ) == 0 );   \
00110                                                                         \
00111                 dest_var[0] = 0x55;                                     \
00112                 dest_var[ sizeof ( dest_var ) - 1 ] = 0x66;             \
00113                 memcpy_var ( ( dest_var + 1 ), src,                     \
00114                              ( sizeof ( dest_var ) - 2 ) );             \
00115                 ok ( dest_var[0] == 0x55 );                             \
00116                 ok ( dest_var[ sizeof ( dest_var ) - 1 ] == 0x66 );     \
00117                 ok ( memcmp ( ( dest_var + 1 ), src,                    \
00118                               ( sizeof ( dest_var ) - 2 ) ) == 0 );     \
00119         } while ( 0 )
00120 
00121 /**
00122  * Test memcpy() speed
00123  *
00124  * @v dest_offset       Destination alignment offset
00125  * @v src_offset        Source alignment offset
00126  * @v len               Length of data to copy
00127  */
00128 static void memcpy_test_speed ( unsigned int dest_offset,
00129                                 unsigned int src_offset, size_t len ) {
00130         struct profiler profiler;
00131         uint8_t *dest;
00132         uint8_t *src;
00133         unsigned int i;
00134 
00135         /* Allocate blocks */
00136         dest = malloc ( len + dest_offset );
00137         assert ( dest != NULL );
00138         src = malloc ( len + src_offset );
00139         assert ( src != NULL );
00140 
00141         /* Generate random source data */
00142         for ( i = 0 ; i < len ; i++ )
00143                 src[ src_offset + i ] = random();
00144 
00145         /* Check correctness of copied data */
00146         memcpy ( ( dest + dest_offset ), ( src + src_offset ), len );
00147         ok ( memcmp ( ( dest + dest_offset ), ( src + src_offset ),
00148                       len ) == 0 );
00149 
00150         /* Profile memcpy() */
00151         memset ( &profiler, 0, sizeof ( profiler ) );
00152         for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
00153                 profile_start ( &profiler );
00154                 memcpy ( ( dest + dest_offset ), ( src + src_offset ), len );
00155                 profile_stop ( &profiler );
00156         }
00157 
00158         /* Free blocks */
00159         free ( dest );
00160         free ( src );
00161 
00162         DBG ( "MEMCPY copied %zd bytes (+%d => +%d) in %ld +/- %ld ticks\n",
00163               len, src_offset, dest_offset, profile_mean ( &profiler ),
00164               profile_stddev ( &profiler ) );
00165 }
00166 
00167 /**
00168  * Perform memcpy() self-tests
00169  *
00170  */
00171 static void memcpy_test_exec ( void ) {
00172         unsigned int dest_offset;
00173         unsigned int src_offset;
00174 
00175         /* Constant-length tests */
00176         MEMCPY_TEST_CONSTANT ( );
00177         MEMCPY_TEST_CONSTANT ( 0x86 );
00178         MEMCPY_TEST_CONSTANT ( 0x8c, 0xd3 );
00179         MEMCPY_TEST_CONSTANT ( 0x4e, 0x08, 0xed );
00180         MEMCPY_TEST_CONSTANT ( 0xcc, 0x61, 0x8f, 0x70 );
00181         MEMCPY_TEST_CONSTANT ( 0x6d, 0x28, 0xe0, 0x9e, 0x6d );
00182         MEMCPY_TEST_CONSTANT ( 0x7d, 0x13, 0x4f, 0xef, 0x17, 0xb3 );
00183         MEMCPY_TEST_CONSTANT ( 0x38, 0xa7, 0xd4, 0x8d, 0x44, 0x01, 0xfd );
00184         MEMCPY_TEST_CONSTANT ( 0x45, 0x9f, 0xf4, 0xf9, 0xf3, 0x0f, 0x99, 0x43 );
00185         MEMCPY_TEST_CONSTANT ( 0x69, 0x8c, 0xf6, 0x12, 0x79, 0x70, 0xd8, 0x1e,
00186                                0x9d );
00187         MEMCPY_TEST_CONSTANT ( 0xbe, 0x53, 0xb4, 0xb7, 0xdd, 0xe6, 0x35, 0x10,
00188                                0x3c, 0xe7 );
00189         MEMCPY_TEST_CONSTANT ( 0xaf, 0x41, 0x8a, 0x88, 0xb1, 0x4e, 0x52, 0xd4,
00190                                0xe6, 0xc3, 0x76 );
00191         MEMCPY_TEST_CONSTANT ( 0xdf, 0x43, 0xe4, 0x5d, 0xad, 0x17, 0x35, 0x38,
00192                                0x1a, 0x1d, 0x57, 0x58 );
00193         MEMCPY_TEST_CONSTANT ( 0x20, 0x52, 0x83, 0x92, 0xb9, 0x85, 0xa4, 0x06,
00194                                0x94, 0xe0, 0x3d, 0x57, 0xd4 );
00195         MEMCPY_TEST_CONSTANT ( 0xf1, 0x67, 0x31, 0x9e, 0x32, 0x98, 0x27, 0xe9,
00196                                0x8e, 0x62, 0xb4, 0x82, 0x7e, 0x02 );
00197         MEMCPY_TEST_CONSTANT ( 0x93, 0xc1, 0x55, 0xe3, 0x60, 0xce, 0xac, 0x1e,
00198                                0xae, 0x9d, 0xca, 0xec, 0x92, 0xb3, 0x38 );
00199         MEMCPY_TEST_CONSTANT ( 0xb3, 0xc1, 0xfa, 0xe7, 0x8a, 0x1c, 0xe4, 0xce,
00200                                0x85, 0xe6, 0x3c, 0xab, 0x1c, 0xa2, 0xaf, 0x7a );
00201         MEMCPY_TEST_CONSTANT ( 0x9b, 0x6e, 0x1c, 0x48, 0x82, 0xd3, 0x6e, 0x58,
00202                                0xa7, 0xb0, 0xe6, 0xea, 0x6d, 0xee, 0xc8, 0xf8,
00203                                0xaf );
00204         MEMCPY_TEST_CONSTANT ( 0x86, 0x6d, 0xb0, 0xf5, 0xf2, 0xc9, 0xcd, 0xfe,
00205                                0xfb, 0x38, 0x67, 0xbc, 0x51, 0x9d, 0x25, 0xbc,
00206                                0x09, 0x88 );
00207         MEMCPY_TEST_CONSTANT ( 0x58, 0xa4, 0x96, 0x9e, 0x98, 0x36, 0xdb, 0xae,
00208                                0x8a, 0x08, 0x7c, 0x64, 0xf9, 0xfb, 0x25, 0xb4,
00209                                0x8e, 0xf3, 0xed );
00210         MEMCPY_TEST_CONSTANT ( 0xc6, 0x3b, 0x84, 0x3c, 0x76, 0x24, 0x8e, 0x42,
00211                                0x11, 0x1f, 0x09, 0x2e, 0x24, 0xbb, 0x67, 0x71,
00212                                0x3a, 0xca, 0x60, 0xdd );
00213         MEMCPY_TEST_CONSTANT ( 0x8e, 0x2d, 0xa9, 0x58, 0x87, 0xe2, 0xac, 0x4b,
00214                                0xc8, 0xbf, 0xa2, 0x4e, 0xee, 0x3a, 0xa6, 0x71,
00215                                0x76, 0xee, 0x42, 0x05, 0x6e );
00216         MEMCPY_TEST_CONSTANT ( 0x8a, 0xda, 0xdf, 0x7b, 0x55, 0x41, 0x8c, 0xcd,
00217                                0x42, 0x40, 0x18, 0xe2, 0x60, 0xc4, 0x7d, 0x64,
00218                                0x00, 0xd5, 0xef, 0xa1, 0x7b, 0x31 );
00219         MEMCPY_TEST_CONSTANT ( 0xd9, 0x25, 0xcb, 0xbb, 0x9c, 0x1d, 0xdd, 0xcd,
00220                                0xde, 0x96, 0xd9, 0x74, 0x13, 0x95, 0xfe, 0x68,
00221                                0x0b, 0x3d, 0x30, 0x8d, 0x0c, 0x1e, 0x6d );
00222         MEMCPY_TEST_CONSTANT ( 0x2d, 0x0d, 0x02, 0x33, 0xd6, 0xbe, 0x6c, 0xa6,
00223                                0x0a, 0xab, 0xe5, 0xda, 0xe2, 0xab, 0x78, 0x3c,
00224                                0xd3, 0xdd, 0xea, 0xfa, 0x1a, 0xe4, 0xf4, 0xb3 );
00225         MEMCPY_TEST_CONSTANT ( 0x6a, 0x34, 0x39, 0xea, 0x29, 0x5f, 0xa6, 0x18,
00226                                0xc1, 0x53, 0x39, 0x78, 0xdb, 0x40, 0xf2, 0x98,
00227                                0x78, 0xcf, 0xee, 0xfd, 0xcd, 0xf8, 0x56, 0xf8,
00228                                0x30 );
00229         MEMCPY_TEST_CONSTANT ( 0xe4, 0xe5, 0x5a, 0x8d, 0xcf, 0x04, 0x29, 0x7c,
00230                                0xa7, 0xd8, 0x43, 0xbf, 0x0b, 0xbf, 0xe7, 0x68,
00231                                0xf7, 0x8c, 0x81, 0xf9, 0x3f, 0xad, 0xa4, 0x40,
00232                                0x38, 0x82 );
00233         MEMCPY_TEST_CONSTANT ( 0x71, 0xcd, 0x3d, 0x26, 0xde, 0x11, 0x23, 0xd5,
00234                                0x42, 0x6e, 0x63, 0x72, 0x53, 0xfc, 0x28, 0x06,
00235                                0x4b, 0xe0, 0x2c, 0x07, 0x6b, 0xe8, 0xd9, 0x5f,
00236                                0xf8, 0x74, 0xed );
00237         MEMCPY_TEST_CONSTANT ( 0x05, 0xb2, 0xae, 0x81, 0x91, 0xc9, 0xa2, 0x5f,
00238                                0xa9, 0x1b, 0x25, 0x7f, 0x32, 0x0c, 0x04, 0x00,
00239                                0xf1, 0x46, 0xab, 0x77, 0x1e, 0x12, 0x27, 0xe7,
00240                                0xf6, 0x1e, 0x0c, 0x29 );
00241         MEMCPY_TEST_CONSTANT ( 0x0e, 0xca, 0xa5, 0x56, 0x3d, 0x99, 0x99, 0xf9,
00242                                0x6e, 0xdd, 0x93, 0x98, 0xec, 0x8b, 0x5c, 0x71,
00243                                0x0c, 0xb0, 0xe6, 0x12, 0xf2, 0x10, 0x1a, 0xbe,
00244                                0x4a, 0xe0, 0xe3, 0x00, 0xf8 );
00245         MEMCPY_TEST_CONSTANT ( 0x40, 0xa8, 0x28, 0x5b, 0x12, 0x0d, 0x80, 0x8e,
00246                                0x8a, 0xd9, 0x92, 0x7a, 0x6e, 0x48, 0x8d, 0x14,
00247                                0x4b, 0xc6, 0xce, 0x21, 0x2f, 0x0e, 0x47, 0xbd,
00248                                0xf1, 0xca, 0x0e, 0x1f, 0x65, 0xc4 );
00249         MEMCPY_TEST_CONSTANT ( 0x84, 0x83, 0x44, 0xe8, 0x1c, 0xbf, 0x23, 0x05,
00250                                0xdf, 0xed, 0x3b, 0xb7, 0x0b, 0x4a, 0x05, 0xec,
00251                                0xb7, 0x6f, 0x1c, 0xfe, 0x05, 0x05, 0x4e, 0xd1,
00252                                0x50, 0x88, 0x81, 0x87, 0x68, 0xf6, 0x66 );
00253         MEMCPY_TEST_CONSTANT ( 0x0d, 0x1d, 0xcf, 0x3e, 0x7c, 0xf8, 0x12, 0x1b,
00254                                0x96, 0x7f, 0xff, 0x27, 0xca, 0xfe, 0xd3, 0x8b,
00255                                0x10, 0xb9, 0x5d, 0x05, 0xad, 0x50, 0xed, 0x35,
00256                                0x32, 0x9c, 0xe6, 0x3b, 0x73, 0xe0, 0x7d );
00257 
00258         /* Speed tests */
00259         memcpy_test_speed ( 0, 0, 64 );
00260         memcpy_test_speed ( 0, 0, 128 );
00261         memcpy_test_speed ( 0, 0, 256 );
00262         for ( dest_offset = 0 ; dest_offset < 4 ; dest_offset++ ) {
00263                 for ( src_offset = 0 ; src_offset < 4 ; src_offset++ ) {
00264                         memcpy_test_speed ( dest_offset, src_offset, 4096 );
00265                 }
00266         }
00267 }
00268 
00269 /** memcpy() self-test */
00270 struct self_test memcpy_test __self_test = {
00271         .name = "memcpy",
00272         .exec = memcpy_test_exec,
00273 };