Skip to content

Commit 4a3a9d8

Browse files
committed
Add Cephes benchmark snippet
1 parent afdfe4b commit 4a3a9d8

File tree

2 files changed

+211
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)