aboutsummaryrefslogtreecommitdiffstats
path: root/bench/libhebimath.h
blob: e2ef9361fc5986f560eac4f68e4528f63d218f91 (plain) (blame)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <hebimath.h>

#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#define BIGINT_LIBRARY "hebimath"

typedef hebi_int z_t[1];

static z_t _0, _1, _a, _b, _m;

static void
zsetup(jmp_buf env)
{
	(void) env;
	hebi_set_u(_0, 0);
	hebi_set_u(_1, 1);
	hebi_init(_a);
	hebi_init(_b);
	hebi_init(_m);
}

static void
zunsetup(void)
{
	hebi_destroy(_0);
	hebi_destroy(_1);
	hebi_destroy(_a);
	hebi_destroy(_b);
	hebi_destroy(_m);
}

#define FAST_RANDOM             0
#define SECURE_RANDOM           0
#define DEFAULT_RANDOM          0
#define FASTEST_RANDOM          0
#define LIBC_RAND_RANDOM        0
#define LIBC_RANDOM_RANDOM      0
#define LIBC_RAND48_RANDOM      0
#define QUASIUNIFORM            0
#define UNIFORM                 1
#define MODUNIFORM              2

#define zperror(x)              ((void)0)
#define zinit                   hebi_init
#define zfree                   hebi_destroy

#define zset                    hebi_set
#define zsetu                   hebi_set_u
#define zseti                   hebi_set_i
#define zneg                    hebi_neg
#define zabs(r, a)              (hebi_sign(a) < 0 ? zneg(r, a) : zset(r, a))
#define zadd_unsigned(r, a, b)  (zabs(_a, a), zabs(_b, b), zadd(r, _a, _b))
#define zsub_unsigned(r, a, b)  (zabs(_a, a), zabs(_b, b), zsub(r, _a, _b))
#define zadd                    hebi_add
#define zsub                    hebi_sub
#define zbtest(a, b)            (hebi_shl(_a, a, b), (hebi_get_u(a) & 1))
#define zeven(a)                (~hebi_get_u(a) & 1)
#define zodd(a)                 (hebi_get_u(a) & 1)
#define zeven_nonzero           zeven
#define zodd_nonzero            zodd
#define zzero(a)                (!zsignum(a))
#define zsignum(a)              (zcmp(a, _0))
#define zswap                   hebi_swap
#define zlsh                    hebi_shl
#define zrsh                    hebi_shr
#define ztrunc(r, a, b)         (hebi_shr(_a, a, b), hebi_shl(_a, _a, b), hebi_sub(r, a, _a))
#define zcmpmag                 hebi_cmp_mag
#define zcmp                    hebi_cmp
#define zcmpi(a, b)             (zseti(_b, b), zcmp(a, _b))
#define zcmpu(a, b)             (zsetu(_b, b), zcmp(a, _b))
#define zmul                    hebi_mul
#define zsqr(r, a)              hebi_mul(r, a, a)
#define zmodmul(r, a, b, m)     (zmul(r, a, b), zmod(r, r, m))
#define zmodsqr(r, a, m)        (zsqr(r, a), zmod(r, r, m))
#define zpow(r, a, b)           zpowu(r, a, hebi_get_u(b))
#define zmodpow(r, a, b, m)     zmodpowu(r, a, hebi_get_u(b), m)
#define zsets(a, s)             hebi_set_str(a, s, 0, 10)
#define zstr(a, s)              hebi_get_str(s, sizeof(s), a, 10) /* Assumes s is [], not * */
#define zdiv(q, a, b)           hebi_div(q, 0, a, b)
#define zmod(r, a, b)           hebi_div(_a, r, a, b)
#define zdivmod(q, r, a, b)     hebi_div(q, r, a, b)
#define zsave(a, s)             zstr(a, s)
#define zload(a, s)             zsets(a, s)

static size_t
zbits(z_t a)
{
	hebi_uword x = x;
	size_t rc = 0;
	zset(_a, a);
	while (zsignum(_a)) {
		x = hebi_get_u(_a);
		zrsh(_a, _a, 8 * sizeof(x));
		rc += 8 * sizeof(x);
	}
	if (rc)
		rc -= 8 * sizeof(x);
	while (x) {
		x >>= 1;
		rc += 1;
	}
	return rc;
}

static size_t
zlsb(z_t a)
{
	hebi_uword x;
	size_t rc = 0;
	if (zzero(a))
		return SIZE_MAX;
	zset(_a, a);
	while (!(x = hebi_get_u(_a))) {
		zrsh(_a, _a, 8 * sizeof(x));
		rc += 8 * sizeof(x);
	}
	while (~x & 1) {
		x >>= 1;
		rc += 1;
	}
	return rc;
}

static void
zptest(z_t w, z_t a, int t)
{
	static int gave_up = 0;
	if (!gave_up) {
		gave_up = 1;
		printf("I'm sorry, primality test has not been implemented.\n");
	}
	(void) w;
	(void) a;
	(void) t;
}

static void
zpowu(z_t r, z_t a, unsigned long long int b)
{
	int neg = zsignum(a) < 0;
	zset(_a, a);
	zsetu(r, 1);
	for (; b; b >>= 1) {
		if (b & 1)
			zmul(r, r, _a);
		zsqr(_a, _a);
	}
	if (neg)
		zneg(r, r);
}

static void
zmodpowu(z_t r, z_t a, unsigned long long int b, z_t m)
{
	int neg = zsignum(a) < 0;
	zset(_a, a);
	zset(_m, m);
	zsetu(r, 1);
	for (; b; b >>= 1) {
		if (b & 1)
			zmodmul(r, r, _a, _m);
		zmodsqr(_a, _a, _m);
	}
	if (neg)
		zneg(r, r);
}

static size_t
zstr_length(z_t a, unsigned long long int radix)
{
	size_t size_total = 1, size_temp;
	zset(_a, a);
	while (!zzero(_a)) {
		zsetu(_m, radix);
		zset(_b, _m);
		size_temp = 1;
		while (zcmpmag(_m, _a) <= 0) {
			zset(_b, _m);
			zsqr(_m, _m);
			size_temp <<= 1;
		}
		size_temp >>= 1;
		size_total += size_temp;
		zdiv(_a, _a, _b);
	}
	return size_total + (zsignum(a) < 0);
}

static void
zsplit(z_t high, z_t low, z_t a, size_t brk)
{
	if (low == a) {
		zrsh(high, a, brk);
		ztrunc(low, a, brk);
	} else {
		ztrunc(low, a, brk);
		zrsh(high, a, brk);
	}
}

static void
zbset(z_t r, z_t a, size_t bit, int mode)
{
	zrsh(_a, a, bit);
	if (mode && zeven(_a)) {
		zlsh(_a, _1, bit);
		zadd(r, a, _a);
	} else if (mode <= 0 && zodd(_a)) {
		zlsh(_a, _1, bit);
		zsub(r, a, _a);
	} else {
		zset(r, a);
	}
}

static void
zrand(z_t r, int dev, int dist, z_t n)
{
	static int gave_up[] = {0, 0, 0};
	if (!gave_up[dist]) {
		gave_up[dist] = 1;
		printf("I'm sorry, prng has not been implemented.\n");
	}
	(void) r;
	(void) dev;
	(void) n;
}

static void
zand(z_t r, z_t a, z_t b)
{
	int neg = hebi_sign(a) < 0 && hebi_sign(b) < 0;
	hebi_uword x;
	size_t i = 0;
	zset(_a, a);
	zset(_b, b);
	zsetu(r, 0);
	while (zsignum(_a) && zsignum(_b)) {
		x = hebi_get_u(_a) & hebi_get_u(_b);
		zsetu(_m, x);
		zlsh(_m, _m, i * 8 * sizeof(x));
		zadd(r, r, _m);
		zrsh(_a, _a, 8 * sizeof(x));
		zrsh(_b, _b, 8 * sizeof(x));
	}
	if (neg)
		zneg(r, r);
}

static void
zor(z_t r, z_t a, z_t b)
{
	int neg = hebi_sign(a) < 0 || hebi_sign(b) < 0;
	hebi_uword x;
	size_t i = 0;
	zset(_a, a);
	zset(_b, b);
	zsetu(r, 0);
	while (zsignum(_a) || zsignum(_b)) {
		x = hebi_get_u(_a) | hebi_get_u(_b);
		zsetu(_m, x);
		zlsh(_m, _m, i * 8 * sizeof(x));
		zadd(r, r, _m);
		zrsh(_a, _a, 8 * sizeof(x));
		zrsh(_b, _b, 8 * sizeof(x));
	}
	if (neg)
		zneg(r, r);
}

static void
zxor(z_t r, z_t a, z_t b)
{
	int neg = (hebi_sign(a) < 0) ^ (hebi_sign(b) < 0);
	hebi_uword x;
	size_t i = 0;
	zset(_a, a);
	zset(_b, b);
	zsetu(r, 0);
	while (zsignum(_a) || zsignum(_b)) {
		x = hebi_get_u(_a) ^ hebi_get_u(_b);
		zsetu(_m, x);
		zlsh(_m, _m, i * 8 * sizeof(x));
		zadd(r, r, _m);
		zrsh(_a, _a, 8 * sizeof(x));
		zrsh(_b, _b, 8 * sizeof(x));
	}
	if (neg)
		zneg(r, r);
}

void
zgcd(z_t r, z_t a, z_t b)
{
	size_t shifts, a_lsb, b_lsb;
	int neg, cmpmag;

	if (zzero(a)) {
		if (r != b)
			zset(r, b);
		return;
	}
	if (zzero(b)) {
		if (r != a)
			zset(r, a);
		return;
	}

	neg = hebi_sign(a) < 0 && hebi_sign(b) < 0;

	a_lsb = zlsb(a);
	b_lsb = zlsb(b);
	shifts = a_lsb < b_lsb ? a_lsb : b_lsb;
	zrsh(_a, a, a_lsb);
	zrsh(_b, b, b_lsb);

	for (;;) {
		if ((cmpmag = zcmpmag(_a, _b)) >= 0) {
			if (cmpmag == 0)
				break;
			zswap(_a, _b);
		}
		zsub(_b, _b, _a);
		zrsh(_b, _b, zlsb(_b));
	}

	zlsh(r, _a, shifts);
	if (neg)
		zneg(r, r);
}

static void
znot(z_t r, z_t a)
{
	size_t bits = zbits(a);
	zsetu(_b, 0);
	zsetu(_a, 1);
	zlsh(_a, _a, bits);
	zadd(_b, _b, _a);
	zsub(_b, _b, _1);
	zxor(r, a, _b);
	zneg(r, r);
}

int
hebi_shl(hebi_int *r, const hebi_int *a, unsigned int b)
{
	zsetu(_a, 2);
	zpowu(_a, _a, b);
	zmul(r, a, _a);
	return hebi_success;
}

int
hebi_shr(hebi_int *r, const hebi_int *a, unsigned int b)
{
	zsetu(_a, 2);
	zpowu(_a, _a, b);
	zdiv(r, a, _a);
	return hebi_success;
}

int
hebi_div(hebi_int *r, hebi_int *m, const hebi_int *a, const hebi_int *b)
{
	/* TODO */
	return hebi_success;
}