]>
Commit | Line | Data |
---|---|---|
6558a267 PW |
1 | /********************************************************************** |
2 | * Copyright (c) 2014 Pieter Wuille * | |
3 | * Distributed under the MIT software license, see the accompanying * | |
4 | * file COPYING or http://www.opensource.org/licenses/mit-license.php.* | |
5 | **********************************************************************/ | |
6 | ||
abe2d3e8 DR |
7 | #ifndef SECP256K1_BENCH_H |
8 | #define SECP256K1_BENCH_H | |
6558a267 | 9 | |
bde2a322 | 10 | #include <stdint.h> |
6558a267 | 11 | #include <stdio.h> |
a58f543f | 12 | #include <string.h> |
6558a267 PW |
13 | #include "sys/time.h" |
14 | ||
bde2a322 | 15 | static int64_t gettime_i64(void) { |
6558a267 PW |
16 | struct timeval tv; |
17 | gettimeofday(&tv, NULL); | |
bde2a322 | 18 | return (int64_t)tv.tv_usec + (int64_t)tv.tv_sec * 1000000LL; |
6558a267 PW |
19 | } |
20 | ||
bde2a322 WL |
21 | #define FP_EXP (6) |
22 | #define FP_MULT (1000000LL) | |
23 | ||
24 | /* Format fixed point number. */ | |
25 | void print_number(const int64_t x) { | |
26 | int64_t x_abs, y; | |
27 | int c, i, rounding; | |
28 | size_t ptr; | |
29 | char buffer[30]; | |
30 | ||
31 | if (x == INT64_MIN) { | |
32 | /* Prevent UB. */ | |
33 | printf("ERR"); | |
34 | return; | |
912f203f | 35 | } |
bde2a322 WL |
36 | x_abs = x < 0 ? -x : x; |
37 | ||
38 | /* Determine how many decimals we want to show (more than FP_EXP makes no | |
39 | * sense). */ | |
40 | y = x_abs; | |
41 | c = 0; | |
42 | while (y > 0LL && y < 100LL * FP_MULT && c < FP_EXP) { | |
43 | y *= 10LL; | |
039723d5 PW |
44 | c++; |
45 | } | |
bde2a322 WL |
46 | |
47 | /* Round to 'c' decimals. */ | |
48 | y = x_abs; | |
49 | rounding = 0; | |
50 | for (i = c; i < FP_EXP; ++i) { | |
51 | rounding = (y % 10) >= 5; | |
52 | y /= 10; | |
53 | } | |
54 | y += rounding; | |
55 | ||
56 | /* Format and print the number. */ | |
57 | ptr = sizeof(buffer) - 1; | |
58 | buffer[ptr] = 0; | |
59 | if (c != 0) { | |
60 | for (i = 0; i < c; ++i) { | |
61 | buffer[--ptr] = '0' + (y % 10); | |
62 | y /= 10; | |
63 | } | |
64 | buffer[--ptr] = '.'; | |
65 | } | |
66 | do { | |
67 | buffer[--ptr] = '0' + (y % 10); | |
68 | y /= 10; | |
69 | } while (y != 0); | |
70 | if (x < 0) { | |
71 | buffer[--ptr] = '-'; | |
72 | } | |
73 | printf("%s", &buffer[ptr]); | |
039723d5 PW |
74 | } |
75 | ||
76 | void run_benchmark(char *name, void (*benchmark)(void*), void (*setup)(void*), void (*teardown)(void*), void* data, int count, int iter) { | |
f735446c | 77 | int i; |
bde2a322 WL |
78 | int64_t min = INT64_MAX; |
79 | int64_t sum = 0; | |
80 | int64_t max = 0; | |
f735446c | 81 | for (i = 0; i < count; i++) { |
bde2a322 | 82 | int64_t begin, total; |
2b199de8 | 83 | if (setup != NULL) { |
912f203f GM |
84 | setup(data); |
85 | } | |
bde2a322 | 86 | begin = gettime_i64(); |
6558a267 | 87 | benchmark(data); |
bde2a322 | 88 | total = gettime_i64() - begin; |
2b199de8 | 89 | if (teardown != NULL) { |
912f203f GM |
90 | teardown(data); |
91 | } | |
92 | if (total < min) { | |
93 | min = total; | |
94 | } | |
95 | if (total > max) { | |
96 | max = total; | |
97 | } | |
6558a267 PW |
98 | sum += total; |
99 | } | |
039723d5 | 100 | printf("%s: min ", name); |
bde2a322 | 101 | print_number(min * FP_MULT / iter); |
039723d5 | 102 | printf("us / avg "); |
bde2a322 | 103 | print_number(((sum * FP_MULT) / count) / iter); |
6066bb68 | 104 | printf("us / max "); |
bde2a322 | 105 | print_number(max * FP_MULT / iter); |
039723d5 | 106 | printf("us\n"); |
6558a267 PW |
107 | } |
108 | ||
a58f543f JN |
109 | int have_flag(int argc, char** argv, char *flag) { |
110 | char** argm = argv + argc; | |
111 | argv++; | |
112 | if (argv == argm) { | |
113 | return 1; | |
114 | } | |
115 | while (argv != NULL && argv != argm) { | |
116 | if (strcmp(*argv, flag) == 0) { | |
117 | return 1; | |
118 | } | |
119 | argv++; | |
120 | } | |
121 | return 0; | |
122 | } | |
123 | ||
abe2d3e8 | 124 | #endif /* SECP256K1_BENCH_H */ |