-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixpointTests.h
More file actions
250 lines (213 loc) · 6.71 KB
/
fixpointTests.h
File metadata and controls
250 lines (213 loc) · 6.71 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#pragma once
#include "fixpoint.h"
#define MP_INT HIDEN_MP_TYPE
#include <gmpxx.h>
#undef MP_INT
#include <cassert>
#ifdef ADDITIONAL_TESTS
#include "../gmpwrap.h"
extern "C" {
#include <mpdecimal.h>
}
#endif //#ifdef ADDITIONAL_TESTS
inline void FixPointPerfTest()
{
//cout << sizeof(unsigned long int) << endl;
timespec t1;
timespec t2;
long long t;
using decimal = FixPoint<37, 19>;
decimal tm1;
tm1 = 1.01;
decimal tm2;
tm2 = 1.02;
// from string tests
decimal tm3;
tm3 = "1.02";
assert(tm2 == tm3);
tm3 = "102e-2";
assert(tm2 == tm3);
tm3 = "102E-2";
assert(tm2 == tm3);
tm3 = "10.2e-1";
assert(tm2 == tm3);
tm3 = "0.0102e2";
assert(tm2 == tm3);
tm3 = "1";
decimal tm4;
tm4 = 1.;
assert(tm4 == tm3);
tm3 = "1.0302";
assert(tm1 * tm2 == tm3);
assert(tm2 / tm1 == "1.00990099009900990095");
tm3 = "1.0099009900990099009";
tm3.round(5);
assert(tm3 == "1.0099");
tm3 = "1.0099009900990099009";
tm3.round(-1);
assert(tm3 == "0");
tm3 = "1.0099009900990099009";
tm3.round(1);
assert(tm3 == "1");
tm3 = "1.0099009900990099009";
tm3.round(0);
assert(tm3 == "1");
tm3 = "1.0099009900990099009";
tm3.mround(3);
assert(tm3 == "1.01");
tm3 = "1.0099009900990099009";
tm3.mround(-1);
assert(tm3 == "0");
tm3 = "1.0099009900990099009";
tm3.mround(0);
assert(tm3 == "1");
tm3 = "1.0099004900990099009";
tm3.mround(6);
assert(tm3 == "1.0099");
assert(tm2 > tm1);
assert(tm1 < tm2);
assert(tm2 >= tm1);
assert(tm1 <= tm2);
//assert(decimal(1900.) == decimal("1900"));
// Performance tests.
std::string tmpstring;
////////////
/// FixPoint
clock_gettime(CLOCK_REALTIME, &t1);
for (int i = 0; i < 1000000; ++i)
{
decimal tm3 = tm1 + tm2;
}
clock_gettime(CLOCK_REALTIME, &t2);
t = t2.tv_sec * 1000000000 + t2.tv_nsec - t1.tv_sec * 1000000000 - t1.tv_nsec;
std::cout << "FixPoint sum time: " << (t/1000000) << "ms." << ((t/1000) % 1000) << "." << (t % 1000) << std::endl;
clock_gettime(CLOCK_REALTIME, &t1);
for (int i = 0; i < 1000000; ++i)
{
decimal tm3 = tm1 * tm2;
}
clock_gettime(CLOCK_REALTIME, &t2);
t = t2.tv_sec * 1000000000 + t2.tv_nsec - t1.tv_sec * 1000000000 - t1.tv_nsec;
std::cout << "FixPoint mul time: " << (t/1000000) << "ms." << ((t/1000) % 1000) << "." << (t % 1000) << std::endl;
clock_gettime(CLOCK_REALTIME, &t1);
for (int i = 0; i < 1000000; ++i)
{
decimal tm3 = tm1 / tm2;
}
clock_gettime(CLOCK_REALTIME, &t2);
t = t2.tv_sec * 1000000000 + t2.tv_nsec - t1.tv_sec * 1000000000 - t1.tv_nsec;
std::cout << "FixPoint div time: " << (t/1000000) << "ms." << ((t/1000) % 1000) << "." << (t % 1000) << std::endl;
clock_gettime(CLOCK_REALTIME, &t1);
tm1 = "1.01";
for (int i = 0; i < 1000000; ++i)
{
decimal tm22;
tm22 = tm2;
decimal tm3 = tm1 / tm22;
}
clock_gettime(CLOCK_REALTIME, &t2);
t = t2.tv_sec * 1000000000 + t2.tv_nsec - t1.tv_sec * 1000000000 - t1.tv_nsec;
std::cout << "FixPoint div with construct time: " << (t/1000000) << "ms." << ((t/1000) % 1000) << "." << (t % 1000) << std::endl;
clock_gettime(CLOCK_REALTIME, &t1);
tm1 = "1.01";
for (int i = 0; i < 1000000; ++i)
{
tmpstring = tm1;
}
clock_gettime(CLOCK_REALTIME, &t2);
t = t2.tv_sec * 1000000000 + t2.tv_nsec - t1.tv_sec * 1000000000 - t1.tv_nsec;
std::cout << "FixPoint to string time: " << (t/1000000) << "ms." << ((t/1000) % 1000) << "." << (t % 1000) << std::endl;
clock_gettime(CLOCK_REALTIME, &t1);
tm1 = "1.01";
for (int i = 0; i < 1000000; ++i)
{
tm1 = "1.0018";
}
clock_gettime(CLOCK_REALTIME, &t2);
t = t2.tv_sec * 1000000000 + t2.tv_nsec - t1.tv_sec * 1000000000 - t1.tv_nsec;
std::cout << "FixPoint from string time: " << (t/1000000) << "ms." << ((t/1000) % 1000) << "." << (t % 1000) << std::endl;
//////////////////////
/// GMP MPQ
mpq_class tz1(101, 100), tz2(102, 100);
clock_gettime(CLOCK_REALTIME, &t1);
for (int i = 0; i < 1000000; ++i)
{
mpq_class tz3 = tz1 + tz2;
}
clock_gettime(CLOCK_REALTIME, &t2);
t = t2.tv_sec * 1000000000 + t2.tv_nsec - t1.tv_sec * 1000000000 - t1.tv_nsec;
std::cout << "MPQ sum time: " << (t/1000000) << "ms." << ((t/1000) % 1000) << "." << (t % 1000) << std::endl;
clock_gettime(CLOCK_REALTIME, &t1);
for (int i = 0; i < 1000000; ++i)
{
mpq_class tz3 = tz1 * tz2;
}
clock_gettime(CLOCK_REALTIME, &t2);
t = t2.tv_sec * 1000000000 + t2.tv_nsec - t1.tv_sec * 1000000000 - t1.tv_nsec;
std::cout << "MPQ mul time: " << (t/1000000) << "ms." << ((t/1000) % 1000) << "." << (t % 1000) << std::endl;
clock_gettime(CLOCK_REALTIME, &t1);
for (int i = 0; i < 1000000; ++i)
{
mpq_class tz3 = tz1 / tz2;
}
clock_gettime(CLOCK_REALTIME, &t2);
t = t2.tv_sec * 1000000000 + t2.tv_nsec - t1.tv_sec * 1000000000 - t1.tv_nsec;
std::cout << "MPQ div time: " << (t/1000000) << "ms." << ((t/1000) % 1000) << "." << (t % 1000) << std::endl;
clock_gettime(CLOCK_REALTIME, &t1);
tz1 = mpq_class (101, 100);
for (int i = 0; i < 1000000; ++i)
{
mpq_class tz2(102, 100);
mpq_class tz3 = tz1 / tz2;
}
clock_gettime(CLOCK_REALTIME, &t2);
t = t2.tv_sec * 1000000000 + t2.tv_nsec - t1.tv_sec * 1000000000 - t1.tv_nsec;
std::cout << "MPQ div with construct time: " << (t/1000000) << "ms." << ((t/1000) % 1000) << "." << (t % 1000) << std::endl;
#ifdef ADDITIONAL_TESTS
clock_gettime(CLOCK_REALTIME, &t1);
for (int i = 0; i < 1000000; ++i)
{
tmpstring = to_string(tz1);
}
clock_gettime(CLOCK_REALTIME, &t2);
t = t2.tv_sec * 1000000000 + t2.tv_nsec - t1.tv_sec * 1000000000 - t1.tv_nsec;
std::cout << "MPQ to string time: " << (t/1000000) << "ms." << ((t/1000) % 1000) << "." << (t % 1000) << std::endl;
clock_gettime(CLOCK_REALTIME, &t1);
for (int i = 0; i < 1000000; ++i)
{
tz1 = to_mpq("1.0018");
}
clock_gettime(CLOCK_REALTIME, &t2);
t = t2.tv_sec * 1000000000 + t2.tv_nsec - t1.tv_sec * 1000000000 - t1.tv_nsec;
std::cout << "MPQ from string time: " << (t/1000000) << "ms." << ((t/1000) % 1000) << "." << (t % 1000) << std::endl;
/////////////
/// MpDecimal compare
mpd_context_t* context = new mpd_context_t();
mpd_init(context, 128);
context->traps = 0;
mpd_t *td1;
mpd_t *td2;
mpd_t *td3;
uint32_t status{0};
td1 = mpd_qnew();
mpd_qset_string(td1, "1.01", context, &status);
td2 = mpd_qnew();
mpd_qset_string(td2, "1.02", context, &status);
td3 = mpd_qnew();
mpd_qmul(td3, td1, td2, context, &status);
std::cout << mpd_qformat(td3, "f", context, &status) << std::endl;
clock_gettime(CLOCK_REALTIME, &t1);
for (int i = 0; i < 1000000; ++i)
{
mpd_del(td3);
td3 = mpd_qnew();
mpd_qadd(td3, td1, td2, context, &status);
}
clock_gettime(CLOCK_REALTIME, &t2);
mpd_del(td1);
mpd_del(td2);
mpd_del(td3);
t = t2.tv_sec * 1000000000 + t2.tv_nsec - t1.tv_sec * 1000000000 - t1.tv_nsec;
std::cout << "MPD sum time: " << (t/1000000) << "ms." << ((t/1000) % 1000) << "." << (t % 1000) << std::endl;
#endif //#ifdef ADDITIONAL_TESTS
}