Skip to content

Commit d03fa69

Browse files
committed
Add _Decimal64 basic functions
1 parent 99b9fdf commit d03fa69

File tree

1 file changed

+59
-10
lines changed

1 file changed

+59
-10
lines changed

test/benchmark_libdfp.c

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void test_comparisons_32(_Decimal32* data, const char* label)
5353
clock_gettime(CLOCK_MONOTONIC, &t2);
5454

5555
uint64_t elapsed_time_us = (uint64_t)((t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_nsec - t1.tv_nsec) / 1000);
56-
printf("comparisons<%-10s >: %-10" PRIu64 " us (s=%zu)\n", label, elapsed_time_us, s);
56+
printf("Comparisons <%-10s >: %-10" PRIu64 " us (s=%zu)\n", label, elapsed_time_us, s);
5757
}
5858

5959
void generate_vector_64(_Decimal64* buffer, size_t buffer_len)
@@ -92,7 +92,7 @@ void test_comparisons_64(_Decimal64* data, const char* label)
9292
clock_gettime(CLOCK_MONOTONIC, &t2);
9393

9494
uint64_t elapsed_time_us = (uint64_t)((t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_nsec - t1.tv_nsec) / 1000);
95-
printf("comparisons<%-10s >: %-10" PRIu64 " us (s=%zu)\n", label, elapsed_time_us, s);
95+
printf("Comparisons <%-10s >: %-10" PRIu64 " us (s=%zu)\n", label, elapsed_time_us, s);
9696
}
9797

9898
void generate_vector_128(_Decimal128* buffer, size_t buffer_len)
@@ -131,7 +131,7 @@ void test_comparisons_128(_Decimal128* data, const char* label)
131131
clock_gettime(CLOCK_MONOTONIC, &t2);
132132

133133
uint64_t elapsed_time_us = (uint64_t)((t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_nsec - t1.tv_nsec) / 1000);
134-
printf("comparisons<%-10s>: %-10" PRIu64 " us (s=%zu)\n", label, elapsed_time_us, s);
134+
printf("Comparisons <%-10s>: %-10" PRIu64 " us (s=%zu)\n", label, elapsed_time_us, s);
135135
}
136136

137137
typedef _Decimal32 (*operation_32)(_Decimal32, _Decimal32);
@@ -152,10 +152,10 @@ _Decimal32 mul_32(_Decimal32 a, _Decimal32 b)
152152

153153
_Decimal32 div_32(_Decimal32 a, _Decimal32 b)
154154
{
155-
return a * b;
155+
return a / b;
156156
}
157157

158-
void test_two_element_operation_32(_Decimal32* data, operation_32 op, const char* label)
158+
void test_two_element_operation_32(_Decimal32* data, operation_32 op, const char* label, const char* op_label)
159159
{
160160
struct timespec t1, t2;
161161
clock_gettime(CLOCK_MONOTONIC, &t1);
@@ -176,7 +176,52 @@ void test_two_element_operation_32(_Decimal32* data, operation_32 op, const char
176176
clock_gettime(CLOCK_MONOTONIC, &t2);
177177

178178
uint64_t elapsed_time_us = (uint64_t)((t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_nsec - t1.tv_nsec) / 1000);
179-
printf("comparisons<%-10s>: %-10" PRIu64 " us (s=%zu)\n", label, elapsed_time_us, s);
179+
printf("%-15s<%-10s >: %-10" PRIu64 " us (s=%zu)\n", op_label, label, elapsed_time_us, s);
180+
}
181+
182+
typedef _Decimal64 (*operation_64)(_Decimal64, _Decimal64);
183+
184+
_Decimal64 add_64(_Decimal64 a, _Decimal64 b)
185+
{
186+
return a + b;
187+
}
188+
_Decimal64 sub_64(_Decimal64 a, _Decimal64 b)
189+
{
190+
return a - b;
191+
}
192+
193+
_Decimal64 mul_64(_Decimal64 a, _Decimal64 b)
194+
{
195+
return a * b;
196+
}
197+
198+
_Decimal64 div_64(_Decimal64 a, _Decimal64 b)
199+
{
200+
return a / b;
201+
}
202+
203+
void test_two_element_operation_64(_Decimal64* data, operation_64 op, const char* label, const char* op_label)
204+
{
205+
struct timespec t1, t2;
206+
clock_gettime(CLOCK_MONOTONIC, &t1);
207+
208+
size_t s = 0;
209+
210+
for (size_t n = 0; n < N; ++n)
211+
{
212+
for (size_t k = 0; k < K - 1; ++k)
213+
{
214+
_Decimal64 val1 = data[k];
215+
_Decimal64 val2 = data[k + 1];
216+
217+
s += (size_t)op(val1, val2);
218+
}
219+
}
220+
221+
clock_gettime(CLOCK_MONOTONIC, &t2);
222+
223+
uint64_t elapsed_time_us = (uint64_t)((t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_nsec - t1.tv_nsec) / 1000);
224+
printf("%-15s<%-10s >: %-10" PRIu64 " us (s=%zu)\n", op_label, label, elapsed_time_us, s);
180225
}
181226

182227
int main()
@@ -206,19 +251,23 @@ int main()
206251

207252
printf("\n===== Addition =====\n");
208253

209-
test_two_element_operation_32(d32_array, add_32, "_Decimal32");
254+
test_two_element_operation_32(d32_array, add_32, "_Decimal32", "Addition");
255+
test_two_element_operation_64(d64_array, add_64, "_Decimal64", "Addition");
210256

211257
printf("\n===== Subtraction =====\n");
212258

213-
test_two_element_operation_32(d32_array, sub_32, "_Decimal32");
259+
test_two_element_operation_32(d32_array, sub_32, "_Decimal32", "Subtraction");
260+
test_two_element_operation_64(d64_array, sub_64, "_Decimal64", "Subtraction");
214261

215262
printf("\n===== Multiplication =====\n");
216263

217-
test_two_element_operation_32(d32_array, mul_32, "_Decimal32");
264+
test_two_element_operation_32(d32_array, mul_32, "_Decimal32", "Multiplication");
265+
test_two_element_operation_64(d64_array, mul_64, "_Decimal64", "Multiplication");
218266

219267
printf("\n===== Division =====\n");
220268

221-
test_two_element_operation_32(d32_array, div_32, "_Decimal32");
269+
test_two_element_operation_32(d32_array, div_32, "_Decimal32", "Division");
270+
test_two_element_operation_64(d64_array, div_64, "_Decimal64", "Division");
222271

223272
free(d32_array);
224273
free(d64_array);

0 commit comments

Comments
 (0)