/
bignum.pmc
1478 lines (1127 loc) · 34 KB
/
bignum.pmc
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
/*
Copyright (C) 2004-2008, Parrot Foundation.
$Id$
=head1 NAME
src/pmc/bignum.pmc - BigNum PMC class
=head1 DESCRIPTION
C<BigNum> provides arbitrary precision floating point mathematic
functions, based on the GMP mpf library.
=head1 SYNOPSIS
Make mixing of classes work, like in:
new $P0 ['BigInt']
new $P1 ['BigNum']
set $P0, 10
set $P1, 2
div P2, P0, P1 # $P2 = $P0 / $P1; (BigNum) 5.0
Make auto-upgrading/downgrading work.
set $N1, $P0
set $N1, $P1
set $I1, $P0
set $I1, $P1
set $P0, $I1
set $P0, $N1
set $P1, $I1
set $P1, $N1
BigNum
=> BigInt => Integer
=> Number: float (can be long double) and double
=> Integer (unsigned long)
=head2 Functions
=over 4
=item C<static void bignum_set(Interp*, PMC *, PMC *)>
=item C<static void bignum_set_si(Interp*, PMC *, long value)>
=item C<static void bignum_set_double(Interp*, PMC *, double value)>
=item C<static void bignum_set_str(Interp*, PMC *, char *, int base)>
=cut
*/
#include "parrot/parrot.h"
#ifdef S_SPLINT_S
# undef PARROT_HAS_GMP /* splint barfs on the gmp.h header */
#endif /* S_SPLINT_S */
/* Uncomment to easily disable it */
/*
#ifdef PARROT_HAS_GMP
# undef PARROT_HAS_GMP
#endif
*/
#undef PARROT_BIGNUM_CAN_BIGINT
#ifdef PARROT_HAS_GMP
# include "pmc_bigint.h"
# include <gmp.h>
typedef struct BIGNUM {
mpf_t b;
} BIGNUM;
#endif
static void
bignum_init(PARROT_INTERP, PMC *self) {
Parrot_BigNum_attributes *attrs =
mem_allocate_zeroed_typed(Parrot_BigNum_attributes);
#ifdef PARROT_HAS_GMP
attrs->bn = mem_allocate_zeroed_typed(BIGNUM);
mpf_clear(attrs->bn->b);
mpf_init(attrs->bn->b);
#else
attrs->bn = NULL;
#endif
PMC_data(self) = attrs;
}
static void
bignum_clear(PARROT_INTERP, PMC *self) {
#ifdef PARROT_HAS_GMP
BIGNUM *bn;
GETATTR_BigNum_bn(interp, self, bn);
mpf_clear(bn->b);
#endif
}
#ifdef PARROT_HAS_GMP
static void
bignum_set(PARROT_INTERP, PMC *dest, PMC *src) {
BIGNUM *bn_dest, *bn_src;
GETATTR_BigNum_bn(interp, dest, bn_dest);
GETATTR_BigNum_bn(interp, src, bn_src);
mpf_set(bn_dest->b, bn_src->b);
}
static void
bignum_set_si(PARROT_INTERP, PMC *self, long value) {
BIGNUM *bn;
GETATTR_BigNum_bn(interp, self, bn);
mpf_set_si(bn->b, value);
}
static void
bignum_set_ui(PARROT_INTERP, PMC *self, unsigned long value) {
BIGNUM *bn;
GETATTR_BigNum_bn(interp, self, bn);
mpf_set_ui(bn->b, value);
}
static void
bignum_set_float(PARROT_INTERP, PMC *self, FLOATVAL value) {
BIGNUM *bn;
GETATTR_BigNum_bn(interp, self, bn);
mpf_set_d(bn->b, (double)value);
}
static void
bignum_set_double(PARROT_INTERP, PMC *self, double value) {
BIGNUM *bn;
GETATTR_BigNum_bn(interp, self, bn);
mpf_set_d(bn->b, value);
}
static void
bignum_set_str(PARROT_INTERP, PMC *self, char *value, int base) {
BIGNUM *bn;
GETATTR_BigNum_bn(interp, self, bn);
mpf_set_str(bn->b, value, base);
}
# ifdef PARROT_BIGNUM_CAN_BIGINT
static void
bignum_set_bigint(PARROT_INTERP, PMC *self, struct BIGINT *value) {
BIGNUM *bn;
struct BIGINT *bi;
GETATTR_BigNum_bn(interp, self, bn);
bi->b = PARROT_BIGINT(value);
mpf_set(bn->b, (mpf_srcptr)bi->b);
}
# endif
static BIGNUM*
bignum_get_self(PARROT_INTERP, PMC *self) {
BIGNUM *bn;
GETATTR_BigNum_bn(interp, self, bn);
return bn;
}
static void
bignum_set_self(PARROT_INTERP, PMC *self, BIGNUM *value) {
BIGNUM *bn;
GETATTR_BigNum_bn(interp, self, bn);
mpf_set(bn->b, (mpf_srcptr)((BIGNUM*)value)->b);
}
static long
bignum_get_si(PARROT_INTERP, PMC *self) {
BIGNUM *bn;
GETATTR_BigNum_bn(interp, self, bn);
if (mpf_fits_slong_p(bn->b))
return mpf_get_si(bn->b);
Parrot_ex_throw_from_c_args(interp, NULL, 1, "bignum_get_si: number too big");
}
static unsigned long
bignum_get_ui(PARROT_INTERP, PMC *self) {
BIGNUM *bn;
GETATTR_BigNum_bn(interp, self, bn);
if (mpf_fits_slong_p(bn->b))
return mpf_get_ui(bn->b);
Parrot_ex_throw_from_c_args(interp, NULL, 1, "bignum_get_ui: number too big");
}
static int
bignum_get_bool(PARROT_INTERP, PMC *self) {
BIGNUM *bn;
GETATTR_BigNum_bn(interp, self, bn);
if (mpf_sgn(bn->b) != 0)
return 1;
else
return 0;
}
static char *
bignum_get_string(PARROT_INTERP, PMC *self, int base) {
BIGNUM *bn;
size_t n;
char *s;
mp_exp_t exponent;
GETATTR_BigNum_bn(interp, self, bn);
n = (mpf_get_prec(bn->b)) / log(base) * log(2);
s = (char *)mem_sys_allocate(n + 5);
return mpf_get_str(s, &exponent, base, 0, bn->b);
}
static char *
bignum_get_string_size(PARROT_INTERP, PMC *self, int base, int digits) {
BIGNUM *bn;
size_t n;
char *s;
mp_exp_t exponent;
GETATTR_BigNum_bn(interp, self, bn);
s = (char *)mem_sys_allocate(digits + 5);
return mpf_get_str(s, &exponent, base, digits, bn->b);
}
static double
bignum_get_double(PARROT_INTERP, PMC *self) {
BIGNUM *bn;
GETATTR_BigNum_bn(interp, self, bn);
return mpf_get_d(bn->b);
}
static FLOATVAL
bignum_get_float(PARROT_INTERP, PMC *self) {
BIGNUM *bn;
GETATTR_BigNum_bn(interp, self, bn);
return mpf_get_d(bn->b);
}
# ifdef PARROT_BIGNUM_CAN_BIGINT
static struct BIGINT
bignum_get_bigint(PARROT_INTERP, PMC *self) {
BIGNUM *bn;
struct BIGINT *bi_dest;
GETATTR_BigNum_bn(interp, self, bn);
mpz_clear(bi_dest->b);
mpz_init(bi_dest->b);
if (mpf_fits_slong_p(bn->b)) {
bi_dest->b = mpf_get_ui(bn->b);
}
else {
Parrot_ex_throw_from_c_args(interp, NULL, 1,
"bignum_get_bigint: Precision loss");
}
return bi_dest;
}
# endif
static void
bignum_add_bignum(PARROT_INTERP, PMC *self, PMC *value, PMC *dest) {
BIGNUM *bn_self, *bn_value, *bn_dest;
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, value, bn_value);
GETATTR_BigNum_bn(interp, dest, bn_dest);
mpf_add(bn_dest->b, bn_self->b, bn_value->b);
}
static void
bignum_add_bignum_int(PARROT_INTERP, PMC *self, INTVAL value, PMC *dest) {
BIGNUM *bn_self, *bn_dest;
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, dest, bn_dest);
if (value < 0)
mpf_sub_ui(bn_dest->b, bn_self->b, (unsigned long int)-value);
else
mpf_add_ui(bn_dest->b, bn_self->b, (unsigned long int)value);
}
static void
bignum_add_bignum_float(PARROT_INTERP, PMC *self, FLOATVAL value, PMC *dest) {
BIGNUM *bn, *bn_self, *bn_dest;
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, dest, bn_dest);
mpf_set_d(bn->b, value);
mpf_add(bn_dest->b, bn_self->b, bn->b);
}
static void
bignum_sub_bignum(PARROT_INTERP, PMC *self, PMC *value, PMC *dest) {
BIGNUM *bn_self, *bn_value, *bn_dest;
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, value, bn_value);
GETATTR_BigNum_bn(interp, dest, bn_dest);
mpf_sub(bn_dest->b, bn_self->b, bn_value->b);
}
static void
bignum_sub_bignum_int(PARROT_INTERP, PMC *self, INTVAL value, PMC *dest) {
BIGNUM *bn_self, *bn_dest;
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, dest, bn_dest);
if (value < 0)
mpf_add_ui(bn_dest->b, bn_self->b, (unsigned long int)-value);
else
mpf_sub_ui(bn_dest->b, bn_self->b, (unsigned long int)value);
}
static void
bignum_sub_bignum_float(PARROT_INTERP, PMC *self, FLOATVAL value, PMC *dest) {
BIGNUM *bn, *bn_self, *bn_dest;
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, dest, bn_dest);
mpf_set_d(bn->b, value);
mpf_sub(bn_dest->b, bn_self->b, bn->b);
}
static void
bignum_mul_bignum(PARROT_INTERP, PMC *self, PMC *value, PMC *dest) {
BIGNUM *bn_self, *bn_value, *bn_dest;
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, value, bn_value);
GETATTR_BigNum_bn(interp, dest, bn_dest);
mpf_mul(bn_dest->b, bn_self->b, bn_value->b);
}
static void
bignum_mul_bignum_int(PARROT_INTERP, PMC *self, INTVAL value, PMC *dest) {
BIGNUM *bn_self, *bn_dest;
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, dest, bn_dest);
mpf_mul_ui(bn_dest->b, bn_self->b, (unsigned long)value);
}
static void
bignum_mul_bignum_float(PARROT_INTERP, PMC *self, FLOATVAL value, PMC *dest) {
BIGNUM *bn, *bn_self, *bn_dest;
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, dest, bn_dest);
mpf_set_d(bn->b, value);
mpf_mul(bn_dest->b, bn_self->b, bn->b);
}
static void
bignum_pow_bignum_int(PARROT_INTERP, PMC *self, INTVAL value, PMC *dest) {
BIGNUM *bn_self, *bn_dest;
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, dest, bn_dest);
mpf_pow_ui(bn_dest->b, bn_self->b, (unsigned long int)value);
}
static void
int_check_divide_zero(PARROT_INTERP, INTVAL value) {
if (value == 0)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_DIV_BY_ZERO,
"Divide by zero");
}
static void
bignum_check_divide_zero(PARROT_INTERP, PMC *value) {
/* Throw an exception if we are dividing by zero. */
BIGNUM *bn;
GETATTR_BigNum_bn(interp, value, bn);
if (mpf_cmp_si(bn->b, 0) == 0)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_DIV_BY_ZERO,
"Divide by zero");
}
static void
bignum_div_bignum(PARROT_INTERP, PMC *self, PMC *value, PMC *dest) {
BIGNUM *bn_self, *bn_value, *bn_dest;
bignum_check_divide_zero(interp, value);
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, value, bn_value);
GETATTR_BigNum_bn(interp, dest, bn_dest);
mpf_div(bn_dest->b, bn_self->b, bn_value->b);
}
static void
bignum_div_bignum_int(PARROT_INTERP, PMC *self, INTVAL value, PMC *dest) {
BIGNUM *bn_self, *bn_dest;
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, dest, bn_dest);
int_check_divide_zero(interp, value);
/* this is mpz_fdiv_q */
if (value < 0) {
mpf_div_ui(bn_dest->b, bn_self->b, (unsigned long int)-value);
mpf_neg(bn_dest->b, bn_dest->b);
}
else
mpf_div_ui(bn_dest->b, bn_self->b, (unsigned long int)value);
}
static void
bignum_div_bignum_float(PARROT_INTERP, PMC *self, FLOATVAL value, PMC *dest) {
BIGNUM *bn, *bn_self, *bn_dest;
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, dest, bn_dest);
int_check_divide_zero(interp, value);
if (value < 0) {
mpf_set_d(bn->b, -value);
mpf_div(bn_dest->b, bn_self->b, bn->b);
mpf_neg(bn_dest->b, bn_dest->b);
}
else
mpf_div(bn_dest->b, bn_self->b, bn->b);
}
/* There's no such mpf_fdiv, only mpz_fdiv and mpf_div */
static void
bignum_fdiv_bignum(PARROT_INTERP, PMC *self, PMC *value, PMC *dest) {
BIGNUM *bn_self, *bn_value, *bn_dest;
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, value, bn_value);
GETATTR_BigNum_bn(interp, dest, bn_dest);
bignum_check_divide_zero(interp, value);
mpf_div(bn_dest->b, bn_self->b, bn_value->b);
}
static void
bignum_fdiv_bignum_int(PARROT_INTERP, PMC *self, INTVAL value, PMC *dest) {
BIGNUM *bn_self, *bn_dest;
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, dest, bn_dest);
int_check_divide_zero(interp, value);
if (value < 0) {
mpf_div_ui(bn_dest->b, bn_self->b, (unsigned long int)-value);
mpf_neg(bn_dest->b, bn_dest->b);
}
else
mpf_div_ui(bn_dest->b, bn_self->b, (unsigned long int)value);
}
static INTVAL
bignum_cmp(PARROT_INTERP, PMC *self, PMC *value) {
BIGNUM *bn_self, *bn_value;
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, value, bn_value);
return mpf_cmp(bn_self->b, bn_value->b);
}
static INTVAL
bignum_cmp_double(PARROT_INTERP, PMC *self, double value) {
BIGNUM *bn;
GETATTR_BigNum_bn(interp, self, bn);
return mpf_cmp_d(bn->b, value);
}
static INTVAL
bignum_cmp_int(PARROT_INTERP, PMC *self, INTVAL value) {
BIGNUM *bn;
GETATTR_BigNum_bn(interp, self, bn);
return mpf_cmp_si(bn->b, value);
}
static INTVAL
bignum_cmp_ulong(PARROT_INTERP, PMC *self, unsigned long value) {
BIGNUM *bn;
GETATTR_BigNum_bn(interp, self, bn);
return mpf_cmp_ui(bn->b, value);
}
static void
bignum_abs(PARROT_INTERP, PMC *self, PMC *dest) {
BIGNUM *bn_self, *bn_dest;
pmc_reuse(interp, dest, enum_class_BigNum, 0);
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, dest, bn_dest);
mpf_abs(bn_dest->b, bn_self->b);
}
static void
bignum_neg(PARROT_INTERP, PMC *self, PMC *dest) {
BIGNUM *bn_self, *bn_dest;
pmc_reuse(interp, dest, enum_class_BigNum, 0);
GETATTR_BigNum_bn(interp, self, bn_self);
GETATTR_BigNum_bn(interp, dest, bn_dest);
mpf_neg(bn_dest->b, bn_self->b);
}
static INTVAL
bignum_get_default_prec(PARROT_INTERP, PMC *self) {
return mpf_get_default_prec();
}
static void
bignum_set_default_prec(PARROT_INTERP, PMC *self, INTVAL prec) {
mpf_set_default_prec(prec);
}
#else /* ifdef PARROT_HAS_GMP */
typedef struct BIGNUM {
FLOATVAL b; /* bogus definition for users without libgmp*/
} BIGNUM;
# define THROW_NYI Parrot_ex_throw_from_c_args(interp, NULL, \
EXCEPTION_LIBRARY_ERROR, "no bignum lib loaded")
# if 0
static void
bignum_init(PARROT_INTERP, PMC *self) {
THROW_NYI;
}
static void
bignum_clear(PARROT_INTERP, PMC *self) {
THROW_NYI;
}
# endif
static void
bignum_set(PARROT_INTERP, PMC *dest, PMC *src) {
THROW_NYI;
}
static void
bignum_set_si(PARROT_INTERP, PMC *self, long value) {
THROW_NYI;
}
static void
bignum_set_double(PARROT_INTERP, PMC *self, double value) {
THROW_NYI;
}
static void
bignum_set_str(PARROT_INTERP, PMC *self, char *value, int base) {
THROW_NYI;
}
static void
bignum_set_self(PARROT_INTERP, PMC *self, BIGNUM *value) {
THROW_NYI;
}
static BIGNUM*
bignum_get_self(PARROT_INTERP, PMC *self) {
THROW_NYI;
}
static char *
bignum_get_string(PARROT_INTERP, PMC *self, int base) {
THROW_NYI;
}
static char *
bignum_get_string_size(PARROT_INTERP, PMC *self, int base, int digits) {
THROW_NYI;
}
static unsigned long
bignum_get_ui(PARROT_INTERP, PMC *self) {
THROW_NYI;
}
static long
bignum_get_si(PARROT_INTERP, PMC *self) {
THROW_NYI;
}
static long
bignum_get_bool(PARROT_INTERP, PMC *self) {
THROW_NYI;
}
static double
bignum_get_double(PARROT_INTERP, PMC *self) {
THROW_NYI;
}
static void
bignum_add_bignum(PARROT_INTERP, PMC *self, PMC *value, PMC *dest) {
THROW_NYI;
}
static void
bignum_add_bignum_int(PARROT_INTERP, PMC *self, INTVAL value, PMC *dest) {
THROW_NYI;
}
static void
bignum_add_bignum_float(PARROT_INTERP, PMC *self, FLOATVAL value, PMC *dest) {
THROW_NYI;
}
static void
bignum_sub_bignum(PARROT_INTERP, PMC *self, PMC *value, PMC *dest) {
THROW_NYI;
}
static void
bignum_sub_bignum_int(PARROT_INTERP, PMC *self, INTVAL value, PMC *dest) {
THROW_NYI;
}
static void
bignum_sub_bignum_float(PARROT_INTERP, PMC *self, FLOATVAL value, PMC *dest) {
THROW_NYI;
}
static void
bignum_mul_bignum(PARROT_INTERP, PMC *self, PMC *value, PMC *dest) {
THROW_NYI;
}
static void
bignum_mul_bignum_int(PARROT_INTERP, PMC *self, INTVAL value, PMC *dest) {
THROW_NYI;
}
static void
bignum_mul_bignum_float(PARROT_INTERP, PMC *self, FLOATVAL value, PMC *dest) {
THROW_NYI;
}
static void
bignum_pow_bignum_int(PARROT_INTERP, PMC *self, INTVAL value, PMC *dest) {
THROW_NYI;
}
static void
bignum_pow_bignum_float(PARROT_INTERP, PMC *self, FLOATVAL value, PMC *dest) {
THROW_NYI;
}
static void
bignum_div_bignum(PARROT_INTERP, PMC *self, PMC *value, PMC *dest) {
THROW_NYI;
}
static void
bignum_div_bignum_int(PARROT_INTERP, PMC *self, INTVAL value, PMC *dest) {
THROW_NYI;
}
static void
bignum_div_bignum_float(PARROT_INTERP, PMC *self, FLOATVAL value, PMC *dest) {
THROW_NYI;
}
static void
bignum_fdiv_bignum(PARROT_INTERP, PMC *self, PMC *value, PMC *dest) {
THROW_NYI;
}
static void
bignum_fdiv_bignum_int(PARROT_INTERP, PMC *self, INTVAL value, PMC *dest) {
THROW_NYI;
}
static void
bignum_fdiv_bignum_float(PARROT_INTERP, PMC *self, FLOATVAL value, PMC *dest) {
THROW_NYI;
}
static INTVAL
bignum_cmp(PARROT_INTERP, PMC *self, PMC *value) {
THROW_NYI;
}
static INTVAL
bignum_cmp_int(PARROT_INTERP, PMC *self, INTVAL value) {
THROW_NYI;
}
static INTVAL
bignum_cmp_float(PARROT_INTERP, PMC *self, FLOATVAL value) {
THROW_NYI;
}
static void
bignum_abs(PARROT_INTERP, PMC *self, PMC *dest) {
THROW_NYI;
}
static void
bignum_neg(PARROT_INTERP, PMC *self, PMC *dest) {
THROW_NYI;
}
static INTVAL
bignum_get_default_prec(PARROT_INTERP, PMC *self) {
THROW_NYI;
}
static void
bignum_set_default_prec(PARROT_INTERP, PMC *self, INTVAL prec) {
THROW_NYI;
}
# undef THROW_NYI
#endif /* ifdef PARROT_HAS_GMP */
pmclass BigNum {
ATTR struct BIGNUM * bn; /*bignum val*/
/*
=back
=head2 Methods
=over 4
=item C<METHOD version()>
Return GMP version string "x.y.z".
=cut
*/
METHOD version() {
STRING *version;
#ifdef PARROT_HAS_GMP
version = Parrot_str_new(INTERP, gmp_version, 0);
#else
version = Parrot_str_new(INTERP, "0.0.0", 5);
#endif
RETURN(STRING *version);
}
/*
=item C<PMC *instantiate(PMC *sig)>
Object constructor. SELF is a BigNum Class object. Return a new
BigNum object.
=cut
*/
VTABLE PMC *instantiate(PMC *sig) {
#ifdef PARROT_HAS_GMP
int argcP = REG_INT(interp, 3);
int base;
PMC *res;
STRING *num;
res = pmc_new(INTERP, enum_class_BigNum);
if (!argcP) {
return res;
}
base = 10;
if (argcP == 2)
base = VTABLE_get_integer(INTERP, REG_PMC(interp, 6));
num = VTABLE_get_string(INTERP, REG_PMC(interp, 5));
VTABLE_set_string_keyed_int(INTERP, res, base, num);
return res;
#else
return PMCNULL;
#endif
}
VTABLE void init() {
bignum_init(INTERP, SELF);
PObj_active_destroy_SET(SELF);
}
VTABLE PMC *clone() {
PMC *res = pmc_new(INTERP, SELF->vtable->base_type);
bignum_set(INTERP, res, SELF);
return res;
}
VTABLE void destroy() {
BIGNUM *bn;
Parrot_BigNum_attributes *attrs;
bignum_clear(INTERP, SELF);
attrs = (Parrot_BigNum_attributes*)PMC_data(SELF);
#ifdef PARROT_HAS_GMP
mem_sys_free(attrs->bn);
#endif
mem_sys_free(attrs);
}
/*
=item C<void set_integer_native(INTVAL value)>
=cut
*/
VTABLE void set_integer_native(INTVAL value) {
bignum_set_si(INTERP, SELF, (long)value);
}
/*
=item C<void set_number_native(FLOATVAL value)>
Sets the value of the BigNum to C<value>.
=cut
*/
VTABLE void set_number_native(FLOATVAL value) {
bignum_set_double(INTERP, SELF, (double)value);
}
/*
=item C<void set_string_native(STRING *value)>
Sets the value of the BigNum to the result of converting C<*value> to a
number.
=item C<void set_string_keyed_int(INTVAL base, STRING *value)>
Same assume number base C<base>.
=cut
*/
VTABLE void set_string_native(STRING *value) {
char * const s = Parrot_str_to_cstring(INTERP, value);
bignum_set_str(INTERP, SELF, s, 10);
Parrot_str_free_cstring(s);
}
VTABLE void set_string_keyed_int(INTVAL base, STRING *value) {
char * const s = Parrot_str_to_cstring(INTERP, value);
bignum_set_str(INTERP, SELF, s, base);
Parrot_str_free_cstring(s);
}
/*
=item C<void set_pmc(PMC *value)>
Sets the value of the BigNum to the BigNum value of C<*value>.
=cut
*/
VTABLE void set_pmc(PMC *value) {
bignum_set_self(INTERP, SELF, bignum_get_self(INTERP, value));
}
/*
=item C<FLOATVAL get_number()>
Down-converts the precise BigNum to an imprecise double.
=cut
*/
VTABLE FLOATVAL get_number() {
return bignum_get_double(INTERP, SELF);
}
/*
=item C<INTVAL get_integer()>
Returns the integer conversion of the BigNum.
=cut
*/
VTABLE INTVAL get_integer() {
return bignum_get_si(INTERP, SELF);
}
/*
=item C<INTVAL get_ulong()>
Returns the unsigned long conversion of the BigNum.
=cut
*/
VTABLE INTVAL get_ulong() {
return bignum_get_ui(INTERP, SELF);
}
/*
=item C<PMC *get_bignum()>
Returns SELF, keeping floating point precision.
=cut
*/
VTABLE PMC *get_bignum() {
return SELF;
}
/*
=item C<FLOATVAL get_bigint()>
Trunc the BigNum to an BigInt.
=cut
*/
VTABLE BIGINT get_bigint() {
#if PARROT_BIGNUM_CAN_BIGINT
return bignum_get_bigint(INTERP, SELF);
#else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_LIBRARY_ERROR,
"no bigint support in bigint");
#endif
}
/*
=item C<INTVAL get_bool()>
Returns the boolean value of the BigNum.
=cut
*/
VTABLE INTVAL get_bool() {
return bignum_get_bool(INTERP, SELF);
}
/*
=item C<STRING *get_string()>
Returns the string representation of the BigNum.
=item C<STRING *get_string_keyed_int(INTVAL base)>
Returns the string representation of the BigNum in base C<base>.
=item C<STRING *get_string_keyed_int_int(INTVAL base, INTVAL digits)>
Returns the string representation of the BigNum in base C<base> with
C<digits> digits.
=item C<STRING *get_repr()>
Returns the string representation of the BigNum with the letter 'N'
appended.
=cut
*/
VTABLE STRING *get_string() {
char *s = bignum_get_string(INTERP, SELF, 10);
STRING *ps = Parrot_str_new(INTERP, s, 0);
mem_sys_free(s);
return ps;
}
VTABLE STRING *get_string_keyed_int(INTVAL base) {
char *s = bignum_get_string(INTERP, SELF, base);
STRING *ps = Parrot_str_new(INTERP, s, 0);
mem_sys_free(s);
return ps;
}
VTABLE STRING *get_string_keyed_int_int(INTVAL base, INTVAL digits) {
char *s = bignum_get_string_size(INTERP, SELF, base, digits);
STRING *ps = Parrot_str_new(INTERP, s, 0);
mem_sys_free(s);
return ps;
}
VTABLE STRING *get_repr() {
STRING *s = SELF.get_string();
return Parrot_str_append(INTERP, s, Parrot_str_new(interp, "N", 1));
}
/*
=item C<void increment()>
Increment the BigNum by 1.0.
=cut
*/
VTABLE void increment() {
bignum_add_bignum_int(INTERP, SELF, 1, SELF);