-
Notifications
You must be signed in to change notification settings - Fork 2
/
MPFR.xs
executable file
·13282 lines (11089 loc) · 325 KB
/
MPFR.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_mpfr_include.h"
#include "math_mpfr_unused.h"
#if NVSIZE == 8
# include "grisu3.h"
#endif
int nnum = 0; /* flag that is incremented whenever a string containing
non-numeric characters is treated as a number */
int nok_pok = 0; /* flag that is incremented whenever a scalar that is both
NOK and POK is passed to new or an overloaded operator */
/* Has inttypes.h been included ? */
int _has_inttypes(void) {
#if defined MATH_MPFR_NEED_LONG_LONG_INT
return 1;
#else
return 0;
#endif
}
int NNW_val(pTHX) {
/* return the numeric value of $Math::MPFR::NNW - ie the no. of non-numeric instances encountered */
return (int)SvIV(get_sv("Math::MPFR::NNW", 0));
}
int NOK_POK_val(pTHX) {
/* return the numeric value of $Math::MPFR::NOK_POK */
return (int)SvIV(get_sv("Math::MPFR::NOK_POK", 0));
}
int _win32_infnanstring(char * s) { /* MS Windows only - detect 1.#INF and 1.#IND
* Need to do this to correctly handle a scalar
* that is both NOK and POK on older win32 perls */
/*************************************
* if input string =~ /^\-1\.#INF$/ return -1
* elsif input string =~ /^\+?1\.#INF$/i return 1
* elsif input string =~ /^(\-|\+)?1\.#IND$/i return 2
* else return 0
**************************************/
#ifdef _WIN32_BIZARRE_INFNAN
int sign = 1;
int factor = 1;
if(s[0] == '-') {
sign = -1;
s++;
}
else {
if(s[0] == '+') s++;
}
if(!strcmp(s, "1.#INF")) return sign;
if(!strcmp(s, "1.#IND")) return 2;
return 0;
#else
PERL_UNUSED_ARG(s);
croak("Math::MPFR::_win32_infnanstring not implemented for this build of perl");
#endif
}
/* _fmt_flt is used by _nvtoa, doubletoa and _mpfrtoa to format numeric strings */
SV * _fmt_flt(pTHX_ char * out, int k, int sign, int max_decimal_prec, int sf) {
/*
out : the string that contains the mantissa of the value as decimal digits;
does not contain a radix point.
k : the exponent of the value.
sign : is 0 if the value is non-negative; else is non-zero.
max_decimal_prec: calculated as ceil(0x1.34413509f79ffp-2 * p) + 1, where p is the precision
in bits of the given floating point value.
sf : If non-zero, then Safefree(out) will be run prior to this function returning;
else Safefree(out) will not be run. The doubletoa function requires that
Safefree(out) is not run.
*/
int k_index, critical, skip;
SV * outsv;
char *bstr;
char str[] = {'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'};
/*******************************/
/* Include following to return *
* the string as is - ie with *
* no reformatting */
/********************************
* int as_is = 1;
* if(as_is) {
* sprintf(str, "e%03d", k);
* strcat(out, str);
* outsv = newSVpv(out, 0);
* if(sf) Safefree(out);
* return outsv;
* }
********************************/
k_index = (int)strlen(out);
critical = k; /* formatting is based around this value */
k -= k_index;
if(critical < -3) {
sprintf(str, "e%03d", critical - 1);
if(sign || k_index > 1) {
/* insert decimal point */
for(skip = k_index + sign; skip > 1 + sign; skip--) {
out[skip] = out[skip - 1 - sign];
}
if(k_index > 1) {
out[1 + sign] = '.';
out[k_index + 1 + sign] = 0;
}
if(sign) {
out[1] = out[0];
out[0] = '-';
}
}
strcat(out, str);
outsv = newSVpv(out, 0);
if(sf) Safefree(out);
return outsv;
}
if(critical <= 0 ) {
/* bstr = concatenate "0." . ("0" x -critical) . out; */
Newxz(bstr, 8 - critical + strlen(out), char); /* Note: 'critical' has -ve value */
/* Newxz(bstr, (int)(16 + ceil(0.30103 * NVSIZE_BITS)), char); <= OLD VERSION */
if(bstr == NULL) croak("Failed to allocate memory for 2nd output string in _fmt_flt sub");
if(sign) bstr[0] = '-';
bstr[0 + sign] = '0';
bstr[1 + sign] = '.';
sign += 2;
for(skip = critical; skip < 0; skip++) {
bstr[sign] = '0';
sign++;
}
bstr[sign] = 0;
strcat(bstr, out);
outsv = newSVpv(bstr, 0);
if(sf) Safefree(out);
Safefree(bstr);
return outsv;
}
if(critical < max_decimal_prec) {
if(sign) {
for(skip = k_index; skip > 0; skip--) out[skip] = out[skip - 1];
out[0] = '-';
out[k_index + 1] = 0;
}
if(k >= 0) {
/* out = concatenate out . ('0' x k); */
for(skip = 0; skip < k; skip++) out[k_index + skip + sign] = '0';
out[k_index + k + sign] = '.';
out[k_index + k + sign + 1] = '0';
out[k_index + k + sign + 2] = 0;
outsv = newSVpv(out, 0);
if(sf) Safefree(out);
return outsv;
}
/* insert decimal point; */
for(skip = k_index + sign; skip > k_index + k + sign; skip--) out[skip] = out[skip - 1];
out[k_index + k + sign] = '.';
out[k_index + sign + 1] = 0;
outsv = newSVpv(out, 0);
if(sf) Safefree(out);
return outsv;
}
if( k_index > 1) {
/* insert decimal point */
for(skip = k_index + sign; skip > 1 + sign; skip--) {
out[skip] = out[skip - 1 - sign];
}
out[1 + sign] = '.';
out[k_index + 1 + sign] = 0;
}
if(sign) {
out[1] = out[0];
out[0] = '-';
}
sprintf(str, "e+%d", critical - 1);
strcat(out, str);
outsv = newSVpv(out, 0);
if(sf) Safefree(out);
return outsv;
}
void Rmpfr_set_default_rounding_mode(pTHX_ SV * round) {
CHECK_ROUNDING_VALUE
mpfr_set_default_rounding_mode((mpfr_rnd_t)SvUV(round));
}
unsigned long Rmpfr_get_default_rounding_mode(void) {
return __gmpfr_default_rounding_mode;
}
SV * Rmpfr_prec_round(pTHX_ mpfr_t * p, SV * prec, SV * round) {
return newSViv(mpfr_prec_round(*p, (mpfr_prec_t)SvIV(prec), (mpfr_rnd_t)SvUV(round)));
}
void DESTROY(pTHX_ mpfr_t * p) {
mpfr_clear(*p);
Safefree(p);
}
void Rmpfr_clear(pTHX_ mpfr_t * p) {
mpfr_clear(*p);
Safefree(p);
}
void Rmpfr_clear_mpfr(mpfr_t * p) {
mpfr_clear(*p);
}
void Rmpfr_clear_ptr(pTHX_ mpfr_t * p) {
Safefree(p);
}
void Rmpfr_clears(pTHX_ SV * p, ...) {
dXSARGS;
long int i;
PERL_UNUSED_ARG(p);
for(i = 0; i < items; i++) {
mpfr_clear(*(INT2PTR(mpfr_t *, SvIVX(SvRV(ST(i))))));
Safefree(INT2PTR(mpfr_t *, SvIVX(SvRV(ST(i)))));
}
XSRETURN(0);
}
SV * Rmpfr_init(pTHX) {
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
NEW_MATH_MPFR_OBJECT("Math::MPFR",Rmpfr_init) /* defined in math_mpfr_include.h */
mpfr_init(*mpfr_t_obj);
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
return obj_ref;
}
SV * Rmpfr_init2(pTHX_ SV * prec) {
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
NEW_MATH_MPFR_OBJECT("Math::MPFR",Rmpfr_init2) /* defined in math_mpfr_include.h */
mpfr_init2 (*mpfr_t_obj, (mpfr_prec_t)SvIV(prec));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
return obj_ref;
}
SV * Rmpfr_init_nobless(pTHX) {
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
NEW_MATH_MPFR_OBJECT(NULL,Rmpfr_init_nobless) /* defined in math_mpfr_include.h */
mpfr_init(*mpfr_t_obj);
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
return obj_ref;
}
SV * Rmpfr_init2_nobless(pTHX_ SV * prec) {
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
NEW_MATH_MPFR_OBJECT(NULL,Rmpfr_init2_nobless) /* defined in math_mpfr_include.h */
mpfr_init2 (*mpfr_t_obj, (mpfr_prec_t)SvIV(prec));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
return obj_ref;
}
void Rmpfr_init_set(pTHX_ mpfr_t * q, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT("Math::MPFR",Rmpfr_init_set) /* defined in math_mpfr_include.h */
ret = mpfr_init_set(*mpfr_t_obj, *q, (mpfr_rnd_t)SvUV(round));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_init_set_ui(pTHX_ SV * q, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT("Math::MPFR",Rmpfr_init_set_ui) /* defined in math_mpfr_include.h */
ret = mpfr_init_set_ui(*mpfr_t_obj, (unsigned long)SvUV(q), (mpfr_rnd_t)SvUV(round));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_init_set_si(pTHX_ SV * q, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT("Math::MPFR",Rmpfr_init_set_si) /* defined in math_mpfr_include.h */
ret = mpfr_init_set_si(*mpfr_t_obj, (long)SvIV(q), (mpfr_rnd_t)SvUV(round));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_init_set_d(pTHX_ SV * q, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT("Math::MPFR",Rmpfr_init_set_d) /* defined in math_mpfr_include.h */
ret = mpfr_init_set_d(*mpfr_t_obj, (double)SvNV(q), (mpfr_rnd_t)SvUV(round));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_init_set_ld(pTHX_ SV * q, SV * round) {
#ifdef USE_LONG_DOUBLE
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT("Math::MPFR",Rmpfr_init_set_ld) /* defined in math_mpfr_include.h */
ret = mpfr_init_set_ld(*mpfr_t_obj, (long double)SvNV(q), (mpfr_rnd_t)SvUV(round));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
RETURN_STACK_2 /*defined in math_mpfr_include.h */
#else
PERL_UNUSED_ARG2(q, round);
croak("Rmpfr_init_set_ld not implemented on this build of perl");
#endif
}
void Rmpfr_init_set_f(pTHX_ mpf_t * q, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT("Math::MPFR",Rmpfr_init_set_f) /* defined in math_mpfr_include.h */
ret = mpfr_init_set_f(*mpfr_t_obj, *q, (mpfr_rnd_t)SvUV(round));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_init_set_z(pTHX_ mpz_t * q, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT("Math::MPFR",Rmpfr_init_set_z) /* defined in math_mpfr_include.h */
ret = mpfr_init_set_z(*mpfr_t_obj, *q, (mpfr_rnd_t)SvUV(round));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_init_set_q(pTHX_ mpq_t * q, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT("Math::MPFR",Rmpfr_init_set_q) /* defined in math_mpfr_include.h */
ret = mpfr_init_set_q(*mpfr_t_obj, *q, (mpfr_rnd_t)SvUV(round));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_init_set_str(pTHX_ SV * q, SV * base, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
#ifdef _WIN32_BIZARRE_INFNAN
int inf_or_nan;
#endif
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
if( FAILS_CHECK_INPUT_BASE )
croak("2nd argument supplied to Rmpfr_init_set str is out of allowable range");
NEW_MATH_MPFR_OBJECT("Math::MPFR",Rmpfr_init_set_str) /* defined in math_mpfr_include.h */
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
#ifdef _WIN32_BIZARRE_INFNAN
inf_or_nan = _win32_infnanstring(SvPV_nolen(q));
if(inf_or_nan) {
mpfr_init(*mpfr_t_obj);
if(inf_or_nan != 2) mpfr_set_inf(*mpfr_t_obj, inf_or_nan);
}
else {
ret = mpfr_init_set_str(*mpfr_t_obj, SvPV_nolen(q), (int)SvIV(base), (mpfr_rnd_t)SvUV(round));
}
#else
ret = mpfr_init_set_str(*mpfr_t_obj, SvPV_nolen(q), (int)SvIV(base), (mpfr_rnd_t)SvUV(round));
#endif
NON_NUMERIC_CHAR_CHECK, "Rmpfr_init_set_str");}
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_init_set_nobless(pTHX_ mpfr_t * q, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT(NULL,Rmpfr_init_set_nobless) /* defined in math_mpfr_include.h */
ret = mpfr_init_set(*mpfr_t_obj, *q, (mpfr_rnd_t)SvUV(round));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_init_set_ui_nobless(pTHX_ SV * q, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT(NULL,Rmpfr_init_set_ui_nobless) /* defined in math_mpfr_include.h */
ret = mpfr_init_set_ui(*mpfr_t_obj, (unsigned long)SvUV(q), (mpfr_rnd_t)SvUV(round));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_init_set_si_nobless(pTHX_ SV * q, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT(NULL,Rmpfr_init_set_si_nobless) /* defined in math_mpfr_include.h */
ret = mpfr_init_set_si(*mpfr_t_obj, (long)SvIV(q), (mpfr_rnd_t)SvUV(round));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_init_set_d_nobless(pTHX_ SV * q, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT(NULL,Rmpfr_init_set_d_nobless) /* defined in math_mpfr_include.h */
ret = mpfr_init_set_d(*mpfr_t_obj, (double)SvNV(q), (mpfr_rnd_t)SvUV(round));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_init_set_ld_nobless(pTHX_ SV * q, SV * round) {
#ifdef USE_LONG_DOUBLE
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT(NULL,Rmpfr_init_set_ld_nobless) /* defined in math_mpfr_include.h */
ret = mpfr_init_set_ld(*mpfr_t_obj, (long double)SvNV(q), (mpfr_rnd_t)SvUV(round));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
RETURN_STACK_2 /*defined in math_mpfr_include.h */
#else
PERL_UNUSED_ARG2(q, round);
croak("Rmpfr_init_set_ld_nobless not implemented on this build of perl");
#endif
}
void Rmpfr_init_set_f_nobless(pTHX_ mpf_t * q, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT(NULL,Rmpfr_init_set_f_nobless) /* defined in math_mpfr_include.h */
ret = mpfr_init_set_f(*mpfr_t_obj, *q, (mpfr_rnd_t)SvUV(round));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_init_set_z_nobless(pTHX_ mpz_t * q, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT(NULL,Rmpfr_init_set_z_nobless) /* defined in math_mpfr_include.h */
ret = mpfr_init_set_z(*mpfr_t_obj, *q, (mpfr_rnd_t)SvUV(round));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_init_set_q_nobless(pTHX_ mpq_t * q, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT(NULL,Rmpfr_init_set_q_nobless) /* defined in math_mpfr_include.h */
ret = mpfr_init_set_q(*mpfr_t_obj, *q, (mpfr_rnd_t)SvUV(round));
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_init_set_str_nobless(pTHX_ SV * q, SV * base, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
if( FAILS_CHECK_INPUT_BASE )
croak("2nd argument supplied to Rmpfr_init_set_str_nobless is out of allowable range");
NEW_MATH_MPFR_OBJECT(NULL,Rmpfr_init_set_str_nobless) /* defined in math_mpfr_include.h */
OBJ_READONLY_ON /*defined in math_mpfr_include.h */
ret = mpfr_init_set_str(*mpfr_t_obj, SvPV_nolen(q), (int)SvIV(base), (mpfr_rnd_t)SvUV(round));
NON_NUMERIC_CHAR_CHECK, "Rmpfr_init_set_str_nobless");}
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_deref2(pTHX_ mpfr_t * p, SV * base, SV * n_digits, SV * round) {
dXSARGS;
char * out;
mpfr_exp_t ptr;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
if( FAILS_CHECK_OUTPUT_BASE )
croak("Second argument supplied to Rmpfr_get_str is not in acceptable range");
out = mpfr_get_str(NULL, &ptr, (int)SvIV(base), (unsigned long)SvUV(n_digits), *p, (mpfr_rnd_t)SvUV(round));
if(out == NULL) croak("An error occurred in memory allocation in mpfr_get_str\n");
ST(0) = MORTALIZED_PV(out); /* defined in math_mpfr_include.h */
mpfr_free_str(out);
ST(1) = sv_2mortal(newSViv(ptr));
XSRETURN(2);
}
void Rmpfr_set_default_prec(pTHX_ SV * prec) {
#if MPFR_VERSION < 262144 /* earlier than version 4.0.0 */
if(SvIV(prec) < 2) croak("Precision must be set to at least 2 for this version (%s) of the mpfr library", MPFR_VERSION_STRING);
#endif
mpfr_set_default_prec((mpfr_prec_t)SvIV(prec));
}
SV * Rmpfr_get_default_prec(pTHX) {
return newSViv(mpfr_get_default_prec());
}
SV * Rmpfr_min_prec(pTHX_ mpfr_t * x) {
return newSViv((mpfr_prec_t)mpfr_min_prec(*x));
}
void Rmpfr_set_prec(pTHX_ mpfr_t * p, SV * prec) {
#if MPFR_VERSION < 262144 /* earlier than version 4.0.0 */
if(SvIV(prec) < 2) croak("Precision must be set to at least 2 for this version (%s) of the mpfr library", MPFR_VERSION_STRING);
#endif
mpfr_set_prec(*p, (mpfr_prec_t)SvIV(prec));
}
void Rmpfr_set_prec_raw(pTHX_ mpfr_t * p, SV * prec) {
#if MPFR_VERSION < 262144 /* earlier than version 4.0.0 */
if(SvIV(prec) < 2) croak("Precision must be set to at least 2 for this version (%s) of the mpfr library", MPFR_VERSION_STRING);
#endif
mpfr_set_prec_raw(*p, (mpfr_prec_t)SvIV(prec));
}
SV * Rmpfr_get_prec(pTHX_ mpfr_t * p) {
return newSViv(mpfr_get_prec(*p));
}
SV * Rmpfr_set(pTHX_ mpfr_t * p, mpfr_t * q, SV * round) {
CHECK_ROUNDING_VALUE
return newSViv(mpfr_set(*p, *q, (mpfr_rnd_t)SvUV(round)));
}
SV * Rmpfr_set_ui(pTHX_ mpfr_t * p, SV * q, SV * round) {
CHECK_ROUNDING_VALUE
return newSViv(mpfr_set_ui(*p, (unsigned long)SvUV(q), (mpfr_rnd_t)SvUV(round)));
}
SV * Rmpfr_set_si(pTHX_ mpfr_t * p, SV * q, SV * round) {
CHECK_ROUNDING_VALUE
return newSViv(mpfr_set_si(*p, (long)SvIV(q), (mpfr_rnd_t)SvUV(round)));
}
SV * Rmpfr_set_uj(pTHX_ mpfr_t * p, SV * q, SV * round) {
CHECK_ROUNDING_VALUE
#ifdef MATH_MPFR_NEED_LONG_LONG_INT
return newSViv(mpfr_set_uj(*p, SvUV(q), (mpfr_rnd_t)SvUV(round)));
#else
croak("Rmpfr_set_uj not implemented on this build of perl");
#endif
}
SV * Rmpfr_set_sj(pTHX_ mpfr_t * p, SV * q, SV * round) {
CHECK_ROUNDING_VALUE
#ifdef MATH_MPFR_NEED_LONG_LONG_INT
return newSViv(mpfr_set_sj(*p, SvIV(q), (mpfr_rnd_t)SvUV(round)));
#else
croak("Rmpfr_set_sj not implemented on this build of perl");
#endif
}
int Rmpfr_set_NV(pTHX_ mpfr_t * p, SV * q, unsigned int round) {
#if MPFR_VERSION_MAJOR < 3
if((mpfr_rnd_t)round > 3)
croak("Illegal rounding value supplied for this version (%s) of the mpfr library", MPFR_VERSION_STRING);
#endif
#if defined(USE_LONG_DOUBLE)
if(!SV_IS_NOK(q))
croak("In Rmpfr_set_NV, 2nd argument is not an NV");
return mpfr_set_ld(*p, (long double)SvNV(q), (mpfr_rnd_t)round);
#elif defined(CAN_PASS_FLOAT128)
if(!SV_IS_NOK(q))
croak("In Rmpfr_set_NV, 2nd argument is not an NV");
return mpfr_set_float128(*p, (float128)SvNV(q), (mpfr_rnd_t)round);
#elif defined(USE_QUADMATH)
char buffer[45];
int exp;
float128 ld;
int returned;
if(!SV_IS_NOK(q))
croak("In Rmpfr_set_NV, 2nd argument is not an NV");
ld = (float128)SvNV(q);
if(ld != ld) {
mpfr_set_nan(*p);
return 0;
}
if(ld != 0.0Q && (ld / ld != 1)) {
returned = ld > 0.0Q ? 1 : -1;
mpfr_set_inf(*p, returned);
return 0;
}
ld = frexpq(ld, &exp); /* 0.5 <= returned value < 1.0 */
/* Convert ld to an integer by right shifting it 113 bits */
ld *= 0x1p+113Q; /* ld *= powq(2.0Q, 113); */
returned = quadmath_snprintf(buffer, 45, "%.0Qf", ld);
if(returned < 0) croak("In Rmpfr_set_NV, encoding error in quadmath_snprintf function");
if(returned >= 45) croak("In Rmpfr_set_NV, buffer given to quadmath_snprintf function was too small");
returned = mpfr_set_str(*p, buffer, 10, (mpfr_rnd_t)round);
mpfr_mul_2si(*p, *p, exp - 113, GMP_RNDN);
return returned;
#else
if(!SV_IS_NOK(q))
croak("In Rmpfr_set_NV, 2nd argument is not an NV");
return mpfr_set_d (*p, SvNV(q), (mpfr_rnd_t)round);
#endif
}
void Rmpfr_init_set_NV(pTHX_ SV * q, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT("Math::MPFR",Rmpfr_init_set_NV) /* defined in math_mpfr_include.h */
mpfr_init(*mpfr_t_obj);
sv_setiv(obj, INT2PTR(IV,mpfr_t_obj));
ret = Rmpfr_set_NV(aTHX_ mpfr_t_obj, q, (mpfr_rnd_t)SvUV(round));
SvREADONLY_on(obj);
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
void Rmpfr_init_set_NV_nobless(pTHX_ SV * q, SV * round) {
dXSARGS;
mpfr_t * mpfr_t_obj;
SV * obj_ref, * obj;
int ret;
PERL_UNUSED_ARG(items);
CHECK_ROUNDING_VALUE
NEW_MATH_MPFR_OBJECT(NULL,Rmpfr_init_set_NV_nobless) /* defined in math_mpfr_include.h */
mpfr_init(*mpfr_t_obj);
sv_setiv(obj, INT2PTR(IV,mpfr_t_obj));
ret = Rmpfr_set_NV(aTHX_ mpfr_t_obj, q, (mpfr_rnd_t)SvUV(round));
SvREADONLY_on(obj);
RETURN_STACK_2 /*defined in math_mpfr_include.h */
}
int Rmpfr_cmp_float128(pTHX_ mpfr_t * a, SV * b) {
#if defined(CAN_PASS_FLOAT128)
mpfr_t t;
int ret;
mpfr_init2(t, FLT128_MANT_DIG);
mpfr_set_float128(t, SvNV(b), GMP_RNDN);
ret = mpfr_cmp(*a, t);
mpfr_clear(t);
return ret;
#elif defined(USE_QUADMATH)
mpfr_t t;
char buffer[45];
int exp;
float128 ld;
int returned;
ld = (float128)SvNV(b);
if(ld != ld || mpfr_nan_p(*a)) {
mpfr_set_erangeflag();
return 0;
}
if(ld != 0.0Q && (ld / ld != 1)) {
if(ld > 0.0Q) {
if(mpfr_inf_p(*a)) {
if(mpfr_signbit(*a)) return -1;
return 0;
}
return -1;
}
if(mpfr_inf_p(*a)) {
if(mpfr_signbit(*a)) return 0;
return 1;
}
return 1;
}
if(ld == 0.0Q) {
if(mpfr_zero_p (*a)) return 0;
if(mpfr_signbit(*a)) return -1;
return 1;
}
ld = frexpq(ld, &exp); /* 0.5 <= returned value < 1.0 */
/* Convert ld to an integer by right shifting it 113 bits */
ld *= 0x1p+113Q; /* ld *= powq(2.0Q, 113); */
returned = quadmath_snprintf(buffer, 45, "%.0Qf", ld);
if(returned < 0) croak("In Rmpfr_set_NV, encoding error in quadmath_snprintf function");
if(returned >= 45) croak("In Rmpfr_set_NV, buffer given to quadmath_snprintf function was too small");
mpfr_init2(t, FLT128_MANT_DIG);
returned = mpfr_set_str(t, buffer, 10, GMP_RNDN);
mpfr_mul_2si(t, t, exp - 113, GMP_RNDN);
returned = mpfr_cmp(*a, t);
mpfr_clear(t);
return returned;
#else
PERL_UNUSED_ARG2(a, b);
croak("Rmpfr_cmp_float128 not available on this build of perl");
#endif
}
int Rmpfr_cmp_NV(pTHX_ mpfr_t * a, SV * b) {
if(!SvNOK(b))
croak("In Rmpfr_cmp_NV, 2nd argument is not an NV");
#if defined(USE_LONG_DOUBLE)
return mpfr_cmp_ld(*a, SvNV(b));
#elif defined(USE_QUADMATH)
return Rmpfr_cmp_float128(aTHX_ a, b);
#else
return mpfr_cmp_d(*a, SvNV(b));
#endif
}
SV * Rmpfr_set_ld(pTHX_ mpfr_t * p, SV * q, SV * round) {
CHECK_ROUNDING_VALUE
#if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
return newSViv(mpfr_set_ld(*p, (long double)SvNV(q), (mpfr_rnd_t)SvUV(round)));
#else
PERL_UNUSED_ARG3(p, q, round);
croak("Rmpfr_set_ld not implemented on this build of perl");
#endif
}
SV * Rmpfr_set_d(pTHX_ mpfr_t * p, SV * q, SV * round) {
CHECK_ROUNDING_VALUE
return newSViv(mpfr_set_d(*p, (double)SvNV(q), (mpfr_rnd_t)SvUV(round)));
}
SV * Rmpfr_set_z(pTHX_ mpfr_t * p, mpz_t * q, SV * round) {
CHECK_ROUNDING_VALUE
return newSViv(mpfr_set_z(*p, *q, (mpfr_rnd_t)SvUV(round)));
}
SV * Rmpfr_set_q(pTHX_ mpfr_t * p, mpq_t * q, SV * round) {
CHECK_ROUNDING_VALUE
return newSViv(mpfr_set_q(*p, *q, (mpfr_rnd_t)SvUV(round)));
}
SV * Rmpfr_set_f(pTHX_ mpfr_t * p, mpf_t * q, SV * round) {
CHECK_ROUNDING_VALUE
return newSViv(mpfr_set_f(*p, *q, (mpfr_rnd_t)SvUV(round)));
}
int Rmpfr_set_str(pTHX_ mpfr_t * p, SV * num, SV * base, SV * round) {
int ret;
#ifdef _WIN32_BIZARRE_INFNAN
int inf_or_nan;
#endif
CHECK_ROUNDING_VALUE
if( FAILS_CHECK_INPUT_BASE )
croak("3rd argument supplied to Rmpfr_set_str is out of allowable range");
#ifdef _WIN32_BIZARRE_INFNAN
inf_or_nan = _win32_infnanstring(SvPV_nolen(num));
if(inf_or_nan) {
if(inf_or_nan == 2) {
mpfr_set_nan(*p);
}
else mpfr_set_inf(*p, inf_or_nan);
}
else {
ret = mpfr_set_str(*p, SvPV_nolen(num), (int)SvIV(base), (mpfr_rnd_t)SvUV(round));
}
#else
ret = mpfr_set_str(*p, SvPV_nolen(num), (int)SvIV(base), (mpfr_rnd_t)SvUV(round));
#endif
NON_NUMERIC_CHAR_CHECK, "Rmpfr_set_str");}
return ret;
}
void Rmpfr_set_inf(mpfr_t * p, int sign) {
mpfr_set_inf(*p, sign);
}
void Rmpfr_set_nan(mpfr_t * p) {
mpfr_set_nan(*p);
}
void Rmpfr_swap(mpfr_t *p, mpfr_t * q) {
mpfr_swap(*p, *q);
}
SV * Rmpfr_get_d(pTHX_ mpfr_t * p, SV * round){
CHECK_ROUNDING_VALUE
return newSVnv(mpfr_get_d(*p, (mpfr_rnd_t)SvUV(round)));
}
SV * Rmpfr_get_d_2exp(pTHX_ SV * exp, mpfr_t * p, SV * round){
long _exp;
double ret;
CHECK_ROUNDING_VALUE
ret = mpfr_get_d_2exp(&_exp, *p, (mpfr_rnd_t)SvUV(round));
sv_setiv(exp, _exp);
return newSVnv(ret);
}
SV * Rmpfr_get_ld_2exp(pTHX_ SV * exp, mpfr_t * p, SV * round){
#if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
# if defined(USE_QUADMATH) && defined(__GNUC__) && ((__GNUC__ > 4 && __GNUC__ < 7) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))
/*
Casting long double Inf to float128 might result in NaN.
This is GCC bug 77265, which was fixed for GCC 7:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77265
The fix might yet be backported to GCC 6. (Not sure.)
It was earlier reported (also by me) to MinGW:
https://sourceforge.net/p/mingw-w64/bugs/479/
Let us take the cautious approach and simply avoid
making that cast. Instead, we will cast the double Inf
to a float128.
*/
if(mpfr_inf_p(*p))
return newSVnv(mpfr_get_d(*p, (mpfr_rnd_t)SvUV(round)));
# endif