-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgmp.h
More file actions
5306 lines (4187 loc) · 127 KB
/
Copy pathgmp.h
File metadata and controls
5306 lines (4187 loc) · 127 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
/* gmp.h -- single-header GMP (mini-gmp subset).
*
* Generated by CMake from mini-gmp/{mini-gmp,mini-mpq}.{h,c}.
* Do not edit -- regenerate by re-running cmake.
*
* True header-only: just `#include "gmp.h"` and use it. Every gmp
* function has internal linkage (static), so multiple translation units
* can include this header without link conflicts.
*
* Licensed under LGPLv3+/GPLv2+ (see COPYING.LESSERv3, COPYINGv2).
*/
#ifndef GMP_SINGLE_HEADER_H
#define GMP_SINGLE_HEADER_H
#if defined(__GNUC__) || defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused-function"
# pragma GCC diagnostic ignored "-Wunused-variable"
# pragma GCC diagnostic ignored "-Wunused-const-variable"
#endif
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable: 4505) /* unreferenced local function */
# pragma warning(disable: 4189) /* local variable initialized but not referenced */
#endif
/* ===== Public interface (mini-gmp.h) ===== */
/* mini-gmp, a minimalistic implementation of a GNU GMP subset.
Copyright 2011-2015, 2017, 2019-2021 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any
later version.
or both in parallel, as here.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library. If not,
see https://www.gnu.org/licenses/. */
/* About mini-gmp: This is a minimal implementation of a subset of the
GMP interface. It is intended for inclusion into applications which
have modest bignums needs, as a fallback when the real GMP library
is not installed.
This file defines the public interface. */
/* For size_t */
#include <stddef.h>
#if defined (__cplusplus)
extern "C" {
#endif
static void mp_set_memory_functions(void *(*)(size_t),
void *(*)(void *, size_t, size_t),
void (*)(void *, size_t));
static void mp_get_memory_functions(void *(**)(size_t),
void *(**)(void *, size_t, size_t),
void (**)(void *, size_t));
#ifndef MINI_GMP_LIMB_TYPE
#define MINI_GMP_LIMB_TYPE long
#endif
typedef unsigned MINI_GMP_LIMB_TYPE mp_limb_t;
typedef long mp_size_t;
typedef unsigned long mp_bitcnt_t;
typedef mp_limb_t *mp_ptr;
typedef const mp_limb_t *mp_srcptr;
typedef struct {
int _mp_alloc; /* Number of *limbs* allocated and pointed
to by the _mp_d field. */
int _mp_size; /* abs(_mp_size) is the number of limbs the
last field points to. If _mp_size is
negative this is a negative number. */
mp_limb_t *_mp_d; /* Pointer to the limbs. */
} __mpz_struct;
typedef __mpz_struct mpz_t[1];
typedef __mpz_struct *mpz_ptr;
typedef const __mpz_struct *mpz_srcptr;
static void mpn_copyi(mp_ptr, mp_srcptr, mp_size_t);
static void mpn_copyd(mp_ptr, mp_srcptr, mp_size_t);
static void mpn_zero(mp_ptr, mp_size_t);
static int mpn_cmp(mp_srcptr, mp_srcptr, mp_size_t);
static int mpn_zero_p(mp_srcptr, mp_size_t);
static mp_limb_t mpn_add_1(mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
static mp_limb_t mpn_add_n(mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
static mp_limb_t mpn_add(mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
static mp_limb_t mpn_sub_1(mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
static mp_limb_t mpn_sub_n(mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
static mp_limb_t mpn_sub(mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
static mp_limb_t mpn_mul_1(mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
static mp_limb_t mpn_addmul_1(mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
static mp_limb_t mpn_submul_1(mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
static mp_limb_t mpn_mul(mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
static void mpn_mul_n(mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
static void mpn_sqr(mp_ptr, mp_srcptr, mp_size_t);
static int mpn_perfect_square_p(mp_srcptr, mp_size_t);
static mp_size_t mpn_sqrtrem(mp_ptr, mp_ptr, mp_srcptr, mp_size_t);
static mp_limb_t mpn_lshift(mp_ptr, mp_srcptr, mp_size_t, unsigned int);
static mp_limb_t mpn_rshift(mp_ptr, mp_srcptr, mp_size_t, unsigned int);
static mp_bitcnt_t mpn_scan0(mp_srcptr, mp_bitcnt_t);
static mp_bitcnt_t mpn_scan1(mp_srcptr, mp_bitcnt_t);
static void mpn_com(mp_ptr, mp_srcptr, mp_size_t);
static mp_limb_t mpn_neg(mp_ptr, mp_srcptr, mp_size_t);
static mp_bitcnt_t mpn_popcount(mp_srcptr, mp_size_t);
static mp_limb_t mpn_invert_3by2(mp_limb_t, mp_limb_t);
#define mpn_invert_limb(x) mpn_invert_3by2 ((x), 0)
static size_t mpn_get_str(unsigned char *, int, mp_ptr, mp_size_t);
static mp_size_t mpn_set_str(mp_ptr, const unsigned char *, size_t, int);
static void mpz_init(mpz_t);
static void mpz_init2(mpz_t, mp_bitcnt_t);
static void mpz_clear(mpz_t);
#define mpz_odd_p(z) (((z)->_mp_size != 0) & (int) (z)->_mp_d[0])
#define mpz_even_p(z) (! mpz_odd_p (z))
static int mpz_sgn(const mpz_t);
static int mpz_cmp_si(const mpz_t, long);
static int mpz_cmp_ui(const mpz_t, unsigned long);
static int mpz_cmp(const mpz_t, const mpz_t);
static int mpz_cmpabs_ui(const mpz_t, unsigned long);
static int mpz_cmpabs(const mpz_t, const mpz_t);
static int mpz_cmp_d(const mpz_t, double);
static int mpz_cmpabs_d(const mpz_t, double);
static void mpz_abs(mpz_t, const mpz_t);
static void mpz_neg(mpz_t, const mpz_t);
static void mpz_swap(mpz_t, mpz_t);
static void mpz_add_ui(mpz_t, const mpz_t, unsigned long);
static void mpz_add(mpz_t, const mpz_t, const mpz_t);
static void mpz_sub_ui(mpz_t, const mpz_t, unsigned long);
static void mpz_ui_sub(mpz_t, unsigned long, const mpz_t);
static void mpz_sub(mpz_t, const mpz_t, const mpz_t);
static void mpz_mul_si(mpz_t, const mpz_t, long int);
static void mpz_mul_ui(mpz_t, const mpz_t, unsigned long int);
static void mpz_mul(mpz_t, const mpz_t, const mpz_t);
static void mpz_mul_2exp(mpz_t, const mpz_t, mp_bitcnt_t);
static void mpz_addmul_ui(mpz_t, const mpz_t, unsigned long int);
static void mpz_addmul(mpz_t, const mpz_t, const mpz_t);
static void mpz_submul_ui(mpz_t, const mpz_t, unsigned long int);
static void mpz_submul(mpz_t, const mpz_t, const mpz_t);
static void mpz_cdiv_qr(mpz_t, mpz_t, const mpz_t, const mpz_t);
static void mpz_fdiv_qr(mpz_t, mpz_t, const mpz_t, const mpz_t);
static void mpz_tdiv_qr(mpz_t, mpz_t, const mpz_t, const mpz_t);
static void mpz_cdiv_q(mpz_t, const mpz_t, const mpz_t);
static void mpz_fdiv_q(mpz_t, const mpz_t, const mpz_t);
static void mpz_tdiv_q(mpz_t, const mpz_t, const mpz_t);
static void mpz_cdiv_r(mpz_t, const mpz_t, const mpz_t);
static void mpz_fdiv_r(mpz_t, const mpz_t, const mpz_t);
static void mpz_tdiv_r(mpz_t, const mpz_t, const mpz_t);
static void mpz_cdiv_q_2exp(mpz_t, const mpz_t, mp_bitcnt_t);
static void mpz_fdiv_q_2exp(mpz_t, const mpz_t, mp_bitcnt_t);
static void mpz_tdiv_q_2exp(mpz_t, const mpz_t, mp_bitcnt_t);
static void mpz_cdiv_r_2exp(mpz_t, const mpz_t, mp_bitcnt_t);
static void mpz_fdiv_r_2exp(mpz_t, const mpz_t, mp_bitcnt_t);
static void mpz_tdiv_r_2exp(mpz_t, const mpz_t, mp_bitcnt_t);
static void mpz_mod(mpz_t, const mpz_t, const mpz_t);
static void mpz_divexact(mpz_t, const mpz_t, const mpz_t);
static int mpz_divisible_p(const mpz_t, const mpz_t);
static int mpz_congruent_p(const mpz_t, const mpz_t, const mpz_t);
static unsigned long mpz_cdiv_qr_ui(mpz_t, mpz_t, const mpz_t, unsigned long);
static unsigned long mpz_fdiv_qr_ui(mpz_t, mpz_t, const mpz_t, unsigned long);
static unsigned long mpz_tdiv_qr_ui(mpz_t, mpz_t, const mpz_t, unsigned long);
static unsigned long mpz_cdiv_q_ui(mpz_t, const mpz_t, unsigned long);
static unsigned long mpz_fdiv_q_ui(mpz_t, const mpz_t, unsigned long);
static unsigned long mpz_tdiv_q_ui(mpz_t, const mpz_t, unsigned long);
static unsigned long mpz_cdiv_r_ui(mpz_t, const mpz_t, unsigned long);
static unsigned long mpz_fdiv_r_ui(mpz_t, const mpz_t, unsigned long);
static unsigned long mpz_tdiv_r_ui(mpz_t, const mpz_t, unsigned long);
static unsigned long mpz_cdiv_ui(const mpz_t, unsigned long);
static unsigned long mpz_fdiv_ui(const mpz_t, unsigned long);
static unsigned long mpz_tdiv_ui(const mpz_t, unsigned long);
static unsigned long mpz_mod_ui(mpz_t, const mpz_t, unsigned long);
static void mpz_divexact_ui(mpz_t, const mpz_t, unsigned long);
static int mpz_divisible_ui_p(const mpz_t, unsigned long);
static unsigned long mpz_gcd_ui(mpz_t, const mpz_t, unsigned long);
static void mpz_gcd(mpz_t, const mpz_t, const mpz_t);
static void mpz_gcdext(mpz_t, mpz_t, mpz_t, const mpz_t, const mpz_t);
static void mpz_lcm_ui(mpz_t, const mpz_t, unsigned long);
static void mpz_lcm(mpz_t, const mpz_t, const mpz_t);
static int mpz_invert(mpz_t, const mpz_t, const mpz_t);
static void mpz_sqrtrem(mpz_t, mpz_t, const mpz_t);
static void mpz_sqrt(mpz_t, const mpz_t);
static int mpz_perfect_square_p(const mpz_t);
static void mpz_pow_ui(mpz_t, const mpz_t, unsigned long);
static void mpz_ui_pow_ui(mpz_t, unsigned long, unsigned long);
static void mpz_powm(mpz_t, const mpz_t, const mpz_t, const mpz_t);
static void mpz_powm_ui(mpz_t, const mpz_t, unsigned long, const mpz_t);
static void mpz_rootrem(mpz_t, mpz_t, const mpz_t, unsigned long);
static int mpz_root(mpz_t, const mpz_t, unsigned long);
static void mpz_fac_ui(mpz_t, unsigned long);
static void mpz_2fac_ui(mpz_t, unsigned long);
static void mpz_mfac_uiui(mpz_t, unsigned long, unsigned long);
static void mpz_bin_uiui(mpz_t, unsigned long, unsigned long);
static int mpz_probab_prime_p(const mpz_t, int);
static int mpz_tstbit(const mpz_t, mp_bitcnt_t);
static void mpz_setbit(mpz_t, mp_bitcnt_t);
static void mpz_clrbit(mpz_t, mp_bitcnt_t);
static void mpz_combit(mpz_t, mp_bitcnt_t);
static void mpz_com(mpz_t, const mpz_t);
static void mpz_and(mpz_t, const mpz_t, const mpz_t);
static void mpz_ior(mpz_t, const mpz_t, const mpz_t);
static void mpz_xor(mpz_t, const mpz_t, const mpz_t);
static mp_bitcnt_t mpz_popcount(const mpz_t);
static mp_bitcnt_t mpz_hamdist(const mpz_t, const mpz_t);
static mp_bitcnt_t mpz_scan0(const mpz_t, mp_bitcnt_t);
static mp_bitcnt_t mpz_scan1(const mpz_t, mp_bitcnt_t);
static int mpz_fits_slong_p(const mpz_t);
static int mpz_fits_ulong_p(const mpz_t);
static int mpz_fits_sint_p(const mpz_t);
static int mpz_fits_uint_p(const mpz_t);
static int mpz_fits_sshort_p(const mpz_t);
static int mpz_fits_ushort_p(const mpz_t);
static long int mpz_get_si(const mpz_t);
static unsigned long int mpz_get_ui(const mpz_t);
static double mpz_get_d(const mpz_t);
static size_t mpz_size(const mpz_t);
static mp_limb_t mpz_getlimbn(const mpz_t, mp_size_t);
static void mpz_realloc2(mpz_t, mp_bitcnt_t);
static mp_srcptr mpz_limbs_read(mpz_srcptr);
static mp_ptr mpz_limbs_modify(mpz_t, mp_size_t);
static mp_ptr mpz_limbs_write(mpz_t, mp_size_t);
static void mpz_limbs_finish(mpz_t, mp_size_t);
static mpz_srcptr mpz_roinit_n(mpz_t, mp_srcptr, mp_size_t);
#define MPZ_ROINIT_N(xp, xs) {{0, (xs),(xp) }}
static void mpz_set_si(mpz_t, signed long int);
static void mpz_set_ui(mpz_t, unsigned long int);
static void mpz_set(mpz_t, const mpz_t);
static void mpz_set_d(mpz_t, double);
static void mpz_init_set_si(mpz_t, signed long int);
static void mpz_init_set_ui(mpz_t, unsigned long int);
static void mpz_init_set(mpz_t, const mpz_t);
static void mpz_init_set_d(mpz_t, double);
static size_t mpz_sizeinbase(const mpz_t, int);
static char *mpz_get_str(char *, int, const mpz_t);
static int mpz_set_str(mpz_t, const char *, int);
static int mpz_init_set_str(mpz_t, const char *, int);
/* This long list taken from gmp.h. */
/* For reference, "defined(EOF)" cannot be used here. In g++ 2.95.4,
<iostream> defines EOF but not FILE. */
#if defined (FILE) \
|| defined (H_STDIO) \
|| defined (_H_STDIO) /* AIX */ \
|| defined (_STDIO_H) /* glibc, Sun, SCO */ \
|| defined (_STDIO_H_) /* BSD, OSF */ \
|| defined (__STDIO_H) /* Borland */ \
|| defined (__STDIO_H__) /* IRIX */ \
|| defined (_STDIO_INCLUDED) /* HPUX */ \
|| defined (__dj_include_stdio_h_) /* DJGPP */ \
|| defined (_FILE_DEFINED) /* Microsoft */ \
|| defined (__STDIO__) /* Apple MPW MrC */ \
|| defined (_MSL_STDIO_H) /* Metrowerks */ \
|| defined (_STDIO_H_INCLUDED) /* QNX4 */ \
|| defined (_ISO_STDIO_ISO_H) /* Sun C++ */ \
|| defined (__STDIO_LOADED) /* VMS */ \
|| defined (_STDIO) /* HPE NonStop */ \
|| defined (__DEFINED_FILE) /* musl */
static size_t mpz_out_str(FILE *, int, const mpz_t);
#endif
static void mpz_import(mpz_t, size_t, int, size_t, int, size_t, const void *);
static void *mpz_export(void *, size_t *, int, size_t, int, size_t, const mpz_t);
#if defined (__cplusplus)
}
#endif
/* ===== Public interface (mini-mpq.h) ===== */
/* mini-mpq, a minimalistic implementation of a GNU GMP subset.
Copyright 2018, 2019 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any
later version.
or both in parallel, as here.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library. If not,
see https://www.gnu.org/licenses/. */
/* Header */
#if defined (__cplusplus)
extern "C" {
#endif
typedef struct {
__mpz_struct _mp_num;
__mpz_struct _mp_den;
} __mpq_struct;
typedef __mpq_struct mpq_t[1];
typedef const __mpq_struct *mpq_srcptr;
typedef __mpq_struct *mpq_ptr;
#define mpq_numref(Q) (&((Q)->_mp_num))
#define mpq_denref(Q) (&((Q)->_mp_den))
static void mpq_abs(mpq_t, const mpq_t);
static void mpq_add(mpq_t, const mpq_t, const mpq_t);
static void mpq_canonicalize(mpq_t);
static void mpq_clear(mpq_t);
static int mpq_cmp(const mpq_t, const mpq_t);
static int mpq_cmp_si(const mpq_t, signed long, unsigned long);
static int mpq_cmp_ui(const mpq_t, unsigned long, unsigned long);
static int mpq_cmp_z(const mpq_t, const mpz_t);
static void mpq_div(mpq_t, const mpq_t, const mpq_t);
static void mpq_div_2exp(mpq_t, const mpq_t, mp_bitcnt_t);
static int mpq_equal(const mpq_t, const mpq_t);
static double mpq_get_d(const mpq_t);
static void mpq_get_den(mpz_t, const mpq_t);
static void mpq_get_num(mpz_t, const mpq_t);
static char *mpq_get_str(char *, int, const mpq_t q);
static void mpq_init(mpq_t);
static void mpq_inv(mpq_t, const mpq_t);
static void mpq_mul(mpq_t, const mpq_t, const mpq_t);
static void mpq_mul_2exp(mpq_t, const mpq_t, mp_bitcnt_t);
static void mpq_neg(mpq_t, const mpq_t);
static void mpq_set(mpq_t, const mpq_t);
static void mpq_set_d(mpq_t, double);
static void mpq_set_den(mpq_t, const mpz_t);
static void mpq_set_num(mpq_t, const mpz_t);
static void mpq_set_si(mpq_t, signed long, unsigned long);
static int mpq_set_str(mpq_t, const char *, int);
static void mpq_set_ui(mpq_t, unsigned long, unsigned long);
static void mpq_set_z(mpq_t, const mpz_t);
static int mpq_sgn(const mpq_t);
static void mpq_sub(mpq_t, const mpq_t, const mpq_t);
static void mpq_swap(mpq_t, mpq_t);
/* This long list taken from gmp.h. */
/* For reference, "defined(EOF)" cannot be used here. In g++ 2.95.4,
<iostream> defines EOF but not FILE. */
#if defined (FILE) \
|| defined (H_STDIO) \
|| defined (_H_STDIO) /* AIX */ \
|| defined (_STDIO_H) /* glibc, Sun, SCO */ \
|| defined (_STDIO_H_) /* BSD, OSF */ \
|| defined (__STDIO_H) /* Borland */ \
|| defined (__STDIO_H__) /* IRIX */ \
|| defined (_STDIO_INCLUDED) /* HPUX */ \
|| defined (__dj_include_stdio_h_) /* DJGPP */ \
|| defined (_FILE_DEFINED) /* Microsoft */ \
|| defined (__STDIO__) /* Apple MPW MrC */ \
|| defined (_MSL_STDIO_H) /* Metrowerks */ \
|| defined (_STDIO_H_INCLUDED) /* QNX4 */ \
|| defined (_ISO_STDIO_ISO_H) /* Sun C++ */ \
|| defined (__STDIO_LOADED) /* VMS */
static size_t mpq_out_str(FILE *, int, const mpq_t);
#endif
static void mpz_set_q(mpz_t, const mpq_t);
#if defined (__cplusplus)
}
#endif
/* ===== Implementation (mini-gmp.c) ===== */
/* mini-gmp, a minimalistic implementation of a GNU GMP subset.
Contributed to the GNU project by Niels Moller
Additional functionalities and improvements by Marco Bodrato.
Copyright 1991-1997, 1999-2022 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any
later version.
or both in parallel, as here.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library. If not,
see https://www.gnu.org/licenses/. */
/* NOTE: All functions in this file which are not declared in
mini-gmp.h are internal, and are not intended to be compatible
with GMP or with future versions of mini-gmp. */
/* Much of the material copied from GMP files, including: gmp-impl.h,
longlong.h, mpn/generic/add_n.c, mpn/generic/addmul_1.c,
mpn/generic/lshift.c, mpn/generic/mul_1.c,
mpn/generic/mul_basecase.c, mpn/generic/rshift.c,
mpn/generic/sbpi1_div_qr.c, mpn/generic/sub_n.c,
mpn/generic/submul_1.c. */
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !defined(MINI_GMP_DONT_USE_FLOAT_H)
#include <float.h>
#endif
/* Macros */
#define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)
#define GMP_LIMB_MAX ((mp_limb_t) ~ (mp_limb_t) 0)
#define GMP_LIMB_HIGHBIT ((mp_limb_t) 1 << (GMP_LIMB_BITS - 1))
#define GMP_HLIMB_BIT ((mp_limb_t) 1 << (GMP_LIMB_BITS / 2))
#define GMP_LLIMB_MASK (GMP_HLIMB_BIT - 1)
#define GMP_ULONG_BITS (sizeof(unsigned long) * CHAR_BIT)
#define GMP_ULONG_HIGHBIT ((unsigned long) 1 << (GMP_ULONG_BITS - 1))
#define GMP_ABS(x) ((x) >= 0 ? (x) : -(x))
#define GMP_NEG_CAST(T,x) (-((T)((x) + 1) - 1))
#define GMP_MIN(a, b) ((a) < (b) ? (a) : (b))
#define GMP_MAX(a, b) ((a) > (b) ? (a) : (b))
#define GMP_CMP(a,b) (((a) > (b)) - ((a) < (b)))
#if defined(DBL_MANT_DIG) && FLT_RADIX == 2
#define GMP_DBL_MANT_BITS DBL_MANT_DIG
#else
#define GMP_DBL_MANT_BITS (53)
#endif
/* Return non-zero if xp,xsize and yp,ysize overlap.
If xp+xsize<=yp there's no overlap, or if yp+ysize<=xp there's no
overlap. If both these are false, there's an overlap. */
#define GMP_MPN_OVERLAP_P(xp, xsize, yp, ysize) \
((xp) + (xsize) > (yp) && (yp) + (ysize) > (xp))
#define gmp_assert_nocarry(x) do { \
mp_limb_t __cy = (x); \
assert (__cy == 0); \
(void) (__cy); \
} while (0)
#define gmp_clz(count, x) do { \
mp_limb_t __clz_x = (x); \
unsigned __clz_c = 0; \
int LOCAL_SHIFT_BITS = 8; \
if (GMP_LIMB_BITS > LOCAL_SHIFT_BITS) \
for (; \
(__clz_x & ((mp_limb_t) 0xff << (GMP_LIMB_BITS - 8))) == 0; \
__clz_c += 8) \
{ __clz_x <<= LOCAL_SHIFT_BITS; } \
for (; (__clz_x & GMP_LIMB_HIGHBIT) == 0; __clz_c++) \
__clz_x <<= 1; \
(count) = __clz_c; \
} while (0)
#define gmp_ctz(count, x) do { \
mp_limb_t __ctz_x = (x); \
unsigned __ctz_c = 0; \
gmp_clz (__ctz_c, __ctz_x & - __ctz_x); \
(count) = GMP_LIMB_BITS - 1 - __ctz_c; \
} while (0)
#define gmp_add_ssaaaa(sh, sl, ah, al, bh, bl) \
do { \
mp_limb_t __x; \
__x = (al) + (bl); \
(sh) = (ah) + (bh) + (__x < (al)); \
(sl) = __x; \
} while (0)
#define gmp_sub_ddmmss(sh, sl, ah, al, bh, bl) \
do { \
mp_limb_t __x; \
__x = (al) - (bl); \
(sh) = (ah) - (bh) - ((al) < (bl)); \
(sl) = __x; \
} while (0)
#define gmp_umul_ppmm(w1, w0, u, v) \
do { \
int LOCAL_GMP_LIMB_BITS = GMP_LIMB_BITS; \
if (sizeof(unsigned int) * CHAR_BIT >= 2 * GMP_LIMB_BITS) \
{ \
unsigned int __ww = (unsigned int) (u) * (v); \
w0 = (mp_limb_t) __ww; \
w1 = (mp_limb_t) (__ww >> LOCAL_GMP_LIMB_BITS); \
} \
else if (GMP_ULONG_BITS >= 2 * GMP_LIMB_BITS) \
{ \
unsigned long int __ww = (unsigned long int) (u) * (v); \
w0 = (mp_limb_t) __ww; \
w1 = (mp_limb_t) (__ww >> LOCAL_GMP_LIMB_BITS); \
} \
else { \
mp_limb_t __x0, __x1, __x2, __x3; \
unsigned __ul, __vl, __uh, __vh; \
mp_limb_t __u = (u), __v = (v); \
assert (sizeof (unsigned) * 2 >= sizeof (mp_limb_t)); \
\
__ul = __u & GMP_LLIMB_MASK; \
__uh = __u >> (GMP_LIMB_BITS / 2); \
__vl = __v & GMP_LLIMB_MASK; \
__vh = __v >> (GMP_LIMB_BITS / 2); \
\
__x0 = (mp_limb_t) __ul * __vl; \
__x1 = (mp_limb_t) __ul * __vh; \
__x2 = (mp_limb_t) __uh * __vl; \
__x3 = (mp_limb_t) __uh * __vh; \
\
__x1 += __x0 >> (GMP_LIMB_BITS / 2);/* this can't give carry */ \
__x1 += __x2; /* but this indeed can */ \
if (__x1 < __x2) /* did we get it? */ \
__x3 += GMP_HLIMB_BIT; /* yes, add it in the proper pos. */ \
\
(w1) = __x3 + (__x1 >> (GMP_LIMB_BITS / 2)); \
(w0) = (__x1 << (GMP_LIMB_BITS / 2)) + (__x0 & GMP_LLIMB_MASK); \
} \
} while (0)
/* If mp_limb_t is of size smaller than int, plain u*v implies
automatic promotion to *signed* int, and then multiply may overflow
and cause undefined behavior. Explicitly cast to unsigned int for
that case. */
#define gmp_umullo_limb(u, v) \
((sizeof(mp_limb_t) >= sizeof(int)) ? (u)*(v) : (unsigned int)(u) * (v))
#define gmp_udiv_qrnnd_preinv(q, r, nh, nl, d, di) \
do { \
mp_limb_t _qh, _ql, _r, _mask; \
gmp_umul_ppmm (_qh, _ql, (nh), (di)); \
gmp_add_ssaaaa (_qh, _ql, _qh, _ql, (nh) + 1, (nl)); \
_r = (nl) - gmp_umullo_limb (_qh, (d)); \
_mask = -(mp_limb_t) (_r > _ql); /* both > and >= are OK */ \
_qh += _mask; \
_r += _mask & (d); \
if (_r >= (d)) \
{ \
_r -= (d); \
_qh++; \
} \
\
(r) = _r; \
(q) = _qh; \
} while (0)
#define gmp_udiv_qr_3by2(q, r1, r0, n2, n1, n0, d1, d0, dinv) \
do { \
mp_limb_t _q0, _t1, _t0, _mask; \
gmp_umul_ppmm ((q), _q0, (n2), (dinv)); \
gmp_add_ssaaaa ((q), _q0, (q), _q0, (n2), (n1)); \
\
/* Compute the two most significant limbs of n - q'd */ \
(r1) = (n1) - gmp_umullo_limb ((d1), (q)); \
gmp_sub_ddmmss ((r1), (r0), (r1), (n0), (d1), (d0)); \
gmp_umul_ppmm (_t1, _t0, (d0), (q)); \
gmp_sub_ddmmss ((r1), (r0), (r1), (r0), _t1, _t0); \
(q)++; \
\
/* Conditionally adjust q and the remainders */ \
_mask = - (mp_limb_t) ((r1) >= _q0); \
(q) += _mask; \
gmp_add_ssaaaa ((r1), (r0), (r1), (r0), _mask & (d1), _mask & (d0)); \
if ((r1) >= (d1)) \
{ \
if ((r1) > (d1) || (r0) >= (d0)) \
{ \
(q)++; \
gmp_sub_ddmmss ((r1), (r0), (r1), (r0), (d1), (d0)); \
} \
} \
} while (0)
/* Swap macros. */
#define MP_LIMB_T_SWAP(x, y) \
do { \
mp_limb_t __mp_limb_t_swap__tmp = (x); \
(x) = (y); \
(y) = __mp_limb_t_swap__tmp; \
} while (0)
#define MP_SIZE_T_SWAP(x, y) \
do { \
mp_size_t __mp_size_t_swap__tmp = (x); \
(x) = (y); \
(y) = __mp_size_t_swap__tmp; \
} while (0)
#define MP_BITCNT_T_SWAP(x,y) \
do { \
mp_bitcnt_t __mp_bitcnt_t_swap__tmp = (x); \
(x) = (y); \
(y) = __mp_bitcnt_t_swap__tmp; \
} while (0)
#define MP_PTR_SWAP(x, y) \
do { \
mp_ptr __mp_ptr_swap__tmp = (x); \
(x) = (y); \
(y) = __mp_ptr_swap__tmp; \
} while (0)
#define MP_SRCPTR_SWAP(x, y) \
do { \
mp_srcptr __mp_srcptr_swap__tmp = (x); \
(x) = (y); \
(y) = __mp_srcptr_swap__tmp; \
} while (0)
#define MPN_PTR_SWAP(xp,xs, yp,ys) \
do { \
MP_PTR_SWAP (xp, yp); \
MP_SIZE_T_SWAP (xs, ys); \
} while(0)
#define MPN_SRCPTR_SWAP(xp,xs, yp,ys) \
do { \
MP_SRCPTR_SWAP (xp, yp); \
MP_SIZE_T_SWAP (xs, ys); \
} while(0)
#define MPZ_PTR_SWAP(x, y) \
do { \
mpz_ptr __mpz_ptr_swap__tmp = (x); \
(x) = (y); \
(y) = __mpz_ptr_swap__tmp; \
} while (0)
#define MPZ_SRCPTR_SWAP(x, y) \
do { \
mpz_srcptr __mpz_srcptr_swap__tmp = (x); \
(x) = (y); \
(y) = __mpz_srcptr_swap__tmp; \
} while (0)
static const int mp_bits_per_limb = GMP_LIMB_BITS;
/* Memory allocation and other helper functions. */
static void
gmp_die(const char *msg) {
fprintf(stderr, "%s\n", msg);
abort();
}
static void *
gmp_default_alloc(size_t size) {
void *p;
assert(size > 0);
p = malloc(size);
if (!p)
gmp_die("gmp_default_alloc: Virtual memory exhausted.");
return p;
}
static void *
gmp_default_realloc(void *old, size_t unused_old_size, size_t new_size) {
void *p;
p = realloc(old, new_size);
if (!p)
gmp_die("gmp_default_realloc: Virtual memory exhausted.");
return p;
}
static void
gmp_default_free(void *p, size_t unused_size) {
free(p);
}
static void * (*gmp_allocate_func)(size_t) = gmp_default_alloc;
static void * (*gmp_reallocate_func)(void *, size_t, size_t) = gmp_default_realloc;
static void (*gmp_free_func)(void *, size_t) = gmp_default_free;
static void
mp_get_memory_functions(void *(**alloc_func)(size_t),
void *(**realloc_func)(void *, size_t, size_t),
void (**free_func)(void *, size_t)) {
if (alloc_func)
*alloc_func = gmp_allocate_func;
if (realloc_func)
*realloc_func = gmp_reallocate_func;
if (free_func)
*free_func = gmp_free_func;
}
static void
mp_set_memory_functions(void *(*alloc_func)(size_t),
void *(*realloc_func)(void *, size_t, size_t),
void (*free_func)(void *, size_t)) {
if (!alloc_func)
alloc_func = gmp_default_alloc;
if (!realloc_func)
realloc_func = gmp_default_realloc;
if (!free_func)
free_func = gmp_default_free;
gmp_allocate_func = alloc_func;
gmp_reallocate_func = realloc_func;
gmp_free_func = free_func;
}
#define gmp_alloc(size) ((*gmp_allocate_func)((size)))
#define gmp_free(p, size) ((*gmp_free_func) ((p), (size)))
#define gmp_realloc(ptr, old_size, size) ((*gmp_reallocate_func)(ptr, old_size, size))
static mp_ptr
gmp_alloc_limbs(mp_size_t size) {
return (mp_ptr) gmp_alloc(size * sizeof (mp_limb_t));
}
static mp_ptr
gmp_realloc_limbs(mp_ptr old, mp_size_t old_size, mp_size_t size) {
assert(size > 0);
return (mp_ptr) gmp_realloc(old, old_size * sizeof (mp_limb_t), size * sizeof (mp_limb_t));
}
static void
gmp_free_limbs(mp_ptr old, mp_size_t size) {
gmp_free(old, size * sizeof (mp_limb_t));
}
/* MPN interface */
static void
mpn_copyi(mp_ptr d, mp_srcptr s, mp_size_t n) {
mp_size_t i;
for (i = 0; i < n; i++)
d[i] = s[i];
}
static void
mpn_copyd(mp_ptr d, mp_srcptr s, mp_size_t n) {
while (--n >= 0)
d[n] = s[n];
}
static int
mpn_cmp(mp_srcptr ap, mp_srcptr bp, mp_size_t n) {
while (--n >= 0) {
if (ap[n] != bp[n])
return ap[n] > bp[n] ? 1 : -1;
}
return 0;
}
static int
mpn_cmp4(mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn) {
if (an != bn)
return an < bn ? -1 : 1;
else