-
Notifications
You must be signed in to change notification settings - Fork 1
/
GMPz.xs
executable file
·7906 lines (6521 loc) · 192 KB
/
GMPz.xs
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
#ifdef __MINGW32__
#ifndef __USE_MINGW_ANSI_STDIO
#define __USE_MINGW_ANSI_STDIO 1
#endif
#endif
#define PERL_NO_GET_CONTEXT 1
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "math_gmpz_include.h"
#include "math_gmpz_unused.h"
SV * MATH_GMPz_IV_MAX(pTHX) {
return newSViv((IV)IV_MAX);
}
SV * MATH_GMPz_IV_MIN(pTHX) {
return newSViv((IV)IV_MIN);
}
SV * MATH_GMPz_UV_MAX(pTHX) {
return newSVuv((UV)UV_MAX);
}
int _is_infstring(char * s) {
int sign = 1;
if(s[0] == '-') {
sign = -1;
s++;
}
else {
if(s[0] == '+') s++;
}
if((s[0] == 'i' || s[0] == 'I') && (s[1] == 'n' || s[1] == 'N') && (s[2] == 'f' || s[2] == 'F'))
return sign;
#ifdef _WIN32_BIZARRE_INFNAN /* older Win32 perls stringify infinities as(-)1.#INF */
if(!strcmp(s, "1.#INF")) return sign;
#endif
return 0;
}
SV * Rmpz_init_set_str_nobless(pTHX_ SV * num, SV * base) {
mpz_t * mpz_t_obj;
int b = (int)SvUV(base);
SV * obj_ref, * obj;
if(b == 1 || b > 62) croak("Second argument supplied to Rmpz_init_set_str_nobless is not in acceptable range");
New(1, mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_create function");
if(mpz_init_set_str (*mpz_t_obj, SvPV_nolen(num), b))
croak("First argument supplied to Rmpz_create_init_nobless is not a valid base %u integer", b);
obj_ref = newSV(0);
obj = newSVrv(obj_ref, NULL);
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
SvREADONLY_on(obj);
return obj_ref;
}
SV * Rmpz_init2_nobless(pTHX_ SV * bits) {
mpz_t * mpz_t_obj;
SV * obj_ref, * obj;
New(1, mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init2_nobless function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, NULL);
mpz_init2 (*mpz_t_obj, (mp_bitcnt_t)SvUV(bits));
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
SvREADONLY_on(obj);
return obj_ref;
}
SV * Rmpz_init_nobless(pTHX) {
mpz_t * mpz_t_obj;
SV * obj_ref, * obj;
New(1, mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init_nobless function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, NULL);
mpz_init(*mpz_t_obj);
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
SvREADONLY_on(obj);
return obj_ref;
}
SV * Rmpz_init_set_nobless(pTHX_ mpz_t * p) {
mpz_t * mpz_t_obj;
SV * obj_ref, * obj;
New(1, mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init_set_nobless function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, NULL);
mpz_init_set(*mpz_t_obj, *p);
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
SvREADONLY_on(obj);
return obj_ref;
}
SV * Rmpz_init_set_ui_nobless(pTHX_ SV * p) {
mpz_t * mpz_t_obj;
SV * obj_ref, * obj;
New(1, mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init_set_ui_nobless function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, NULL);
mpz_init_set_ui(*mpz_t_obj, (unsigned long)SvUV(p));
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
SvREADONLY_on(obj);
return obj_ref;
}
SV * Rmpz_init_set_si_nobless(pTHX_ SV * p) {
mpz_t * mpz_t_obj;
SV * obj_ref, * obj;
New(1, mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init_set_si_nobless function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, NULL);
mpz_init_set_si(*mpz_t_obj, (long)SvIV(p));
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
SvREADONLY_on(obj);
return obj_ref;
}
SV * Rmpz_init_set_d_nobless(pTHX_ SV * p) {
mpz_t * mpz_t_obj;
SV * obj_ref, * obj;
double d = SvNV(p);
if(d != d) croak("In Rmpz_set_d, cannot coerce a NaN to a Math::GMPz value");
if(d != 0 && (d / d != 1))
croak("In Rmpz_init_set_d_nobless, cannot coerce an Inf to a Math::GMPz value");
New(1, mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init_set_d_nobless function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, NULL);
mpz_init_set_d(*mpz_t_obj, d);
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
SvREADONLY_on(obj);
return obj_ref;
}
SV * Rmpz_init(pTHX) {
mpz_t * mpz_t_obj;
SV * obj_ref, * obj;
New(1, mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, "Math::GMPz");
mpz_init(*mpz_t_obj);
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
SvREADONLY_on(obj);
return obj_ref;
}
SV * Rmpz_init_set(pTHX_ mpz_t * p) {
mpz_t * mpz_t_obj;
SV * obj_ref, * obj;
New(1, mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init_set function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, "Math::GMPz");
mpz_init_set(*mpz_t_obj, *p);
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
SvREADONLY_on(obj);
return obj_ref;
}
SV * Rmpz_init_set_ui(pTHX_ SV * p) {
mpz_t * mpz_t_obj;
SV * obj_ref, * obj;
New(1, mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init_set_ui function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, "Math::GMPz");
mpz_init_set_ui(*mpz_t_obj, (unsigned long)SvUV(p));
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
SvREADONLY_on(obj);
return obj_ref;
}
SV * Rmpz_init_set_si(pTHX_ SV * p) {
mpz_t * mpz_t_obj;
SV * obj_ref, * obj;
New(1, mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init_set_si function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, "Math::GMPz");
mpz_init_set_si(*mpz_t_obj, (long)SvIV(p));
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
SvREADONLY_on(obj);
return obj_ref;
}
void Rmpz_set_uj(mpz_t * copy, UV original) {
#ifdef MATH_GMPZ_NEED_LONG_LONG_INT
mpz_set_ui(*copy, original >> 32);
if(mpz_cmp_ui(*copy, 0))
mpz_mul_2exp(*copy, *copy, 32);
mpz_add_ui(*copy, *copy, original & 4294967295UL);
#else
PERL_UNUSED_ARG2(copy, original);
croak("Rmpz_set_uj function not implemented on this build of perl");
#endif
}
void Rmpz_set_sj(mpz_t * copy, IV original) {
#ifdef MATH_GMPZ_NEED_LONG_LONG_INT
if(original >= 0) Rmpz_set_uj(copy, original);
else {
Rmpz_set_uj(copy, -original);
mpz_neg(*copy, *copy);
}
#else
PERL_UNUSED_ARG2(copy, original);
croak("Rmpz_set_sj function not implemented on this build of perl");
#endif
}
/* also handles UV values */
void Rmpz_set_IV(pTHX_ mpz_t * copy, SV * original) {
if(SV_IS_IOK(original)) {
#ifndef MATH_GMPZ_NEED_LONG_LONG_INT
if(SvUOK(original)) mpz_set_ui(*copy, SvUVX(original));
else mpz_set_si(*copy, SvIVX(original));
}
#else
if(SvUOK(original)) Rmpz_set_uj(copy, SvUVX(original));
else Rmpz_set_sj(copy, SvIVX(original));
}
#endif
else croak("Arg provided to Rmpz_set_IV is not an IV");
}
/* also handles UV values */
SV * Rmpz_init_set_IV(pTHX_ SV * p) {
mpz_t * mpz_t_obj;
SV * obj_ref, * obj;
New(1, mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init_set_IV function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, "Math::GMPz");
mpz_init(*mpz_t_obj);
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
Rmpz_set_IV(aTHX_ mpz_t_obj, p);
SvREADONLY_on(obj);
return obj_ref;
}
/* also handles UV values */
SV * Rmpz_init_set_IV_nobless(pTHX_ SV * p) {
mpz_t * mpz_t_obj;
SV * obj_ref, * obj;
New(1, mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init_set_IV_nobless function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, "NULL");
mpz_init(*mpz_t_obj);
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
Rmpz_set_IV(aTHX_ mpz_t_obj, p);
SvREADONLY_on(obj);
return obj_ref;
}
/* also handles UV values */
SV * _Rmpz_get_IV(pTHX_ mpz_t * n) {
#ifdef MATH_GMPZ_NEED_LONG_LONG_INT
int negative = 0;
char * out;
SV * outsv;
mpz_t temp;
mpz_t _uv_max;
mpz_t _iv_max;
mpz_t _iv_min;
#endif
if(mpz_fits_slong_p(*n))
return newSViv(mpz_get_si(*n));
if(mpz_fits_ulong_p(*n))
return newSVuv(mpz_get_ui(*n));
#ifndef MATH_GMPZ_NEED_LONG_LONG_INT
if(mpz_cmp_ui(*n, 0) > 0)
return newSVuv(mpz_get_ui(*n));
return newSViv(mpz_get_si(*n));
#else
if(mpz_sgn(*n) < 0) negative = 1;
Newxz(out, 24, char);
if(out == NULL)
croak("Failed to allocate memory in Rmpz_get_IV function");
if(negative) { /* must be less than LONG_MIN */
mpz_init_set_str(_iv_min, SvPV_nolen(MATH_GMPz_IV_MIN(aTHX)), 10);
if(mpz_cmp(*n, _iv_min) < 0) { /* must be less than IV_MIN */
mpz_clear(_iv_min);
mpz_init(temp);
mpz_init_set_str(_iv_max, SvPV_nolen(MATH_GMPz_IV_MAX(aTHX)), 10);
mpz_abs(temp, *n);
mpz_and(temp, temp, _iv_max);
mpz_clear(_iv_max);
mpz_neg(temp, temp);
mpz_get_str(out, 10, temp);
mpz_clear(temp);
outsv = newSVpv(out, 0);
Safefree(out);
return outsv;
}
else { /* must fit into an IV */
mpz_clear(_iv_min);
mpz_get_str(out, 10, *n);
outsv = newSVpv(out, 0);
Safefree(out);
return outsv;
}
}
else { /* it's +ve */
mpz_init_set_str(_uv_max, SvPV_nolen(MATH_GMPz_UV_MAX(aTHX)), 10);
if(mpz_cmp(*n, _uv_max) > 0) { /* needs to be truncated */
mpz_init_set(temp, *n);
mpz_and(temp, temp, _uv_max);
mpz_clear(_uv_max);
mpz_get_str(out, 10, temp);
mpz_clear(temp);
outsv = newSVpv(out, 0);
Safefree(out);
return outsv;
}
else { /* must fit into a UV */
mpz_clear(_uv_max);
mpz_get_str(out, 10, *n);
outsv = newSVpv(out, 0);
Safefree(out);
return outsv;
}
}
#endif
}
/* also handles UVs */
int Rmpz_fits_IV_p(pTHX_ mpz_t * n) {
#ifndef MATH_GMPZ_NEED_LONG_LONG_INT
if(mpz_fits_slong_p(*n) || mpz_fits_ulong_p(*n)) return 1;
return 0;
#else
mpz_t t;
if(mpz_fits_slong_p(*n) || mpz_fits_ulong_p(*n)) return 1;
mpz_init_set_str(t, SvPV_nolen(MATH_GMPz_UV_MAX(aTHX)), 10);
if(mpz_cmp(*n, t) > 0) {
mpz_clear(t);
return 0;
}
mpz_set_str(t, SvPV_nolen(MATH_GMPz_IV_MIN(aTHX)), 10);
if(mpz_cmp(*n, t) < 0) {
mpz_clear(t);
return 0;
}
mpz_clear(t);
return 1;
#endif
}
double Rmpz_get_d(mpz_t * n) {
return mpz_get_d(*n);
}
NV Rmpz_get_NV(mpz_t * n) {
#if NVSIZE == 8
return mpz_get_d(*n);
#else
/* Rely on strtold/strtoflt128 to correctly handle *
* the stringified form of the mpz_t argument */
NV d;
char * out;
Newxz(out, mpz_sizeinbase(*n, 10) + 3, char);
if(out == NULL) croak("Failed to allocate memory in Rmpz_get_NV function");
mpz_get_str(out, 10, *n);
# if defined(USE_LONG_DOUBLE)
d = strtold(out, NULL);
# elif defined(USE_QUADMATH)
d = strtoflt128(out, NULL);
# else
Safefree(out);
croak("Unrecognized nvtype");
# endif
Safefree(out);
return d;
#endif
}
int Rmpz_cmp_uj(mpz_t * a, UV b) {
#if defined(MATH_GMPZ_NEED_LONG_LONG_INT)
int ret;
mpz_t temp;
mpz_init2(temp, 64);
Rmpz_set_uj(&temp, b);
ret = mpz_cmp(*a, temp);
mpz_clear(temp);
return ret;
#else
PERL_UNUSED_ARG2(a, b);
croak("Rmpz_cmp_uj is unavailable because MATH_GMPZ_NEED_LONG_LONG_INT is not defined");
#endif
}
int Rmpz_cmp_sj(mpz_t * a, IV b) {
#if defined(MATH_GMPZ_NEED_LONG_LONG_INT)
int ret;
mpz_t temp;
mpz_init2(temp, 64);
Rmpz_set_sj(&temp, b);
ret = mpz_cmp(*a, temp);
mpz_clear(temp);
return ret;
#else
PERL_UNUSED_ARG2(a, b);
croak("Rmpz_cmp_sj is unavailable because MATH_GMPZ_NEED_LONG_LONG_INT is not defined");
#endif
}
/* also handles UV values */
int Rmpz_cmp_IV(pTHX_ mpz_t *a, SV * b) {
if(!SV_IS_IOK(b))
croak("Arg provided to Rmpz_cmp_IV is not an IV");
#if defined(MATH_GMPZ_NEED_LONG_LONG_INT)
if(SvUOK(b)) {
return Rmpz_cmp_uj(a, SvUV(b));
}
return Rmpz_cmp_sj(a, SvIV(b));
#else
if(SvUOK(b)) {
return mpz_cmp_ui(*a, SvUV(b));
}
return mpz_cmp_si(*a, SvIV(b));
#endif
}
SV * Rmpz_init_set_d(pTHX_ SV * p) {
mpz_t * mpz_t_obj;
SV * obj_ref, * obj;
double d = SvNV(p);
if(d != d) croak("In Rmpz_init_set_d, cannot coerce a NaN to a Math::GMPz value");
if(d != 0 && (d / d != 1))
croak("In Rmpz_init_set_d, cannot coerce an Inf to a Math::GMPz value");
New(1, mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init_set_d function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, "Math::GMPz");
mpz_init_set_d(*mpz_t_obj, d);
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
SvREADONLY_on(obj);
return obj_ref;
}
void _mpf_set_dd(mpf_t * q, SV * p) {
#if defined(NV_IS_DOUBLEDOUBLE) /* double-double */
double msd; /* Most Significant Double */
mpf_t t, d;
long double lsd; /* Will be assigned the Least Siginficant Double */
msd = (double)SvNV(p);
if(msd != 0.0) {
if(msd != msd) croak("In _Rmpf_set_ld, cannot coerce a NaN to a Math::GMPf object");
if(msd / msd != 1.0) croak("In _Rmpf_set_ld, cannot coerce an Inf to a Math::GMPf object");
}
lsd = SvNV(p) - (long double)msd;
mpf_init2(t, 2098);
mpf_init2(d, 53);
mpf_set_d(t, msd);
mpf_set_d(d, (double)lsd);
mpf_add(t, t, d);
mpf_clear(d);
mpf_set(*q, t);
mpf_clear(t);
#else
PERL_UNUSED_ARG2(q, p);
croak("_mpf_set_dd is unavailable because NV is not DoubleDouble");
#endif
}
void Rmpz_set_NV(pTHX_ mpz_t * copy, SV * original) {
#if defined(LD_PRINTF_BROKEN)
/* use quadmath_snprintf() instead of broken printf() */
__float128 nv;
#else
NV nv;
#endif
#if defined(USE_QUADMATH) || defined(USE_LONG_DOUBLE)
# if defined(NV_IS_DOUBLEDOUBLE)
mpf_t f;
# elif defined(LD_PRINTF_BROKEN)
__float128 buffer_size;
int returned;
char * buffer;
# else
NV buffer_size;
int returned;
char * buffer;
# endif
#endif
if(!SV_IS_NOK(original))
croak("In Rmpz_set_NV, 2nd argument is not an NV");
nv = SvNV(original); /* First check that the NV flag is set *
* else flags of "original" might change */
if(nv != nv) croak("In Rmpz_set_NV, cannot coerce a NaN to a Math::GMPz value");
if(nv != 0 && (nv / nv != 1))
croak("In Rmpz_set_NV, cannot coerce an Inf to a Math::GMPz value");
#if defined(USE_QUADMATH) || defined(LD_PRINTF_BROKEN)
buffer_size = nv < 0.0Q ? nv * -1.0Q : nv;
buffer_size = ceilq(logq(buffer_size + 1) / 2.30258509299404568401799145468436418Q);
Newxz(buffer, (int)buffer_size + 5, char);
nv = nv >= 0 ? floorq(nv) : ceilq(nv);
returned = quadmath_snprintf(buffer, (size_t)buffer_size + 5, "%.0Qf", nv);
if(returned < 0) croak("In Rmpz_set_NV, encoding error in quadmath_snprintf function");
if(returned >= buffer_size + 5) croak("In Rmpz_set_NV, buffer given to quadmath_snprintf function was too small");
mpz_set_str(*copy, buffer, 10);
Safefree(buffer);
#elif defined(USE_LONG_DOUBLE)
PERL_UNUSED_VAR(returned);
# if defined(NV_IS_DOUBLEDOUBLE)
mpf_init2(f, 2098);
_mpf_set_dd(&f, original);
mpz_set_f(*copy, f);
mpf_clear(f);
# else
buffer_size = nv < 0.0L ? nv * -1.0L : nv;
buffer_size = ceill(logl(buffer_size + 1) / 2.30258509299404568401799145468436418L);
Newxz(buffer, buffer_size + 5, char);
nv = nv >= 0 ? floorl(nv) : ceill(nv);
if(sprintf(buffer, "%.0Lf", nv) >= (int)buffer_size + 5) croak("In Rmpz_set_NV, buffer overflow in sprintf function");
mpz_set_str(*copy, buffer, 10);
Safefree(buffer);
# endif
#else
mpz_set_d(*copy, nv);
#endif
}
SV * Rmpz_init_set_NV(pTHX_ SV * p) {
mpz_t * mpz_t_obj;
SV * obj_ref, * obj;
Newx(mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init_set_NV function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, "Math::GMPz");
mpz_init(*mpz_t_obj);
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
Rmpz_set_NV(aTHX_ mpz_t_obj, p);
SvREADONLY_on(obj);
return obj_ref;
}
SV * Rmpz_init_set_NV_nobless(pTHX_ SV * p) {
mpz_t * mpz_t_obj;
SV * obj_ref, * obj;
Newx(mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init_set_NV_nobless function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, "NULL");
mpz_init(*mpz_t_obj);
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
Rmpz_set_NV(aTHX_ mpz_t_obj, p);
SvREADONLY_on(obj);
return obj_ref;
}
SV * Rmpz_init_set_str(pTHX_ SV * num, SV * base) {
mpz_t * mpz_t_obj;
int b = (int)SvUV(base);
SV * obj_ref, * obj;
if(b == 1 || b > 62) croak("Second argument supplied to Rmpz_init_set_str is not in acceptable range");
New(1, mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init_set_str function");
if(mpz_init_set_str (*mpz_t_obj, SvPV_nolen(num), b))
croak("First argument supplied to Rmpz_init_set_str is not a valid base %u integer", b);
obj_ref = newSV(0);
obj = newSVrv(obj_ref, "Math::GMPz");
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
SvREADONLY_on(obj);
return obj_ref;
}
SV * Rmpz_init2(pTHX_ SV * bits) {
mpz_t * mpz_t_obj;
SV * obj_ref, * obj;
New(1, mpz_t_obj, 1, mpz_t);
if(mpz_t_obj == NULL) croak("Failed to allocate memory in Rmpz_init2 function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, "Math::GMPz");
mpz_init2 (*mpz_t_obj, (mp_bitcnt_t)SvUV(bits));
sv_setiv(obj, INT2PTR(IV, mpz_t_obj));
SvREADONLY_on(obj);
return obj_ref;
}
SV * Rmpz_get_str(pTHX_ mpz_t * p, SV * base) {
char * out;
SV * outsv;
int c = (int)SvIV(base), b = (int)SvIV(base);
if((b > -2 && b < 2) || b < -36 || b > 62) croak("Second argument supplied to Rmpz_get_str is not in acceptable range");
if(c < 0) c *= -1;
New(2, out, mpz_sizeinbase(*p, c) + 5, char);
if(out == NULL) croak("Failed to allocate memory in Rmpz_deref function");
mpz_get_str(out, b, *p);
outsv = newSVpv(out, 0);
Safefree(out);
return outsv;
}
void DESTROY(pTHX_ mpz_t * p) {
/* printf("Destroying mpz "); */
mpz_clear(*p);
Safefree(p);
/* printf("...destroyed\n"); */
}
void Rmpz_clear(pTHX_ mpz_t * p) {
mpz_clear(*p);
Safefree(p);
}
void Rmpz_clear_mpz(mpz_t * p) {
mpz_clear(*p);
}
void Rmpz_clear_ptr(pTHX_ mpz_t * p) {
Safefree(p);
}
void Rmpz_realloc2(pTHX_ mpz_t * integer, SV * bits){
mpz_realloc2(*integer, (mp_bitcnt_t)SvUV(bits));
}
void Rmpz_set(mpz_t * copy, mpz_t * original) {
mpz_set(*copy, *original);
}
void Rmpz_set_q(mpz_t * copy, mpq_t * original) {
mpz_set_q(*copy, *original);
}
void Rmpz_set_f(mpz_t * copy, mpf_t * original) {
mpz_set_f(*copy, *original);
}
void Rmpz_set_si(mpz_t * copy, long original) {
mpz_set_si(*copy, original);
}
void Rmpz_set_ui(mpz_t * copy, unsigned long original) {
mpz_set_ui(*copy, original);
}
void Rmpz_set_d(mpz_t * copy, double d) {
if(d != d) croak("In Rmpz_set_d, cannot coerce a NaN to a Math::GMPz value");
if(d != 0 && (d / d != 1))
croak("In Rmpz_set_d, cannot coerce an Inf to a Math::GMPz value");
mpz_set_d(*copy, d);
}
void Rmpz_set_str(pTHX_ mpz_t * copy, SV * original, int base) {
if(base == 1 || base > 62) croak("Second argument supplied to Rmpz_set_str is not in acceptable range");
if(mpz_set_str(*copy, SvPV_nolen(original), base))
croak("Second argument supplied to Rmpz_set_str is not a valid base %u integer", base);
}
void Rmpz_swap(mpz_t * a, mpz_t * b) {
mpz_swap(*a, *b);
}
unsigned long Rmpz_get_ui(mpz_t * n) {
return mpz_get_ui(*n);
}
long Rmpz_get_si(mpz_t * n) {
return mpz_get_si(*n);
}
void Rmpz_get_d_2exp(pTHX_ mpz_t * n) {
dXSARGS;
double d;
long exp;
PERL_UNUSED_ARG(items);
d = mpz_get_d_2exp(&exp, *n);
ST(0) = sv_2mortal(newSVnv(d));
ST(1) = sv_2mortal(newSVuv(exp));
XSRETURN(2);
}
SV * Rmpz_getlimbn(pTHX_ mpz_t * p, SV * n) {
return newSVuv(mpz_getlimbn(*p, (mp_size_t)SvUV(n)));
}
void Rmpz_add(mpz_t * dest, mpz_t * src1, mpz_t * src2) {
mpz_add(*dest, *src1, *src2 );
}
void Rmpz_add_ui(mpz_t * dest, mpz_t * src, unsigned long num) {
mpz_add_ui(*dest, *src, num);
/* return sv_setref_pv(newSViv(0), Nullch, INT2PTR(mpz_t *, SvIVX(SvRV(dest)))); */
}
void Rmpz_sub(mpz_t * dest, mpz_t * src1, mpz_t * src2) {
mpz_sub(*dest, *src1, *src2 );
}
void Rmpz_sub_ui(mpz_t * dest, mpz_t * src, unsigned long num) {
mpz_sub_ui(*dest, *src, num);
}
void Rmpz_ui_sub(mpz_t * dest, unsigned long num, mpz_t * src) {
mpz_ui_sub(*dest, num, *src);
}
void Rmpz_mul(mpz_t * dest, mpz_t * src1, mpz_t * src2) {
mpz_mul(*dest, *src1, *src2 );
}
void Rmpz_mul_si(mpz_t * dest, mpz_t * src, long num) {
mpz_mul_si(*dest, *src, num);
}
void Rmpz_mul_ui(mpz_t * dest, mpz_t * src, unsigned long num) {
mpz_mul_ui(*dest, *src, num);
}
void Rmpz_addmul(mpz_t * dest, mpz_t * src1, mpz_t * src2) {
mpz_addmul(*dest, *src1, *src2 );
}
void Rmpz_addmul_ui(mpz_t * dest, mpz_t * src, unsigned long num) {
mpz_addmul_ui(*dest, *src, num);
}
void Rmpz_submul(mpz_t * dest, mpz_t * src1, mpz_t * src2) {
mpz_submul(*dest, *src1, *src2 );
}
void Rmpz_submul_ui(mpz_t * dest, mpz_t * src, unsigned long num) {
mpz_submul_ui(*dest, *src, num);
}
void Rmpz_mul_2exp(pTHX_ mpz_t * dest, mpz_t * src1, SV * b) {
CHECK_MP_BITCNT_T_OVERFLOW(b)
mpz_mul_2exp(*dest, *src1, (mp_bitcnt_t)SvUVX(b));
}
void Rmpz_div_2exp(pTHX_ mpz_t * dest, mpz_t * src1, SV * b) {
CHECK_MP_BITCNT_T_OVERFLOW(b)
mpz_div_2exp(*dest, *src1, (mp_bitcnt_t)SvUVX(b));
}
void Rmpz_neg(mpz_t * dest, mpz_t * src) {
mpz_neg(*dest, *src );
}
void Rmpz_abs(mpz_t * dest, mpz_t * src) {
mpz_abs(*dest, *src );
}
void Rmpz_cdiv_q( mpz_t * q, mpz_t * n, mpz_t * d) {
if(!mpz_cmp_ui(*d, 0))
croak("Division by 0 not allowed in Rmpz_cdiv_q");
mpz_cdiv_q(*q, *n, *d);
}
void Rmpz_cdiv_r( mpz_t * mod, mpz_t * n, mpz_t * d) {
if(!mpz_cmp_ui(*d, 0))
croak("Division by 0 not allowed in Rmpz_cdiv_r");
mpz_cdiv_r(*mod, *n, *d);
}
void Rmpz_cdiv_qr( mpz_t * q, mpz_t * r, mpz_t * n, mpz_t * d) {
if(!mpz_cmp_ui(*d, 0))
croak("Division by 0 not allowed in Rmpz_cdiv_qr");
mpz_cdiv_qr(*q, *r, *n, *d);
}
unsigned long Rmpz_cdiv_q_ui( mpz_t * q, mpz_t * n, unsigned long d) {
if(d == 0)
croak("Division by 0 not allowed in Rmpz_cdiv_q_ui");
return mpz_cdiv_q_ui(*q, *n, d);
}
unsigned long Rmpz_cdiv_r_ui( mpz_t * q, mpz_t * n, unsigned long d) {
if(d == 0)
croak("Division by 0 not allowed in Rmpz_cdiv_r_ui");
return mpz_cdiv_r_ui(*q, *n, d);
}
unsigned long Rmpz_cdiv_qr_ui( mpz_t * q, mpz_t * r, mpz_t * n, unsigned long d) {
if(d == 0)
croak("Division by 0 not allowed in Rmpz_cdiv_qr_ui");
return mpz_cdiv_qr_ui(*q, *r, *n, d);
}
unsigned long Rmpz_cdiv_ui( mpz_t * n, unsigned long d) {
if(d == 0)
croak("Division by 0 not allowed in Rmpz_cdiv_ui");
return mpz_cdiv_ui(*n, d);
}
void Rmpz_cdiv_q_2exp(pTHX_ mpz_t * q, mpz_t * n, SV * b) {
CHECK_MP_BITCNT_T_OVERFLOW(b)
mpz_cdiv_q_2exp(*q, *n, (mp_bitcnt_t)SvUVX(b));
}
void Rmpz_cdiv_r_2exp(pTHX_ mpz_t * r, mpz_t * n, SV * b) {
CHECK_MP_BITCNT_T_OVERFLOW(b)
mpz_cdiv_r_2exp(*r, *n, (mp_bitcnt_t)SvUVX(b));
}
void Rmpz_fdiv_q( mpz_t * q, mpz_t * n, mpz_t * d) {
if(!mpz_cmp_ui(*d, 0))
croak("Division by 0 not allowed in Rmpz_fdiv_q");
mpz_fdiv_q(*q, *n, *d);
}
void Rmpz_div( mpz_t * q, mpz_t * n, mpz_t * d) {
if(!mpz_cmp_ui(*d, 0))
croak("Division by 0 not allowed in Rmpz_div");
mpz_div(*q, *n, *d);
}
/* % mpz-t (modulus) operator */
void Rmpz_fdiv_r( mpz_t * mod, mpz_t * n, mpz_t * d) {
if(!mpz_cmp_ui(*d, 0))
croak("Division by 0 not allowed in Rmpz_fdiv_r");
mpz_fdiv_r(*mod, *n, *d);
}
void Rmpz_fdiv_qr( mpz_t * q, mpz_t * r, mpz_t * n, mpz_t * d) {
if(!mpz_cmp_ui(*d, 0))
croak("Division by 0 not allowed in Rmpz_fdiv_qr");
mpz_fdiv_qr(*q, *r, *n, *d);
}
void Rmpz_divmod( mpz_t * q, mpz_t * r, mpz_t * n, mpz_t * d) {
if(!mpz_cmp_ui(*d, 0))
croak("Division by 0 not allowed in Rmpz_divmod");
mpz_divmod(*q, *r, *n, *d);
}
unsigned long Rmpz_fdiv_q_ui( mpz_t * q, mpz_t * n, unsigned long d) {
if(d == 0)
croak("Division by 0 not allowed in Rmpz_fdiv_q_ui");
return mpz_fdiv_q_ui(*q, *n, d);
}
unsigned long Rmpz_div_ui( mpz_t * q, mpz_t * n, unsigned long d) {
if(d == 0)
croak("Division by 0 not allowed in Rmpz_div_ui");
return mpz_div_ui(*q, *n, d);
}
unsigned long Rmpz_fdiv_r_ui( mpz_t * q, mpz_t * n, unsigned long d) {
if(d == 0)
croak("Division by 0 not allowed in Rmpz_fdiv_r_ui");
return mpz_fdiv_r_ui(*q, *n, d);
}