Skip to content

Commit 99b9fdf

Browse files
committed
Add basic functions for _Decimal32
1 parent 1f9a2ee commit 99b9fdf

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

test/benchmark_libdfp.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,51 @@ void test_comparisons_128(_Decimal128* data, const char* label)
134134
printf("comparisons<%-10s>: %-10" PRIu64 " us (s=%zu)\n", label, elapsed_time_us, s);
135135
}
136136

137+
typedef _Decimal32 (*operation_32)(_Decimal32, _Decimal32);
138+
139+
_Decimal32 add_32(_Decimal32 a, _Decimal32 b)
140+
{
141+
return a + b;
142+
}
143+
_Decimal32 sub_32(_Decimal32 a, _Decimal32 b)
144+
{
145+
return a - b;
146+
}
147+
148+
_Decimal32 mul_32(_Decimal32 a, _Decimal32 b)
149+
{
150+
return a * b;
151+
}
152+
153+
_Decimal32 div_32(_Decimal32 a, _Decimal32 b)
154+
{
155+
return a * b;
156+
}
157+
158+
void test_two_element_operation_32(_Decimal32* data, operation_32 op, const char* label)
159+
{
160+
struct timespec t1, t2;
161+
clock_gettime(CLOCK_MONOTONIC, &t1);
162+
163+
size_t s = 0;
164+
165+
for (size_t n = 0; n < N; ++n)
166+
{
167+
for (size_t k = 0; k < K - 1; ++k)
168+
{
169+
_Decimal32 val1 = data[k];
170+
_Decimal32 val2 = data[k + 1];
171+
172+
s += (size_t)op(val1, val2);
173+
}
174+
}
175+
176+
clock_gettime(CLOCK_MONOTONIC, &t2);
177+
178+
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);
180+
}
181+
137182
int main()
138183
{
139184
// One time init of random number generator
@@ -148,6 +193,8 @@ int main()
148193
return 1;
149194
}
150195

196+
printf("===== Comparisons =====\n");
197+
151198
generate_vector_32(d32_array, K);
152199
test_comparisons_32(d32_array, "_Decimal32");
153200

@@ -157,6 +204,22 @@ int main()
157204
generate_vector_128(d128_array, K);
158205
test_comparisons_128(d128_array, "_Decimal128");
159206

207+
printf("\n===== Addition =====\n");
208+
209+
test_two_element_operation_32(d32_array, add_32, "_Decimal32");
210+
211+
printf("\n===== Subtraction =====\n");
212+
213+
test_two_element_operation_32(d32_array, sub_32, "_Decimal32");
214+
215+
printf("\n===== Multiplication =====\n");
216+
217+
test_two_element_operation_32(d32_array, mul_32, "_Decimal32");
218+
219+
printf("\n===== Division =====\n");
220+
221+
test_two_element_operation_32(d32_array, div_32, "_Decimal32");
222+
160223
free(d32_array);
161224
free(d64_array);
162225
free(d128_array);

0 commit comments

Comments
 (0)