summaryrefslogtreecommitdiffstats
path: root/test.c
blob: 2a2934aa5bd485385eba3a6dbc64c651784682f7 (plain)
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
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include "pbn.h"

static void dump_div(struct pbn **qp, struct pbn **rp, struct pbn *n, struct pbn *d)
{
    int rv;

    pbn_dump(stdout, n);
    printf(" / ");
    pbn_dump(stdout, d);
    putchar('\n');

    rv = pbn_div(qp, rp, n, d);

    if (rv) {
        printf("-> %d\n", rv);
    } else {
        pbn_dump(stdout, *qp);
        printf(" : ");
        pbn_dump(stdout, *rp);
        putchar('\n');
    }
}

static void divide_test(void)
{
    struct pbn *n, *d, *r, *q;

    printf("\n*** Divide test ***\n\n");

    /* 2^128/2^64 */

    n = pbn_new(5);
    n->num[4] = 1;		/* 2^128 */
    pbn_adjust_bits(n);

    d = pbn_new(3);
    d->num[2] = 1;		/* 2^64 */
    pbn_adjust_bits(d);

    dump_div(&q, &r, n, d);

    /* 2^128/3 */

    n = pbn_new(5);
    n->num[4] = 1;		/* 2^128 */
    pbn_adjust_bits(n);

    d = pbn_int(3);

    dump_div(&q, &r, n, d);

    /* 2^128/(3*2^64) */

    n = pbn_new(5);
    n->num[4] = 1;		/* 2^128 */
    pbn_adjust_bits(n);

    d = pbn_new(3);
    d->num[2] = 3;		/* 3*2^64 */
    pbn_adjust_bits(d);

    dump_div(&q, &r, n, d);

    /* 2^128/(3*2^64+2^31+1) */

    n = pbn_new(5);
    n->num[4] = 1;		/* 2^128 */
    pbn_adjust_bits(n);

    d = pbn_new(3);
    d->num[2] = 3;		/* 3*2^64 */
    d->num[0] = 0x80000001;     /* +2^31+1 */
    pbn_adjust_bits(d);

    dump_div(&q, &r, n, d);

    /* Something that needs an addback... */

    n = pbn_new(5);
    n->num[4] = 0x7fffffff;
    n->num[3] = 0x80000000;
    pbn_adjust_bits(n);

    d = pbn_new(3);
    d->num[2] = 0x80000000;
    d->num[0] = 0x00000001;
    pbn_adjust_bits(d);

    dump_div(&q, &r, n, d);
}


static void small_divide_test(void)
{
    struct pbn *n, *q;
    pbn_slimb_t r;
    int rv;

    printf("\n*** Small divide test ***\n\n");

    /* 2^128/3 */

    n = pbn_new(5);
    n->num[4] = 1;		/* 2^128 */
    pbn_adjust_bits(n);

    rv = pbn_divs(&q, &r, n, 3);

    pbn_dump(stdout, q);
    printf(" : %"PBN_LIMB_xFMT"\n", r);
}

int main(int argc, char *argv[])
{
    uint64_t i, j;
    struct pbn *m, *mj;
    int nx = (argc < 2) ? 64 : atoi(argv[1]);
    int n;

    m = pbn_int(5);
    i = 5;

    printf("\n*** Multiply test ***\n\n");

    n = nx;
    while (n--) {
	printf("%016"PRIx64" : ", i);
	pbn_dump(stdout, m);
	putchar('\n');

	i *= 5;
	m = pbn_mul(m, pbn_int(5));
    }
    pbn_free(m);

    printf("\n*** Add test ***\n\n");

    m = pbn_int(5);
    i = 5;
    n = nx;
    while (n--) {
	printf("%016"PRIx64" : ", i);
	pbn_dump(stdout, m);
	putchar('\n');

	i += i;
	m = pbn_add(m, pbn_ref(m));
    }

    printf("\n*** Subtract test ***\n\n");

    n = nx;
    j = 20;
    mj = pbn_int(20);
    while (n--) {
	printf("%016"PRIx64" : ", i);
	pbn_dump(stdout, m);
	printf(" : ");
	pbn_dump(stdout, mj);
	putchar('\n');

	i -= j;
	j += j;
	m = pbn_sub(m, pbn_ref(mj));
	mj = pbn_add(mj, pbn_ref(mj));
    }

    printf("\n*** Shift test ***\n\n");

    printf("[ 0] ");
    pbn_dump(stdout, m);
    printf(" (%d)", m->bits);
    putchar('\n');

    for (n = 1; n <= 8; n++) {
	m = pbn_shl(m, n);
	printf("[%2d] ", n);
	pbn_dump(stdout, m);
	printf(" (%d)", m->bits);
	putchar('\n');
    }

    for (n = 1; n <= 16; n++) {
	m = pbn_shr(m, n);
	printf("[%2d] ", n);
	pbn_dump(stdout, m);
	printf(" (%d)", m->bits);
	putchar('\n');
    }
    pbn_free(m);

    divide_test();
    small_divide_test();

    return 0;
}