Skip to content

Commit 8e687ef

Browse files
committed
Add _Decimal128 benchmarks
1 parent d03fa69 commit 8e687ef

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

test/benchmark_libdfp.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,51 @@ void test_two_element_operation_64(_Decimal64* data, operation_64 op, const char
224224
printf("%-15s<%-10s >: %-10" PRIu64 " us (s=%zu)\n", op_label, label, elapsed_time_us, s);
225225
}
226226

227+
typedef _Decimal128 (*operation_128)(_Decimal128, _Decimal128);
228+
229+
_Decimal128 add_128(_Decimal128 a, _Decimal128 b)
230+
{
231+
return a + b;
232+
}
233+
_Decimal128 sub_128(_Decimal128 a, _Decimal128 b)
234+
{
235+
return a - b;
236+
}
237+
238+
_Decimal128 mul_128(_Decimal128 a, _Decimal128 b)
239+
{
240+
return a * b;
241+
}
242+
243+
_Decimal128 div_128(_Decimal128 a, _Decimal128 b)
244+
{
245+
return a / b;
246+
}
247+
248+
void test_two_element_operation_128(_Decimal128* data, operation_128 op, const char* label, const char* op_label)
249+
{
250+
struct timespec t1, t2;
251+
clock_gettime(CLOCK_MONOTONIC, &t1);
252+
253+
size_t s = 0;
254+
255+
for (size_t n = 0; n < N; ++n)
256+
{
257+
for (size_t k = 0; k < K - 1; ++k)
258+
{
259+
_Decimal128 val1 = data[k];
260+
_Decimal128 val2 = data[k + 1];
261+
262+
s += (size_t)op(val1, val2);
263+
}
264+
}
265+
266+
clock_gettime(CLOCK_MONOTONIC, &t2);
267+
268+
uint64_t elapsed_time_us = (uint64_t)((t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_nsec - t1.tv_nsec) / 1000);
269+
printf("%-15s<%-10s>: %-10" PRIu64 " us (s=%zu)\n", op_label, label, elapsed_time_us, s);
270+
}
271+
227272
int main()
228273
{
229274
// One time init of random number generator
@@ -253,21 +298,25 @@ int main()
253298

254299
test_two_element_operation_32(d32_array, add_32, "_Decimal32", "Addition");
255300
test_two_element_operation_64(d64_array, add_64, "_Decimal64", "Addition");
301+
test_two_element_operation_128(d128_array, add_128, "_Decimal128", "Addition");
256302

257303
printf("\n===== Subtraction =====\n");
258304

259305
test_two_element_operation_32(d32_array, sub_32, "_Decimal32", "Subtraction");
260306
test_two_element_operation_64(d64_array, sub_64, "_Decimal64", "Subtraction");
307+
test_two_element_operation_128(d128_array, sub_128, "_Decimal128", "Subtraction");
261308

262309
printf("\n===== Multiplication =====\n");
263310

264311
test_two_element_operation_32(d32_array, mul_32, "_Decimal32", "Multiplication");
265312
test_two_element_operation_64(d64_array, mul_64, "_Decimal64", "Multiplication");
313+
test_two_element_operation_128(d128_array, mul_128, "_Decimal128", "Multiplication");
266314

267315
printf("\n===== Division =====\n");
268316

269317
test_two_element_operation_32(d32_array, div_32, "_Decimal32", "Division");
270318
test_two_element_operation_64(d64_array, div_64, "_Decimal64", "Division");
319+
test_two_element_operation_128(d128_array, div_128, "_Decimal128", "Division");
271320

272321
free(d32_array);
273322
free(d64_array);

0 commit comments

Comments
 (0)