Skip to content

Commit d076cdf

Browse files
committed
Add Cephes benchmark
1 parent 72413fe commit d076cdf

File tree

2 files changed

+219
-0
lines changed

2 files changed

+219
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
2+
# VARIABLES #
3+
4+
ifndef VERBOSE
5+
QUIET := @
6+
endif
7+
8+
# Specify the path to Cephes:
9+
CEPHES ?=
10+
11+
# Specify a list of Cephes source files:
12+
CEPHES_SRC ?=
13+
14+
# Determine the OS:
15+
#
16+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
17+
# [2]: http://stackoverflow.com/a/27776822/2225624
18+
OS ?= $(shell uname)
19+
ifneq (, $(findstring MINGW,$(OS)))
20+
OS := WINNT
21+
else
22+
ifneq (, $(findstring MSYS,$(OS)))
23+
OS := WINNT
24+
else
25+
ifneq (, $(findstring CYGWIN,$(OS)))
26+
OS := WINNT
27+
endif
28+
endif
29+
endif
30+
31+
# Define the program used for compiling C source files:
32+
ifdef C_COMPILER
33+
CC := $(C_COMPILER)
34+
else
35+
CC := gcc
36+
endif
37+
38+
# Define the command-line options when compiling C files:
39+
CFLAGS ?= \
40+
-std=c99 \
41+
-O3 \
42+
-Wall \
43+
-pedantic \
44+
-Wno-deprecated-declarations
45+
46+
# Determine whether to generate [position independent code][1]:
47+
#
48+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
49+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
50+
ifeq ($(OS), WINNT)
51+
fPIC ?=
52+
else
53+
fPIC ?= -fPIC
54+
endif
55+
56+
# List of C targets:
57+
c_targets := benchmark.out
58+
59+
60+
# TARGETS #
61+
62+
# Default target.
63+
#
64+
# This target is the default target.
65+
66+
all: $(c_targets)
67+
68+
.PHONY: all
69+
70+
71+
# Compile C source.
72+
#
73+
# This target compiles C source files.
74+
75+
$(c_targets): %.out: %.c
76+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $(CEPHES_SRC) $< -lm
77+
78+
79+
# Run a benchmark.
80+
#
81+
# This target runs a benchmark.
82+
83+
run: $(c_targets)
84+
$(QUIET) ./$<
85+
86+
.PHONY: run
87+
88+
89+
# Perform clean-up.
90+
#
91+
# This target removes generated files.
92+
93+
clean:
94+
$(QUIET) -rm -f *.o *.out
95+
96+
.PHONY: clean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* Benchmark Cephes `igam`.
3+
*/
4+
#include <stdlib.h>
5+
#include <stdio.h>
6+
#include <math.h>
7+
#include <sys/time.h>
8+
9+
#define NAME "gammainc"
10+
#define ITERATIONS 1000000
11+
#define REPEATS 3
12+
13+
/**
14+
* Define prototypes for external functions.
15+
*/
16+
extern double igam( double x, double y );
17+
18+
/**
19+
* Prints the TAP version.
20+
*/
21+
void print_version() {
22+
printf( "TAP version 13\n" );
23+
}
24+
25+
/**
26+
* Prints the TAP summary.
27+
*
28+
* @param total total number of tests
29+
* @param passing total number of passing tests
30+
*/
31+
void print_summary( int total, int passing ) {
32+
printf( "#\n" );
33+
printf( "1..%d\n", total ); // TAP plan
34+
printf( "# total %d\n", total );
35+
printf( "# pass %d\n", passing );
36+
printf( "#\n" );
37+
printf( "# ok\n" );
38+
}
39+
40+
/**
41+
* Prints benchmarks results.
42+
*
43+
* @param elapsed elapsed time in seconds
44+
*/
45+
void print_results( double elapsed ) {
46+
double rate = (double)ITERATIONS / elapsed;
47+
printf( " ---\n" );
48+
printf( " iterations: %d\n", ITERATIONS );
49+
printf( " elapsed: %0.9f\n", elapsed );
50+
printf( " rate: %0.9f\n", rate );
51+
printf( " ...\n" );
52+
}
53+
54+
/**
55+
* Returns a clock time.
56+
*
57+
* @return clock time
58+
*/
59+
double tic() {
60+
struct timeval now;
61+
gettimeofday( &now, NULL );
62+
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
63+
}
64+
65+
/**
66+
* Generates a random double on the interval [0,1].
67+
*
68+
* @return random double
69+
*/
70+
double rand_double() {
71+
int r = rand();
72+
return (double)r / ( (double)RAND_MAX + 1.0 );
73+
}
74+
75+
/**
76+
* Runs a benchmark.
77+
*
78+
* @return elapsed time in seconds
79+
*/
80+
double benchmark() {
81+
double elapsed;
82+
double x;
83+
double y;
84+
double z;
85+
double t;
86+
int i;
87+
88+
t = tic();
89+
for ( i = 0; i < ITERATIONS; i++ ) {
90+
x = ( 100.0*rand_double() ) - 0.0;
91+
y = ( 99.9*rand_double() ) + 0.1;
92+
z = igam( x, y );
93+
if ( z != z ) {
94+
printf( "should not return NaN\n" );
95+
break;
96+
}
97+
}
98+
elapsed = tic() - t;
99+
if ( z != z ) {
100+
printf( "should not return NaN\n" );
101+
}
102+
return elapsed;
103+
}
104+
105+
/**
106+
* Main execution sequence.
107+
*/
108+
int main( void ) {
109+
double elapsed;
110+
int i;
111+
112+
// Use the current time to seed the random number generator:
113+
srand( time( NULL ) );
114+
115+
print_version();
116+
for ( i = 0; i < REPEATS; i++ ) {
117+
printf( "# c::cephes::%s\n", NAME );
118+
elapsed = benchmark();
119+
print_results( elapsed );
120+
printf( "ok %d benchmark finished\n", i+1 );
121+
}
122+
print_summary( REPEATS, REPEATS );
123+
}

0 commit comments

Comments
 (0)