forked from cppalliance/decimal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_libdfp.c
326 lines (255 loc) · 8.63 KB
/
benchmark_libdfp.c
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2024 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <inttypes.h>
#define K 2000000
#define N 5
double float_rand(double min, double max)
{
float scale = rand() / (double) RAND_MAX;
return min + scale * (max - min);
}
__attribute__ ((__noinline__)) void generate_vector_32(_Decimal32* buffer, size_t buffer_len)
{
size_t i = 0;
while (i < buffer_len)
{
buffer[i] = float_rand(0.0, 1.0);
++i;
}
}
__attribute__ ((__noinline__)) void test_comparisons_32(_Decimal32* data, const char* label)
{
struct timespec t1, t2;
clock_gettime(CLOCK_MONOTONIC, &t1);
size_t s = 0;
for (size_t n = 0; n < N; ++n)
{
for (size_t k = 0; k < K - 1; ++k)
{
_Decimal32 val1 = data[k];
_Decimal32 val2 = data[k + 1];
s += (size_t)(val1 > val2);
s += (size_t)(val1 >= val2);
s += (size_t)(val1 < val2);
s += (size_t)(val1 <= val2);
s += (size_t)(val1 == val2);
s += (size_t)(val1 != val2);
}
}
clock_gettime(CLOCK_MONOTONIC, &t2);
uint64_t elapsed_time_us = (uint64_t)((t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_nsec - t1.tv_nsec) / 1000);
printf("Comparisons <%-10s >: %-10" PRIu64 " us (s=%zu)\n", label, elapsed_time_us, s);
}
__attribute__ ((__noinline__)) void generate_vector_64(_Decimal64* buffer, size_t buffer_len)
{
size_t i = 0;
while (i < buffer_len)
{
buffer[i] = float_rand(0.0, 1.0);
++i;
}
}
__attribute__ ((__noinline__)) void test_comparisons_64(_Decimal64* data, const char* label)
{
struct timespec t1, t2;
clock_gettime(CLOCK_MONOTONIC, &t1);
size_t s = 0;
for (size_t n = 0; n < N; ++n)
{
for (size_t k = 0; k < K - 1; ++k)
{
_Decimal64 val1 = data[k];
_Decimal64 val2 = data[k + 1];
s += (size_t)(val1 > val2);
s += (size_t)(val1 >= val2);
s += (size_t)(val1 < val2);
s += (size_t)(val1 <= val2);
s += (size_t)(val1 == val2);
s += (size_t)(val1 != val2);
}
}
clock_gettime(CLOCK_MONOTONIC, &t2);
uint64_t elapsed_time_us = (uint64_t)((t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_nsec - t1.tv_nsec) / 1000);
printf("Comparisons <%-10s >: %-10" PRIu64 " us (s=%zu)\n", label, elapsed_time_us, s);
}
__attribute__ ((__noinline__)) void generate_vector_128(_Decimal128* buffer, size_t buffer_len)
{
size_t i = 0;
while (i < buffer_len)
{
buffer[i] = float_rand(0.0, 1.0);
++i;
}
}
__attribute__ ((__noinline__)) void test_comparisons_128(_Decimal128* data, const char* label)
{
struct timespec t1, t2;
clock_gettime(CLOCK_MONOTONIC, &t1);
size_t s = 0;
for (size_t n = 0; n < N; ++n)
{
for (size_t k = 0; k < K - 1; ++k)
{
_Decimal128 val1 = data[k];
_Decimal128 val2 = data[k + 1];
s += (size_t)(val1 > val2);
s += (size_t)(val1 >= val2);
s += (size_t)(val1 < val2);
s += (size_t)(val1 <= val2);
s += (size_t)(val1 == val2);
s += (size_t)(val1 != val2);
}
}
clock_gettime(CLOCK_MONOTONIC, &t2);
uint64_t elapsed_time_us = (uint64_t)((t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_nsec - t1.tv_nsec) / 1000);
printf("Comparisons <%-10s>: %-10" PRIu64 " us (s=%zu)\n", label, elapsed_time_us, s);
}
typedef _Decimal32 (*operation_32)(_Decimal32, _Decimal32);
_Decimal32 add_32(_Decimal32 a, _Decimal32 b)
{
return a + b;
}
_Decimal32 sub_32(_Decimal32 a, _Decimal32 b)
{
return a - b;
}
_Decimal32 mul_32(_Decimal32 a, _Decimal32 b)
{
return a * b;
}
_Decimal32 div_32(_Decimal32 a, _Decimal32 b)
{
return a / b;
}
__attribute__ ((__noinline__)) void test_two_element_operation_32(_Decimal32* data, operation_32 op, const char* label, const char* op_label)
{
struct timespec t1, t2;
clock_gettime(CLOCK_MONOTONIC, &t1);
size_t s = 0;
for (size_t n = 0; n < N; ++n)
{
for (size_t k = 0; k < K - 1; ++k)
{
_Decimal32 val1 = data[k];
_Decimal32 val2 = data[k + 1];
s += (size_t)op(val1, val2);
}
}
clock_gettime(CLOCK_MONOTONIC, &t2);
uint64_t elapsed_time_us = (uint64_t)((t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_nsec - t1.tv_nsec) / 1000);
printf("%-15s<%-10s >: %-10" PRIu64 " us (s=%zu)\n", op_label, label, elapsed_time_us, s);
}
typedef _Decimal64 (*operation_64)(_Decimal64, _Decimal64);
_Decimal64 add_64(_Decimal64 a, _Decimal64 b)
{
return a + b;
}
_Decimal64 sub_64(_Decimal64 a, _Decimal64 b)
{
return a - b;
}
_Decimal64 mul_64(_Decimal64 a, _Decimal64 b)
{
return a * b;
}
_Decimal64 div_64(_Decimal64 a, _Decimal64 b)
{
return a / b;
}
__attribute__ ((__noinline__)) void test_two_element_operation_64(_Decimal64* data, operation_64 op, const char* label, const char* op_label)
{
struct timespec t1, t2;
clock_gettime(CLOCK_MONOTONIC, &t1);
size_t s = 0;
for (size_t n = 0; n < N; ++n)
{
for (size_t k = 0; k < K - 1; ++k)
{
_Decimal64 val1 = data[k];
_Decimal64 val2 = data[k + 1];
s += (size_t)op(val1, val2);
}
}
clock_gettime(CLOCK_MONOTONIC, &t2);
uint64_t elapsed_time_us = (uint64_t)((t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_nsec - t1.tv_nsec) / 1000);
printf("%-15s<%-10s >: %-10" PRIu64 " us (s=%zu)\n", op_label, label, elapsed_time_us, s);
}
typedef _Decimal128 (*operation_128)(_Decimal128, _Decimal128);
_Decimal128 add_128(_Decimal128 a, _Decimal128 b)
{
return a + b;
}
_Decimal128 sub_128(_Decimal128 a, _Decimal128 b)
{
return a - b;
}
_Decimal128 mul_128(_Decimal128 a, _Decimal128 b)
{
return a * b;
}
_Decimal128 div_128(_Decimal128 a, _Decimal128 b)
{
return a / b;
}
__attribute__ ((__noinline__)) void test_two_element_operation_128(_Decimal128* data, operation_128 op, const char* label, const char* op_label)
{
struct timespec t1, t2;
clock_gettime(CLOCK_MONOTONIC, &t1);
size_t s = 0;
for (size_t n = 0; n < N; ++n)
{
for (size_t k = 0; k < K - 1; ++k)
{
_Decimal128 val1 = data[k];
_Decimal128 val2 = data[k + 1];
s += (size_t)op(val1, val2);
}
}
clock_gettime(CLOCK_MONOTONIC, &t2);
uint64_t elapsed_time_us = (uint64_t)((t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_nsec - t1.tv_nsec) / 1000);
printf("%-15s<%-10s>: %-10" PRIu64 " us (s=%zu)\n", op_label, label, elapsed_time_us, s);
}
int main()
{
// One time init of random number generator
srand(time(NULL));
_Decimal32* d32_array = malloc(K * sizeof(_Decimal32));
_Decimal64* d64_array = malloc(K * sizeof(_Decimal64));
_Decimal128* d128_array = malloc(K * sizeof(_Decimal128));
if (d32_array == NULL || d64_array == NULL || d128_array == NULL)
{
return 1;
}
printf("===== Comparisons =====\n");
generate_vector_32(d32_array, K);
test_comparisons_32(d32_array, "_Decimal32");
generate_vector_64(d64_array, K);
test_comparisons_64(d64_array, "_Decimal64");
generate_vector_128(d128_array, K);
test_comparisons_128(d128_array, "_Decimal128");
printf("\n===== Addition =====\n");
test_two_element_operation_32(d32_array, add_32, "_Decimal32", "Addition");
test_two_element_operation_64(d64_array, add_64, "_Decimal64", "Addition");
test_two_element_operation_128(d128_array, add_128, "_Decimal128", "Addition");
printf("\n===== Subtraction =====\n");
test_two_element_operation_32(d32_array, sub_32, "_Decimal32", "Subtraction");
test_two_element_operation_64(d64_array, sub_64, "_Decimal64", "Subtraction");
test_two_element_operation_128(d128_array, sub_128, "_Decimal128", "Subtraction");
printf("\n===== Multiplication =====\n");
test_two_element_operation_32(d32_array, mul_32, "_Decimal32", "Multiplication");
test_two_element_operation_64(d64_array, mul_64, "_Decimal64", "Multiplication");
test_two_element_operation_128(d128_array, mul_128, "_Decimal128", "Multiplication");
printf("\n===== Division =====\n");
test_two_element_operation_32(d32_array, div_32, "_Decimal32", "Division");
test_two_element_operation_64(d64_array, div_64, "_Decimal64", "Division");
test_two_element_operation_128(d128_array, div_128, "_Decimal128", "Division");
free(d32_array);
free(d64_array);
free(d128_array);
return 0;
}