Sindbad~EG File Manager
/* Bignum arithmetic package by M. Beeson */
/* Uses dynamic arrays instead of linked lists */
/* includes addition, subtraction, multiplication, division, exponentiation,
gcd, conversion to decimal form, and output. */
/* bignum digits are 'unsigned', which makes them either 16 or 32 bits on
the architectures in use at the time of writing. However, the code should
also work on future architectures with for example 64 bit integers, except
as follows: Functions 'longmult' and 'longdiv' are written in assembly
language and conditionally compiled. They will work correctly for 16-bit
ints and 32-bit longs or for 32-bit ints and 32-bit longs; if in
the future we have 32-bit ints and 64-bit longs or eventually even
64-bit ints and 64-bit longs, more work will be required.
*/
typedef unsigned digit;
/* don't change the definition of digit; the use of MAX assumes that
digits and the .ln field of bignums are the same. */
typedef struct { unsigned ln; /* number of "digits" */
digit *val; /*least significant digits first */
} bignum;
typedef struct { int sign; /* 1 is positive, -1 is negative, 0 is zero */
bignum n; /* numerator */
bignum d; /* denominator */
} bigrat;
digit *getspace(unsigned); /* bignum.c */
void freespace(digit *); /* release space allocated by getspace */
#define NN 32U
/* Microsoft Visual C 5.0 doesn't compile the following properly.
It evaluates (trialq >> NN) to a nonzero value when trialq is a digit!
#define NN ((unsigned)(sizeof(digit)<<3))
*/
#define LEFTDIGIT ((unsigned)(1 << (NN-1))) /* 1 in the leftmost place of a digit */
#define MAX ((digit) -1)
#define INC(i) ((i==0)?1:((i==1)?2:((i==2)?0:(i++%3)))) /* ++i mod 3 */
/* Function prototypes */
int smallgcd(digit,digit);
void longmult(digit x, digit y, digit *lo, digit *hi);
void longdiv(unsigned x, unsigned y, unsigned z, unsigned *q, unsigned *r);
MEXPORT_BIGNUMS void bigplus2(bignum x, bignum y, bignum *ansp);
MEXPORT_BIGNUMS void bigplus(bignum x, bignum y, bignum *ansp);
MEXPORT_BIGNUMS void mult_by_digit2(bignum x, digit d, bignum *ansp);
MEXPORT_BIGNUMS void mult_by_digit(bignum x, digit d, bignum *ansp);
MEXPORT_BIGNUMS void bigmult2(bignum x, bignum y, bignum *ansp);
MEXPORT_BIGNUMS void bigmult(bignum x, bignum y, bignum *ansp);
MEXPORT_BIGNUMS int compare(bignum x, bignum y);
MEXPORT_BIGNUMS digit mod_digit(bignum x, digit y);
MEXPORT_BIGNUMS bignum btod(bignum x, unsigned n);
MEXPORT_BIGNUMS void bigminus2(bignum x, bignum y, bignum *ansp);
MEXPORT_BIGNUMS void bigminus(bignum x, bignum y, bignum *ansp);
void right_shift(bignum x, int i, bignum *y);
MEXPORT_BIGNUMS void divide_by_digit(bignum x, digit y, bignum *q, digit *r);
MEXPORT_BIGNUMS void divide_by_digit2(bignum x, digit y, bignum *q, digit *r);
MEXPORT_BIGNUMS bignum long_to_bignum(unsigned long x);
MEXPORT_BIGNUMS int bigdivide(bignum x, bignum y, bignum *q, bignum *r);
MEXPORT_BIGNUMS int bigdivide2(bignum x, bignum y, bignum *q, bignum *r);
MEXPORT_BIGNUMS int bigpower(bignum x, digit d, bignum *ansp);
MEXPORT_BIGNUMS int bigsqrt(bignum x, bignum *ansp, bignum *remp);
MEXPORT_BIGNUMS int bigsqrt2(bignum x, bignum *ans, bignum *remp);
MEXPORT_BIGNUMS void biggcd(bignum x, bignum y, bignum *ansp);
int convert_and_print(bignum x, int separator);
MEXPORT_BIGNUMS int bigroot(unsigned n, bignum x, bignum *ansp, bignum *remp);
MEXPORT_BIGNUMS int bigroot2(unsigned n, bignum x, bignum *ansp, bignum *remp);
MEXPORT_BIGNUMS int bigfactorial(unsigned n, bignum *ansp);
MEXPORT_BIGNUMS int string_bignum(char *s, unsigned n, bignum *xp);
MEXPORT_BIGNUMS char *bignum_string(bignum x, int separator);
MEXPORT_BIGNUMS int get_small_factors(bignum, unsigned *, int *, bignum *,unsigned *);
MEXPORT_BIGNUMS void modexp(bignum, bignum,bignum, bignum *); /* file bigmod.c */
MEXPORT_BIGNUMS void modexp2(bignum, bignum,bignum, bignum *); /* file bigmod.c */
MEXPORT_BIGNUMS int bignum_double(bignum,double *);
MEXPORT_BIGNUMS int bigrat_double(bignum,bignum, double *);
MEXPORT_BIGNUMS void fftmult(bignum,bignum,bignum *);
#define freespace(x) free2(x)
MEXPORT_BIGNUMS unsigned addmod(unsigned a, unsigned b, unsigned m);
MEXPORT_BIGNUMS unsigned mulmod(unsigned a, unsigned b, unsigned m);
MEXPORT_BIGNUMS int intbinomial(long n, long k, bignum *ans);
MEXPORT_BIGNUMS int ratbinomial(long n, long d, long k, bigrat *ans);
MEXPORT_BIGNUMS bignum bigint(long n);
MEXPORT_BIGNUMS int bignum_long(bignum b, long *ans);
MEXPORT_BIGNUMS int primality_test(bignum b);
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists