7
7
#ifndef SECP256K1_BENCH_H
8
8
#define SECP256K1_BENCH_H
9
9
10
+ #include <stdint.h>
10
11
#include <stdio.h>
11
12
#include <string.h>
12
- #include <math.h>
13
13
#include "sys/time.h"
14
14
15
- static double gettimedouble (void ) {
15
+ static int64_t gettime_i64 (void ) {
16
16
struct timeval tv ;
17
17
gettimeofday (& tv , NULL );
18
- return tv .tv_usec * 0.000001 + tv .tv_sec ;
18
+ return ( int64_t ) tv .tv_usec + ( int64_t ) tv .tv_sec * 1000000LL ;
19
19
}
20
20
21
- void print_number (double x ) {
22
- double y = x ;
23
- int c = 0 ;
24
- if (y < 0.0 ) {
25
- y = - y ;
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 ;
26
35
}
27
- while (y > 0 && y < 100.0 ) {
28
- y *= 10.0 ;
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 ;
29
44
c ++ ;
30
45
}
31
- printf ("%.*f" , c , x );
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 ]);
32
74
}
33
75
34
76
void run_benchmark (char * name , void (* benchmark )(void * ), void (* setup )(void * ), void (* teardown )(void * ), void * data , int count , int iter ) {
35
77
int i ;
36
- double min = HUGE_VAL ;
37
- double sum = 0. 0 ;
38
- double max = 0. 0 ;
78
+ int64_t min = INT64_MAX ;
79
+ int64_t sum = 0 ;
80
+ int64_t max = 0 ;
39
81
for (i = 0 ; i < count ; i ++ ) {
40
- double begin , total ;
82
+ int64_t begin , total ;
41
83
if (setup != NULL ) {
42
84
setup (data );
43
85
}
44
- begin = gettimedouble ();
86
+ begin = gettime_i64 ();
45
87
benchmark (data );
46
- total = gettimedouble () - begin ;
88
+ total = gettime_i64 () - begin ;
47
89
if (teardown != NULL ) {
48
90
teardown (data );
49
91
}
@@ -56,11 +98,11 @@ void run_benchmark(char *name, void (*benchmark)(void*), void (*setup)(void*), v
56
98
sum += total ;
57
99
}
58
100
printf ("%s: min " , name );
59
- print_number (min * 1000000.0 / iter );
101
+ print_number (min * FP_MULT / iter );
60
102
printf ("us / avg " );
61
- print_number ((sum / count ) * 1000000.0 / iter );
103
+ print_number ((( sum * FP_MULT ) / count ) / iter );
62
104
printf ("us / max " );
63
- print_number (max * 1000000.0 / iter );
105
+ print_number (max * FP_MULT / iter );
64
106
printf ("us\n" );
65
107
}
66
108
0 commit comments