-
Notifications
You must be signed in to change notification settings - Fork 3
/
gmpxx.h
3434 lines (3017 loc) · 114 KB
/
gmpxx.h
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
/* gmpxx.h -- C++ class wrapper for GMP types. -*- C++ -*-
Copyright 2001-2003, 2006, 2008, 2011, 2012 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/. */
#ifndef __GMP_PLUSPLUS__
#define __GMP_PLUSPLUS__
#include <iosfwd>
#include <cstring> /* for strlen */
#include <limits> /* numeric_limits */
#include <utility>
#include <algorithm> /* swap */
#include <string>
#include <stdexcept>
#include <cfloat>
#include <gmp.h>
// wrapper for gcc's __builtin_constant_p
// __builtin_constant_p has been in gcc since forever,
// but g++-3.4 miscompiles it.
#if __GMP_GNUC_PREREQ(4, 2)
#define __GMPXX_CONSTANT(X) __builtin_constant_p(X)
#else
#define __GMPXX_CONSTANT(X) false
#endif
#define __GMPXX_CONSTANT_TRUE(X) (__GMPXX_CONSTANT(X) && (X))
// Use C++11 features
#ifndef __GMPXX_USE_CXX11
#if __cplusplus >= 201103L
#define __GMPXX_USE_CXX11 1
#else
#define __GMPXX_USE_CXX11 0
#endif
#endif
#if __GMPXX_USE_CXX11
#define __GMPXX_NOEXCEPT noexcept
#include <type_traits> // for common_type
#else
#define __GMPXX_NOEXCEPT
#endif
// Max allocations for plain types when converted to GMP types
#if GMP_NAIL_BITS != 0 && ! defined _LONG_LONG_LIMB
#define __GMPZ_ULI_LIMBS 2
#else
#define __GMPZ_ULI_LIMBS 1
#endif
#define __GMPXX_BITS_TO_LIMBS(n) (((n) + (GMP_NUMB_BITS - 1)) / GMP_NUMB_BITS)
#define __GMPZ_DBL_LIMBS __GMPXX_BITS_TO_LIMBS(DBL_MAX_EXP)+1
#define __GMPQ_NUM_DBL_LIMBS __GMPZ_DBL_LIMBS
#define __GMPQ_DEN_DBL_LIMBS __GMPXX_BITS_TO_LIMBS(DBL_MANT_DIG+1-DBL_MIN_EXP)+1
// The final +1s are a security margin. The current implementation of
// mpq_set_d seems to need it for the denominator.
inline void __mpz_set_ui_safe(mpz_ptr p, unsigned long l)
{
p->_mp_size = (l != 0);
p->_mp_d[0] = l & GMP_NUMB_MASK;
#if __GMPZ_ULI_LIMBS > 1
l >>= GMP_NUMB_BITS;
p->_mp_d[1] = l;
p->_mp_size += (l != 0);
#endif
}
inline void __mpz_set_si_safe(mpz_ptr p, long l)
{
if(l < 0)
{
__mpz_set_ui_safe(p, -static_cast<unsigned long>(l));
mpz_neg(p, p);
}
else
__mpz_set_ui_safe(p, l);
// Note: we know the high bit of l is 0 so we could do slightly better
}
// Fake temporary variables
#define __GMPXX_TMPZ_UI \
mpz_t temp; \
mp_limb_t limbs[__GMPZ_ULI_LIMBS]; \
temp->_mp_d = limbs; \
__mpz_set_ui_safe (temp, l)
#define __GMPXX_TMPZ_SI \
mpz_t temp; \
mp_limb_t limbs[__GMPZ_ULI_LIMBS]; \
temp->_mp_d = limbs; \
__mpz_set_si_safe (temp, l)
#define __GMPXX_TMPZ_D \
mpz_t temp; \
mp_limb_t limbs[__GMPZ_DBL_LIMBS]; \
temp->_mp_d = limbs; \
temp->_mp_alloc = __GMPZ_DBL_LIMBS; \
mpz_set_d (temp, d)
#define __GMPXX_TMPQ_UI \
mpq_t temp; \
mp_limb_t limbs[__GMPZ_ULI_LIMBS+1]; \
mpq_numref(temp)->_mp_d = limbs; \
__mpz_set_ui_safe (mpq_numref(temp), l); \
mpq_denref(temp)->_mp_d = limbs + __GMPZ_ULI_LIMBS; \
mpq_denref(temp)->_mp_size = 1; \
mpq_denref(temp)->_mp_d[0] = 1
#define __GMPXX_TMPQ_SI \
mpq_t temp; \
mp_limb_t limbs[__GMPZ_ULI_LIMBS+1]; \
mpq_numref(temp)->_mp_d = limbs; \
__mpz_set_si_safe (mpq_numref(temp), l); \
mpq_denref(temp)->_mp_d = limbs + __GMPZ_ULI_LIMBS; \
mpq_denref(temp)->_mp_size = 1; \
mpq_denref(temp)->_mp_d[0] = 1
#define __GMPXX_TMPQ_D \
mpq_t temp; \
mp_limb_t limbs[__GMPQ_NUM_DBL_LIMBS + __GMPQ_DEN_DBL_LIMBS]; \
mpq_numref(temp)->_mp_d = limbs; \
mpq_numref(temp)->_mp_alloc = __GMPQ_NUM_DBL_LIMBS; \
mpq_denref(temp)->_mp_d = limbs + __GMPQ_NUM_DBL_LIMBS; \
mpq_denref(temp)->_mp_alloc = __GMPQ_DEN_DBL_LIMBS; \
mpq_set_d (temp, d)
inline unsigned long __gmpxx_abs_ui (signed long l)
{
return l >= 0 ? static_cast<unsigned long>(l)
: -static_cast<unsigned long>(l);
}
/**************** Function objects ****************/
/* Any evaluation of a __gmp_expr ends up calling one of these functions
all intermediate functions being inline, the evaluation should optimize
to a direct call to the relevant function, thus yielding no overhead
over the C interface. */
struct __gmp_unary_plus
{
static void eval(mpz_ptr z, mpz_srcptr w) { mpz_set(z, w); }
static void eval(mpq_ptr q, mpq_srcptr r) { mpq_set(q, r); }
static void eval(mpf_ptr f, mpf_srcptr g) { mpf_set(f, g); }
};
struct __gmp_unary_minus
{
static void eval(mpz_ptr z, mpz_srcptr w) { mpz_neg(z, w); }
static void eval(mpq_ptr q, mpq_srcptr r) { mpq_neg(q, r); }
static void eval(mpf_ptr f, mpf_srcptr g) { mpf_neg(f, g); }
};
struct __gmp_unary_com
{
static void eval(mpz_ptr z, mpz_srcptr w) { mpz_com(z, w); }
};
struct __gmp_binary_plus
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_add(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{
// Ideally, those checks should happen earlier so that the tree
// generated for a+0+b would just be sum(a,b).
if (__GMPXX_CONSTANT(l) && l == 0)
{
if (z != w) mpz_set(z, w);
}
else
mpz_add_ui(z, w, l);
}
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{ eval(z, w, l); }
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{
if (l >= 0)
eval(z, w, static_cast<unsigned long>(l));
else
mpz_sub_ui(z, w, -static_cast<unsigned long>(l));
}
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{ eval(z, w, l); }
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{ __GMPXX_TMPZ_D; mpz_add (z, w, temp); }
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{ eval(z, w, d); }
static void eval(mpq_ptr q, mpq_srcptr r, mpq_srcptr s)
{ mpq_add(q, r, s); }
static void eval(mpq_ptr q, mpq_srcptr r, unsigned long int l)
{
if (__GMPXX_CONSTANT(l) && l == 0)
{
if (q != r) mpq_set(q, r);
}
else
{
if (q == r)
mpz_addmul_ui(mpq_numref(q), mpq_denref(q), l);
else
{
mpz_mul_ui(mpq_numref(q), mpq_denref(r), l);
mpz_add(mpq_numref(q), mpq_numref(q), mpq_numref(r));
mpz_set(mpq_denref(q), mpq_denref(r));
}
}
}
static void eval(mpq_ptr q, unsigned long int l, mpq_srcptr r)
{ eval(q, r, l); }
static inline void eval(mpq_ptr q, mpq_srcptr r, signed long int l);
// defined after __gmp_binary_minus
static void eval(mpq_ptr q, signed long int l, mpq_srcptr r)
{ eval(q, r, l); }
static void eval(mpq_ptr q, mpq_srcptr r, double d)
{ __GMPXX_TMPQ_D; mpq_add (q, r, temp); }
static void eval(mpq_ptr q, double d, mpq_srcptr r)
{ eval(q, r, d); }
static void eval(mpq_ptr q, mpq_srcptr r, mpz_srcptr z)
{
if (q == r)
mpz_addmul(mpq_numref(q), mpq_denref(q), z);
else
{
mpz_mul(mpq_numref(q), mpq_denref(r), z);
mpz_add(mpq_numref(q), mpq_numref(q), mpq_numref(r));
mpz_set(mpq_denref(q), mpq_denref(r));
}
}
static void eval(mpq_ptr q, mpz_srcptr z, mpq_srcptr r)
{ eval(q, r, z); }
static void eval(mpf_ptr f, mpf_srcptr g, mpf_srcptr h)
{ mpf_add(f, g, h); }
static void eval(mpf_ptr f, mpf_srcptr g, unsigned long int l)
{ mpf_add_ui(f, g, l); }
static void eval(mpf_ptr f, unsigned long int l, mpf_srcptr g)
{ mpf_add_ui(f, g, l); }
static void eval(mpf_ptr f, mpf_srcptr g, signed long int l)
{
if (l >= 0)
mpf_add_ui(f, g, l);
else
mpf_sub_ui(f, g, -static_cast<unsigned long>(l));
}
static void eval(mpf_ptr f, signed long int l, mpf_srcptr g)
{ eval(f, g, l); }
static void eval(mpf_ptr f, mpf_srcptr g, double d)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_add(f, g, temp);
mpf_clear(temp);
}
static void eval(mpf_ptr f, double d, mpf_srcptr g)
{ eval(f, g, d); }
};
struct __gmp_binary_minus
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_sub(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{
if (__GMPXX_CONSTANT(l) && l == 0)
{
if (z != w) mpz_set(z, w);
}
else
mpz_sub_ui(z, w, l);
}
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{
if (__GMPXX_CONSTANT(l) && l == 0)
{
mpz_neg(z, w);
}
else
mpz_ui_sub(z, l, w);
}
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{
if (l >= 0)
eval(z, w, static_cast<unsigned long>(l));
else
mpz_add_ui(z, w, -static_cast<unsigned long>(l));
}
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{
if (l >= 0)
eval(z, static_cast<unsigned long>(l), w);
else
{
mpz_add_ui(z, w, -static_cast<unsigned long>(l));
mpz_neg(z, z);
}
}
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{ __GMPXX_TMPZ_D; mpz_sub (z, w, temp); }
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{ __GMPXX_TMPZ_D; mpz_sub (z, temp, w); }
static void eval(mpq_ptr q, mpq_srcptr r, mpq_srcptr s)
{ mpq_sub(q, r, s); }
static void eval(mpq_ptr q, mpq_srcptr r, unsigned long int l)
{
if (__GMPXX_CONSTANT(l) && l == 0)
{
if (q != r) mpq_set(q, r);
}
else
{
if (q == r)
mpz_submul_ui(mpq_numref(q), mpq_denref(q), l);
else
{
mpz_mul_ui(mpq_numref(q), mpq_denref(r), l);
mpz_sub(mpq_numref(q), mpq_numref(r), mpq_numref(q));
mpz_set(mpq_denref(q), mpq_denref(r));
}
}
}
static void eval(mpq_ptr q, unsigned long int l, mpq_srcptr r)
{ eval(q, r, l); mpq_neg(q, q); }
static void eval(mpq_ptr q, mpq_srcptr r, signed long int l)
{
if (l >= 0)
eval(q, r, static_cast<unsigned long>(l));
else
__gmp_binary_plus::eval(q, r, -static_cast<unsigned long>(l));
}
static void eval(mpq_ptr q, signed long int l, mpq_srcptr r)
{ eval(q, r, l); mpq_neg(q, q); }
static void eval(mpq_ptr q, mpq_srcptr r, double d)
{ __GMPXX_TMPQ_D; mpq_sub (q, r, temp); }
static void eval(mpq_ptr q, double d, mpq_srcptr r)
{ __GMPXX_TMPQ_D; mpq_sub (q, temp, r); }
static void eval(mpq_ptr q, mpq_srcptr r, mpz_srcptr z)
{
if (q == r)
mpz_submul(mpq_numref(q), mpq_denref(q), z);
else
{
mpz_mul(mpq_numref(q), mpq_denref(r), z);
mpz_sub(mpq_numref(q), mpq_numref(r), mpq_numref(q));
mpz_set(mpq_denref(q), mpq_denref(r));
}
}
static void eval(mpq_ptr q, mpz_srcptr z, mpq_srcptr r)
{ eval(q, r, z); mpq_neg(q, q); }
static void eval(mpf_ptr f, mpf_srcptr g, mpf_srcptr h)
{ mpf_sub(f, g, h); }
static void eval(mpf_ptr f, mpf_srcptr g, unsigned long int l)
{ mpf_sub_ui(f, g, l); }
static void eval(mpf_ptr f, unsigned long int l, mpf_srcptr g)
{ mpf_ui_sub(f, l, g); }
static void eval(mpf_ptr f, mpf_srcptr g, signed long int l)
{
if (l >= 0)
mpf_sub_ui(f, g, l);
else
mpf_add_ui(f, g, -static_cast<unsigned long>(l));
}
static void eval(mpf_ptr f, signed long int l, mpf_srcptr g)
{
if (l >= 0)
mpf_sub_ui(f, g, l);
else
mpf_add_ui(f, g, -static_cast<unsigned long>(l));
mpf_neg(f, f);
}
static void eval(mpf_ptr f, mpf_srcptr g, double d)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_sub(f, g, temp);
mpf_clear(temp);
}
static void eval(mpf_ptr f, double d, mpf_srcptr g)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_sub(f, temp, g);
mpf_clear(temp);
}
};
// defined here so it can reference __gmp_binary_minus
inline void
__gmp_binary_plus::eval(mpq_ptr q, mpq_srcptr r, signed long int l)
{
if (l >= 0)
eval(q, r, static_cast<unsigned long>(l));
else
__gmp_binary_minus::eval(q, r, -static_cast<unsigned long>(l));
}
struct __gmp_binary_lshift
{
static void eval(mpz_ptr z, mpz_srcptr w, mp_bitcnt_t l)
{
if (__GMPXX_CONSTANT(l) && (l == 0))
{
if (z != w) mpz_set(z, w);
}
else
mpz_mul_2exp(z, w, l);
}
static void eval(mpq_ptr q, mpq_srcptr r, mp_bitcnt_t l)
{
if (__GMPXX_CONSTANT(l) && (l == 0))
{
if (q != r) mpq_set(q, r);
}
else
mpq_mul_2exp(q, r, l);
}
static void eval(mpf_ptr f, mpf_srcptr g, mp_bitcnt_t l)
{ mpf_mul_2exp(f, g, l); }
};
struct __gmp_binary_rshift
{
static void eval(mpz_ptr z, mpz_srcptr w, mp_bitcnt_t l)
{
if (__GMPXX_CONSTANT(l) && (l == 0))
{
if (z != w) mpz_set(z, w);
}
else
mpz_fdiv_q_2exp(z, w, l);
}
static void eval(mpq_ptr q, mpq_srcptr r, mp_bitcnt_t l)
{
if (__GMPXX_CONSTANT(l) && (l == 0))
{
if (q != r) mpq_set(q, r);
}
else
mpq_div_2exp(q, r, l);
}
static void eval(mpf_ptr f, mpf_srcptr g, mp_bitcnt_t l)
{ mpf_div_2exp(f, g, l); }
};
struct __gmp_binary_multiplies
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_mul(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{
// gcc-3.3 doesn't have __builtin_ctzl. Don't bother optimizing for old gcc.
#if __GMP_GNUC_PREREQ(3, 4)
if (__GMPXX_CONSTANT(l) && (l & (l-1)) == 0)
{
if (l == 0)
{
z->_mp_size = 0;
}
else
{
__gmp_binary_lshift::eval(z, w, __builtin_ctzl(l));
}
}
else
#endif
mpz_mul_ui(z, w, l);
}
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{ eval(z, w, l); }
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{
if (__GMPXX_CONSTANT_TRUE(l >= 0))
eval(z, w, static_cast<unsigned long>(l));
else if (__GMPXX_CONSTANT_TRUE(l <= 0))
{
eval(z, w, -static_cast<unsigned long>(l));
mpz_neg(z, z);
}
else
mpz_mul_si (z, w, l);
}
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{ eval(z, w, l); }
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{ __GMPXX_TMPZ_D; mpz_mul (z, w, temp); }
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{ eval(z, w, d); }
static void eval(mpq_ptr q, mpq_srcptr r, mpq_srcptr s)
{ mpq_mul(q, r, s); }
static void eval(mpq_ptr q, mpq_srcptr r, unsigned long int l)
{
#if __GMP_GNUC_PREREQ(3, 4)
if (__GMPXX_CONSTANT(l) && (l & (l-1)) == 0)
{
if (l == 0)
{
mpq_set_ui(q, 0, 1);
}
else
{
__gmp_binary_lshift::eval(q, r, __builtin_ctzl(l));
}
}
else
#endif
{
__GMPXX_TMPQ_UI;
mpq_mul (q, r, temp);
}
}
static void eval(mpq_ptr q, unsigned long int l, mpq_srcptr r)
{ eval(q, r, l); }
static void eval(mpq_ptr q, mpq_srcptr r, signed long int l)
{
if (__GMPXX_CONSTANT_TRUE(l >= 0))
eval(q, r, static_cast<unsigned long>(l));
else if (__GMPXX_CONSTANT_TRUE(l <= 0))
{
eval(q, r, -static_cast<unsigned long>(l));
mpq_neg(q, q);
}
else
{
__GMPXX_TMPQ_SI;
mpq_mul (q, r, temp);
}
}
static void eval(mpq_ptr q, signed long int l, mpq_srcptr r)
{ eval(q, r, l); }
static void eval(mpq_ptr q, mpq_srcptr r, double d)
{ __GMPXX_TMPQ_D; mpq_mul (q, r, temp); }
static void eval(mpq_ptr q, double d, mpq_srcptr r)
{ eval(q, r, d); }
static void eval(mpf_ptr f, mpf_srcptr g, mpf_srcptr h)
{ mpf_mul(f, g, h); }
static void eval(mpf_ptr f, mpf_srcptr g, unsigned long int l)
{ mpf_mul_ui(f, g, l); }
static void eval(mpf_ptr f, unsigned long int l, mpf_srcptr g)
{ mpf_mul_ui(f, g, l); }
static void eval(mpf_ptr f, mpf_srcptr g, signed long int l)
{
if (l >= 0)
mpf_mul_ui(f, g, l);
else
{
mpf_mul_ui(f, g, -static_cast<unsigned long>(l));
mpf_neg(f, f);
}
}
static void eval(mpf_ptr f, signed long int l, mpf_srcptr g)
{ eval(f, g, l); }
static void eval(mpf_ptr f, mpf_srcptr g, double d)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_mul(f, g, temp);
mpf_clear(temp);
}
static void eval(mpf_ptr f, double d, mpf_srcptr g)
{ eval(f, g, d); }
};
struct __gmp_binary_divides
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_tdiv_q(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{
#if __GMP_GNUC_PREREQ(3, 4)
// Don't optimize division by 0...
if (__GMPXX_CONSTANT(l) && (l & (l-1)) == 0 && l != 0)
{
if (l == 1)
{
if (z != w) mpz_set(z, w);
}
else
mpz_tdiv_q_2exp(z, w, __builtin_ctzl(l));
// warning: do not use rshift (fdiv)
}
else
#endif
mpz_tdiv_q_ui(z, w, l);
}
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{
if (mpz_sgn(w) >= 0)
{
if (mpz_fits_ulong_p(w))
mpz_set_ui(z, l / mpz_get_ui(w));
else
mpz_set_ui(z, 0);
}
else
{
mpz_neg(z, w);
if (mpz_fits_ulong_p(z))
{
mpz_set_ui(z, l / mpz_get_ui(z));
mpz_neg(z, z);
}
else
mpz_set_ui(z, 0);
}
}
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{
if (l >= 0)
eval(z, w, static_cast<unsigned long>(l));
else
{
eval(z, w, -static_cast<unsigned long>(l));
mpz_neg(z, z);
}
}
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{
if (mpz_fits_slong_p(w))
mpz_set_si(z, l / mpz_get_si(w));
else
{
/* if w is bigger than a long then the quotient must be zero, unless
l==LONG_MIN and w==-LONG_MIN in which case the quotient is -1 */
mpz_set_si (z, (mpz_cmpabs_ui (w, __gmpxx_abs_ui(l)) == 0 ? -1 : 0));
}
}
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{ __GMPXX_TMPZ_D; mpz_tdiv_q (z, w, temp); }
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{ __GMPXX_TMPZ_D; mpz_tdiv_q (z, temp, w); }
static void eval(mpq_ptr q, mpq_srcptr r, mpq_srcptr s)
{ mpq_div(q, r, s); }
static void eval(mpq_ptr q, mpq_srcptr r, unsigned long int l)
{
#if __GMP_GNUC_PREREQ(3, 4)
if (__GMPXX_CONSTANT(l) && (l & (l-1)) == 0 && l != 0)
__gmp_binary_rshift::eval(q, r, __builtin_ctzl(l));
else
#endif
{
__GMPXX_TMPQ_UI;
mpq_div (q, r, temp);
}
}
static void eval(mpq_ptr q, unsigned long int l, mpq_srcptr r)
{ __GMPXX_TMPQ_UI; mpq_div (q, temp, r); }
static void eval(mpq_ptr q, mpq_srcptr r, signed long int l)
{
if (__GMPXX_CONSTANT_TRUE(l >= 0))
eval(q, r, static_cast<unsigned long>(l));
else if (__GMPXX_CONSTANT_TRUE(l <= 0))
{
eval(q, r, -static_cast<unsigned long>(l));
mpq_neg(q, q);
}
else
{
__GMPXX_TMPQ_SI;
mpq_div (q, r, temp);
}
}
static void eval(mpq_ptr q, signed long int l, mpq_srcptr r)
{ __GMPXX_TMPQ_SI; mpq_div (q, temp, r); }
static void eval(mpq_ptr q, mpq_srcptr r, double d)
{ __GMPXX_TMPQ_D; mpq_div (q, r, temp); }
static void eval(mpq_ptr q, double d, mpq_srcptr r)
{ __GMPXX_TMPQ_D; mpq_div (q, temp, r); }
static void eval(mpf_ptr f, mpf_srcptr g, mpf_srcptr h)
{ mpf_div(f, g, h); }
static void eval(mpf_ptr f, mpf_srcptr g, unsigned long int l)
{ mpf_div_ui(f, g, l); }
static void eval(mpf_ptr f, unsigned long int l, mpf_srcptr g)
{ mpf_ui_div(f, l, g); }
static void eval(mpf_ptr f, mpf_srcptr g, signed long int l)
{
if (l >= 0)
mpf_div_ui(f, g, l);
else
{
mpf_div_ui(f, g, -static_cast<unsigned long>(l));
mpf_neg(f, f);
}
}
static void eval(mpf_ptr f, signed long int l, mpf_srcptr g)
{
if (l >= 0)
mpf_ui_div(f, l, g);
else
{
mpf_ui_div(f, -static_cast<unsigned long>(l), g);
mpf_neg(f, f);
}
}
static void eval(mpf_ptr f, mpf_srcptr g, double d)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_div(f, g, temp);
mpf_clear(temp);
}
static void eval(mpf_ptr f, double d, mpf_srcptr g)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_div(f, temp, g);
mpf_clear(temp);
}
};
struct __gmp_binary_modulus
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_tdiv_r(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ mpz_tdiv_r_ui(z, w, l); }
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{
if (mpz_sgn(w) >= 0)
{
if (mpz_fits_ulong_p(w))
mpz_set_ui(z, l % mpz_get_ui(w));
else
mpz_set_ui(z, l);
}
else
{
mpz_neg(z, w);
if (mpz_fits_ulong_p(z))
mpz_set_ui(z, l % mpz_get_ui(z));
else
mpz_set_ui(z, l);
}
}
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{
mpz_tdiv_r_ui (z, w, __gmpxx_abs_ui(l));
}
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{
if (mpz_fits_slong_p(w))
mpz_set_si(z, l % mpz_get_si(w));
else
{
/* if w is bigger than a long then the remainder is l unchanged,
unless l==LONG_MIN and w==-LONG_MIN in which case it's 0 */
mpz_set_si (z, mpz_cmpabs_ui (w, __gmpxx_abs_ui(l)) == 0 ? 0 : l);
}
}
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{ __GMPXX_TMPZ_D; mpz_tdiv_r (z, w, temp); }
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{ __GMPXX_TMPZ_D; mpz_tdiv_r (z, temp, w); }
};
struct __gmp_binary_and
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_and(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ __GMPXX_TMPZ_UI; mpz_and (z, w, temp); }
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{ eval(z, w, l); }
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{ __GMPXX_TMPZ_SI; mpz_and (z, w, temp); }
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{ eval(z, w, l); }
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{ __GMPXX_TMPZ_D; mpz_and (z, w, temp); }
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{ eval(z, w, d); }
};
struct __gmp_binary_ior
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_ior(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ __GMPXX_TMPZ_UI; mpz_ior (z, w, temp); }
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{ eval(z, w, l); }
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{ __GMPXX_TMPZ_SI; mpz_ior (z, w, temp); }
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{ eval(z, w, l); }
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{ __GMPXX_TMPZ_D; mpz_ior (z, w, temp); }
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{ eval(z, w, d); }
};
struct __gmp_binary_xor
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_xor(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ __GMPXX_TMPZ_UI; mpz_xor (z, w, temp); }
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{ eval(z, w, l); }
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{ __GMPXX_TMPZ_SI; mpz_xor (z, w, temp); }
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{ eval(z, w, l); }
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{ __GMPXX_TMPZ_D; mpz_xor (z, w, temp); }
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{ eval(z, w, d); }
};
struct __gmp_cmp_function
{
static int eval(mpz_srcptr z, mpz_srcptr w) { return mpz_cmp(z, w); }
static int eval(mpz_srcptr z, unsigned long int l)
{ return mpz_cmp_ui(z, l); }
static int eval(unsigned long int l, mpz_srcptr z)
{ return -mpz_cmp_ui(z, l); }
static int eval(mpz_srcptr z, signed long int l)
{ return mpz_cmp_si(z, l); }
static int eval(signed long int l, mpz_srcptr z)
{ return -mpz_cmp_si(z, l); }
static int eval(mpz_srcptr z, double d)
{ return mpz_cmp_d(z, d); }
static int eval(double d, mpz_srcptr z)
{ return -mpz_cmp_d(z, d); }
static int eval(mpq_srcptr q, mpq_srcptr r) { return mpq_cmp(q, r); }
static int eval(mpq_srcptr q, unsigned long int l)
{ return mpq_cmp_ui(q, l, 1); }
static int eval(unsigned long int l, mpq_srcptr q)
{ return -mpq_cmp_ui(q, l, 1); }
static int eval(mpq_srcptr q, signed long int l)
{ return mpq_cmp_si(q, l, 1); }
static int eval(signed long int l, mpq_srcptr q)
{ return -mpq_cmp_si(q, l, 1); }
static int eval(mpq_srcptr q, double d)
{ __GMPXX_TMPQ_D; return mpq_cmp (q, temp); }
static int eval(double d, mpq_srcptr q)
{ __GMPXX_TMPQ_D; return mpq_cmp (temp, q); }
static int eval(mpq_srcptr q, mpz_srcptr z)
{ return mpq_cmp_z(q, z); }
static int eval(mpz_srcptr z, mpq_srcptr q)
{ return -mpq_cmp_z(q, z); }
static int eval(mpf_srcptr f, mpf_srcptr g) { return mpf_cmp(f, g); }
static int eval(mpf_srcptr f, unsigned long int l)
{ return mpf_cmp_ui(f, l); }
static int eval(unsigned long int l, mpf_srcptr f)
{ return -mpf_cmp_ui(f, l); }
static int eval(mpf_srcptr f, signed long int l)
{ return mpf_cmp_si(f, l); }
static int eval(signed long int l, mpf_srcptr f)
{ return -mpf_cmp_si(f, l); }
static int eval(mpf_srcptr f, double d)
{ return mpf_cmp_d(f, d); }
static int eval(double d, mpf_srcptr f)
{ return -mpf_cmp_d(f, d); }
static int eval(mpf_srcptr f, mpz_srcptr z)
{ return mpf_cmp_z(f, z); }
static int eval(mpz_srcptr z, mpf_srcptr f)
{ return -mpf_cmp_z(f, z); }
static int eval(mpf_srcptr f, mpq_srcptr q)
{
mpf_t qf;
mpf_init(qf); /* Should we use the precision of f? */
mpf_set_q(qf, q);
int ret = eval(f, qf);
mpf_clear(qf);
return ret;
}
static int eval(mpq_srcptr q, mpf_srcptr f)
{ return -eval(f, q); }
};
struct __gmp_binary_equal
{
static bool eval(mpz_srcptr z, mpz_srcptr w) { return mpz_cmp(z, w) == 0; }
static bool eval(mpz_srcptr z, unsigned long int l)
{ return mpz_cmp_ui(z, l) == 0; }
static bool eval(unsigned long int l, mpz_srcptr z)
{ return eval(z, l); }
static bool eval(mpz_srcptr z, signed long int l)
{ return mpz_cmp_si(z, l) == 0; }
static bool eval(signed long int l, mpz_srcptr z)
{ return eval(z, l); }
static bool eval(mpz_srcptr z, double d)
{ return mpz_cmp_d(z, d) == 0; }
static bool eval(double d, mpz_srcptr z)
{ return eval(z, d); }
static bool eval(mpq_srcptr q, mpq_srcptr r)
{ return mpq_equal(q, r) != 0; }
static bool eval(mpq_srcptr q, unsigned long int l)
{ return mpq_cmp_ui(q, l, 1) == 0; }
static bool eval(unsigned long int l, mpq_srcptr q)
{ return eval(q, l); }
static bool eval(mpq_srcptr q, signed long int l)
{ return mpq_cmp_si(q, l, 1) == 0; }
static bool eval(signed long int l, mpq_srcptr q)
{ return eval(q, l); }
static bool eval(mpq_srcptr q, double d)
{ __GMPXX_TMPQ_D; return mpq_equal (q, temp) != 0; }
static bool eval(double d, mpq_srcptr q)
{ return eval(q, d); }
static bool eval(mpq_srcptr q, mpz_srcptr z)
{ return mpq_cmp_z(q, z) == 0; }
static bool eval(mpz_srcptr z, mpq_srcptr q)
{ return eval(q, z); }
static bool eval(mpf_srcptr f, mpf_srcptr g) { return mpf_cmp(f, g) == 0; }
static bool eval(mpf_srcptr f, unsigned long int l)
{ return mpf_cmp_ui(f, l) == 0; }
static bool eval(unsigned long int l, mpf_srcptr f)
{ return eval(f, l); }
static bool eval(mpf_srcptr f, signed long int l)
{ return mpf_cmp_si(f, l) == 0; }
static bool eval(signed long int l, mpf_srcptr f)
{ return eval(f, l); }
static bool eval(mpf_srcptr f, double d)
{ return mpf_cmp_d(f, d) == 0; }
static bool eval(double d, mpf_srcptr f)
{ return eval(f, d); }
static bool eval(mpf_srcptr f, mpz_srcptr z)
{ return mpf_cmp_z(f, z) == 0; }
static bool eval(mpz_srcptr z, mpf_srcptr f)
{ return eval(f, z); }
static bool eval(mpf_srcptr f, mpq_srcptr q)
{ return __gmp_cmp_function::eval(f, q) == 0; }
static bool eval(mpq_srcptr q, mpf_srcptr f)
{ return eval(f, q); }
};
struct __gmp_binary_less
{
static bool eval(mpz_srcptr z, mpz_srcptr w) { return mpz_cmp(z, w) < 0; }
static bool eval(mpz_srcptr z, unsigned long int l)
{ return mpz_cmp_ui(z, l) < 0; }
static bool eval(unsigned long int l, mpz_srcptr z)
{ return mpz_cmp_ui(z, l) > 0; }
static bool eval(mpz_srcptr z, signed long int l)
{ return mpz_cmp_si(z, l) < 0; }
static bool eval(signed long int l, mpz_srcptr z)
{ return mpz_cmp_si(z, l) > 0; }