-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
47 lines (38 loc) · 1.36 KB
/
example.c
File metadata and controls
47 lines (38 loc) · 1.36 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
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE(x) (sizeof x / sizeof x[0])
int64_t mdiv(int64_t *x, size_t n, int64_t y);
typedef struct {
size_t const n; // Size of the dividend
int64_t const *x; // Dividend
int64_t const y; // Divisor
} data_t;
static const data_t data[] = {
{1, (int64_t[1]){13}, 5}, // Dividend is `13`.
{2, (int64_t[2]){0, 13}, 5}, // Dividend is `13 << 64` (239807672958224171008).
{3, (int64_t[3]){1, 1, 1}, 2}, // Dividend is `(1 << 128) + (1 << 64) + 1` (340282366920938463481821351505477763073).
};
int main() {
for (size_t i = 0; i < SIZE(data); ++i) {
size_t n = data[i].n;
int64_t *work_space = malloc(n * sizeof(int64_t));
assert(work_space);
memcpy(work_space, data[i].x, n * sizeof(int64_t));
printf("Test case %zu:\n", i);
printf("Dividend: ");
for (size_t j = 0; j < data[i].n; j++) {
printf("0x%016" PRIx64 " ", data[i].x[j]);
}
printf("\nDivisor: 0x%016" PRIx64 "\n", data[i].y);
int64_t r = mdiv(work_space, n, data[i].y);
printf("Quotient: ");
for (size_t j = 0; j < n; ++j) {
printf("0x%016" PRIx64 " ", work_space[j]);
}
printf("\nRemainder: 0x%016" PRIx64 "\n\n", r);
free(work_space);
}
}