3232/**
3333* Prints the TAP version.
3434*/
35- static void print_version ( void ) {
36- printf ( "TAP version 13\n" );
35+ static void print_version (void ) {
36+ printf ("TAP version 13\n" );
3737}
3838
3939/**
@@ -42,13 +42,13 @@ static void print_version( void ) {
4242* @param total total number of tests
4343* @param passing total number of passing tests
4444*/
45- static void print_summary ( int total , int passing ) {
46- printf ( "#\n" );
47- printf ( "1..%d\n" , total ); // TAP plan
48- printf ( "# total %d\n" , total );
49- printf ( "# pass %d\n" , passing );
50- printf ( "#\n" );
51- printf ( "# ok\n" );
45+ static void print_summary (int total , int passing ) {
46+ printf ("#\n" );
47+ printf ("1..%d\n" , total ); // TAP plan
48+ printf ("# total %d\n" , total );
49+ printf ("# pass %d\n" , passing );
50+ printf ("#\n" );
51+ printf ("# ok\n" );
5252}
5353
5454/**
@@ -57,34 +57,34 @@ static void print_summary( int total, int passing ) {
5757* @param iterations number of iterations
5858* @param elapsed elapsed time in seconds
5959*/
60- static void print_results ( int iterations , double elapsed ) {
60+ static void print_results (int iterations , double elapsed ) {
6161 double rate = (double )iterations / elapsed ;
62- printf ( " ---\n" );
63- printf ( " iterations: %d\n" , iterations );
64- printf ( " elapsed: %0.9f\n" , elapsed );
65- printf ( " rate: %0.9f\n" , rate );
66- printf ( " ...\n" );
62+ printf (" ---\n" );
63+ printf (" iterations: %d\n" , iterations );
64+ printf (" elapsed: %0.9f\n" , elapsed );
65+ printf (" rate: %0.9f\n" , rate );
66+ printf (" ...\n" );
6767}
6868
6969/**
7070* Returns a clock time.
7171*
7272* @return clock time
7373*/
74- static double tic ( void ) {
74+ static double tic (void ) {
7575 struct timeval now ;
76- gettimeofday ( & now , NULL );
77- return (double )now .tv_sec + (double )now .tv_usec / 1.0e6 ;
76+ gettimeofday (& now , NULL );
77+ return (double )now .tv_sec + (double )now .tv_usec / 1.0e6 ;
7878}
7979
8080/**
8181* Generates a random number on the interval [0,1).
8282*
8383* @return random number
8484*/
85- static float rand_float ( void ) {
85+ static float rand_float (void ) {
8686 int r = rand ();
87- return (float )r / ( (float )RAND_MAX + 1.0f );
87+ return (float )r / ((float )RAND_MAX + 1.0f );
8888}
8989
9090/**
@@ -94,36 +94,46 @@ static float rand_float( void ) {
9494* @param len array length
9595* @return elapsed time in seconds
9696*/
97- static double benchmark ( int iterations , int len ) {
97+ static double benchmark (int iterations , int len ) {
9898 double elapsed ;
99- float x [ len ];
100- float v ;
99+ float * x = malloc (len * sizeof (float ));
100+ if (x == NULL ) {
101+ fprintf (stderr , "Memory allocation failed\n" );
102+ exit (1 );
103+ }
104+
105+ float v = 0.0f ;
101106 double t ;
102107 int i ;
103108
104- for ( i = 0 ; i < len ; i ++ ) {
105- x [ i ] = ( rand_float ()* 20000.0f ) - 10000.0f ;
109+ for (i = 0 ; i < len ; i ++ ) {
110+ x [i ] = (rand_float () * 20000.0f ) - 10000.0f ;
106111 }
107- v = 0.0f ;
112+
108113 t = tic ();
109- for ( i = 0 ; i < iterations ; i ++ ) {
110- v = stdlib_strided_smean ( len , x , 1 );
111- if ( v != v ) {
112- printf ( "should not return NaN\n" );
114+ for (i = 0 ; i < iterations ; i ++ ) {
115+ v = stdlib_strided_smean (len , x , 1 );
116+ if (v != v ) {
117+ printf ("should not return NaN\n" );
113118 break ;
114119 }
115120 }
121+
116122 elapsed = tic () - t ;
117- if ( v != v ) {
118- printf ( "should not return NaN\n" );
123+
124+ if (v != v ) {
125+ printf ("should not return NaN\n" );
119126 }
127+
128+ free (x ); // <<< free before returning
120129 return elapsed ;
121130}
122131
132+
123133/**
124134* Main execution sequence.
125135*/
126- int main ( void ) {
136+ int main (void ) {
127137 double elapsed ;
128138 int count ;
129139 int iter ;
@@ -132,20 +142,20 @@ int main( void ) {
132142 int j ;
133143
134144 // Use the current time to seed the random number generator:
135- srand ( time ( NULL ) );
145+ srand (time (NULL ) );
136146
137147 print_version ();
138148 count = 0 ;
139- for ( i = MIN ; i <= MAX ; i ++ ) {
140- len = pow ( 10 , i );
141- iter = ITERATIONS / pow ( 10 , i - 1 );
142- for ( j = 0 ; j < REPEATS ; j ++ ) {
149+ for (i = MIN ; i <= MAX ; i ++ ) {
150+ len = pow (10 , i );
151+ iter = ITERATIONS / pow (10 , i - 1 );
152+ for (j = 0 ; j < REPEATS ; j ++ ) {
143153 count += 1 ;
144- printf ( "# c::%s:len=%d\n" , NAME , len );
145- elapsed = benchmark ( iter , len );
146- print_results ( iter , elapsed );
147- printf ( "ok %d benchmark finished\n" , count );
154+ printf ("# c::%s:len=%d\n" , NAME , len );
155+ elapsed = benchmark (iter , len );
156+ print_results (iter , elapsed );
157+ printf ("ok %d benchmark finished\n" , count );
148158 }
149159 }
150- print_summary ( count , count );
160+ print_summary (count , count );
151161}
0 commit comments