-
Notifications
You must be signed in to change notification settings - Fork 17
/
vmbignum.cpp
6124 lines (5207 loc) · 181 KB
/
vmbignum.cpp
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 RCSID
static char RCSid[] =
"$Header$";
#endif
/*
* Copyright (c) 2000, 2002 Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
vmbignum.cpp - big number metaclass
Function
Notes
Modified
02/18/00 MJRoberts - Creation
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <stdarg.h>
#include <math.h>
#include <float.h>
#include "t3std.h"
#include "vmtype.h"
#include "vmmcreg.h"
#include "vmbignum.h"
#include "vmobj.h"
#include "vmerr.h"
#include "vmerrnum.h"
#include "vmfile.h"
#include "vmpool.h"
#include "vmstack.h"
#include "utf8.h"
#include "vmstr.h"
#include "vmbif.h"
#include "vmmeta.h"
#include "vmlst.h"
#include "vmdatasrc.h"
/* ------------------------------------------------------------------------ */
/*
* statics
*/
/* metaclass registration object */
static CVmMetaclassBigNum metaclass_reg_obj;
CVmMetaclass *CVmObjBigNum::metaclass_reg_ = &metaclass_reg_obj;
/* function table */
int (CVmObjBigNum::
*CVmObjBigNum::func_table_[])(VMG_ vm_obj_id_t self,
vm_val_t *retval, uint *argc) =
{
&CVmObjBigNum::getp_undef,
&CVmObjBigNum::getp_format,
&CVmObjBigNum::getp_equal_rnd,
&CVmObjBigNum::getp_get_prec,
&CVmObjBigNum::getp_set_prec,
&CVmObjBigNum::getp_frac,
&CVmObjBigNum::getp_whole,
&CVmObjBigNum::getp_round_dec,
&CVmObjBigNum::getp_abs,
&CVmObjBigNum::getp_ceil,
&CVmObjBigNum::getp_floor,
&CVmObjBigNum::getp_get_scale,
&CVmObjBigNum::getp_scale,
&CVmObjBigNum::getp_negate,
&CVmObjBigNum::getp_copy_sign,
&CVmObjBigNum::getp_is_neg,
&CVmObjBigNum::getp_remainder,
&CVmObjBigNum::getp_sin,
&CVmObjBigNum::getp_cos,
&CVmObjBigNum::getp_tan,
&CVmObjBigNum::getp_deg2rad,
&CVmObjBigNum::getp_rad2deg,
&CVmObjBigNum::getp_asin,
&CVmObjBigNum::getp_acos,
&CVmObjBigNum::getp_atan,
&CVmObjBigNum::getp_sqrt,
&CVmObjBigNum::getp_ln,
&CVmObjBigNum::getp_exp,
&CVmObjBigNum::getp_log10,
&CVmObjBigNum::getp_pow,
&CVmObjBigNum::getp_sinh,
&CVmObjBigNum::getp_cosh,
&CVmObjBigNum::getp_tanh,
&CVmObjBigNum::getp_pi,
&CVmObjBigNum::getp_e,
&CVmObjBigNum::getp_numType
};
/* constant value 1 */
const unsigned char CVmObjBigNum::one_[] =
{
/* number of digits - we just need one to represent the integer 1 */
0x01, 0x00,
/* exponent */
0x01, 0x00,
/* flags */
0x00,
/* mantissa - just one digit is needed, but pad out the byte with zero */
0x10
};
#if 0
/*
* Pi to 2048 digits
*/
const unsigned char CVmObjBigNum::pi_[] =
{
/* number of digits of precision */
0x00, 0x08,
/* base-10 exponent */
0x01, 0x00,
/* flags */
0x00,
/*
* the first 2048 decimal digits of pi, packed two to a byte (typed
* in from memory - I hope I got everything right :)
*/
/* 1-100 */
0x31, 0x41, 0x59, 0x26, 0x53, 0x58, 0x97, 0x93, 0x23, 0x84,
0x62, 0x64, 0x33, 0x83, 0x27, 0x95, 0x02, 0x88, 0x41, 0x97,
0x16, 0x93, 0x99, 0x37, 0x51, 0x05, 0x82, 0x09, 0x74, 0x94,
0x45, 0x92, 0x30, 0x78, 0x16, 0x40, 0x62, 0x86, 0x20, 0x89,
0x98, 0x62, 0x80, 0x34, 0x82, 0x53, 0x42, 0x11, 0x70, 0x67,
/* 101-200 */
0x98, 0x21, 0x48, 0x08, 0x65, 0x13, 0x28, 0x23, 0x06, 0x64,
0x70, 0x93, 0x84, 0x46, 0x09, 0x55, 0x05, 0x82, 0x23, 0x17,
0x25, 0x35, 0x94, 0x08, 0x12, 0x84, 0x81, 0x11, 0x74, 0x50,
0x28, 0x41, 0x02, 0x70, 0x19, 0x38, 0x52, 0x11, 0x05, 0x55,
0x96, 0x44, 0x62, 0x29, 0x48, 0x95, 0x49, 0x30, 0x38, 0x19,
/* 201-300 */
0x64, 0x42, 0x88, 0x10, 0x97, 0x56, 0x65, 0x93, 0x34, 0x46,
0x12, 0x84, 0x75, 0x64, 0x82, 0x33, 0x78, 0x67, 0x83, 0x16,
0x52, 0x71, 0x20, 0x19, 0x09, 0x14, 0x56, 0x48, 0x56, 0x69,
0x23, 0x46, 0x03, 0x48, 0x61, 0x04, 0x54, 0x32, 0x66, 0x48,
0x21, 0x33, 0x93, 0x60, 0x72, 0x60, 0x24, 0x91, 0x41, 0x27,
/* 301-400 */
0x37, 0x24, 0x58, 0x70, 0x06, 0x60, 0x63, 0x15, 0x58, 0x81,
0x74, 0x88, 0x15, 0x20, 0x92, 0x09, 0x62, 0x82, 0x92, 0x54,
0x09, 0x17, 0x15, 0x36, 0x43, 0x67, 0x89, 0x25, 0x90, 0x36,
0x00, 0x11, 0x33, 0x05, 0x30, 0x54, 0x88, 0x20, 0x46, 0x65,
0x21, 0x38, 0x41, 0x46, 0x95, 0x19, 0x41, 0x51, 0x16, 0x09,
/* 401-500 */
0x43, 0x30, 0x57, 0x27, 0x03, 0x65, 0x75, 0x95, 0x91, 0x95,
0x30, 0x92, 0x18, 0x61, 0x17, 0x38, 0x19, 0x32, 0x61, 0x17,
0x93, 0x10, 0x51, 0x18, 0x54, 0x80, 0x74, 0x46, 0x23, 0x79,
0x96, 0x27, 0x49, 0x56, 0x73, 0x51, 0x88, 0x57, 0x52, 0x72,
0x48, 0x91, 0x22, 0x79, 0x38, 0x18, 0x30, 0x11, 0x94, 0x91,
/* 501-600 */
0x29, 0x83, 0x36, 0x73, 0x36, 0x24, 0x40, 0x65, 0x66, 0x43,
0x08, 0x60, 0x21, 0x39, 0x49, 0x46, 0x39, 0x52, 0x24, 0x73,
0x71, 0x90, 0x70, 0x21, 0x79, 0x86, 0x09, 0x43, 0x70, 0x27,
0x70, 0x53, 0x92, 0x17, 0x17, 0x62, 0x93, 0x17, 0x67, 0x52,
0x38, 0x46, 0x74, 0x81, 0x84, 0x67, 0x66, 0x94, 0x05, 0x13,
/* 601-700 */
0x20, 0x00, 0x56, 0x81, 0x27, 0x14, 0x52, 0x63, 0x56, 0x08,
0x27, 0x78, 0x57, 0x71, 0x34, 0x27, 0x57, 0x78, 0x96, 0x09,
0x17, 0x36, 0x37, 0x17, 0x87, 0x21, 0x46, 0x84, 0x40, 0x90,
0x12, 0x24, 0x95, 0x34, 0x30, 0x14, 0x65, 0x49, 0x58, 0x53,
0x71, 0x05, 0x07, 0x92, 0x27, 0x96, 0x89, 0x25, 0x89, 0x23,
/* 701-800 */
0x54, 0x20, 0x19, 0x95, 0x61, 0x12, 0x12, 0x90, 0x21, 0x96,
0x08, 0x64, 0x03, 0x44, 0x18, 0x15, 0x98, 0x13, 0x62, 0x97,
0x74, 0x77, 0x13, 0x09, 0x96, 0x05, 0x18, 0x70, 0x72, 0x11,
0x34, 0x99, 0x99, 0x99, 0x83, 0x72, 0x97, 0x80, 0x49, 0x95,
0x10, 0x59, 0x73, 0x17, 0x32, 0x81, 0x60, 0x96, 0x31, 0x85,
/* 801-900 */
0x95, 0x02, 0x44, 0x59, 0x45, 0x53, 0x46, 0x90, 0x83, 0x02,
0x64, 0x25, 0x22, 0x30, 0x82, 0x53, 0x34, 0x46, 0x85, 0x03,
0x52, 0x61, 0x93, 0x11, 0x88, 0x17, 0x10, 0x10, 0x00, 0x31,
0x37, 0x83, 0x87, 0x52, 0x88, 0x65, 0x87, 0x53, 0x32, 0x08,
0x38, 0x14, 0x20, 0x61, 0x71, 0x77, 0x66, 0x91, 0x47, 0x30,
/* 901-1000 */
0x35, 0x98, 0x25, 0x34, 0x90, 0x42, 0x87, 0x55, 0x46, 0x87,
0x31, 0x15, 0x95, 0x62, 0x86, 0x38, 0x82, 0x35, 0x37, 0x87,
0x59, 0x37, 0x51, 0x95, 0x77, 0x81, 0x85, 0x77, 0x80, 0x53,
0x21, 0x71, 0x22, 0x68, 0x06, 0x61, 0x30, 0x01, 0x92, 0x78,
0x76, 0x61, 0x11, 0x95, 0x90, 0x92, 0x16, 0x42, 0x01, 0x98,
/* 1001-1100 */
0x93, 0x80, 0x95, 0x25, 0x72, 0x01, 0x06, 0x54, 0x85, 0x86,
0x32, 0x78, 0x86, 0x59, 0x36, 0x15, 0x33, 0x81, 0x82, 0x79,
0x68, 0x23, 0x03, 0x01, 0x95, 0x20, 0x35, 0x30, 0x18, 0x52,
0x96, 0x89, 0x95, 0x77, 0x36, 0x22, 0x59, 0x94, 0x13, 0x89,
0x12, 0x49, 0x72, 0x17, 0x75, 0x28, 0x34, 0x79, 0x13, 0x15,
/* 1101-1200 */
0x15, 0x57, 0x48, 0x57, 0x24, 0x24, 0x54, 0x15, 0x06, 0x95,
0x95, 0x08, 0x29, 0x53, 0x31, 0x16, 0x86, 0x17, 0x27, 0x85,
0x58, 0x89, 0x07, 0x50, 0x98, 0x38, 0x17, 0x54, 0x63, 0x74,
0x64, 0x93, 0x93, 0x19, 0x25, 0x50, 0x60, 0x40, 0x09, 0x27,
0x70, 0x16, 0x71, 0x13, 0x90, 0x09, 0x84, 0x88, 0x24, 0x01,
/* 1201-1300 */
0x28, 0x58, 0x36, 0x16, 0x03, 0x56, 0x37, 0x07, 0x66, 0x01,
0x04, 0x71, 0x01, 0x81, 0x94, 0x29, 0x55, 0x59, 0x61, 0x98,
0x94, 0x67, 0x67, 0x83, 0x74, 0x49, 0x44, 0x82, 0x55, 0x37,
0x97, 0x74, 0x72, 0x68, 0x47, 0x10, 0x40, 0x47, 0x53, 0x46,
0x46, 0x20, 0x80, 0x46, 0x68, 0x42, 0x59, 0x06, 0x94, 0x91,
/* 1301-1400 */
0x29, 0x33, 0x13, 0x67, 0x70, 0x28, 0x98, 0x91, 0x52, 0x10,
0x47, 0x52, 0x16, 0x20, 0x56, 0x96, 0x60, 0x24, 0x05, 0x80,
0x38, 0x15, 0x01, 0x93, 0x51, 0x12, 0x53, 0x38, 0x24, 0x30,
0x03, 0x55, 0x87, 0x64, 0x02, 0x47, 0x49, 0x64, 0x73, 0x26,
0x39, 0x14, 0x19, 0x92, 0x72, 0x60, 0x42, 0x69, 0x92, 0x27,
/* 1401-1500 */
0x96, 0x78, 0x23, 0x54, 0x78, 0x16, 0x36, 0x00, 0x93, 0x41,
0x72, 0x16, 0x41, 0x21, 0x99, 0x24, 0x58, 0x63, 0x15, 0x03,
0x02, 0x86, 0x18, 0x29, 0x74, 0x55, 0x57, 0x06, 0x74, 0x98,
0x38, 0x50, 0x54, 0x94, 0x58, 0x85, 0x86, 0x92, 0x69, 0x95,
0x69, 0x09, 0x27, 0x21, 0x07, 0x97, 0x50, 0x93, 0x02, 0x95,
/* 1501-1600 */
0x53, 0x21, 0x16, 0x53, 0x44, 0x98, 0x72, 0x02, 0x75, 0x59,
0x60, 0x23, 0x64, 0x80, 0x66, 0x54, 0x99, 0x11, 0x98, 0x81,
0x83, 0x47, 0x97, 0x75, 0x35, 0x66, 0x36, 0x98, 0x07, 0x42,
0x65, 0x42, 0x52, 0x78, 0x62, 0x55, 0x18, 0x18, 0x41, 0x75,
0x74, 0x67, 0x28, 0x90, 0x97, 0x77, 0x72, 0x79, 0x38, 0x00,
/* 1601-1700 */
0x08, 0x16, 0x47, 0x06, 0x00, 0x16, 0x14, 0x52, 0x49, 0x19,
0x21, 0x73, 0x21, 0x72, 0x14, 0x77, 0x23, 0x50, 0x14, 0x14,
0x41, 0x97, 0x35, 0x68, 0x54, 0x81, 0x61, 0x36, 0x11, 0x57,
0x35, 0x25, 0x52, 0x13, 0x34, 0x75, 0x74, 0x18, 0x49, 0x46,
0x84, 0x38, 0x52, 0x33, 0x23, 0x90, 0x73, 0x94, 0x14, 0x33,
/* 1701-1800 */
0x34, 0x54, 0x77, 0x62, 0x41, 0x68, 0x62, 0x51, 0x89, 0x83,
0x56, 0x94, 0x85, 0x56, 0x20, 0x99, 0x21, 0x92, 0x22, 0x18,
0x42, 0x72, 0x55, 0x02, 0x54, 0x25, 0x68, 0x87, 0x67, 0x17,
0x90, 0x49, 0x46, 0x01, 0x65, 0x34, 0x66, 0x80, 0x49, 0x88,
0x62, 0x72, 0x32, 0x79, 0x17, 0x86, 0x08, 0x57, 0x84, 0x38,
/* 1801-1900 */
0x38, 0x27, 0x96, 0x79, 0x76, 0x68, 0x14, 0x54, 0x10, 0x09,
0x53, 0x88, 0x37, 0x86, 0x36, 0x09, 0x50, 0x68, 0x00, 0x64,
0x22, 0x51, 0x25, 0x20, 0x51, 0x17, 0x39, 0x29, 0x84, 0x89,
0x60, 0x84, 0x12, 0x84, 0x88, 0x62, 0x69, 0x45, 0x60, 0x42,
0x41, 0x96, 0x52, 0x85, 0x02, 0x22, 0x10, 0x66, 0x11, 0x86,
/* 1901-2000 */
0x30, 0x67, 0x44, 0x27, 0x86, 0x22, 0x03, 0x91, 0x94, 0x94,
0x50, 0x47, 0x12, 0x37, 0x13, 0x78, 0x69, 0x60, 0x95, 0x63,
0x64, 0x37, 0x19, 0x17, 0x28, 0x74, 0x67, 0x76, 0x46, 0x57,
0x57, 0x39, 0x62, 0x41, 0x38, 0x90, 0x86, 0x58, 0x32, 0x64,
0x59, 0x95, 0x81, 0x33, 0x90, 0x47, 0x80, 0x27, 0x59, 0x00,
/* 2001-2048 */
0x99, 0x46, 0x57, 0x64, 0x07, 0x89, 0x51, 0x26, 0x94, 0x68,
0x39, 0x83, 0x52, 0x59, 0x57, 0x09, 0x82, 0x58, 0x22, 0x62,
0x05, 0x22, 0x48, 0x94
};
#endif
/* ------------------------------------------------------------------------ */
/*
* The BigNumber internal cache. This is meant to be private to the
* vmbignum module, but since the module is actually split between
* vmbignum.cpp and vmbignumlib.cpp, we need to use a C++ global. We use
* the S_ naming prefix to emphasize that it's conceptually a "static"
* variable, private to this two-file module.
*/
extern CVmBigNumCache *S_bignum_cache;
/* ------------------------------------------------------------------------ */
/*
* Static creation methods. These routines allocate an object ID and
* create a new object.
*/
/* create dynamically using stack arguments */
vm_obj_id_t CVmObjBigNum::create_from_stack(VMG_ const uchar **pc_ptr,
uint argc)
{
vm_obj_id_t id;
vm_val_t *val;
size_t digits;
const char *strval = 0;
const CVmObjBigNum *objval = 0;
/* check arguments */
if (argc < 1 || argc > 2)
err_throw(VMERR_WRONG_NUM_OF_ARGS);
/*
* check the first argument, which gives the source value - this can be
* an integer, a string, or another BigNumber value
*/
val = G_stk->get(0);
if (val->typ == VM_INT)
{
/* we'll use the integer value */
}
else if (val->typ == VM_OBJ && is_bignum_obj(vmg_ val->val.obj))
{
/* we'll use the BigNumber value as the source */
objval = (CVmObjBigNum *)vm_objp(vmg_ val->val.obj);
}
else if ((strval = val->get_as_string(vmg0_)) != 0)
{
/* we'll parse the string value */
}
else
{
/* it's not a source type we accept */
err_throw(VMERR_NUM_VAL_REQD);
}
/*
* get the precision value, if provided; if not, infer it from the
* source value
*/
if (argc > 1)
{
/* make sure it's an integer */
if (G_stk->get(1)->typ != VM_INT)
err_throw(VMERR_INT_VAL_REQD);
/* retrieve the value */
digits = (size_t)G_stk->get(1)->val.intval;
}
else if (strval != 0)
{
/* parse the string to infer the precision */
digits = precision_from_string(
strval + VMB_LEN, vmb_get_len(strval), 10);
}
else if (objval != 0)
{
/* use the same precision as the source BigNumber value */
digits = get_prec(objval->get_ext());
}
else
{
/* use a default precision */
digits = 32;
}
/* create the value */
if (strval != 0)
{
/* create the value from the string */
id = vm_new_id(vmg_ FALSE, FALSE, FALSE);
new (vmg_ id) CVmObjBigNum(vmg_ strval + VMB_LEN,
vmb_get_len(strval), digits);
}
else if (objval != 0)
{
vm_val_t new_val;
/* create the value based on the BigNumber value */
round_val(vmg_ &new_val, objval->get_ext(), digits, TRUE);
/* return the new object ID */
id = new_val.val.obj;
}
else
{
/* create the value based on the integer value */
id = vm_new_id(vmg_ FALSE, FALSE, FALSE);
new (vmg_ id) CVmObjBigNum(vmg_ (long)val->val.intval, digits);
}
/* discard arguments */
G_stk->discard(argc);
/* return the new object */
return id;
}
/* create with a given precision */
vm_obj_id_t CVmObjBigNum::create(VMG_ int in_root_set, size_t digits)
{
vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE);
new (vmg_ id) CVmObjBigNum(vmg_ digits);
return id;
}
/* create from an integer value */
vm_obj_id_t CVmObjBigNum::create(VMG_ int in_root_set,
long val, size_t digits)
{
vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE);
new (vmg_ id) CVmObjBigNum(vmg_ val, digits);
return id;
}
/* create from an unsigned integer value */
vm_obj_id_t CVmObjBigNum::createu(VMG_ int in_root_set,
ulong val, size_t digits)
{
vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE);
new (vmg_ id) CVmObjBigNum(vmg_ digits);
set_uint_val(((CVmObjBigNum *)vm_objp(vmg_ id))->get_ext(), val);
return id;
}
/* create from a 64-bit unsigned int expressed as two 32-bit segments */
vm_obj_id_t CVmObjBigNum::create_int64(VMG_ int in_root_set,
uint32_t hi, uint32_t lo)
{
/*
* Store into our portable 8-byte format: the portable format is
* little-endian, so simply store the low part in the first four bytes,
* and the high part in the second four bytes.
*/
char buf[8];
oswp4(buf, lo);
oswp4(buf+4, hi);
/*
* Now create the value from the buffer. (This could be implemented a
* little more efficiently by working directly from the integers, but
* this is certainly not on any critical paths, and the create_rp8 code
* already exists and works.)
*/
return create_rp8(vmg_ in_root_set, buf);
}
/* create from a 64-bit unsigned int expressed in portable 8-byte format */
vm_obj_id_t CVmObjBigNum::create_rp8(VMG_ int in_root_set,
const char *buf)
{
/*
* create the BigNumber object big enough to hold a 64-bit integer
* value, which can have up to 20 digits
*/
vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE);
new (vmg_ id) CVmObjBigNum(vmg_ 20);
CVmObjBigNum *n = (CVmObjBigNum *)vm_objp(vmg_ id);
/* set the value */
n->set_rp8((const uchar *)buf);
/* the source value is unsigned, so it's always non-negative */
set_neg(n->get_ext(), FALSE);
/* return the object */
return id;
}
/* create from a 64-bit signed int */
vm_obj_id_t CVmObjBigNum::create_rp8s(VMG_ int in_root_set,
const char *buf)
{
/*
* create the BigNumber object big enough to hold a 64-bit integer
* value, which can have up to 20 digits
*/
vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE);
new (vmg_ id) CVmObjBigNum(vmg_ 20);
CVmObjBigNum *n = (CVmObjBigNum *)vm_objp(vmg_ id);
/* make a private copy of the source value */
uchar p[8];
memcpy(p, buf, 8);
/*
* If the value is negative, as indicated by the sign bit on the most
* significant byte, get the absolute value by computing the two's
* complement. This makes the arithmetic easy because we can do the
* bit twiddling with unsigned values, and just set the BigNum sign bit
* when we're done with all of that.
*
* Note that this 2's complement arithmetic is truly portable - it
* doesn't matter whether the local machine's native int type is
* big-endian or little-endian or some bizarre interleaved-endian, or
* if it uses 2's complement notation or some other wacky bygone scheme
* (1's complement, sign bit only, etc). The reason this code is
* portable regardless of the local notation is that we're not
* operating on local int types: we're operating on a buffer that's in
* a universal notation that we define as 64-bit 2's complement
* little-endian. We're doing our work at the byte level, so as long
* as the local machine has 8+-bit bytes (which is a requirement for
* the platform to have a C compiler), this code is portable.
*/
int neg = (p[7] & 0x80) != 0;
if (neg)
twos_complement_p8(p);
/* set the 64-bit int value */
n->set_rp8(p);
/* set the sign bit */
set_neg(n->get_ext(), neg);
/* return the object */
return id;
}
/* set the value from an unsigned 64-bit little-endian buffer */
void CVmObjBigNum::set_rp8(const uchar *p)
{
/*
* set up temporary registers with enough precision for 64-bit ints (we
* need at most 20 digits for these values)
*/
char tmp1[5 + 20] = { 20, 0, 0, 0, 0 };
char tmp2[5 + 20] = { 20, 0, 0, 0, 0 };
/*
* shift in bits 24 at a time - this makes the math fit in 32-bit ints
* so that we don't have to worry about int overflows
*/
set_uint_val(get_ext(),
(((ulong)p[7]) << 16) | (((ulong)p[6]) << 8) | p[5]);
mul_by_long(get_ext(), 0x01000000);
set_uint_val(tmp1, (((ulong)p[4]) << 16) | (((ulong)p[3]) << 8) | p[2]);
compute_sum_into(tmp2, get_ext(), tmp1);
mul_by_long(tmp2, 0x10000);
set_uint_val(tmp1, (((ulong)p[1]) << 8) | p[0]);
compute_sum_into(get_ext(), tmp2, tmp1);
}
/* create from a string value */
vm_obj_id_t CVmObjBigNum::create(VMG_ int in_root_set,
const char *str, size_t len, size_t digits)
{
vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE);
new (vmg_ id) CVmObjBigNum(vmg_ str, len, digits);
return id;
}
/* create from a string value, inferring the required precision */
vm_obj_id_t CVmObjBigNum::create(VMG_ int in_root_set,
const char *str, size_t len)
{
int digits = precision_from_string(str, len, 10);
vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE);
new (vmg_ id) CVmObjBigNum(vmg_ str, len, digits);
return id;
}
/* create from a string value in a given radix */
vm_obj_id_t CVmObjBigNum::create_radix(
VMG_ int in_root_set, const char *str, size_t len, int radix)
{
int digits = precision_from_string(str, len, radix);
vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE);
if (radix == 10)
new (vmg_ id) CVmObjBigNum(vmg_ str, len, digits);
else
{
new (vmg_ id) CVmObjBigNum(vmg_ digits);
((CVmObjBigNum *)vm_objp(vmg_ id))->set_str_val(str, len, radix);
}
return id;
}
/* create from a double value, with enough precision for a native double */
vm_obj_id_t CVmObjBigNum::create(VMG_ int in_root_set, double val)
{
int digits = DBL_DIG + 2;
vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE);
new (vmg_ id) CVmObjBigNum(vmg_ val, digits);
return id;
}
/* create from a double value, with specified precision */
vm_obj_id_t CVmObjBigNum::create(VMG_ int in_root_set, double val,
size_t digits)
{
vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE);
new (vmg_ id) CVmObjBigNum(vmg_ val, digits);
return id;
}
/*
* Create from a BER-encoded compressed integer value
*/
vm_obj_id_t CVmObjBigNum::create_from_ber(VMG_ int in_root_set,
const char *buf, size_t len)
{
/*
* Create the object. The source value has 7 bits of precision per
* byte. We need log(2)/log(10) digits of precision per bit of
* precision, which is about 0.30103 digits per bit.
*/
int prec = (int)(len*7*0.30103 + 0.5);
vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE);
new (vmg_ id) CVmObjBigNum(vmg_ prec);
CVmObjBigNum *n = (CVmObjBigNum *)vm_objp(vmg_ id);
char *ext = n->get_ext();
/* allocate two temporary registers for intermediate sums */
char *t1, *t2;
uint hdl1, hdl2;
alloc_temp_regs((size_t)prec, 2, &t1, &hdl1, &t2, &hdl2);
/* zero the accumulator (start with t1) */
set_zero(t1);
/* set up a stack register for the base-128 digits */
char dig[5 + 3] = { 3, 0, 0, 0, 0 };
/* starting at the most significant byte, shift in the bits */
for (size_t i = 0 ; i < len ; ++i)
{
/* multiply the accumulator by 2^7 */
mul_by_long(t1, 128);
/* add in the next base-128 digit */
set_uint_val(dig, buf[i] & 0x7f);
compute_sum_into(t2, t1, dig);
/* swap registers so that t2 becomes the accumulator */
char *tt = t1; t1 = t2; t2 = tt;
}
/* copy the accumulator into the result */
copy_val(ext, t1, FALSE);
/* done with the temporary registers */
release_temp_regs(2, hdl1, hdl2);
/* return the new ID */
return id;
}
/*
* Create from a floating point value encoded in an IEEE 754-2008 binary
* interchange format, little-endian order. We accept bit sizes of 16, 32,
* 64, and 128 (half, single, double, and quad precision).
*
*/
vm_obj_id_t CVmObjBigNum::create_from_ieee754(
VMG_ int in_root_set, const char *buf, int bits)
{
/*
* Create the object. Use the decimal equivalent precision of the IEEE
* 754 object. Note that we use 17 digits for doubles; the binary type
* stores 53 bits of mantissa == 15.95 decimal digits, but there's a
* pathological case at 1+epsilon, which is 1 + 2e-16, which requires
* 17 decimal digits to store.
*/
int prec = (bits == 16 ? 4 : bits == 32 ? 8 : bits == 64 ? 17 :
bits == 128 ? 35 : 8);
vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE);
new (vmg_ id) CVmObjBigNum(vmg_ prec);
CVmObjBigNum *n = (CVmObjBigNum *)vm_objp(vmg_ id);
/* decode the IEEE 754 value */
n->set_ieee754_value(vmg_ buf, bits);
/* return the new object id */
return id;
}
/* ------------------------------------------------------------------------ */
/*
* Cast a value to BigNumber
*/
void CVmObjBigNum::cast_to_bignum(VMG_ vm_val_t *bnval,
const vm_val_t *srcval)
{
const char *str;
if (srcval->typ == VM_INT)
{
/* create from the integer value */
bnval->set_obj(create(
vmg_ FALSE, (long)srcval->val.intval, (size_t)10));
}
else if ((str = srcval->get_as_string(vmg0_)) != 0)
{
/* get the string length and buffer pointer */
size_t len = vmb_get_len(str);
str += VMB_LEN;
/* create from the string value */
bnval->set_obj(create(vmg_ FALSE, str, len));
}
else if (srcval->typ == VM_OBJ && is_bignum_obj(vmg_ srcval->val.obj))
{
/* it's already a BigNumber - just return the same value */
bnval->set_obj(srcval->val.obj);
}
else
{
/* can't cast to BigNumber */
err_throw(VMERR_NO_BIGNUM_CONV);
}
}
/*
* Promote an integer value to BigNumber
*/
void CVmObjBigNum::promote_int(VMG_ vm_val_t *val) const
{
val->set_obj(create(vmg_ FALSE, (long)val->val.intval, (size_t)10));
}
/* ------------------------------------------------------------------------ */
/*
* Constructors. These are called indirectly through our static
* creation methods.
*/
/*
* Create with no extension
*/
CVmObjBigNum::CVmObjBigNum()
{
/* no extension */
ext_ = 0;
}
/*
* Create with a given precision
*/
CVmObjBigNum::CVmObjBigNum(VMG_ size_t digits)
{
/* allocate space */
alloc_bignum(vmg_ digits);
}
/*
* Create with a given integer value
*/
CVmObjBigNum::CVmObjBigNum(VMG_ long val, size_t digits)
{
/* allocate space */
alloc_bignum(vmg_ digits);
/* set the value */
set_int_val(ext_, val);
}
/*
* Create with a given string as the source value
*/
CVmObjBigNum::CVmObjBigNum(VMG_ const char *str, size_t len, size_t digits)
{
/* allocate space */
alloc_bignum(vmg_ digits);
/* set the value */
set_str_val(str, len);
}
/*
* Create from a double value
*/
CVmObjBigNum::CVmObjBigNum(VMG_ double val, size_t digits)
{
/* allocate space */
alloc_bignum(vmg_ digits);
/* set the value */
set_double_val(ext_, val);
}
/* ------------------------------------------------------------------------ */
/*
* Delete
*/
void CVmObjBigNum::notify_delete(VMG_ int in_root_set)
{
/*
* free our extension - do this only if it's not in the root set,
* because extension will be directly in the image data for a root
* set object
*/
if (ext_ != 0 && !in_root_set)
G_mem->get_var_heap()->free_mem(ext_);
}
/* ------------------------------------------------------------------------ */
/*
* Allocate space for a given precision
*/
void CVmObjBigNum::alloc_bignum(VMG_ size_t digits)
{
/* allocate space for the given number of elements */
ext_ = (char *)G_mem->get_var_heap()
->alloc_mem(calc_alloc(digits), this);
/* set the precision */
set_prec(ext_, digits);
/* initialize the value to zero */
set_int_val(ext_, 0);
/* clear the flags */
ext_[VMBN_FLAGS] = 0;
}
/* ------------------------------------------------------------------------ */
/*
* Write to a 'data' mode file
*/
int CVmObjBigNum::write_to_data_file(CVmDataSource *fp)
{
char buf[16];
/* write the number of digits (i.e., the precision) */
oswp2(buf, get_prec(ext_));
if (fp->write(buf, 2))
return 1;
/* write our entire extension */
if (fp->write(ext_, calc_alloc(get_prec(ext_))))
return 1;
/* success */
return 0;
}
/*
* Read from a 'data' mode file and instantiate a new BigNumber object to
* hold the result
*/
int CVmObjBigNum::read_from_data_file(VMG_ vm_val_t *retval, CVmDataSource *fp)
{
char buf[16];
size_t prec;
CVmObjBigNum *bignum;
/* read the precision */
if (fp->read(buf, 2))
return 1;
prec = osrp2(buf);
/* create a BigNumber with the required precision */
retval->set_obj(create(vmg_ FALSE, prec));
bignum = (CVmObjBigNum *)vm_objp(vmg_ retval->val.obj);
/* read the bytes into the new object's extension */
if (fp->read(bignum->get_ext(), calc_alloc(prec)))
return 1;
/* success */
return 0;
}
/* ------------------------------------------------------------------------ */
/*
* Set my value to a string
*/
void CVmObjBigNum::set_str_val(const char *str, size_t len)
{
/* parse the string into my extension */
parse_str_into(ext_, str, len);
}
/*
* Set my string value with a given radix. If the radix is decimal, we'll
* use the regular floating point parser. Otherwise we'll parse it as an
* integer value in the given radix.
*/
void CVmObjBigNum::set_str_val(const char *str, size_t len, int radix)
{
/* parse the string into my extension using the given radix */
parse_str_into(ext_, str, len, radix);
}
/* ------------------------------------------------------------------------ */
/*
* IEEE 754-2008 binary interchange format buffer manipulation. We arrange
* our buffer in little-endian order to match the idiosyncratic TADS
* pack/unpack formats, but otherwise we use the standard IEEE formats.
*/
/* IEEE 754-2008 buffer */
struct ieee754
{
ieee754(char *buf, int bits)
{
/* remember the buffer and bit size */
this->buf = (unsigned char *)buf;
this->bits = bits;
this->bytes = bits/8;
/* get the mantissa size (in bits) for the format size */
mbits = (bits == 16 ? 10 :
bits == 32 ? 23 :
bits == 64 ? 52 :
bits == 128 ? 112 :
0);
if (mbits == 0)
err_throw(VMERR_NO_DOUBLE_CONV);
/* the exponent uses the leftover bits, minus the sign bit */
ebits = bits - mbits - 1;
/* figure the exponent bias - it's the halfway point for the range */
ebias = (1 << (ebits - 1)) - 1;
/* figure the exponent range */
emin = 1 - ebias;
emax = ebias;
}
/*
* Get the number of decimal digits this type can represent. The
* decimal precision is fractional, since the IEEE type is binary; we
* round up to the next integer. There's a
*/
int decimal_digits()
{
return bits == 16 ? 4 : bits == 32 ? 8 : bits == 64 ? 17 :
bits == 128 ? 35 : 0;
}
/* get the maximum number of decimal digits in the exponent */
int decimal_exp_digits()
{
return ebits <= 5 ? 2 : ebits <= 7 ? 3 : ebits <= 11 ? 4 : 5;
}
/* is the value infinity? */
int is_infinity() const
{
/* infinity has an exponent of emax+1 and a zero mantissa */
return get_exp() == emax + 1 && is_mantissa_zero();
}
/* set Infinity */
void set_infinity(int neg)
{
/*
* Infinity is represented with the maximum exponent plus one, and
* the mantissa set to all zeros. Infinities can be positive or
* negative, so set the sign bit.
*/
memset(buf, 0, bytes);
set_exp(emax + 1);
set_sign(neg);
}
/* is the value zero? */
int is_zero() const
{
/*
* a zero is represented by all bits zero except possibly the sign
* bit, which is 0 for +0 and 1 for -0
*/
int sum = 0;
for (int i = 0 ; i < bytes - 1 ; ++i)
sum += buf[i];
return sum == 0 && (buf[bytes-1] & 0x7f) == 0;
}
/* set Zero */
void set_zero(int neg)
{
/*
* Zero is represented with an exponent of zero and all zero bits
* in the mantissa. Zeros can be positive or negative, so set the
* sign bit.
*/
memset(buf, 0, bytes);
set_sign(neg);
}
/* is this a NAN value? */
int is_nan() const
{
/* a NAN value has an exponent of emax+1 and a non-zero mantissa */
return get_exp() == emax + 1 && !is_mantissa_zero();