-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbitl.c
527 lines (466 loc) · 15.3 KB
/
bitl.c
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
// Big interger toy library
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <printf.h>
#include "bitl.h"
//---------------------------------------------------
// unsigned long long int
//---------------------------------------------------
uintbig_t UINTBIG_ZERO = ULL_TO_ULLL (0);
uintbig_t UINTBIG_MAX = { {ULLONG_MAX, ULLONG_MAX, ULLONG_MAX, ULLONG_MAX}
};
int
uintbig_cmp (uintbig_t a, uintbig_t b)
{
for (size_t i = UBI_LENGTH; i > 0; i--)
if (a.array[i - 1] > b.array[i - 1])
return 1;
else if (a.array[i - 1] < b.array[i - 1])
return -1;
return 0;
}
int
uintbig_is_zero (uintbig_t a)
{
for (size_t i = 0; i < UBI_LENGTH; i++)
if (a.array[i])
return 0;
return 1;
}
uintbig_t
uintbig_swapbits (uintbig_t a)
{
for (size_t j = 0; j < UBI_LENGTH; j++)
a.array[j] = ~a.array[j];
return a;
}
uintbig_t
uintbig_and (uintbig_t a, uintbig_t b)
{
for (size_t j = 0; j < UBI_LENGTH; j++)
a.array[j] &= b.array[j];
return a;
}
uintbig_t
uintbig_or (uintbig_t a, uintbig_t b)
{
for (size_t j = 0; j < UBI_LENGTH; j++)
a.array[j] |= b.array[j];
return a;
}
uintbig_t
uintbig_xor (uintbig_t a, uintbig_t b)
{
for (size_t j = 0; j < UBI_LENGTH; j++)
a.array[j] ^= b.array[j];
return a;
}
uintbig_t
uintbig_add (uintbig_t a, uintbig_t b)
{
uintbig_t carry = UINTBIG_ZERO;
for (size_t i = 0; i < UBI_LENGTH; i++)
{
// if a.array[i] + b.array[i] <= ULLONG_MAX
if (a.array[i] <= ULLONG_MAX - b.array[i])
a.array[i] += b.array[i];
else // a.array[i] > ULLONG_MAX - b.array[i]
{
a.array[i] -= (ULLONG_MAX - b.array[i]) + 1ULL; // --> 0 <= a.array[i] <= ULLONG_MAX - 1
if (i < UBI_LENGTH - 1)
carry.array[i + 1] = 1ULL;
/* else wrap around */
}
if (a.array[i] <= ULLONG_MAX - carry.array[i])
a.array[i] += carry.array[i];
else
{
a.array[i] -= (ULLONG_MAX - carry.array[i]) + 1ULL;
if (i < UBI_LENGTH - 1)
carry.array[i + 1] = 1ULL;
/* else wrap around */
}
}
return a;
}
uintbig_t
uintbig_sub (uintbig_t a, uintbig_t b)
{
uintbig_t carry = UINTBIG_ZERO;
for (size_t i = 0; i < UBI_LENGTH; i++)
{
if (a.array[i] >= b.array[i])
a.array[i] -= b.array[i];
else // if (a.array[i] < b.array[i])
{
a.array[i] += (ULLONG_MAX - b.array[i]) + 1ULL;
if (i < UBI_LENGTH - 1)
carry.array[i + 1] = 1ULL;
/* else wrap around */
}
if (a.array[i] >= carry.array[i])
a.array[i] -= carry.array[i];
else
{
a.array[i] += (ULLONG_MAX - carry.array[i]) + 1ULL;
if (i < UBI_LENGTH - 1)
carry.array[i + 1] = 1ULL;
/* else wrap around */
}
}
return a;
}
uintbig_t
uintbig_sl (uintbig_t a, size_t shift)
{
if (uintbig_is_zero (a) || shift >= UBI_NB_BITS)
return UINTBIG_ZERO;
for (; shift >= ULL_NB_BITS; shift -= ULL_NB_BITS)
{
for (size_t j = UBI_LENGTH; j > 1; j--)
a.array[j - 1] = a.array[j - 2];
a.array[0] = 0;
}
static unsigned long long int ULLONG_LAST = ~((~0ULL) >> 1);
// a << shift
for (size_t i = 0; i < shift; i++)
{
if (a.array[UBI_LENGTH - 1] & ULLONG_LAST)
{
/* a is truncated */
}
for (size_t j = UBI_LENGTH; j > 1; j--)
{
a.array[j - 1] <<= 1;
if (a.array[j - 2] & ULLONG_LAST)
a.array[j - 1] |= 1ULL;
}
a.array[0] <<= 1;
}
return a;
}
uintbig_t
uintbig_sr (uintbig_t a, size_t shift)
{
if (uintbig_is_zero (a) || shift >= UBI_NB_BITS)
return UINTBIG_ZERO;
for (; shift >= ULL_NB_BITS; shift -= ULL_NB_BITS)
{
for (size_t j = 0; j < UBI_LENGTH - 1; j++)
a.array[j] = a.array[j + 1];
a.array[UBI_LENGTH - 1] = 0;
}
static unsigned long long int ULLONG_LAST = ~((~0ULL) >> 1);
// a >> shift
for (size_t i = 0; i < shift; i++)
{
for (size_t j = 0; j < UBI_LENGTH - 1; j++)
{
a.array[j] >>= 1;
if (a.array[j + 1] & 1ULL)
a.array[j] |= ULLONG_LAST;
}
a.array[UBI_LENGTH - 1] >>= 1;
}
return a;
}
//---------------------------------------------------
// signed long long int
//---------------------------------------------------
intbig_t INTBIG_ZERO = (intbig_t) { {0}
};
intbig_t INTBIG_MAX = { {ULLONG_MAX, ULLONG_MAX, ULLONG_MAX, LLONG_MAX}
};
intbig_t INTBIG_MIN = { {~ULLONG_MAX, ~ULLONG_MAX, ~ULLONG_MAX, ~(typeof (INTBIG_MIN.array[0])) LLONG_MAX}
};
int
intbig_is_negative (intbig_t a)
{
static unsigned long long int ULLONG_LAST = ~((~0ULL) >> 1);
if (a.array[UBI_LENGTH - 1] & ULLONG_LAST)
return 1;
else
return 0;
}
int
intbig_is_positive (intbig_t a)
{
return !intbig_is_negative (a) && !intbig_is_zero (a);
}
intbig_t
intbig_swapbits (intbig_t a)
{
for (size_t j = 0; j < UBI_LENGTH; j++)
a.array[j] = ~a.array[j];
return a;
}
intbig_t
intbig_opposite (intbig_t a)
{
// By convention, the opposite of a is coded as ~(a-1).
// and therefore, the opposite of ~a+1 is coded as a.
// As the opposite of the opposite is neutral, and opposition is bijective, ~a+1 is coded as the opposite of a.
if (intbig_is_negative (a))
return uintbig_add (intbig_swapbits (a), ULL_TO_ULLL (1));
else // also works if a is equal to 0.
return intbig_swapbits (uintbig_sub (a, ULL_TO_ULLL (1)));
}
intbig_t
intbig_abs (intbig_t a)
{
if (intbig_is_negative (a))
return uintbig_add (intbig_swapbits (a), ULL_TO_ULLL (1));
else
return a;
}
intbig_t
LL_TO_LLL (signed long long int ll)
{
if (ll >= 0)
return ULL_TO_ULLL ((long long unsigned int) ll);
else
return intbig_opposite (ULL_TO_ULLL ((long long unsigned int) (-ll)));
}
intbig_t
ULLL_TO_LLL (uintbig_t ua)
{
// Might truncate.
ua.array[UBI_LENGTH - 1] &= ~(0ULL) >> 1;
return ua;
}
int
intbig_cmp (intbig_t a, intbig_t b)
{
int s;
if ((s = intbig_is_negative (b)) != intbig_is_negative (a))
return s ? 1 : -1;
else
return uintbig_cmp (a, b); // Also works if a and b are negative.
}
int
intbig_is_zero (intbig_t a)
{
return uintbig_is_zero (a);
}
intbig_t
intbig_add (intbig_t a, intbig_t b)
{
return uintbig_add (a, b);
}
intbig_t
intbig_sub (intbig_t a, intbig_t b)
{
return uintbig_sub (a, b);
}
//---------------------------------------------------
// printf extension (%V for intbig_t, %U for uintbig_t)
//---------------------------------------------------
// see register_printf_specifier
// https://sourceware.org/bugzilla/attachment.cgi?id=3874&action=view
// http://www.gnu.org/software/libc/manual/html_node/Customizing-Printf.html#Customizing-Printf
static int PA_INTBIG;
static wchar_t SPEC_INTBIG = L'V';
static int PA_UINTBIG;
static wchar_t SPEC_UINTBIG = L'U';
#define DEFINE_PRINTER(name, printer) \
static int \
uintbig_to_##name (FILE * f, uintbig_t a, char flag) \
{ \
(void) f; \
char fmt0[] = "%0llu"; \
char fmt1[] = "2^%u"; \
char fmtn[] = "%0llu x 2^%u"; \
if (flag == '\'') \
fmtn[1] = fmt0[1] = flag; \
\
int ret = 0; \
ret += printer (f, "("); \
int more = 0; \
for (size_t j = UBI_LENGTH; j > 1; j--) \
if (a.array[j - 1]) \
{ \
if (more) \
ret += printer (f, " + "); \
if (a.array[j - 1] == 1) \
ret += printer (f, fmt1, (j - 1) * ULL_NB_BITS); \
else \
ret += printer (f, fmtn, a.array[j - 1], (j - 1) * ULL_NB_BITS); \
more = 1; \
} \
\
if (!more || a.array[0]) \
{ \
if (more) \
ret += printer (f, " + "); \
ret += printer (f, fmt0, a.array[0]); \
} \
ret += printer (f, ")"); \
\
return ret; \
} \
\
static int \
intbig_to_##name (FILE * f, intbig_t a, char flag_sign, char flag_group) \
{ \
(void) f; \
int ret = 0; \
\
if (intbig_is_negative (a)) \
{ \
ret += printer (f, "%c", '-'); \
a = intbig_opposite (a); \
} \
else if (intbig_is_zero (a) || flag_sign == ' ') \
ret += printer (f, " "); \
else if (flag_sign == '+') \
ret += printer (f, "+"); \
\
ret += uintbig_to_##name (f, a, flag_group); \
\
return ret; \
} \
struct __useless_struct_##name##_IMPL
// The macro DEFINE_PRINTER avoids the use of a pointer to a variadic function fsinkf (file, ...) = snprinf (0, 0, ...)
DEFINE_PRINTER (stream, fprintf);
#define FSINKF(file, ...) snprintf (0, 0, __VA_ARGS__)
DEFINE_PRINTER (sink, FSINKF);
static int
intbig_printf (FILE *stream, const struct printf_info *info, const void *const args[])
{
// info->spec == SPEC_INTBIG
intbig_t a;
memcpy (&a, *((void *const *const *) args)[0], sizeof (a)); // uh !
int ret = intbig_to_sink (0, a, info->showsign ? '+' : info->space ? ' ' : 0,
info->group ? '\'' : 0);
char *str = calloc ((size_t) (ret + 1), sizeof (*str));
FILE *buffer = fmemopen (str, (size_t) (ret + 1), "w");
intbig_to_stream (buffer, a, info->showsign ? '+' : info->space ? ' ' : 0, info->group ? '\'' : 0);
fclose (buffer);
ret = fprintf (stream, "%*s", (info->left ? -info->width : info->width), str);
free (str);
return ret;
}
static int
uintbig_printf (FILE *stream, const struct printf_info *info, const void *const args[])
{
// info->spec == SPEC_UINTBIG
uintbig_t a;
memcpy (&a, *((void *const *const *) args)[0], sizeof (a)); // uh ! again
int ret = uintbig_to_sink (0, a, info->group ? '\'' : 0);
char *str = calloc ((size_t) (ret + 1), sizeof (*str));
FILE *buffer = fmemopen (str, (size_t) (ret + 1), "w");
uintbig_to_stream (buffer, a, info->group ? '\'' : 0);
fclose (buffer);
ret = fprintf (stream, "%*s", (info->left ? -info->width : info->width), str);
free (str);
return ret;
}
static int
intbig_print_arginfo_size (const struct printf_info *info, size_t n, int *argtypes, int *size)
{
(void) info;
/* We always take exactly one argument and this is big integer */
if (n > 0)
argtypes[0] = PA_INTBIG;
*size = sizeof (intbig_t);
return 1;
}
static int
uintbig_print_arginfo_size (const struct printf_info *info, size_t n, int *argtypes, int *size)
{
(void) info;
/* We always take exactly one argument and this is big integer */
if (n > 0)
argtypes[0] = PA_UINTBIG;
*size = sizeof (uintbig_t);
return 1;
}
static void
uintbig_va (void *mem, va_list *ap)
{
uintbig_t v = va_arg (*ap, uintbig_t);
memcpy (mem, &v, sizeof (v));
}
static void
intbig_va (void *mem, va_list *ap)
{
intbig_t v = va_arg (*ap, intbig_t);
memcpy (mem, &v, sizeof (v));
}
void
xintbig_printf_init (void)
{
static int XINTBIG_PRINTF_INIT_DONE;
if (XINTBIG_PRINTF_INIT_DONE)
return;
XINTBIG_PRINTF_INIT_DONE = 1;
PA_INTBIG = register_printf_type (intbig_va);
PA_UINTBIG = register_printf_type (uintbig_va);
register_printf_specifier (SPEC_UINTBIG, uintbig_printf, uintbig_print_arginfo_size);
register_printf_specifier (SPEC_INTBIG, intbig_printf, intbig_print_arginfo_size);
}
//---------------------------------------------------
// Unit testing
//---------------------------------------------------
#ifdef TUEXE
int
main (void)
{
setlocale (LC_ALL, "");
xintbig_printf_init ();
printf ("%'llu;%'llu\n", 0ULL, ULLONG_MAX);
printf ("%+'lli;%+'lli\n", LLONG_MIN, ~LLONG_MIN);
printf ("%+'lli;%+'lli\n", LLONG_MAX, ~LLONG_MAX);
printf ("%+'lli;%+'lli\n", 0LL, ~0LL);
printf ("%+'lli;%+'lli\n", 10LL, ~10LL);
printf ("%+'lli;%+'lli\n", -10LL, ~-10LL);
# define PU(a) printf ("[%'100" PRIUINTBIG "]\n", a)
uintbig_t a = UINTBIG_ZERO;
a = ULL_TO_ULLL (1);
PU (a);
PU (a = uintbig_sl (a, 70 + 64));
PU (a = uintbig_sr (a, 69 + 64));
uintbig_t b = ULL_TO_ULLL (~0ULL);
PU (b);
PU (a = uintbig_add (a, b));
PU (a = uintbig_sub (a, b));
PU ((a = (uintbig_t)
{
{
~0ULL, ~0ULL, 1, 0}}));
PU (uintbig_add (a, ULL_TO_ULLL (1)));
# define PS(a) printf ("[%'+100" PRIINTBIG "]\n", a)
intbig_t sa;
printf ("%+'V -- %+'V\n", INTBIG_MIN, INTBIG_MAX);
PS (uintbig_sub (ULL_TO_ULLL (17), ULL_TO_ULLL (117)));
PS (a = uintbig_sub (ULL_TO_ULLL (ULLONG_MAX), ULL_TO_ULLL (10058117ULL + LLONG_MAX)));
PS (a = uintbig_sub (a, ULL_TO_ULLL (10058117ULL + (ULLONG_MAX - LLONG_MAX))));
PS (sa = INTBIG_ZERO);
PS (sa = intbig_opposite (sa));
PS (sa = LL_TO_LLL (-1LL));
PS (sa = intbig_opposite (sa));
PS (sa = intbig_opposite (sa));
PS (sa = LL_TO_LLL (1LL << 62));
PS (sa = intbig_opposite (sa));
PS (sa = intbig_opposite (sa));
printf ("%i\n", intbig_cmp (INTBIG_ZERO, INTBIG_ZERO));
printf ("%i\n", intbig_cmp (INTBIG_ZERO, LL_TO_LLL (1LL)));
printf ("%i\n", intbig_cmp (LL_TO_LLL (1LL), INTBIG_ZERO));
printf ("%i\n", intbig_cmp (INTBIG_ZERO, intbig_opposite (LL_TO_LLL (1LL))));
printf ("%i\n", intbig_cmp (intbig_opposite (LL_TO_LLL (1LL)), INTBIG_ZERO));
intbig_t sb = LL_TO_LLL (1LL << 62);
PS (sb);
PS (sa = intbig_add (sa, sb));
PS (sa = intbig_sub (sa, sb));
PS (sa = intbig_sub (sa, sb));
PS (sa = intbig_sub (sa, sb));
PS (sa = intbig_add (sa, sb));
PS (sa = intbig_add (sa, sb));
PS (sa = intbig_add (sa, LL_TO_LLL (-187496325)));
PS (intbig_sub (ULLL_TO_LLL (uintbig_sl (intbig_abs (sa), 151)), LL_TO_LLL (14789)));
PS (intbig_opposite (intbig_sub (ULLL_TO_LLL (uintbig_sl (intbig_abs (sa), 151)), LL_TO_LLL (14789))));
}
#endif