-
Notifications
You must be signed in to change notification settings - Fork 0
/
GMPq.xs
executable file
·3526 lines (2910 loc) · 86.4 KB
/
GMPq.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_gmpq_include.h"
#include "math_gmpq_unused.h"
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;
}
void Rmpq_canonicalize (mpq_t * p) {
mpq_canonicalize(*p);
}
SV * Rmpq_init(pTHX) {
mpq_t * mpq_t_obj;
SV * obj_ref, * obj;
New(1, mpq_t_obj, 1, mpq_t);
if(mpq_t_obj == NULL) croak("Failed to allocate memory in Rmpq_init function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, "Math::GMPq");
mpq_init (*mpq_t_obj);
sv_setiv(obj, INT2PTR(IV, mpq_t_obj));
SvREADONLY_on(obj);
return obj_ref;
}
SV * Rmpq_init_nobless(pTHX) {
mpq_t * mpq_t_obj;
SV * obj_ref, * obj;
New(1, mpq_t_obj, 1, mpq_t);
if(mpq_t_obj == NULL) croak("Failed to allocate memory in Rmpq_init_nobless function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, NULL);
mpq_init (*mpq_t_obj);
sv_setiv(obj, INT2PTR(IV, mpq_t_obj));
SvREADONLY_on(obj);
return obj_ref;
}
void DESTROY(pTHX_ mpq_t * p) {
/* printf("Destroying mpq "); */
mpq_clear(*p);
Safefree(p);
/* printf("...destroyed\n"); */
}
void Rmpq_clear(pTHX_ mpq_t * p) {
mpq_clear(*p);
Safefree(p);
}
void Rmpq_clear_mpq(mpq_t * p) {
mpq_clear(*p);
}
void Rmpq_clear_ptr(pTHX_ mpq_t * p) {
Safefree(p);
}
void Rmpq_set(mpq_t * p1, mpq_t * p2) {
mpq_set(*p1, *p2);
}
void Rmpq_swap(mpq_t * p1, mpq_t * p2) {
mpq_swap(*p1, *p2);
}
void Rmpq_set_z(mpq_t * p1, mpz_t * p2) {
mpq_set_z(*p1, *p2);
}
void Rmpq_set_ui(mpq_t * p1, unsigned long p2, unsigned long p3) {
mpq_set_ui(*p1, p2, p3);
}
void Rmpq_set_si(mpq_t * p1, long p2, long p3) {
mpq_set_si(*p1, p2, p3);
}
void Rmpq_set_str(pTHX_ mpq_t * p1, SV * p2, SV * base) {
unsigned long b = (unsigned long)SvUV(base);
if(b == 1 || b > 62) croak ("%u is not a valid base in Rmpq_set_str", (int)b);
if(mpq_set_str(*p1, SvPV_nolen(p2), (int)SvIV(base)))
croak("String supplied to Rmpq_set_str function is not a valid base %u number", (int)SvUV(base));
}
double Rmpq_get_d(mpq_t * p) {
return mpq_get_d(*p);
}
void Rmpq_set_d(mpq_t * p, double d){
if(d != d) croak ("In Rmpq_set_d, cannot coerce a NaN to a Math::GMPq value");
if(d != 0 && d / d != 1) croak ("In Rmpq_set_d, cannot coerce an Inf to a Math::GMPq value");
mpq_set_d(*p, d);
}
void _mpf_set_doubledouble(mpf_t * q, SV * p) {
#if defined(USE_LONG_DOUBLE) && REQUIRED_LDBL_MANT_DIG == 2098
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 _mpf_set_doubledouble (called from Rmpq_set_NV), cannot coerce a NaN to a Math::GMPf object");
if(msd / msd != 1.0) croak("In _mpf_set_doubledouble (called from Rmpq_set_NV), 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_doubledouble not implemented because NV is not a doubledouble");
#endif
}
void Rmpq_set_NV(pTHX_ mpq_t * copy, SV * original) {
#if defined(USE_QUADMATH)
char * buffer;
int exp, exp2 = 0;
__float128 ld, buffer_size;
int returned;
if(!SV_IS_NOK(original))
croak("In Rmpq_set_NV, 2nd argument is not an NV");
ld = (__float128)SvNV(original);
if(ld != ld) croak("In Rmpq_set_NV, cannot coerce a NaN to a Math::GMPq value");
if(ld != 0 && (ld / ld != 1))
croak("In Rmpq_set_NV, cannot coerce an Inf to a Math::GMPq value");
ld = frexpq((__float128)SvNV(original), &exp);
while(ld != floorq(ld)) {
ld *= 2;
exp2 += 1;
}
buffer_size = ld < 0.0Q ? ld * -1.0Q : ld;
buffer_size = ceill(logq(buffer_size + 1) / 2.30258509299404568401799145468436418Q);
Newxz(buffer, (int)buffer_size + 5, char);
returned = quadmath_snprintf(buffer, (size_t)buffer_size + 5, "%.0Qf", ld);
if(returned < 0) croak("In Rmpq_set_NV, encoding error in quadmath_snprintf function");
if(returned >= buffer_size + 5) croak("In Rmpq_set_NV, buffer given to quadmath_snprintf function was too small");
mpq_set_str(*copy, buffer, 10);
Safefree(buffer);
if (exp2 > exp) mpq_div_2exp(*copy, *copy, exp2 - exp);
else mpq_mul_2exp(*copy, *copy, exp - exp2);
#elif defined(USE_LONG_DOUBLE)
# if REQUIRED_LDBL_MANT_DIG == 2098
mpf_t t;
if(!SV_IS_NOK(original))
croak("In Rmpq_set_NV, 2nd argument is not an NV");
mpf_init2(t, 2098);
_mpf_set_doubledouble(&t, original);
mpq_set_f(*copy, t);
mpf_clear(t);
# else
char * buffer;
int exp, exp2 = 0;
long double ld, buffer_size;
if(!SV_IS_NOK(original))
croak("In Rmpq_set_NV, 2nd argument is not an NV");
ld = (long double)SvNV(original);
if(ld != ld) croak("In Rmpq_set_NV, cannot coerce a NaN to a Math::GMPq value");
if(ld != 0 && (ld / ld != 1))
croak("In Rmpq_set_NV, cannot coerce an Inf to a Math::GMPq value");
ld = frexpl((long double)SvNV(original), &exp);
while(ld != floorl(ld)) {
ld *= 2;
exp2 += 1;
}
buffer_size = ld < 0.0L ? ld * -1.0L : ld;
buffer_size = ceill(logl(buffer_size + 1) / 2.30258509299404568401799145468436418L);
Newxz(buffer, (int)buffer_size + 5, char);
if(sprintf(buffer, "%.0Lf", ld) >= (int)buffer_size + 5) croak("In Rmpq_set_NV, buffer overflow in sprintf function");
mpq_set_str(*copy, buffer, 10);
Safefree(buffer);
if (exp2 > exp) mpq_div_2exp(*copy, *copy, exp2 - exp);
else mpq_mul_2exp(*copy, *copy, exp - exp2);
# endif
#else
double d;
if(!SV_IS_NOK(original))
croak("In Rmpq_set_NV, 2nd argument is not an NV");
d = SvNV(original);
if(d != d) croak("In Rmpq_set_NV, cannot coerce a NaN to a Math::GMPq value");
if(d != 0 && (d / d != 1))
croak("In Rmpq_set_NV, cannot coerce an Inf to a Math::GMPq value");
mpq_set_d(*copy, d);
#endif
}
int Rmpq_cmp_NV(pTHX_ mpq_t * a, SV * b) {
mpq_t t;
int returned;
if(!SvNOK(b))
croak("In Rmpq_cmp_NV, 2nd argument is not an NV");
#if defined(USE_QUADMATH)
char * buffer;
int exp, exp2 = 0;
__float128 ld, buffer_size;
ld = (__float128)SvNV(b);
if(ld != ld) croak("In Rmpq_cmp_NV, cannot compare a NaN to a Math::GMPq value");
if(ld != 0 && (ld / ld != 1)) {
if(ld > 0) return -1;
return 1;
}
ld = frexpq((__float128)SvNV(b), &exp);
while(ld != floorq(ld)) {
ld *= 2;
exp2 += 1;
}
buffer_size = ld < 0.0Q ? ld * -1.0Q : ld;
buffer_size = ceilq(logq(buffer_size + 1) / 2.30258509299404568401799145468436418Q);
Newxz(buffer, (int)buffer_size + 5, char);
returned = quadmath_snprintf(buffer, (size_t)buffer_size + 5, "%.0Qf", ld);
if(returned < 0) croak("In Rmpq_cmp_NV, encoding error in quadmath_snprintf function");
if(returned >= buffer_size + 5) croak("In Rmpq_cmp_NV, buffer given to quadmath_snprintf function was too small");
mpq_init(t);
mpq_set_str(t, buffer, 10);
Safefree(buffer);
if (exp2 > exp) mpq_div_2exp(t, t, exp2 - exp);
else mpq_mul_2exp(t, t, exp - exp2);
#elif defined(USE_LONG_DOUBLE)
# if REQUIRED_LDBL_MANT_DIG == 2098
mpf_t temp;
long double ld = (long double)SvNV(b);
if(ld != ld) croak("In Rmpq_cmp_NV, cannot compare a NaN to a Math::GMPq value");
if(ld != 0 && ld / ld != 1) {
if(ld > 0) return -1;
return 1;
}
mpq_init(t);
mpf_init2(temp, 2098);
_mpf_set_doubledouble(&temp, b);
mpq_set_f(t, temp);
mpf_clear(temp);
# else
char * buffer;
int exp, exp2 = 0;
long double ld, buffer_size;
ld = (long double)SvNV(b);
if(ld != ld) croak("In Rmpq_cmp_NV, cannot compare a NaN to a Math::GMPq value");
if(ld != 0 && (ld / ld != 1)) {
if(ld > 0) return -1;
return 1;
}
ld = frexpl((long double)SvNV(b), &exp);
while(ld != floorl(ld)) {
ld *= 2;
exp2 += 1;
}
buffer_size = ld < 0.0L ? ld * -1.0L : ld;
buffer_size = ceill(logl(buffer_size + 1) / 2.30258509299404568401799145468436418L);
Newxz(buffer, (int)buffer_size + 5, char);
if(sprintf(buffer, "%.0Lf", ld) >= (int)buffer_size + 5) croak("In Rmpq_cmp_NV, buffer overflow in sprintf function");
mpq_init(t);
mpq_set_str(t, buffer, 10);
Safefree(buffer);
if (exp2 > exp) mpq_div_2exp(t, t, exp2 - exp);
else mpq_mul_2exp(t, t, exp - exp2);
# endif
#else
double d = SvNV(b);
if(d != d) croak("In Rmpq_cmp_NV, cannot compare a NaN to a Math::GMPq value");
if(d != 0 && (d / d != 1)) {
if(d > 0) return -1;
return 1;
}
mpq_init(t);
mpq_set_d(t, d);
#endif
returned = mpq_cmp(*a, t);
mpq_clear(t);
return returned;
}
void Rmpq_set_f(mpq_t * p, mpf_t * f) {
mpq_set_f(*p, *f);
}
SV * Rmpq_get_str(pTHX_ mpq_t * p, SV * base){
char * out;
SV * outsv;
unsigned long b = (unsigned long)SvUV(base);
New(123, out, mpz_sizeinbase(mpq_numref(*p), b) + mpz_sizeinbase(mpq_denref(*p), b) + 3, char);
if(out == NULL) croak ("Failed to allocate memory in Rmpq_get_str function");
mpq_get_str(out, b, *p);
outsv = newSVpv(out, 0);
Safefree(out);
return outsv;
}
int Rmpq_cmp(mpq_t * p1, mpq_t * p2) {
return mpq_cmp(*p1, *p2);
}
int Rmpq_cmp_ui(mpq_t * p1, unsigned long n, unsigned long d) {
return mpq_cmp_ui(*p1, n, d);
}
int Rmpq_cmp_si(mpq_t * p1, long n, unsigned long d) {
return mpq_cmp_si(*p1, n, d);
}
int Rmpq_cmp_z(mpq_t * p, mpz_t * z) {
#if __GNU_MP_RELEASE >= 60099
return mpq_cmp_z(*p, *z);
#else
int ret;
mpz_t temp;
mpz_init_set(temp, *z);
mpz_mul(temp, temp, mpq_denref(*p));
ret = mpz_cmp(mpq_numref(*p), temp);
mpz_clear(temp);
return ret;
#endif
}
int Rmpq_sgn(mpq_t * p) {
return mpq_sgn(*p);
}
int Rmpq_equal(mpq_t * p1, mpq_t * p2) {
return mpq_equal(*p1, *p2);
}
void Rmpq_add(mpq_t * p1, mpq_t * p2, mpq_t * p3) {
mpq_add(*p1, *p2, *p3);
}
void Rmpq_sub(mpq_t * p1, mpq_t * p2, mpq_t * p3) {
mpq_sub(*p1, *p2, *p3);
}
void Rmpq_mul(mpq_t * p1, mpq_t * p2, mpq_t * p3) {
mpq_mul(*p1, *p2, *p3);
}
void Rmpq_div(mpq_t * p1, mpq_t * p2, mpq_t * p3) {
if(!mpq_cmp_ui(*p3, 0, 1))
croak("Division by 0 not allowed in Math::GMPq::Rmpq_div");
mpq_div(*p1, *p2, *p3);
}
void Rmpq_mul_2exp(pTHX_ mpq_t * p1, mpq_t * p2, SV * p3) {
mpq_mul_2exp(*p1, *p2, (mp_bitcnt_t)SvUV(p3));
}
void Rmpq_div_2exp(pTHX_ mpq_t * p1, mpq_t * p2, SV * p3) {
mpq_div_2exp(*p1, *p2, (mp_bitcnt_t)SvUV(p3));
}
void Rmpq_neg(mpq_t * p1, mpq_t * p2) {
mpq_neg(*p1, *p2);
}
void Rmpq_abs(mpq_t * p1, mpq_t * p2) {
mpq_abs(*p1, *p2);
}
void Rmpq_inv(mpq_t * p1, mpq_t * p2) {
mpq_inv(*p1, *p2);
}
SV * _Rmpq_out_str(pTHX_ mpq_t * p, int base){
size_t ret;
if(base < 2 || base > 36)
croak("2nd argument supplied to Rmpq_out_str is out of allowable range (must be between 2 and 36 inclusive)");
ret = mpq_out_str(NULL, base, *p);
fflush(stdout);
return newSVuv(ret);
}
SV * _Rmpq_out_strS(pTHX_ mpq_t * p, int base, SV * suff) {
size_t ret;
if(base < 2 || base > 36)
croak("2nd argument supplied to Rmpq_out_str is out of allowable range (must be between 2 and 36 inclusive)");
ret = mpq_out_str(NULL, base, *p);
printf("%s", SvPV_nolen(suff));
fflush(stdout);
return newSVuv(ret);
}
SV * _Rmpq_out_strP(pTHX_ SV * pre, mpq_t * p, int base) {
size_t ret;
if(base < 2 || base > 36)
croak("2nd argument supplied to Rmpq_out_str is out of allowable range (must be between 2 and 36 inclusive)");
printf("%s", SvPV_nolen(pre));
ret = mpq_out_str(NULL, base, *p);
fflush(stdout);
return newSVuv(ret);
}
SV * _Rmpq_out_strPS(pTHX_ SV * pre, mpq_t * p, int base, SV * suff) {
size_t ret;
if(base < 2 || base > 36)
croak("2nd argument supplied to Rmpq_out_str is out of allowable range (must be between 2 and 36 inclusive)");
printf("%s", SvPV_nolen(pre));
ret = mpq_out_str(NULL, base, *p);
printf("%s", SvPV_nolen(suff));
fflush(stdout);
return newSVuv(ret);
}
SV * _TRmpq_out_str(pTHX_ FILE * stream, int base, mpq_t * p) {
size_t ret;
ret = mpq_out_str(stream, base, *p);
fflush(stream);
return newSVuv(ret);
}
SV * _TRmpq_out_strS(pTHX_ FILE * stream, int base, mpq_t * p, SV * suff) {
size_t ret;
ret = mpq_out_str(stream, base, *p);
fflush(stream);
fprintf(stream, "%s", SvPV_nolen(suff));
fflush(stream);
return newSVuv(ret);
}
SV * _TRmpq_out_strP(pTHX_ SV * pre, FILE * stream, int base, mpq_t * p) {
size_t ret;
fprintf(stream, "%s", SvPV_nolen(pre));
fflush(stream);
ret = mpq_out_str(stream, base, *p);
fflush(stream);
return newSVuv(ret);
}
SV * _TRmpq_out_strPS(pTHX_ SV * pre, FILE * stream, int base, mpq_t * p, SV * suff) {
size_t ret;
fprintf(stream, "%s", SvPV_nolen(pre));
fflush(stream);
ret = mpq_out_str(stream, base, *p);
fflush(stream);
fprintf(stream, "%s", SvPV_nolen(suff));
fflush(stream);
return newSVuv(ret);
}
SV * TRmpq_inp_str(pTHX_ mpq_t * p, FILE * stream, SV * base) {
size_t ret;
ret = mpq_inp_str(*p, stream, (int)SvIV(base));
/* fflush(stream); */
return newSVuv(ret);
}
SV * Rmpq_inp_str(pTHX_ mpq_t * p, int base){
size_t ret;
ret = mpq_inp_str(*p, NULL, base);
/* fflush(stdin); */
return newSVuv(ret);
}
void Rmpq_numref(mpz_t * z, mpq_t * r) {
mpz_set(*z, mpq_numref(*r));
}
void Rmpq_denref(mpz_t * z, mpq_t * r) {
mpz_set(*z, mpq_denref(*r));
}
void Rmpq_get_num(mpz_t * z, mpq_t * r) {
mpq_get_num(*z, *r);
}
void Rmpq_get_den(mpz_t * z, mpq_t * r) {
mpq_get_den(*z, *r);
}
void Rmpq_set_num(mpq_t * r, mpz_t * z) {
mpq_set_num(*r, *z);
}
void Rmpq_set_den(mpq_t * r, mpz_t * z) {
mpq_set_den(*r, *z);
}
SV * get_refcnt(pTHX_ SV * s) {
return newSVuv(SvREFCNT(s));
}
void Rmpq_add_z(mpq_t * rop, mpq_t * op, mpz_t * z) {
if(rop != op) mpq_set(*rop, *op);
mpz_addmul(mpq_numref(*rop), mpq_denref(*rop), *z);
}
void Rmpq_sub_z(mpq_t * rop, mpq_t * op, mpz_t * z) {
if(rop != op) mpq_set(*rop, *op);
mpz_submul(mpq_numref(*rop), mpq_denref(*rop), *z);
}
void Rmpq_z_sub(mpq_t * rop, mpz_t * z, mpq_t * op) {
if(rop != op) mpq_set(*rop, *op);
mpz_submul(mpq_numref(*rop), mpq_denref(*rop), *z);
mpz_neg(mpq_numref(*rop), mpq_numref(*rop));
}
void Rmpq_mul_z(mpq_t * rop, mpq_t * op, mpz_t * z) {
if(rop != op) mpq_set(*rop, *op);
mpz_mul(mpq_numref(*rop), mpq_numref(*rop), *z);
mpq_canonicalize(*rop);
}
void Rmpq_div_z(mpq_t * rop, mpq_t * op, mpz_t * z) {
if(!mpz_cmp_ui(*z, 0))
croak("Division by 0 not allowed in Math::GMPq::Rmpq_div_z");
if(rop != op) mpq_set(*rop, *op);
mpz_mul(mpq_denref(*rop), mpq_denref(*rop), *z);
mpq_canonicalize(*rop);
}
void Rmpq_z_div(mpq_t * rop, mpz_t * z, mpq_t * op) {
if(!mpq_cmp_ui(*op, 0, 1))
croak("Division by 0 not allowed in Math::GMPq::Rmpq_z_div");
if(rop != op) mpq_set(*rop, *op);
mpq_inv(*rop, *rop);
mpz_mul(mpq_numref(*rop), mpq_numref(*rop), *z);
mpq_canonicalize(*rop);
}
void Rmpq_pow_ui(mpq_t * rop, mpq_t * op, unsigned long ui) {
mpz_pow_ui(mpq_numref(*rop), mpq_numref(*op), ui);
mpz_pow_ui(mpq_denref(*rop), mpq_denref(*op), ui);
}
/* Also handles UV values */
void Rmpq_set_IV(pTHX_ mpq_t * a, SV * my_iv1, SV * my_iv2) {
mpq_t temp;
if(SV_IS_IOK(my_iv1) && SV_IS_IOK(my_iv2)) {
mpq_init(temp);
#ifdef MATH_GMPQ_NEED_LONG_LONG_INT
mpq_set_str(*a, SvPV_nolen(my_iv1), 10);
mpq_set_str(temp, SvPV_nolen(my_iv2), 10);
mpq_div(*a, *a, temp);
#else
if(SvUOK(my_iv1)) mpq_set_ui(*a, SvUVX(my_iv1), SvUV(my_iv2));
else mpq_set_si(*a, SvIVX(my_iv1), SvUV(my_iv2));
#endif
mpq_clear(temp);
}
else croak("Arg provided to Rmpq_set_IV not an IV");
}
int Rmpq_cmp_IV(pTHX_ mpq_t * q, SV * iv1, SV * iv2) {
mpq_t temp;
int ret;
if(!SvIOK(iv1) || !SvIOK(iv2))
croak("Arg provided to Rmpq_cmp_IV is not an IV");
mpq_init(temp);
Rmpq_set_IV(aTHX_ &temp, iv1, iv2);
ret = mpq_cmp(*q, temp);
mpq_clear(temp);
return ret;
}
/* Finish typemapping - typemap 1st arg only */
SV * overload_mul(pTHX_ SV * a, SV * b, SV * third) {
mpq_t * mpq_t_obj;
SV * obj_ref, * obj;
const char * h;
int object = 0;
PERL_UNUSED_ARG(third);
if(sv_isobject(b)) {
object = 1;
h = HvNAME(SvSTASH(SvRV(b)));
if(strEQ(h, "Math::MPFR")) { /* will return */
_overload_callback("Math::MPFR::overload_mul", "Math::GMPq::overload_mul", newSViv(0));
}
}
/* create new object */
New(1, mpq_t_obj, 1, mpq_t);
if(mpq_t_obj == NULL) croak("Failed to allocate memory in overload_mul function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, "Math::GMPq");
mpq_init(*mpq_t_obj);
sv_setiv(obj, INT2PTR(IV, mpq_t_obj));
SvREADONLY_on(obj);
if(SV_IS_IOK(b)) {
Rmpq_set_IV(aTHX_ mpq_t_obj, b, newSViv(1));
mpq_mul(*mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))), *mpq_t_obj);
return obj_ref;
}
#if defined(GMPQ_PV_NV_BUG)
if( (SV_IS_POK(b) && !SV_IS_NOK(b))
||
(SV_IS_POK(b) && SV_IS_NOK(b) && SvIOKp(b)) ) {
#else
if(SV_IS_POK(b)) {
#endif
if(mpq_set_str(*mpq_t_obj, SvPV_nolen(b), 0))
croak("Invalid string supplied to Math::GMPq::overload_mul");
mpq_canonicalize(*mpq_t_obj);
mpq_mul(*mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))), *mpq_t_obj);
return obj_ref;
}
if(SV_IS_NOK(b)) {
Rmpq_set_NV(aTHX_ mpq_t_obj, b);
mpq_mul(*mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))), *mpq_t_obj);
return obj_ref;
}
if(object) {
if(strEQ(h, "Math::GMPq")) {
mpq_mul(*mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))), *(INT2PTR(mpq_t *, SvIVX(SvRV(b)))));
return obj_ref;
}
if(strEQ(h, "Math::GMPz") || strEQ(h, "Math::GMP")) {
Rmpq_mul_z(mpq_t_obj, INT2PTR(mpq_t *, SvIVX(SvRV(a))), INT2PTR(mpz_t *, SvIVX(SvRV(b))));
return obj_ref;
}
}
croak("Invalid argument supplied to Math::GMPq::overload_mul");
}
SV * overload_add(pTHX_ SV * a, SV * b, SV * third) {
mpq_t * mpq_t_obj;
SV * obj_ref, * obj;
const char *h;
int object = 0;
PERL_UNUSED_ARG(third);
if(sv_isobject(b)) {
object = 1;
h = HvNAME(SvSTASH(SvRV(b)));
if(strEQ(h, "Math::MPFR")) { /* will return */
_overload_callback("Math::MPFR::overload_add", "Math::GMPq::overload_add", newSViv(0));
}
}
/* create new object */
New(1, mpq_t_obj, 1, mpq_t);
if(mpq_t_obj == NULL) croak("Failed to allocate memory in overload_add function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, "Math::GMPq");
mpq_init(*mpq_t_obj);
sv_setiv(obj, INT2PTR(IV, mpq_t_obj));
SvREADONLY_on(obj);
if(SV_IS_IOK(b)) {
Rmpq_set_IV(aTHX_ mpq_t_obj, b, newSViv(1));
mpq_add(*mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))), *mpq_t_obj);
return obj_ref;
}
#if defined(GMPQ_PV_NV_BUG)
if( (SV_IS_POK(b) && !SV_IS_NOK(b))
||
(SV_IS_POK(b) && SV_IS_NOK(b) && SvIOKp(b)) ) {
#else
if(SV_IS_POK(b)) {
#endif
if(mpq_set_str(*mpq_t_obj, SvPV_nolen(b), 0))
croak("Invalid string supplied to Math::GMPq::overload_add");
mpq_canonicalize(*mpq_t_obj);
mpq_add(*mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))), *mpq_t_obj);
return obj_ref;
}
if(SV_IS_NOK(b)) {
Rmpq_set_NV(aTHX_ mpq_t_obj, b);
mpq_add(*mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))), *mpq_t_obj);
return obj_ref;
}
if(object) {
if(strEQ(h, "Math::GMPq")) {
mpq_add(*mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))), *(INT2PTR(mpq_t *, SvIVX(SvRV(b)))));
return obj_ref;
}
if(strEQ(h, "Math::GMPz") || strEQ(h, "Math::GMP")) {
Rmpq_add_z(mpq_t_obj, INT2PTR(mpq_t *, SvIVX(SvRV(a))), INT2PTR(mpz_t *, SvIVX(SvRV(b))));
return obj_ref;
}
}
croak("Invalid argument supplied to Math::GMPq::overload_add");
}
SV * overload_sub(pTHX_ SV * a, SV * b, SV * third) {
mpq_t * mpq_t_obj;
SV * obj_ref, * obj;
const char *h;
int object = 0;
if(sv_isobject(b)) {
object = 1;
h = HvNAME(SvSTASH(SvRV(b)));
if(strEQ(h, "Math::MPFR")) { /* will return */
_overload_callback("Math::MPFR::overload_sub", "Math::GMPq::overload_sub", &PL_sv_yes);
}
}
/* create new object */
New(1, mpq_t_obj, 1, mpq_t);
if(mpq_t_obj == NULL) croak("Failed to allocate memory in overload_sub function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, "Math::GMPq");
mpq_init(*mpq_t_obj);
sv_setiv(obj, INT2PTR(IV, mpq_t_obj));
SvREADONLY_on(obj);
if(SV_IS_IOK(b)) {
Rmpq_set_IV(aTHX_ mpq_t_obj, b, newSViv(1));
if(SWITCH_ARGS) mpq_sub(*mpq_t_obj, *mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))));
else mpq_sub(*mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))), *mpq_t_obj);
return obj_ref;
}
#if defined(GMPQ_PV_NV_BUG)
if( (SV_IS_POK(b) && !SV_IS_NOK(b))
||
(SV_IS_POK(b) && SV_IS_NOK(b) && SvIOKp(b)) ) {
#else
if(SV_IS_POK(b)) {
#endif
if(mpq_set_str(*mpq_t_obj, SvPV_nolen(b), 0))
croak("Invalid string supplied to Math::GMPq::overload_sub");
mpq_canonicalize(*mpq_t_obj);
if(SWITCH_ARGS) mpq_sub(*mpq_t_obj, *mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))));
else mpq_sub(*mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))), *mpq_t_obj);
return obj_ref;
}
if(SV_IS_NOK(b)) {
Rmpq_set_NV(aTHX_ mpq_t_obj, b);
if(SWITCH_ARGS) mpq_sub(*mpq_t_obj, *mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))));
else mpq_sub(*mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))), *mpq_t_obj);
return obj_ref;
}
if(object) {
if(strEQ(h, "Math::GMPq")) {
mpq_sub(*mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))), *(INT2PTR(mpq_t *, SvIVX(SvRV(b)))));
return obj_ref;
}
if(strEQ(h, "Math::GMPz") || strEQ(h, "Math::GMP")) {
if(SWITCH_ARGS) {
Rmpq_z_sub(mpq_t_obj, INT2PTR(mpz_t *, SvIVX(SvRV(b))), INT2PTR(mpq_t *, SvIVX(SvRV(a))));
}
else {
Rmpq_sub_z(mpq_t_obj, INT2PTR(mpq_t *, SvIVX(SvRV(a))), INT2PTR(mpz_t *, SvIVX(SvRV(b))));
}
return obj_ref;
}
}
croak("Invalid argument supplied to Math::GMPq::overload_sub function");
}
SV * overload_div(pTHX_ SV * a, SV * b, SV * third) {
mpq_t * mpq_t_obj;
SV * obj_ref, * obj;
const char *h;
int object = 0;
if(sv_isobject(b)) {
object = 1;
h = HvNAME(SvSTASH(SvRV(b)));
if(strEQ(h, "Math::MPFR")) { /* will return */
/* divby0 is allowed here */
_overload_callback("Math::MPFR::overload_div", "Math::GMPq::overload_div", &PL_sv_yes);
}
}
/* create new object */
New(1, mpq_t_obj, 1, mpq_t);
if(mpq_t_obj == NULL) croak("Failed to allocate memory in overload_div function");
obj_ref = newSV(0);
obj = newSVrv(obj_ref, "Math::GMPq");
mpq_init(*mpq_t_obj);
sv_setiv(obj, INT2PTR(IV, mpq_t_obj));
SvREADONLY_on(obj);
if(SV_IS_IOK(b)) {
if(SvIV(b) == 0)
croak("Division by 0 not allowed in Math::GMPq::overload_div");
Rmpq_set_IV(aTHX_ mpq_t_obj, b, newSViv(1));
if(SWITCH_ARGS) mpq_div(*mpq_t_obj, *mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))));
else mpq_div(*mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))), *mpq_t_obj);
return obj_ref;
}
#if defined(GMPQ_PV_NV_BUG)
if( (SV_IS_POK(b) && !SV_IS_NOK(b))
||
(SV_IS_POK(b) && SV_IS_NOK(b) && SvIOKp(b)) ) {
#else
if(SV_IS_POK(b)) {
#endif
if(mpq_set_str(*mpq_t_obj, SvPV_nolen(b), 0))
croak("Invalid string supplied to Math::GMPq::overload_div");
mpq_canonicalize(*mpq_t_obj);
if(!mpq_cmp_ui(*mpq_t_obj, 0, 1))
croak("Division by 0 not allowed in Math::GMPq::overload_div");
if(SWITCH_ARGS) mpq_div(*mpq_t_obj, *mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))));
else mpq_div(*mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))), *mpq_t_obj);
return obj_ref;
}
if(SV_IS_NOK(b)) {
if(SvNV(b) == 0)
croak("Division by 0 not allowed in Math::GMPq::overload_div");
/* If SvNV(b) is Inf or Nan, this will be caught by Rmpq_set_NV */
Rmpq_set_NV(aTHX_ mpq_t_obj, b);
if(SWITCH_ARGS) mpq_div(*mpq_t_obj, *mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))));
else mpq_div(*mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))), *mpq_t_obj);
return obj_ref;
}
if(object) {
if(strEQ(h, "Math::GMPq")) {
if(!mpq_cmp_ui(*(INT2PTR(mpq_t *, SvIVX(SvRV(b)))), 0, 1))
croak("Division by 0 not allowed in Math::GMPq::overload_div");
mpq_div(*mpq_t_obj, *(INT2PTR(mpq_t *, SvIVX(SvRV(a)))), *(INT2PTR(mpq_t *, SvIVX(SvRV(b)))));
return obj_ref;
}
if(strEQ(h, "Math::GMPz") || strEQ(h, "Math::GMP")) {
if(SWITCH_ARGS) {
/* Rmpq_z_div will catch divby0 */
Rmpq_z_div(mpq_t_obj, INT2PTR(mpz_t *, SvIVX(SvRV(b))), INT2PTR(mpq_t *, SvIVX(SvRV(a))));
}
else {
/* Rmpq_z_div will catch divby0 */
Rmpq_div_z(mpq_t_obj, INT2PTR(mpq_t *, SvIVX(SvRV(a))), INT2PTR(mpz_t *, SvIVX(SvRV(b))));
}
return obj_ref;
}
}
croak("Invalid argument supplied to Math::GMPq::overload_div function");
}
SV * overload_string(pTHX_ mpq_t * p, SV * second, SV * third) {
char * out;
SV * outsv;
PERL_UNUSED_ARG2(second, third);
New(123, out, mpz_sizeinbase(mpq_numref(*p), 10) + mpz_sizeinbase(mpq_denref(*p), 10) + 3, char);
if(out == NULL) croak ("Failed to allocate memory in overload_string function");
mpq_get_str(out, 10, *p);
outsv = newSVpv(out, 0);
Safefree(out);
return outsv;
}
NV _mpf_get_float128(mpf_t * x) {
#if defined(USE_QUADMATH)
mpf_t t;
long i, exp, retract = 0, bits = 113;
char *out;
__float128 ret = 0.0Q, sign = 1.0Q;
__float128 add_on[113] = {
5192296858534827628530496329220096e0Q, 2596148429267413814265248164610048e0Q,
1298074214633706907132624082305024e0Q, 649037107316853453566312041152512e0Q,
324518553658426726783156020576256e0Q, 162259276829213363391578010288128e0Q,
81129638414606681695789005144064e0Q, 40564819207303340847894502572032e0Q,
20282409603651670423947251286016e0Q, 10141204801825835211973625643008e0Q,
5070602400912917605986812821504e0Q, 2535301200456458802993406410752e0Q,
1267650600228229401496703205376e0Q, 633825300114114700748351602688e0Q,
316912650057057350374175801344e0Q, 158456325028528675187087900672e0Q, 79228162514264337593543950336e0Q,
39614081257132168796771975168e0Q, 19807040628566084398385987584e0Q, 9903520314283042199192993792e0Q,
4951760157141521099596496896e0Q, 2475880078570760549798248448e0Q, 1237940039285380274899124224e0Q,
618970019642690137449562112e0Q, 309485009821345068724781056e0Q, 154742504910672534362390528e0Q,