--- /dev/null
+#include "EXTERN.h"
+#include "perl.h"
+#include "XSUB.h"
+
+double XS_BASE = 0;
+double XS_BASE_LEN = 0;
+
+MODULE = Math::BigInt::FastCalc PACKAGE = Math::BigInt::FastCalc
+
+ #############################################################################
+ # 2002-08-12 0.03 Tels unreleased
+ # * is_zero/is_one/is_odd/is_even/len work now (pass v1.61 tests)
+ # 2002-08-13 0.04 Tels unreleased
+ # * returns no/yes for is_foo() methods to be faster
+ # 2002-08-18 0.06alpha
+ # * added _num(), _inc() and _dec()
+ # 2002-08-25 0.06 Tels
+ # * added __strip_zeros(), _copy()
+ # 2004-08-13 0.07 Tels
+ # * added _is_two(), _is_ten(), _ten()
+
+void
+_set_XS_BASE(BASE, BASE_LEN)
+ SV* BASE
+ SV* BASE_LEN
+
+ CODE:
+ XS_BASE = SvNV(BASE);
+ XS_BASE_LEN = SvIV(BASE_LEN);
+
+##############################################################################
+# _copy
+
+void
+_copy(class, x)
+ SV* x
+ INIT:
+ AV* a;
+ AV* a2;
+ I32 elems;
+
+ CODE:
+ a = (AV*)SvRV(x); /* ref to aray, don't check ref */
+ elems = av_len(a); /* number of elems in array */
+ a2 = (AV*)sv_2mortal((SV*)newAV());
+ av_extend (a2, elems); /* prepadd */
+ while (elems >= 0)
+ {
+ /* av_store( a2, elems, newSVsv( (SV*)*av_fetch(a, elems, 0) ) ); */
+
+ /* looking and trying to preserve IV is actually slower when copying */
+ /* temp = (SV*)*av_fetch(a, elems, 0);
+ if (SvIOK(temp))
+ {
+ av_store( a2, elems, newSViv( SvIV( (SV*)*av_fetch(a, elems, 0) )));
+ }
+ else
+ {
+ av_store( a2, elems, newSVnv( SvNV( (SV*)*av_fetch(a, elems, 0) )));
+ }
+ */
+ av_store( a2, elems, newSVnv( SvNV( (SV*)*av_fetch(a, elems, 0) )));
+ elems--;
+ }
+ ST(0) = sv_2mortal( newRV_inc((SV*) a2) );
+
+##############################################################################
+# __strip_zeros (also check for empty arrays from div)
+
+void
+__strip_zeros(x)
+ SV* x
+ INIT:
+ AV* a;
+ SV* temp;
+ I32 elems;
+ I32 index;
+
+ CODE:
+ a = (AV*)SvRV(x); /* ref to aray, don't check ref */
+ elems = av_len(a); /* number of elems in array */
+ ST(0) = x; /* we return x */
+ if (elems == -1)
+ {
+ av_push (a, newSViv(0)); /* correct empty arrays */
+ XSRETURN(1);
+ }
+ if (elems == 0)
+ {
+ XSRETURN(1); /* nothing to do since only one elem */
+ }
+ index = elems;
+ while (index > 0)
+ {
+ temp = *av_fetch(a, index, 0); /* fetch ptr to current element */
+ if (SvNV(temp) != 0)
+ {
+ break;
+ }
+ index--;
+ }
+ if (index < elems)
+ {
+ index = elems - index;
+ while (index-- > 0)
+ {
+ av_pop (a);
+ }
+ }
+ XSRETURN(1);
+
+##############################################################################
+# decrement (subtract one)
+
+void
+_dec(class,x)
+ SV* x
+ INIT:
+ AV* a;
+ SV* temp;
+ I32 elems;
+ I32 index;
+ NV MAX;
+
+ CODE:
+ a = (AV*)SvRV(x); /* ref to aray, don't check ref */
+ elems = av_len(a); /* number of elems in array */
+ ST(0) = x; /* we return x */
+
+ MAX = XS_BASE - 1;
+ index = 0;
+ while (index <= elems)
+ {
+ temp = *av_fetch(a, index, 0); /* fetch ptr to current element */
+ sv_setnv (temp, SvNV(temp)-1);
+ if (SvNV(temp) >= 0)
+ {
+ break; /* early out */
+ }
+ sv_setnv (temp, MAX); /* overflow, so set this to $MAX */
+ index++;
+ }
+ /* do have more than one element? */
+ /* (more than one because [0] should be kept as single-element) */
+ if (elems > 0)
+ {
+ temp = *av_fetch(a, elems, 0); /* fetch last element */
+ if (SvIV(temp) == 0) /* did last elem overflow? */
+ {
+ av_pop(a); /* yes, so shrink array */
+ /* aka remove leading zeros */
+ }
+ }
+ XSRETURN(1); /* return x */
+
+##############################################################################
+# increment (add one)
+
+void
+_inc(class,x)
+ SV* x
+ INIT:
+ AV* a;
+ SV* temp;
+ I32 elems;
+ I32 index;
+ NV BASE;
+
+ CODE:
+ a = (AV*)SvRV(x); /* ref to aray, don't check ref */
+ elems = av_len(a); /* number of elems in array */
+ ST(0) = x; /* we return x */
+
+ BASE = XS_BASE;
+ index = 0;
+ while (index <= elems)
+ {
+ temp = *av_fetch(a, index, 0); /* fetch ptr to current element */
+ sv_setnv (temp, SvNV(temp)+1);
+ if (SvNV(temp) < BASE)
+ {
+ XSRETURN(1); /* return (early out) */
+ }
+ sv_setiv (temp, 0); /* overflow, so set this elem to 0 */
+ index++;
+ }
+ temp = *av_fetch(a, elems, 0); /* fetch last element */
+ if (SvIV(temp) == 0) /* did last elem overflow? */
+ {
+ av_push(a, newSViv(1)); /* yes, so extend array by 1 */
+ }
+ XSRETURN(1); /* return x */
+
+##############################################################################
+# Make a number (scalar int/float) from a BigInt object
+
+void
+_num(class,x)
+ SV* x
+ INIT:
+ AV* a;
+ NV fac;
+ SV* temp;
+ NV num;
+ I32 elems;
+ I32 index;
+ NV BASE;
+
+ CODE:
+ a = (AV*)SvRV(x); /* ref to aray, don't check ref */
+ elems = av_len(a); /* number of elems in array */
+
+ if (elems == 0) /* only one element? */
+ {
+ ST(0) = *av_fetch(a, 0, 0); /* fetch first (only) element */
+ XSRETURN(1); /* return it */
+ }
+ fac = 1.0; /* factor */
+ index = 0;
+ num = 0.0;
+ BASE = XS_BASE;
+ while (index <= elems)
+ {
+ temp = *av_fetch(a, index, 0); /* fetch current element */
+ num += fac * SvNV(temp);
+ fac *= BASE;
+ index++;
+ }
+ ST(0) = newSVnv(num);
+
+##############################################################################
+
+void
+_zero(class)
+ INIT:
+ AV* a;
+
+ CODE:
+ a = newAV();
+ av_push (a, newSViv( 0 )); /* zero */
+ ST(0) = newRV_noinc((SV*) a);
+
+##############################################################################
+
+void
+_one(class)
+ INIT:
+ AV* a;
+
+ CODE:
+ a = newAV();
+ av_push (a, newSViv( 1 )); /* one */
+ ST(0) = newRV_noinc((SV*) a);
+
+##############################################################################
+
+void
+_two(class)
+ INIT:
+ AV* a;
+
+ CODE:
+ a = newAV();
+ av_push (a, newSViv( 2 )); /* two */
+ ST(0) = newRV_noinc((SV*) a);
+
+##############################################################################
+
+void
+_ten(class)
+ INIT:
+ AV* a;
+
+ CODE:
+ a = newAV();
+ av_push (a, newSViv( 10 )); /* ten */
+ ST(0) = newRV_noinc((SV*) a);
+
+##############################################################################
+
+void
+_is_even(class, x)
+ SV* x
+ INIT:
+ AV* a;
+ SV* temp;
+
+ CODE:
+ a = (AV*)SvRV(x); /* ref to aray, don't check ref */
+ temp = *av_fetch(a, 0, 0); /* fetch first element */
+ ST(0) = boolSV((SvIV(temp) & 1) == 0);
+
+##############################################################################
+
+void
+_is_odd(class, x)
+ SV* x
+ INIT:
+ AV* a;
+ SV* temp;
+
+ CODE:
+ a = (AV*)SvRV(x); /* ref to aray, don't check ref */
+ temp = *av_fetch(a, 0, 0); /* fetch first element */
+ ST(0) = boolSV((SvIV(temp) & 1) != 0);
+
+##############################################################################
+
+void
+_is_one(class, x)
+ SV* x
+ INIT:
+ AV* a;
+ SV* temp;
+
+ CODE:
+ a = (AV*)SvRV(x); /* ref to aray, don't check ref */
+ if ( av_len(a) != 0)
+ {
+ ST(0) = &PL_sv_no;
+ XSRETURN(1); /* len != 1, can't be '1' */
+ }
+ temp = *av_fetch(a, 0, 0); /* fetch first element */
+ ST(0) = boolSV((SvIV(temp) == 1));
+
+##############################################################################
+
+void
+_is_two(class, x)
+ SV* x
+ INIT:
+ AV* a;
+ SV* temp;
+
+ CODE:
+ a = (AV*)SvRV(x); /* ref to aray, don't check ref */
+ if ( av_len(a) != 0)
+ {
+ ST(0) = &PL_sv_no;
+ XSRETURN(1); /* len != 1, can't be '2' */
+ }
+ temp = *av_fetch(a, 0, 0); /* fetch first element */
+ ST(0) = boolSV((SvIV(temp) == 2));
+
+##############################################################################
+
+void
+_is_ten(class, x)
+ SV* x
+ INIT:
+ AV* a;
+ SV* temp;
+
+ CODE:
+ a = (AV*)SvRV(x); /* ref to aray, don't check ref */
+ if ( av_len(a) != 0)
+ {
+ ST(0) = &PL_sv_no;
+ XSRETURN(1); /* len != 1, can't be '10' */
+ }
+ temp = *av_fetch(a, 0, 0); /* fetch first element */
+ ST(0) = boolSV((SvIV(temp) == 10));
+
+##############################################################################
+
+void
+_is_zero(class, x)
+ SV* x
+ INIT:
+ AV* a;
+ SV* temp;
+
+ CODE:
+ a = (AV*)SvRV(x); /* ref to aray, don't check ref */
+ if ( av_len(a) != 0)
+ {
+ ST(0) = &PL_sv_no;
+ XSRETURN(1); /* len != 1, can't be '0' */
+ }
+ temp = *av_fetch(a, 0, 0); /* fetch first element */
+ ST(0) = boolSV((SvIV(temp) == 0));
+
+##############################################################################
+
+void
+_len(class,x)
+ SV* x
+ INIT:
+ AV* a;
+ SV* temp;
+ NV elems;
+ STRLEN len;
+
+ CODE:
+ a = (AV*)SvRV(x); /* ref to aray, don't check ref */
+ elems = (NV) av_len(a); /* number of elems in array */
+ temp = *av_fetch(a, elems, 0); /* fetch last element */
+ SvPV(temp, len); /* convert to string & store length */
+ len += XS_BASE_LEN * elems;
+ ST(0) = newSViv(len);
+
+##############################################################################
+
+void
+_acmp(class, cx, cy);
+ SV* cx
+ SV* cy
+ INIT:
+ AV* array_x;
+ AV* array_y;
+ I32 elemsx, elemsy, diff;
+ SV* tempx;
+ SV* tempy;
+ STRLEN lenx;
+ STRLEN leny;
+ NV diff_nv;
+ I32 diff_str;
+
+ CODE:
+ array_x = (AV*)SvRV(cx); /* ref to aray, don't check ref */
+ array_y = (AV*)SvRV(cy); /* ref to aray, don't check ref */
+ elemsx = av_len(array_x);
+ elemsy = av_len(array_y);
+ diff = elemsx - elemsy; /* difference */
+
+ if (diff > 0)
+ {
+ ST(0) = newSViv(1); /* len differs: X > Y */
+ XSRETURN(1);
+ }
+ if (diff < 0)
+ {
+ ST(0) = newSViv(-1); /* len differs: X < Y */
+ XSRETURN(1);
+ }
+ /* both have same number of elements, so check length of last element
+ and see if it differes */
+ tempx = *av_fetch(array_x, elemsx, 0); /* fetch last element */
+ tempy = *av_fetch(array_y, elemsx, 0); /* fetch last element */
+ SvPV(tempx, lenx); /* convert to string & store length */
+ SvPV(tempy, leny); /* convert to string & store length */
+ diff_str = (I32)lenx - (I32)leny;
+ if (diff_str > 0)
+ {
+ ST(0) = newSViv(1); /* same len, but first elems differs in len */
+ XSRETURN(1);
+ }
+ if (diff_str < 0)
+ {
+ ST(0) = newSViv(-1); /* same len, but first elems differs in len */
+ XSRETURN(1);
+ }
+ /* same number of digits, so need to make a full compare */
+ diff_nv = 0;
+ while (elemsx >= 0)
+ {
+ tempx = *av_fetch(array_x, elemsx, 0); /* fetch curr x element */
+ tempy = *av_fetch(array_y, elemsx, 0); /* fetch curr y element */
+ diff_nv = SvNV(tempx) - SvNV(tempy);
+ if (diff_nv != 0)
+ {
+ break;
+ }
+ elemsx--;
+ }
+ if (diff_nv > 0)
+ {
+ ST(0) = newSViv(1);
+ XSRETURN(1);
+ }
+ if (diff_nv < 0)
+ {
+ ST(0) = newSViv(-1);
+ XSRETURN(1);
+ }
+ ST(0) = newSViv(0); /* equal */
+
--- /dev/null
+#!/usr/bin/perl -w
+
+use strict;
+use Test;
+
+BEGIN
+ {
+ $| = 1;
+ chdir 't' if -d 't';
+ unshift @INC, '../lib'; # for running manually
+ unshift @INC, '../blib/arch'; # for running manually
+ plan tests => 308;
+ }
+
+use Math::BigInt::FastCalc;
+
+my ($BASE_LEN, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN_SMALL, $MAX_VAL) =
+ Math::BigInt::FastCalc->_base_len();
+
+print "# BASE_LEN = $BASE_LEN\n";
+print "# MAX_VAL = $MAX_VAL\n";
+print "# AND_BITS = $AND_BITS\n";
+print "# XOR_BITS = $XOR_BITS\n";
+print "# IOR_BITS = $OR_BITS\n";
+
+# testing of Math::BigInt::FastCalc
+
+my $C = 'Math::BigInt::FastCalc'; # pass classname to sub's
+
+# _new and _str
+my $x = $C->_new("123"); my $y = $C->_new("321");
+ok (ref($x),'ARRAY'); ok ($C->_str($x),123); ok ($C->_str($y),321);
+
+###############################################################################
+# _add, _sub, _mul, _div
+ok ($C->_str($C->_add($x,$y)),444);
+ok ($C->_str($C->_sub($x,$y)),123);
+ok ($C->_str($C->_mul($x,$y)),39483);
+ok ($C->_str($C->_div($x,$y)),123);
+
+###############################################################################
+# check that mul/div doesn't change $y
+# and returns the same reference, not something new
+ok ($C->_str($C->_mul($x,$y)),39483);
+ok ($C->_str($x),39483); ok ($C->_str($y),321);
+
+ok ($C->_str($C->_div($x,$y)),123);
+ok ($C->_str($x),123); ok ($C->_str($y),321);
+
+$x = $C->_new("39483");
+my ($x1,$r1) = $C->_div($x,$y);
+ok ("$x1","$x");
+$C->_inc($x1);
+ok ("$x1","$x");
+ok ($C->_str($r1),'0');
+
+$x = $C->_new("39483"); # reset
+
+###############################################################################
+my $z = $C->_new("2");
+ok ($C->_str($C->_add($x,$z)),39485);
+my ($re,$rr) = $C->_div($x,$y);
+
+ok ($C->_str($re),123); ok ($C->_str($rr),2);
+
+# is_zero, _is_one, _one, _zero
+ok ($C->_is_zero($x)||0,0);
+ok ($C->_is_one($x)||0,0);
+
+ok ($C->_str($C->_zero()),"0");
+ok ($C->_str($C->_one()),"1");
+
+# _two() and _ten()
+ok ($C->_str($C->_two()),"2");
+ok ($C->_str($C->_ten()),"10");
+ok ($C->_is_ten($C->_two())||0,0);
+ok ($C->_is_two($C->_two()),1);
+ok ($C->_is_ten($C->_ten()),1);
+ok ($C->_is_two($C->_ten())||0,0);
+
+ok ($C->_is_one($C->_one()),1);
+ok ($C->_is_one($C->_two()) || 0,0);
+ok ($C->_is_one($C->_ten()) || 0,0);
+
+ok ($C->_is_one($C->_zero()) || 0,0);
+
+ok ($C->_is_zero($C->_zero()),1);
+
+ok ($C->_is_zero($C->_one()) || 0,0);
+
+# is_odd, is_even
+ok ($C->_is_odd($C->_one()),1); ok ($C->_is_odd($C->_zero())||0,0);
+ok ($C->_is_even($C->_one()) || 0,0); ok ($C->_is_even($C->_zero()),1);
+
+# _len
+$x = $C->_new("1"); ok ($C->_len($x),1);
+$x = $C->_new("12"); ok ($C->_len($x),2);
+$x = $C->_new("123"); ok ($C->_len($x),3);
+$x = $C->_new("1234"); ok ($C->_len($x),4);
+$x = $C->_new("12345"); ok ($C->_len($x),5);
+$x = $C->_new("123456"); ok ($C->_len($x),6);
+$x = $C->_new("1234567"); ok ($C->_len($x),7);
+$x = $C->_new("12345678"); ok ($C->_len($x),8);
+$x = $C->_new("123456789"); ok ($C->_len($x),9);
+
+$x = $C->_new("8"); ok ($C->_len($x),1);
+$x = $C->_new("21"); ok ($C->_len($x),2);
+$x = $C->_new("321"); ok ($C->_len($x),3);
+$x = $C->_new("4321"); ok ($C->_len($x),4);
+$x = $C->_new("54321"); ok ($C->_len($x),5);
+$x = $C->_new("654321"); ok ($C->_len($x),6);
+$x = $C->_new("7654321"); ok ($C->_len($x),7);
+$x = $C->_new("87654321"); ok ($C->_len($x),8);
+$x = $C->_new("987654321"); ok ($C->_len($x),9);
+
+for (my $i = 1; $i < 9; $i++)
+ {
+ my $a = "$i" . '0' x ($i-1);
+ $x = $C->_new($a);
+ print "# Tried len '$a'\n" unless ok ($C->_len($x),$i);
+ }
+
+# _digit
+$x = $C->_new("123456789");
+ok ($C->_digit($x,0),9);
+ok ($C->_digit($x,1),8);
+ok ($C->_digit($x,2),7);
+ok ($C->_digit($x,-1),1);
+ok ($C->_digit($x,-2),2);
+ok ($C->_digit($x,-3),3);
+
+# _copy
+foreach (qw/ 1 12 123 1234 12345 123456 1234567 12345678 123456789/)
+ {
+ $x = $C->_new("$_");
+ ok ($C->_str($C->_copy($x)),"$_");
+ ok ($C->_str($x),"$_"); # did _copy destroy original x?
+ }
+
+# _zeros
+$x = $C->_new("1256000000"); ok ($C->_zeros($x),6);
+$x = $C->_new("152"); ok ($C->_zeros($x),0);
+$x = $C->_new("123000"); ok ($C->_zeros($x),3);
+$x = $C->_new("0"); ok ($C->_zeros($x),0);
+
+# _lsft, _rsft
+$x = $C->_new("10"); $y = $C->_new("3");
+ok ($C->_str($C->_lsft($x,$y,10)),10000);
+$x = $C->_new("20"); $y = $C->_new("3");
+ok ($C->_str($C->_lsft($x,$y,10)),20000);
+
+$x = $C->_new("128"); $y = $C->_new("4");
+ok ($C->_str($C->_lsft($x,$y,2)), 128 << 4);
+
+$x = $C->_new("1000"); $y = $C->_new("3");
+ok ($C->_str($C->_rsft($x,$y,10)),1);
+$x = $C->_new("20000"); $y = $C->_new("3");
+ok ($C->_str($C->_rsft($x,$y,10)),20);
+$x = $C->_new("256"); $y = $C->_new("4");
+ok ($C->_str($C->_rsft($x,$y,2)),256 >> 4);
+
+$x = $C->_new("6411906467305339182857313397200584952398");
+$y = $C->_new("45");
+ok ($C->_str($C->_rsft($x,$y,10)),0);
+
+# _acmp
+$x = $C->_new("123456789");
+$y = $C->_new("987654321");
+ok ($C->_acmp($x,$y),-1);
+ok ($C->_acmp($y,$x),1);
+ok ($C->_acmp($x,$x),0);
+ok ($C->_acmp($y,$y),0);
+$x = $C->_new("12");
+$y = $C->_new("12");
+ok ($C->_acmp($x,$y),0);
+$x = $C->_new("21");
+ok ($C->_acmp($x,$y),1);
+ok ($C->_acmp($y,$x),-1);
+$x = $C->_new("123456789");
+$y = $C->_new("1987654321");
+ok ($C->_acmp($x,$y),-1);
+ok ($C->_acmp($y,$x),+1);
+
+$x = $C->_new("1234567890123456789");
+$y = $C->_new("987654321012345678");
+ok ($C->_acmp($x,$y),1);
+ok ($C->_acmp($y,$x),-1);
+ok ($C->_acmp($x,$x),0);
+ok ($C->_acmp($y,$y),0);
+
+$x = $C->_new("1234");
+$y = $C->_new("987654321012345678");
+ok ($C->_acmp($x,$y),-1);
+ok ($C->_acmp($y,$x),1);
+ok ($C->_acmp($x,$x),0);
+ok ($C->_acmp($y,$y),0);
+
+# _modinv
+$x = $C->_new("8");
+$y = $C->_new("5033");
+my ($xmod,$sign) = $C->_modinv($x,$y);
+ok ($C->_str($xmod),'629'); # -629 % 5033 == 4404
+ok ($sign, '-');
+
+# _div
+$x = $C->_new("3333"); $y = $C->_new("1111");
+ok ($C->_str(scalar $C->_div($x,$y)),3);
+$x = $C->_new("33333"); $y = $C->_new("1111"); ($x,$y) = $C->_div($x,$y);
+ok ($C->_str($x),30); ok ($C->_str($y),3);
+$x = $C->_new("123"); $y = $C->_new("1111");
+($x,$y) = $C->_div($x,$y); ok ($C->_str($x),0); ok ($C->_str($y),123);
+
+# _num
+foreach (qw/1 12 123 1234 12345 1234567 12345678 123456789 1234567890/)
+ {
+ $x = $C->_new("$_");
+ ok (ref($x)||'','ARRAY'); ok ($C->_str($x),"$_");
+ $x = $C->_num($x); ok (ref($x)||'',''); ok ($x,$_);
+ }
+
+# _sqrt
+$x = $C->_new("144"); ok ($C->_str($C->_sqrt($x)),'12');
+$x = $C->_new("144000000000000"); ok ($C->_str($C->_sqrt($x)),'12000000');
+
+# _root
+$x = $C->_new("81"); my $n = $C->_new("3"); # 4*4*4 = 64, 5*5*5 = 125
+ok ($C->_str($C->_root($x,$n)),'4'); # 4.xx => 4.0
+$x = $C->_new("81"); $n = $C->_new("4"); # 3*3*3*3 == 81
+ok ($C->_str($C->_root($x,$n)),'3');
+
+# _pow (and _root)
+$x = $C->_new("0"); $n = $C->_new("3"); # 0 ** y => 0
+ok ($C->_str($C->_pow($x,$n)), 0);
+$x = $C->_new("3"); $n = $C->_new("0"); # x ** 0 => 1
+ok ($C->_str($C->_pow($x,$n)), 1);
+$x = $C->_new("1"); $n = $C->_new("3"); # 1 ** y => 1
+ok ($C->_str($C->_pow($x,$n)), 1);
+$x = $C->_new("5"); $n = $C->_new("1"); # x ** 1 => x
+ok ($C->_str($C->_pow($x,$n)), 5);
+
+$x = $C->_new("81"); $n = $C->_new("3"); # 81 ** 3 == 531441
+ok ($C->_str($C->_pow($x,$n)),81 ** 3);
+
+ok ($C->_str($C->_root($x,$n)),81);
+
+$x = $C->_new("81");
+ok ($C->_str($C->_pow($x,$n)),81 ** 3);
+ok ($C->_str($C->_pow($x,$n)),'150094635296999121'); # 531441 ** 3 ==
+
+ok ($C->_str($C->_root($x,$n)),'531441');
+ok ($C->_str($C->_root($x,$n)),'81');
+
+$x = $C->_new("81"); $n = $C->_new("14");
+ok ($C->_str($C->_pow($x,$n)),'523347633027360537213511521');
+ok ($C->_str($C->_root($x,$n)),'81');
+
+$x = $C->_new("523347633027360537213511520");
+ok ($C->_str($C->_root($x,$n)),'80');
+
+$x = $C->_new("523347633027360537213511522");
+ok ($C->_str($C->_root($x,$n)),'81');
+
+my $res = [ qw/ 9 31 99 316 999 3162 9999/ ];
+
+# 99 ** 2 = 9801, 999 ** 2 = 998001 etc
+for my $i (2 .. 9)
+ {
+ $x = '9' x $i; $x = $C->_new($x);
+ $n = $C->_new("2");
+ my $rc = '9' x ($i-1). '8' . '0' x ($i-1) . '1';
+ print "# _pow( ", '9' x $i, ", 2) \n" unless
+ ok ($C->_str($C->_pow($x,$n)),$rc);
+
+ if ($i <= 7)
+ {
+ $x = '9' x $i; $x = $C->_new($x);
+ $n = '9' x $i; $n = $C->_new($n);
+ print "# _root( ", '9' x $i, ", ", 9 x $i, ") \n" unless
+ ok ($C->_str($C->_root($x,$n)),'1');
+
+ $x = '9' x $i; $x = $C->_new($x);
+ $n = $C->_new("2");
+ print "# _root( ", '9' x $i, ", ", 9 x $i, ") \n" unless
+ ok ($C->_str($C->_root($x,$n)), $res->[$i-2]);
+ }
+ }
+
+##############################################################################
+# _fac
+$x = $C->_new("0"); ok ($C->_str($C->_fac($x)),'1');
+$x = $C->_new("1"); ok ($C->_str($C->_fac($x)),'1');
+$x = $C->_new("2"); ok ($C->_str($C->_fac($x)),'2');
+$x = $C->_new("3"); ok ($C->_str($C->_fac($x)),'6');
+$x = $C->_new("4"); ok ($C->_str($C->_fac($x)),'24');
+$x = $C->_new("5"); ok ($C->_str($C->_fac($x)),'120');
+$x = $C->_new("10"); ok ($C->_str($C->_fac($x)),'3628800');
+$x = $C->_new("11"); ok ($C->_str($C->_fac($x)),'39916800');
+$x = $C->_new("12"); ok ($C->_str($C->_fac($x)),'479001600');
+$x = $C->_new("13"); ok ($C->_str($C->_fac($x)),'6227020800');
+
+# test that _fac modifes $x in place for small arguments
+$x = $C->_new("3"); $C->_fac($x); ok ($C->_str($x),'6');
+$x = $C->_new("13"); $C->_fac($x); ok ($C->_str($x),'6227020800');
+
+##############################################################################
+# _inc and _dec
+foreach (qw/1 11 121 1231 12341 1234561 12345671 123456781 1234567891/)
+ {
+ $x = $C->_new("$_"); $C->_inc($x);
+ print "# \$x = ",$C->_str($x),"\n"
+ unless ok ($C->_str($x),substr($_,0,length($_)-1) . '2');
+ $C->_dec($x); ok ($C->_str($x),$_);
+ }
+foreach (qw/19 119 1219 12319 1234519 12345619 123456719 1234567819/)
+ {
+ $x = $C->_new("$_"); $C->_inc($x);
+ print "# \$x = ",$C->_str($x),"\n"
+ unless ok ($C->_str($x),substr($_,0,length($_)-2) . '20');
+ $C->_dec($x); ok ($C->_str($x),$_);
+ }
+foreach (qw/999 9999 99999 9999999 99999999 999999999 9999999999 99999999999/)
+ {
+ $x = $C->_new("$_"); $C->_inc($x);
+ print "# \$x = ",$C->_str($x),"\n"
+ unless ok ($C->_str($x), '1' . '0' x (length($_)));
+ $C->_dec($x); ok ($C->_str($x),$_);
+ }
+
+$x = $C->_new("1000"); $C->_inc($x); ok ($C->_str($x),'1001');
+$C->_dec($x); ok ($C->_str($x),'1000');
+
+my $BL;
+{
+ no strict 'refs';
+ $BL = &{"$C"."::_base_len"}();
+}
+
+$x = '1' . '0' x $BL;
+$z = '1' . '0' x ($BL-1); $z .= '1';
+$x = $C->_new($x); $C->_inc($x); ok ($C->_str($x),$z);
+
+$x = '1' . '0' x $BL; $z = '9' x $BL;
+$x = $C->_new($x); $C->_dec($x); ok ($C->_str($x),$z);
+
+# should not happen:
+# $x = $C->_new("-2"); $y = $C->_new("4"); ok ($C->_acmp($x,$y),-1);
+
+###############################################################################
+# _mod
+$x = $C->_new("1000"); $y = $C->_new("3");
+ok ($C->_str(scalar $C->_mod($x,$y)),1);
+$x = $C->_new("1000"); $y = $C->_new("2");
+ok ($C->_str(scalar $C->_mod($x,$y)),0);
+
+# _and, _or, _xor
+$x = $C->_new("5"); $y = $C->_new("2");
+ok ($C->_str(scalar $C->_xor($x,$y)),7);
+$x = $C->_new("5"); $y = $C->_new("2");
+ok ($C->_str(scalar $C->_or($x,$y)),7);
+$x = $C->_new("5"); $y = $C->_new("3");
+ok ($C->_str(scalar $C->_and($x,$y)),1);
+
+# _from_hex, _from_bin
+ok ($C->_str( $C->_from_hex("0xFf")),255);
+ok ($C->_str( $C->_from_bin("0b10101011")),160+11);
+
+# _as_hex, _as_bin
+ok ($C->_str( $C->_from_hex( $C->_as_hex( $C->_new("128")))), 128);
+ok ($C->_str( $C->_from_bin( $C->_as_bin( $C->_new("128")))), 128);
+ok ($C->_str( $C->_from_hex( $C->_as_hex( $C->_new("0")))), 0);
+ok ($C->_str( $C->_from_bin( $C->_as_bin( $C->_new("0")))), 0);
+ok ($C->_as_hex( $C->_new("0")), '0x0');
+ok ($C->_as_bin( $C->_new("0")), '0b0');
+ok ($C->_as_hex( $C->_new("12")), '0xc');
+ok ($C->_as_bin( $C->_new("12")), '0b1100');
+
+# _check
+$x = $C->_new("123456789");
+ok ($C->_check($x),0);
+ok ($C->_check(123),'123 is not a reference');
+
+###############################################################################
+# __strip_zeros
+
+{
+ no strict 'refs';
+ # correct empty arrays
+ $x = &{$C."::__strip_zeros"}([]); ok (@$x,1); ok ($x->[0],0);
+ # don't strip single elements
+ $x = &{$C."::__strip_zeros"}([0]); ok (@$x,1); ok ($x->[0],0);
+ $x = &{$C."::__strip_zeros"}([1]); ok (@$x,1); ok ($x->[0],1);
+ # don't strip non-zero elements
+ $x = &{$C."::__strip_zeros"}([0,1]);
+ ok (@$x,2); ok ($x->[0],0); ok ($x->[1],1);
+ $x = &{$C."::__strip_zeros"}([0,1,2]);
+ ok (@$x,3); ok ($x->[0],0); ok ($x->[1],1); ok ($x->[2],2);
+
+ # but strip leading zeros
+ $x = &{$C."::__strip_zeros"}([0,1,2,0]);
+ ok (@$x,3); ok ($x->[0],0); ok ($x->[1],1); ok ($x->[2],2);
+
+ $x = &{$C."::__strip_zeros"}([0,1,2,0,0]);
+ ok (@$x,3); ok ($x->[0],0); ok ($x->[1],1); ok ($x->[2],2);
+
+ $x = &{$C."::__strip_zeros"}([0,1,2,0,0,0]);
+ ok (@$x,3); ok ($x->[0],0); ok ($x->[1],1); ok ($x->[2],2);
+
+ # collapse multiple zeros
+ $x = &{$C."::__strip_zeros"}([0,0,0,0]);
+ ok (@$x,1); ok ($x->[0],0);
+}
+
+# done
+
+1;
+