aboutsummaryrefslogtreecommitdiffstats
path: root/bench/benchmark.c
blob: 87e15caee4abcf38bc9833f8348724446132db04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <time.h>
#include <stdio.h>

#ifdef BENCHMARK_LIB
# include BENCHMARK_LIB
#else
# include "../zahl.h"
# define BIGINT_LIBRARY "libzahl"
#endif


#ifndef CLOCK_MONOTONIC_RAW
# define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
#endif


#define BENCHMARK(INSTRUCTION, FAST)\
	do {\
		i = FAST ? 1000000L : 1000L;\
		clock_gettime(CLOCK_MONOTONIC_RAW, &start);\
		while (i--) {\
			(void)INSTRUCTION;\
		}\
		clock_gettime(CLOCK_MONOTONIC_RAW, &end);\
		end.tv_sec -= start.tv_sec;\
		end.tv_nsec -= start.tv_nsec;\
		if (end.tv_nsec < 0) {\
			end.tv_nsec += 1000000000L;\
			end.tv_sec -= 1;\
		}\
		printf("%s: %lli.%09li %s (152 bits)\n",\
		       #INSTRUCTION,\
		       (long long int)(end.tv_sec), end.tv_nsec,\
		       FAST ? "µs" : "ms");\
	} while (0)


int
main(int argc, char *argv[])
{
	struct timespec start, end;
	char buf[1000];
	z_t a, b, c, d, tiny;
	jmp_buf jmp;
	size_t i;

	if (setjmp(jmp)) {
		zperror(argv[0]);
		return 1;
	}
	zsetup(jmp);
	zinit(a);
	zinit(b);
	zinit(c);
	zinit(d);
	zinit(tiny);

	zsets(a, "5495468234592964023447280368442884381000481887");
	zsets(b, "4781084818570683458641843084358135840548636081");
	zsets(tiny, "5");

	BENCHMARK(zset(c, a), 1);
	BENCHMARK(zseti(c, 1000000000LL), 1);
	BENCHMARK(zsetu(c, 1000000000ULL), 1);
	BENCHMARK(zneg(c, a), 1);
	BENCHMARK(zneg(a, a), 1);
	BENCHMARK(zabs(c, a), 1);
	BENCHMARK(zabs(a, a), 1);
	BENCHMARK(zadd_unsigned(c, a, b), 1);
	BENCHMARK(zsub_unsigned(c, a, b), 1);
	BENCHMARK(zadd(c, a, b), 1);
	BENCHMARK(zsub(c, a, b), 1);
	BENCHMARK(zand(c, a, b), 1);
	BENCHMARK(zor(c, a, b), 1);
	BENCHMARK(zxor(c, a, b), 1);
	BENCHMARK(znot(c, a), 1);
	BENCHMARK(zeven(a), 1);
	BENCHMARK(zodd(a), 1);
	BENCHMARK(zeven_nonzero(a), 1);
	BENCHMARK(zodd_nonzero(a), 1);
	BENCHMARK(zzero(a), 1);
	BENCHMARK(zsignum(a), 1);
	BENCHMARK(zbits(a), 1);
	BENCHMARK(zlsb(a), 1);
	BENCHMARK(zswap(a, b), 1);
	BENCHMARK(zlsh(c, a, 76), 1);
	BENCHMARK(zrsh(c, a, 76), 1);
	BENCHMARK(ztrunc(c, a, 76), 1);
	BENCHMARK(ztrunc(c, c, 76), 1);
	BENCHMARK(zsplit(c, d, a, 76), 1);
	BENCHMARK(zcmpmag(a, b), 1);
	BENCHMARK(zcmp(a, b), 1);
	BENCHMARK(zcmpi(a, 1000000000LL), 1);
	BENCHMARK(zcmpu(a, 1000000000ULL), 1);
	BENCHMARK(zbset(c, a, 76, 1), 1);
	BENCHMARK(zbset(a, a, 76, 1), 1);
	BENCHMARK(zbset(c, a, 76, 0), 1);
	BENCHMARK(zbset(c, c, 76, 0), 1);
	BENCHMARK(zbset(c, a, 76, -1), 1);
	BENCHMARK(zbset(a, a, 76, -1), 1);
	BENCHMARK(zbtest(a, 76), 1);
#ifndef HEBIMATH
	BENCHMARK(zgcd(c, a, b), 0);
#endif
	BENCHMARK(zmul(c, a, b), 0);
	BENCHMARK(zmul(c, a, a), 0);
	BENCHMARK(zsqr(c, a), 0);
#ifndef HEBIMATH
	zsets(d, "1484298084218938358480511181388394862858002249");
	BENCHMARK(zmodmul(c, a, b, d), 0);
	BENCHMARK(zmodmul(c, a, a, d), 0);
	BENCHMARK(zmodsqr(c, a, d), 0);
	BENCHMARK(zmodmul(c, a, b, tiny), 0);
	BENCHMARK(zmodmul(c, a, a, tiny), 0);
	BENCHMARK(zmodsqr(c, a, tiny), 0);
	zsets(d, "12");
	BENCHMARK(zpow(c, a, d), 0);   /* Memory corruption when using hebimath */
	BENCHMARK(zpowu(c, a, 12), 0); /* Memory corruption when using hebimath */
	BENCHMARK(zmodpow(c, a, d, b), 0);
	BENCHMARK(zmodpowu(c, a, 12, b), 0);
#endif
	BENCHMARK(zsets(c, "5495468234592964023447280368442884381000481887"), 0);
	BENCHMARK(zstr_length(a, 10), 0);
	BENCHMARK(zstr(a, buf, 0), 0);
	BENCHMARK(zstr(a, buf, sizeof(buf) - 1), 0);
	BENCHMARK(zrand(c, DEFAULT_RANDOM, QUASIUNIFORM, a), 0);
	BENCHMARK(zrand(c, DEFAULT_RANDOM, UNIFORM, a), 0);
	BENCHMARK(zptest(d, a, 5), 0);
	BENCHMARK(zsave(a, buf), 1);
	BENCHMARK(zload(a, buf), 1);
	BENCHMARK(zdiv(c, a, b), 1);
	BENCHMARK(zmod(c, a, b), 1);
	BENCHMARK(zdivmod(c, d, a, b), 1);
#ifndef HEBIMATH
	BENCHMARK(zdiv(c, a, tiny), 0);
	BENCHMARK(zmod(c, a, tiny), 0);
	BENCHMARK(zdivmod(c, d, a, tiny), 0);
#endif

	zfree(a);
	zfree(b);
	zfree(c);
	zfree(d);
	zfree(tiny);
	zunsetup();
	return 0;
	(void) argc;
}