forked from diofant/zz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzz.c
More file actions
2676 lines (2436 loc) · 64.6 KB
/
zz.c
File metadata and controls
2676 lines (2436 loc) · 64.6 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
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2024-2026 Sergey B Kirpichev
This file is part of the ZZ Library.
The ZZ Library is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License (LGPL) as
published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version. See
<https://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include "zz-impl.h"
#undef zz_set
#undef zz_get
#undef zz_cmp
#undef zz_add
#undef zz_sub
#undef zz_mul
#undef zz_div
#if GMP_NAIL_BITS != 0
# error "GMP_NAIL_BITS expected to be 0"
#endif
#if GMP_LIMB_BITS != 64
# error "GMP_LIMB_BITS expected to be 64"
#endif
#if ZZ_DIGIT_T_BITS < DBL_MANT_DIG
# error ZZ_DIGIT_T_BITS expected to be more than ZZ_DIGIT_T_BITS
#endif
const char *
zz_get_version(void)
{
return VERSION;
}
zz_bitcnt_t
zz_get_bitcnt_max(void)
{
return ZZ_BITS_MAX;
}
size_t
zz_sizeof(const zz_t *u)
{
return sizeof(zz_t) + (size_t)GETALLOC(u)*sizeof(zz_digit_t);
}
static struct {
void *(*default_allocate_func)(size_t);
void *(*default_reallocate_func)(void *, size_t, size_t);
void (*default_free_func)(void *, size_t);
void *(*malloc)(size_t);
void *(*realloc)(void *, size_t, size_t);
void (*free)(void *, size_t);
} zz_state;
#define TRACKER_SIZE_INCR 64
_Thread_local struct {
size_t size;
size_t alloc;
void **ptrs;
} zz_tracker = {0, 0, NULL};
/* Thin wrappers over system allocation routines to
support GMP's argument convention. */
#define zz_malloc malloc
static void *
zz_realloc(void *ptr, size_t old_size, size_t new_size)
{
return realloc(ptr, new_size);
}
static void
zz_free(void *ptr, size_t size)
{
free(ptr);
}
/* Following functions are used to handle all *temporary* allocations in the
GMP, apart from temporary space from alloca() if that function is available
and GMP is configured to use it (bad idea).
Calls to GMP's functions, which do memory allocations, must be preceded by
*one* "if (TMP_OVERFLOW)" block (TMP_OVERFLOW macro calls setjmp), that will
handle memory failure *after* freeing *all* GMP's allocations in the
zz_reallocate_function().
Basically, GMP's functions for integers fall into two camps. First,
low-level functions, prefixed by "mpn_", which we prefer. (See zz_mul() as
an example.) Those will use only zz_allocate_function() and
zz_free_function() to allocate temporary storage (not for output variables).
This allocation happens essentially in LIFO way and we take that into
account for optimization of the memory tracking.
Not all mpn_*() functions do memory allocation. Sometimes it's obvious
(e.g. mpn_cmp() or mpn_add/sub()), sometimes - not (e.g. mpn_get/set_str()
for power of 2 bases). Though, these details aren't documented and if you
feel that in the given case things might be changed - please add the "if
(TMP_OVERFLOW)" block.
Second, in few cases we use integer functions, working with objects of type
mpz_t, prefixed by "mpz_". Input variables for them must be created by the
TMP_MPZ macro. Output variables (allocated by the GMP) are considered
temporary and cleared automatically on memory failure. Here is an example:
zz_err
mul(const zz_t *u, const zz_t *v, zz_t *w)
{
mpz_t z;
TMP_MPZ(a, u);
TMP_MPZ(b, v);
if (TMP_OVERFLOW) {
// Memory allocation failure happened in mpz_mul.
// No need to call mpz_clear(z)!
return ZZ_MEM;
}
mpz_init(z);
mpz_mul(z, a, b);
// Success! Resize w and copy z's content to it.
if (zz_set_mpz_t(z, w)) {
mpz_clear(z);
return ZZ_MEM;
}
mpz_clear(z); // That finally clear all temporary allocations.
return ZZ_OK;
}
Note that our memory allocation functions are generic enough to work also
for usual GMP usage, without above assumptions. Of course, unless memory
allocation failure happens, which will lead to undefined behavior (calling
longjmp() without a prior call to setjmp()).
Don't forget known pitfails of working with setjmp/longjmp:
* Don't use VLA (may introduce memory leaks).
* Declare variables you care about (e.g. to free memory in
case of failure) in the scope of the setjmp invocation - with
volatile type qualifier. See zz_gcd() as an example. */
static void *
zz_reallocate_function(void *ptr, size_t old_size, size_t new_size)
{
if (zz_tracker.size >= zz_tracker.alloc) {
/* Reallocation shouldn't be required. Unless...
you are using the mpz_t from the GNU GMP with
our memory functions. */
void **tmp = zz_tracker.ptrs;
size_t old_alloc = zz_tracker.alloc;
zz_tracker.alloc += TRACKER_SIZE_INCR;
zz_tracker.ptrs = zz_state.realloc(zz_tracker.ptrs,
old_alloc * sizeof(void *),
zz_tracker.alloc * sizeof(void *));
if (!zz_tracker.ptrs) {
/* LCOV_EXCL_START */
zz_tracker.alloc = old_alloc;
zz_tracker.ptrs = tmp;
goto err;
/* LCOV_EXCL_STOP */
}
}
if (!ptr) {
void *ret = zz_state.malloc(new_size);
if (!ret) {
goto err;
}
zz_tracker.ptrs[zz_tracker.size] = ret;
zz_tracker.size++;
return ret;
}
size_t i = zz_tracker.size - 1;
for (;; i--) {
if (zz_tracker.ptrs[i] == ptr) {
break;
}
}
void *ret = zz_state.realloc(ptr, old_size, new_size);
if (!ret) {
goto err; /* LCOV_EXCL_LINE */
}
zz_tracker.ptrs[i] = ret;
return ret;
err:
i = zz_tracker.size - 1;
while (zz_tracker.size > 0) {
zz_state.free(zz_tracker.ptrs[i], 0);
zz_tracker.ptrs[i] = NULL;
zz_tracker.size--;
i--;
}
zz_state.free(zz_tracker.ptrs, zz_tracker.alloc * sizeof(void *));
zz_tracker.alloc = 0;
zz_tracker.ptrs = NULL;
longjmp(zz_env, 1);
}
static void *
zz_allocate_function(size_t size)
{
return zz_reallocate_function(NULL, 0, size);
}
static void
zz_free_function(void *ptr, size_t size)
{
for (size_t i = zz_tracker.size - 1; i >= 0; i--) {
if (zz_tracker.ptrs[i] == ptr) {
zz_tracker.ptrs[i] = NULL;
break;
}
}
zz_state.free(ptr, size);
size_t i = zz_tracker.size - 1;
while (zz_tracker.size > 0) {
if (zz_tracker.ptrs[i]) {
return;
}
zz_tracker.size--;
i--;
}
zz_state.free(zz_tracker.ptrs, zz_tracker.alloc * sizeof(void *));
zz_tracker.alloc = 0;
zz_tracker.ptrs = NULL;
}
zz_err
zz_setup(void)
{
mp_get_memory_functions(&zz_state.default_allocate_func,
&zz_state.default_reallocate_func,
&zz_state.default_free_func);
mp_set_memory_functions(zz_allocate_function,
zz_reallocate_function,
zz_free_function);
zz_state.malloc = &zz_malloc;
zz_state.realloc = &zz_realloc;
zz_state.free = &zz_free;
return ZZ_OK;
}
void
zz_set_memory_funcs(void *(*malloc) (size_t),
void *(*realloc) (void *, size_t, size_t),
void (*free) (void *, size_t))
{
if (!malloc) {
malloc = zz_malloc;
}
if (!realloc) {
realloc = zz_realloc;
}
if (!free) {
free = zz_free;
}
zz_state.malloc = malloc;
zz_state.realloc = realloc;
zz_state.free = free;
}
size_t
zz_get_alloc_state(void)
{
return zz_tracker.alloc;
}
void
zz_finish(void)
{
mp_set_memory_functions(zz_state.default_allocate_func,
zz_state.default_reallocate_func,
zz_state.default_free_func);
zz_state.malloc = &zz_malloc;
zz_state.realloc = &zz_realloc;
zz_state.free = &zz_free;
}
zz_err
zz_init(zz_t *u)
{
SETNEG(false, u);
SETALLOC(0, u);
u->size = 0;
u->digits = NULL;
return ZZ_OK;
}
static zz_err
zz_resize(zz_size_t size, zz_t *u)
{
if (GETALLOC(u) >= size) {
u->size = size;
if (!u->size) {
SETNEG(false, u);
}
return ZZ_OK;
}
zz_size_t alloc = size;
zz_digit_t *t = u->digits;
u->digits = realloc(u->digits, (size_t)alloc * ZZ_DIGIT_T_BYTES);
if (u->digits) {
SETALLOC(alloc, u);
u->size = alloc;
return ZZ_OK;
}
/* LCOV_EXCL_START */
u->digits = t;
return ZZ_MEM;
/* LCOV_EXCL_STOP */
}
void
zz_clear(zz_t *u)
{
free(u->digits);
SETNEG(false, u);
SETALLOC(0, u);
u->size = 0;
u->digits = NULL;
}
inline static void
zz_normalize(zz_t *u)
{
while (u->size && u->digits[u->size - 1] == 0) {
u->size--;
}
if (!u->size) {
SETNEG(false, u);
}
}
zz_ord
zz_cmp(const zz_t *u, const zz_t *v)
{
if (u == v) {
return ZZ_EQ;
}
bool u_negative = ISNEG(u);
zz_ord res = u_negative ? ZZ_LT : ZZ_GT;
if (u_negative != ISNEG(v)) {
return res;
}
else if (u->size != v->size) {
return (u->size < v->size) ? -res : res;
}
res = mpn_cmp(u->digits, v->digits, u->size);
return u_negative ? -res : res;
}
zz_ord
zz_cmp_i64(const zz_t *u, int64_t v)
{
bool u_negative = ISNEG(u);
zz_ord res = u_negative ? ZZ_LT : ZZ_GT;
if (u_negative != (v < 0) || u->size > 1) {
return res;
}
else if (!u->size) {
return v ? -res : ZZ_EQ;
}
zz_digit_t uu = u->digits[0], uv = ABS_CAST(zz_digit_t, v);
if (uu == uv) {
return ZZ_EQ;
}
return uu > uv ? res : -res;
}
zz_err
zz_set_mpz_t(mpz_t u, zz_t *v)
{
if (zz_resize(abs(u->_mp_size), v)) {
return ZZ_MEM; /* LCOV_EXCL_LINE */
}
mpn_copyi(v->digits, u->_mp_d, v->size);
SETNEG(u->_mp_size < 0, v);
return ZZ_OK;
}
zz_err
zz_set_i32(int32_t u, zz_t *v)
{
if (!u) {
v->size = 0;
SETNEG(false, v);
return ZZ_OK;
}
if (zz_resize(1, v)) {
return ZZ_MEM; /* LCOV_EXCL_LINE */
}
SETNEG(u < 0, v);
v->digits[0] = (zz_digit_t)abs(u);
return ZZ_OK;
}
zz_err
zz_set_i64(int64_t u, zz_t *v)
{
if (!u) {
v->size = 0;
SETNEG(false, v);
return ZZ_OK;
}
if (zz_resize(1, v)) {
return ZZ_MEM; /* LCOV_EXCL_LINE */
}
SETNEG(u < 0, v);
v->digits[0] = ABS_CAST(zz_digit_t, u);
return ZZ_OK;
}
zz_err
zz_set_u64(uint64_t u, zz_t *v)
{
if (!u) {
v->size = 0;
SETNEG(false, v);
return ZZ_OK;
}
if (zz_resize(1, v)) {
return ZZ_MEM; /* LCOV_EXCL_LINE */
}
SETNEG(false, v);
v->digits[0] = u;
return ZZ_OK;
}
#define DIGITS_PER_DOUBLE ((53 + ZZ_DIGIT_T_BITS - 2) / ZZ_DIGIT_T_BITS + 1)
zz_err
zz_set_double(double u, zz_t *v)
{
#if defined(__MINGW32__) && defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wfloat-conversion"
#endif
if (isnan(u)) {
#if defined(__MINGW32__) && defined(__GNUC__)
# pragma GCC diagnostic pop
#endif
return ZZ_VAL;
}
#if defined(__MINGW32__) && defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wfloat-conversion"
#endif
if (isinf(u)) {
#if defined(__MINGW32__) && defined(__GNUC__)
# pragma GCC diagnostic pop
#endif
return ZZ_BUF;
}
if (zz_resize(DIGITS_PER_DOUBLE, v)) {
return ZZ_MEM; /* LCOV_EXCL_LINE */
}
/* v has enough space to prevent new allocation by GMP */
TMP_MPZ(z, v);
mpz_set_d(z, u);
SETNEG(u < 0, v);
v->size = abs(z->_mp_size);
return ZZ_OK;
}
zz_err
zz_get_i32(const zz_t *u, int32_t *v)
{
zz_size_t n = u->size;
if (!n) {
*v = 0;
return ZZ_OK;
}
if (n > 1) {
return ZZ_BUF;
}
zz_digit_t uv = u->digits[0];
if (ISNEG(u)) {
if (uv <= INT32_MAX + (zz_digit_t)1) {
*v = -1 - (int32_t)((uv - 1) & INT32_MAX);
return ZZ_OK;
}
}
else {
if (uv <= INT32_MAX) {
*v = (int32_t)uv;
return ZZ_OK;
}
}
return ZZ_BUF;
}
zz_err
zz_get_i64(const zz_t *u, int64_t *v)
{
zz_size_t n = u->size;
if (!n) {
*v = 0;
return ZZ_OK;
}
if (n > 1) {
return ZZ_BUF;
}
zz_digit_t uv = u->digits[0];
if (ISNEG(u)) {
if (uv <= INT64_MAX + (zz_digit_t)1) {
*v = -1 - (int64_t)((uv - 1) & INT64_MAX);
return ZZ_OK;
}
}
else {
if (uv <= INT64_MAX) {
*v = (int64_t)uv;
return ZZ_OK;
}
}
return ZZ_BUF;
}
zz_err
zz_get_u32(const zz_t *u, uint32_t *v)
{
if (ISNEG(u)) {
return ZZ_VAL;
}
if (!u->size) {
*v = 0;
return ZZ_OK;
}
if (u->size > 1 || u->digits[0] > UINT32_MAX) {
return ZZ_BUF;
}
*v = (uint32_t)u->digits[0];
return ZZ_OK;
}
zz_err
zz_get_u64(const zz_t *u, uint64_t *v)
{
if (ISNEG(u)) {
return ZZ_VAL;
}
if (u->size > 1) {
return ZZ_BUF;
}
*v = u->size ? u->digits[0] : 0;
return ZZ_OK;
}
bool
zz_iszero(const zz_t *u)
{
return u->size == 0;
}
bool
zz_isneg(const zz_t *u)
{
return ISNEG(u);
}
bool
zz_isodd(const zz_t *u)
{
return u->size && u->digits[0] & 1;
}
zz_err
zz_pos(const zz_t *u, zz_t *v)
{
if (u != v) {
if (!u->size) {
return zz_set_i64(0, v);
}
if (zz_resize(u->size, v)) {
return ZZ_MEM; /* LCOV_EXCL_LINE */
}
SETNEG(ISNEG(u), v);
mpn_copyi(v->digits, u->digits, u->size);
}
return ZZ_OK;
}
zz_err
zz_abs(const zz_t *u, zz_t *v)
{
if (u != v && zz_pos(u, v)) {
return ZZ_MEM; /* LCOV_EXCL_LINE */
}
SETNEG(false, v);
return ZZ_OK;
}
zz_err
zz_neg(const zz_t *u, zz_t *v)
{
if (u != v && zz_pos(u, v)) {
return ZZ_MEM; /* LCOV_EXCL_LINE */
}
if (v->size) {
SETNEG(!ISNEG(u), v);
}
return ZZ_OK;
}
zz_err
zz_sizeinbase(const zz_t *u, int base, size_t *len)
{
const int abase = abs(base);
if (abase < 2 || abase > 36) {
return ZZ_VAL;
}
*len = mpn_sizeinbase(u->digits, u->size, abase);
return ZZ_OK;
}
zz_err
zz_get_str(const zz_t *u, int base, char *str)
{
/* Maps 1-byte integer to digit character for bases up to 36. */
const char *NUM_TO_TEXT = "0123456789abcdefghijklmnopqrstuvwxyz";
if (base < 0) {
base = -base;
NUM_TO_TEXT = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
}
if (base < 2 || base > 36) {
return ZZ_VAL;
}
unsigned char *p = (unsigned char *)str;
size_t len;
if (!u->size) {
*(p++) = '0';
goto end;
}
if (ISNEG(u)) {
*(p++) = '-';
}
if ((base & (base - 1)) == 0) {
len = mpn_get_str(p, base, u->digits, u->size);
}
else { /* generic base, not power of 2, input might be clobbered */
zz_digit_t *volatile tmp = malloc(ZZ_DIGIT_T_BYTES * (size_t)u->size);
if (!tmp || TMP_OVERFLOW) {
/* LCOV_EXCL_START */
free(tmp);
return ZZ_MEM;
/* LCOV_EXCL_STOP */
}
mpn_copyi(tmp, u->digits, u->size);
len = mpn_get_str(p, base, tmp, u->size);
free(tmp);
}
for (size_t i = 0; i < len; i++) {
*p = (unsigned char)NUM_TO_TEXT[*p];
p++;
}
end:
*p = '\0';
return ZZ_OK;
}
/* Table of digit values for 8-bit string->mpz conversion.
Note that when converting a base B string, a char c is a legitimate
base B digit iff DIGIT_VALUE_TAB[c] < B. */
const int DIGIT_VALUE_TAB[] =
{
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1,-1,-1,-1,-1,-1,
-1,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,-1,-1,-1,-1,-1,
-1,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,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1,-1,-1,-1,-1,-1,
-1,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,-1,-1,-1,-1,-1,
-1,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,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
};
zz_err
zz_set_str(const char *str, int base, zz_t *u)
{
if (base && (base < 2 || base > 36)) {
return ZZ_VAL;
}
size_t len = strlen(str);
unsigned char *volatile buf = malloc(len), *p = (unsigned char *)buf;
if (!buf) {
return ZZ_MEM; /* LCOV_EXCL_LINE */
}
memcpy(buf, str, len);
while (len && isspace(*p)) {
p++;
len--;
}
if (!len) {
goto err;
}
bool negative = (p[0] == '-');
p += negative;
len -= negative;
if (!len) {
goto err;
}
if (!negative && p[0] == '+') {
p++;
len--;
}
if (!len || p[0] == '_') {
goto err;
}
if (p[0] == '0' && base == 0) {
if (len == 1) {
free(buf);
return zz_set_i64(0, u);
}
else if (tolower(p[1]) == 'b') {
base = 2;
}
else if (tolower(p[1]) == 'o') {
base = 8;
}
else if (tolower(p[1]) == 'x') {
base = 16;
}
else if (!isspace(p[1])) {
goto err;
}
p += 2;
len -= 2;
if (len && p[0] == '_') {
p++;
len--;
}
}
if (p[0] == '0' && len >= 2
&& ((base == 2 && tolower(p[1]) == 'b')
|| (base == 8 && tolower(p[1]) == 'o')
|| (base == 16 && tolower(p[1]) == 'x')))
{
p += 2;
len -= 2;
}
if (base == 0) {
base = 10;
}
if (!len || (len && p[0] == '_')) {
goto err;
}
size_t new_len = len;
for (size_t i = 0; i < len; i++) {
if (p[i] == '_') {
if (i == len - 1 || p[i + 1] == '_') {
goto err;
}
new_len--;
memmove(p + i, p + i + 1, len - i - 1);
}
unsigned char c = (unsigned char)DIGIT_VALUE_TAB[p[i]];
if (c < base) {
p[i] = c;
}
else {
if (!isspace(p[i])) {
goto err;
}
new_len--;
for (size_t j = i + 1; j < len; j++) {
if (!isspace(p[j])) {
goto err;
}
new_len--;
}
break;
}
}
len = new_len;
new_len = 1 + len/2;
if (new_len > ZZ_DIGITS_MAX) {
return ZZ_BUF; /* LCOV_EXCL_LINE */
}
if (zz_resize((zz_size_t)new_len, u) || TMP_OVERFLOW) {
/* LCOV_EXCL_START */
free(buf);
return ZZ_MEM;
/* LCOV_EXCL_STOP */
}
SETNEG(negative, u);
u->size = (zz_size_t)mpn_set_str(u->digits, p, len, base);
free(buf);
if (zz_resize(u->size, u)) {
return ZZ_MEM; /* LCOV_EXCL_LINE */
}
zz_normalize(u);
return ZZ_OK;
err:
free(buf);
return ZZ_VAL;
}
static bool
zz_tstbit(const zz_t *u, zz_bitcnt_t idx)
{
zz_size_t digit_idx = (zz_size_t)(idx / ZZ_DIGIT_T_BITS);
assert(u->size > digit_idx);
return (u->digits[digit_idx] >> (idx%ZZ_DIGIT_T_BITS)) & 1;
}
zz_err
zz_get_double(const zz_t *u, double *d)
{
if (u->size > DBL_MAX_EXP/ZZ_DIGIT_T_BITS + 1) {
*d = ISNEG(u) ? -INFINITY : INFINITY;
return ZZ_BUF;
}
zz_bitcnt_t bits = zz_bitlen(u);
TMP_MPZ(z, u);
*d = mpz_get_d(z); /* round towards zero */
if (DBL_MANT_DIG < bits && bits <= DBL_MAX_EXP) {
bits -= DBL_MANT_DIG + 1;
if (zz_tstbit(u, bits)) {
zz_bitcnt_t tz = zz_lsbpos(u);
if (tz < bits || (tz == bits && zz_tstbit(u, bits + 1))) {
*d = nextafter(*d, 2 * (*d)); /* round away from zero */
}
}
}
#if defined(__MINGW32__) && defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wfloat-conversion"
#endif
if (isinf(*d)) {
#if defined(__MINGW32__) && defined(__GNUC__)
# pragma GCC diagnostic pop
#endif
return ZZ_BUF;
}
return ZZ_OK;
}
zz_bitcnt_t
zz_bitlen(const zz_t *u)
{
return u->size ? (zz_bitcnt_t)mpn_sizeinbase(u->digits, u->size, 2) : 0;
}
zz_bitcnt_t
zz_lsbpos(const zz_t *u)
{
#if ULONG_MAX == ZZ_DIGIT_T_MAX
return u->size ? mpn_scan1(u->digits, 0) : 0;
#else
zz_bitcnt_t lsb_pos = 0;
for (zz_size_t i = 0; i < u->size; i++) {
zz_digit_t digit = u->digits[i];
if (digit) {
for (;;) {
if (digit & 1) {
return lsb_pos;
}
lsb_pos += 1;
digit >>= 1;
}
}
else {
lsb_pos += 64;
}
}
return lsb_pos;
#endif
}
zz_bitcnt_t
zz_bitcnt(const zz_t *u)
{
#if ULONG_MAX == ZZ_DIGIT_T_MAX
return u->size ? mpn_popcount(u->digits, u->size) : 0;
#else
zz_bitcnt_t count = 0;
for (zz_size_t i = 0; i < u->size; i++) {
zz_digit_t digit = u->digits[i];
while (digit) {
count += digit & 1;
digit >>= 1;
}
}
return count;
#endif
}
static const zz_layout native_layout = {
.bits_per_digit = ZZ_DIGIT_T_BITS,
.digit_size = sizeof(zz_digit_t),
.digits_order = -1,
.digit_endianness = 0,
};
const zz_layout *
zz_get_layout(void)
{
return &native_layout;
}
zz_err
zz_import(size_t len, const void *digits, zz_layout layout, zz_t *u)
{
size_t size = (len*layout.bits_per_digit
+ (ZZ_DIGIT_T_BITS - 1))/ZZ_DIGIT_T_BITS;
if (len > SIZE_MAX / layout.bits_per_digit || size > INT_MAX) {
return ZZ_BUF; /* LCOV_EXCL_LINE */
}
if (zz_resize((zz_size_t)size, u)) {
return ZZ_MEM; /* LCOV_EXCL_LINE */
}
if (layout.digit_size == 1 && layout.bits_per_digit == 8
&& layout.digits_order == 1 && !layout.digit_endianness)
{
u->size = (zz_size_t)mpn_set_str(u->digits, digits, len, 256);
zz_normalize(u);
return ZZ_OK;
}
assert(layout.digit_size*8 >= layout.bits_per_digit);
mpn_import(u->digits, (mp_size_t *)&u->size, len, layout.digits_order,
layout.digit_size, layout.digit_endianness,
(size_t)(layout.digit_size*8 - layout.bits_per_digit),
digits);
return ZZ_OK;
}