|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2024 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +#include "stdlib/math/base/special/minmaxabsf.h" |
| 20 | +#include <stdlib.h> |
| 21 | +#include <stdio.h> |
| 22 | +#include <math.h> |
| 23 | +#include <time.h> |
| 24 | +#include <sys/time.h> |
| 25 | + |
| 26 | +#define NAME "minmaxabsf" |
| 27 | +#define ITERATIONS 1000000 |
| 28 | +#define REPEATS 3 |
| 29 | + |
| 30 | +/** |
| 31 | +* Prints the TAP version. |
| 32 | +*/ |
| 33 | +static void print_version( void ) { |
| 34 | + printf( "TAP version 13\n" ); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | +* Prints the TAP summary. |
| 39 | +* |
| 40 | +* @param total total number of tests |
| 41 | +* @param passing total number of passing tests |
| 42 | +*/ |
| 43 | +static void print_summary( int total, int passing ) { |
| 44 | + printf( "#\n" ); |
| 45 | + printf( "1..%d\n", total ); // TAP plan |
| 46 | + printf( "# total %d\n", total ); |
| 47 | + printf( "# pass %d\n", passing ); |
| 48 | + printf( "#\n" ); |
| 49 | + printf( "# ok\n" ); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | +* Prints benchmarks results. |
| 54 | +* |
| 55 | +* @param elapsed elapsed time in seconds |
| 56 | +*/ |
| 57 | +static void print_results( double elapsed ) { |
| 58 | + double rate = (double)ITERATIONS / elapsed; |
| 59 | + printf( " ---\n" ); |
| 60 | + printf( " iterations: %d\n", ITERATIONS ); |
| 61 | + printf( " elapsed: %0.9f\n", elapsed ); |
| 62 | + printf( " rate: %0.9f\n", rate ); |
| 63 | + printf( " ...\n" ); |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | +* Returns a clock time. |
| 68 | +* |
| 69 | +* @return clock time |
| 70 | +*/ |
| 71 | +static double tic( void ) { |
| 72 | + struct timeval now; |
| 73 | + gettimeofday( &now, NULL ); |
| 74 | + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | +* Generates a random number on the interval [0,1). |
| 79 | +* |
| 80 | +* @return random number |
| 81 | +*/ |
| 82 | +static double rand_double( void ) { |
| 83 | + int r = rand(); |
| 84 | + return (double)r / ( (double)RAND_MAX + 1.0 ); |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | +* Runs a benchmark. |
| 89 | +* |
| 90 | +* @return elapsed time in seconds |
| 91 | +*/ |
| 92 | +static double benchmark( void ) { |
| 93 | + double elapsed; |
| 94 | + double t; |
| 95 | + float x[ 100 ]; |
| 96 | + float y[ 100 ]; |
| 97 | + float min; |
| 98 | + float max; |
| 99 | + int i; |
| 100 | + |
| 101 | + for ( i = 0; i < 100; i++ ) { |
| 102 | + x[ i ] = ( 1000.0f * rand_double() ) - 500.0f; |
| 103 | + y[ i ] = ( 1000.0f * rand_double() ) - 500.0f; |
| 104 | + } |
| 105 | + |
| 106 | + t = tic(); |
| 107 | + for ( i = 0; i < ITERATIONS; i++ ) { |
| 108 | + stdlib_base_minmaxabsf( x[ i % 100 ], y[ i % 100 ], &min, &max ); |
| 109 | + if ( min != min || max != max ) { |
| 110 | + printf( "should not return NaN\n" ); |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | + elapsed = tic() - t; |
| 115 | + if ( min != min || max != max ) { |
| 116 | + printf( "should not return NaN\n" ); |
| 117 | + } |
| 118 | + return elapsed; |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | +* Main execution sequence. |
| 123 | +*/ |
| 124 | +int main( void ) { |
| 125 | + double elapsed; |
| 126 | + int i; |
| 127 | + |
| 128 | + // Use the current time to seed the random number generator: |
| 129 | + srand( time( NULL ) ); |
| 130 | + |
| 131 | + print_version(); |
| 132 | + for ( i = 0; i < REPEATS; i++ ) { |
| 133 | + printf( "# c::native::%s\n", NAME ); |
| 134 | + elapsed = benchmark(); |
| 135 | + print_results( elapsed ); |
| 136 | + printf( "ok %d benchmark finished\n", i+1 ); |
| 137 | + } |
| 138 | + print_summary( REPEATS, REPEATS ); |
| 139 | +} |
0 commit comments