2 # Copyright (C) 2001-2007, Parrot Foundation.
7 use lib qw( . lib ../lib ../../lib );
15 t/pmc/bigint.t - BigInt PMC
19 % prove t/pmc/bigint.t
27 if ( $PConfig{gmp} ) {
31 plan skip_all => "No BigInt Lib configured";
34 my $vers_check = <<'EOP';
49 print " is buggy with huge digit multiply - please upgrade\n"
62 if ( $PConfig{gmp} ) {
65 my $parrot = '.' . $PConfig{slash} . 'parrot' . $PConfig{exe};
66 my $test = 'temp_gmp_vers.pir';
67 open my $O, '>', "$test" or die "can't open $test: $!";
70 my $warn = `$parrot $test`;
75 pasm_output_is( <<'CODE', <<'OUT', "create" );
83 pasm_output_is( <<'CODE', <<'OUT', "set/get int" );
98 pasm_output_is( <<"CODE", <<'OUT', "set int, get double" );
99 .include 'fp_equality.pasm'
103 .fp_eq_pasm(N1, 999999.0, OK1)
109 .fp_eq_pasm(N1, -999999.0, OK2)
115 .fp_eq_pasm(N1, 2.147483646e9, OK3)
121 .fp_eq_pasm(N1, -2.147483646e9, OK4)
132 pasm_output_is( <<'CODE', <<'OUT', "set double, get str" );
142 pasm_output_is( <<'CODE', <<'OUT', "set str, get str" );
144 set P0, "1230000000000"
152 pasm_output_is( <<'CODE', <<'OUT', "add" );
162 set P0, "12345678987654321"
163 set P1, "10000000000000000"
174 pasm_output_is( <<'CODE', <<'OUT', "add_int" );
182 set P0, "100000000000000000000"
190 100000000000001000000
193 pasm_output_is( <<'CODE', <<'OUTPUT', "sub bigint" );
204 set P0, "123456789012345678"
207 set P3, "123456789012340000"
211 set P1, "223456789012345678"
213 set P3, "-100000000000000000"
224 pasm_output_is( <<'CODE', <<'OUTPUT', "sub native int" );
233 set P0, "123456789012345678"
236 set P3, "123456789012340000"
246 pasm_output_is( <<'CODE', <<'OUTPUT', "sub other int" );
257 set P0, "123456789012345678"
260 set P3, "123456789012340000"
272 set P0, "9876543219876543"
274 set P3, "9876543219876499"
286 pasm_output_is( <<'CODE', <<'OUT', "mul" );
301 pasm_output_is( <<'CODE', <<'OUT', "mul_int" );
313 pasm_output_is( <<'CODE', <<'OUT', "div bigint" );
315 set P0, "100000000000000000000"
317 set P1, "100000000000000000000"
326 set P3, "10000000000000"
334 set P3, "10000000000000000000"
341 set P3, "-100000000000000000000"
354 pasm_output_is( <<'CODE', <<'OUT', "div native int" );
356 set P0, "100000000000000000000"
360 set P2, "10000000000000000000"
365 set P0, "100000000000000"
377 pasm_output_is( <<'CODE', <<'OUT', "div other int" );
379 set P0, "100000000000000000000"
385 set P2, "10000000000000000000"
390 set P0, "100000000000000"
404 for my $op ( "/", "%" ) {
405 for my $type ( "BigInt", "Integer" ) {
406 pir_output_is( <<"CODE", <<OUTPUT, "bigint $op by zero $type" );
408 \$P0 = new ['BigInt']
409 set \$P0, "1000000000000000000000"
410 \$P1 = new ['BigInt']
411 ## divide by a zero $type
419 get_results '0', \$P0
433 my ( $a, $b, $c, $d, $e );
434 if ( $PConfig{intvalsize} == 8 ) {
435 $a = '9223372036854775806'; # 2**63-2
437 $c = '9223372036854775807'; # still Integer
438 $d = '9223372036854775808'; # no more Integer
439 $e = '9223372036854775809'; # still no more Integer
441 elsif ( $PConfig{intvalsize} == 4 ) {
442 $a = '2147483646'; # 2**31-2
444 $c = '2147483647'; # still Integer
445 $d = '2147483648'; # no more PerlInt
446 $e = '2147483649'; # still no more PerlInt
449 die "\$PConfig{intvalsize} == $PConfig{intvalsize}?\n";
452 pasm_output_is( <<CODE, <<OUT, "add overflow Integer" );
481 pasm_output_is( <<CODE, <<OUT, "add overflow Integer" );
511 pasm_output_is( <<'CODE', <<'OUT', "abs" );
513 set P0, "-1230000000000"
530 pir_output_is( << 'CODE', << 'OUTPUT', "check whether interface is done" );
534 pmc1 = new ['BigInt']
536 does bool1, pmc1, "scalar"
539 does bool1, pmc1, "no_interface"
549 pasm_output_is( <<"CODE", <<'OUTPUT', "Truth" );
551 set P0, "123456789123456789"
565 pasm_output_is( <<"CODE", <<'OUTPUT', "neg" );
568 set P0, "123456789123456789"
570 set P1, "-123456789123456789"
579 pir_output_is( <<'CODE', <<'OUTPUT', "pi() generator" );
581 .local pmc k, a, b, a1, b1
598 .local pmc ta, tb, ta1, tb1
615 unless d == d1 goto end_yield
640 if i < 1000 goto loop
648 k, a, b, a1, b1 = 2, 4, 1, 12, 4
650 p, q, k = k*k, 2*k+1, k+1
651 a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
655 a, a1 = 10*(a%b), 10*(a1%b1)
660 for i in xrange(1, 1001):
675 31415926535897932384626433832795028841971693993751
676 05820974944592307816406286208998628034825342117067
677 98214808651328230664709384460955058223172535940812
678 84811174502841027019385211055596446229489549303819
679 64428810975665933446128475648233786783165271201909
680 14564856692346034861045432664821339360726024914127
681 37245870066063155881748815209209628292540917153643
682 67892590360011330530548820466521384146951941511609
683 43305727036575959195309218611738193261179310511854
684 80744623799627495673518857527248912279381830119491
685 29833673362440656643086021394946395224737190702179
686 86094370277053921717629317675238467481846766940513
687 20005681271452635608277857713427577896091736371787
688 21468440901224953430146549585371050792279689258923
689 54201995611212902196086403441815981362977477130996
690 05187072113499999983729780499510597317328160963185
691 95024459455346908302642522308253344685035261931188
692 17101000313783875288658753320838142061717766914730
693 35982534904287554687311595628638823537875937519577
694 81857780532171226806613001927876611195909216420198
698 pasm_output_is( <<'CODE', <<'OUT', "shl_bigint" );
708 set P0, "100000000000"
720 pir_output_is( <<'CODE', <<'OUT', "shl_bigint with a negative shift" );
721 ## cf the shr_bigint case.
730 set $P0, "102400000000000"
740 pasm_output_is( <<'CODE', <<'OUT', "shl_int" );
750 set P0, "100000000000"
756 set P0, "100000000000"
769 pir_output_is( <<'CODE', <<'OUT', "shl_int with a negative shift" );
770 ## cf the shr_int case.
774 $P1 = new ['Integer']
779 set $P0, "200000000000"
783 set $P0, "102400000000000"
794 pir_output_like( <<'CODE', <<'OUT', "shl_int and i_shl_int promote Integer to Bigint" );
795 ## The result on the second line is a BigInt on 32-bit systems and still an
796 ## Integer on 64-bit systems.
798 $P0 = new ['Integer']
800 $P1 = new ['Integer']
802 $P2 = new ['Integer']
803 ## shift by 10 bits . . .
809 ## then by 20 bits . . .
811 $P3 = new ['Integer']
818 ## then by another 40 bits (total 60) in place.
828 (Integer|BigInt) 1048577048576
829 BigInt 1152922657528351582846976
833 pir_error_output_like( <<'CODE', <<'OUT', "shl_int throws an error when promotion is disabled" );
834 .include "errors.pasm"
836 errorson .PARROT_ERRORS_OVERFLOW_FLAG
837 $P0 = new ['Integer']
839 $P1 = new ['Integer']
841 $P2 = new ['Integer']
842 ## shift by 10 bits . . .
863 pir_output_is( <<'CODE', <<'OUT', "shl_int by 64 bits also promotes to Bigint" );
864 ## The C << and >> ops take the right arg modulo the word size in bits (at least
865 ## on all the systems I have available), so both 32- and 64-bit systems treat
866 ## shifting by 64 bits as shifting by zero.
868 $P0 = new ['Integer']
870 $P1 = new ['Integer']
879 BigInt 18446762520453625325551616
883 <<'CODE', <<'OUT', "shr_int and i_shr_int with a neg shift promote Integer to Bigint" );
885 $P0 = new ['Integer']
887 $P1 = new ['Integer']
889 $P2 = new ['Integer']
890 ## shift by 10 bits . . .
896 ## then by another 50 bits (total 60) in place.
906 BigInt 1152922657528351582846976
909 pasm_output_is( <<'CODE', <<'OUT', "shr_bigint" );
919 set P0, "102400000000000"
931 pir_output_is( <<'CODE', <<'OUT', "shr_bigint with a negative shift" );
932 ## cf the shl_bigint case.
941 set $P0, "100000000000"
951 pasm_output_is( <<'CODE', <<'OUT', "shr_int" );
961 set P0, "200000000000"
967 set P0, "102400000000000"
980 pir_output_is( <<'CODE', <<'OUT', "shr_int with a negative shift" );
981 ## cf the shl_int case.
985 $P1 = new ['Integer']
990 set $P0, "100000000000"
1004 pir_output_is( <<'CODE', <<'OUT', "BUG #34949 gt" );
1019 pir_output_is( <<'CODE', <<'OUT', "BUG #34949 ge" );
1034 pir_output_is( <<'CODE', <<'OUT', "BUG #34949 ne" );
1049 pir_output_is( <<'CODE', <<'OUT', "BUG #34949 eq" );
1064 pir_output_is( <<'CODE', <<'OUT', "BUG #34949 le" );
1079 pir_output_is( <<'CODE', <<'OUT', "BUG #34949 lt" );
1096 # cperl-indent-level: 4
1099 # vim: expandtab shiftwidth=4: