Skip to content
Tcl Source Code Documentation
Login

Documentation

/*
 *----------------------------------------------------------------------
 *
 * tclBigInt.c --
 *
 *	Procedures that manipulate arbitrary-precision integers within
 *	the Tcl core.
 *
 * Copyright (c) 2005 by Kevin B. Kenny.  All rights reserved.
 *
 * Based loosely on various files from the 'mpexpr' module, which
 * is copyright (c) 1994 by David I. Bell. (Note that all of the
 * code here has been reimplemented; none of Bell's original expression
 * remains.)
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id: tclBigInt.c,v 1.1.2.4 2004/12/14 18:43:41 kennykb Exp $"
 *
 *----------------------------------------------------------------------
 */

#include "tclInt.h"

/*
 * The 'BigInt' structure holds an arbitrary-precision integer.
 */

typedef struct BigInt {

    size_t len;			/* Number of values in the array */
    int signum;			/* Sign of the integer */
    Tcl_NarrowUInt v[1];	/* Little-endian array containing
				 * the absolute value of the integer */

} BigInt;

/*
 * Check assumptions about widths of various values and fail if they
 * are incorrect.
 */

#if ( SIZEOF_INT % TCL_SIZEOF_NARROW_INT != 0 )
#error "narrow integers are smaller than ordinary ones and do not divide them"
#endif
#if (TCL_SIZEOF_WIDE_INT > TCL_SIZEOF_NARROW_INT ) && ( TCL_SIZEOF_WIDE_INT % TCL_SIZEOF_NARROW_INT != 0 )
#error "narrow integers are smaller than wide ones and do not divide them"
#endif

#define NARROW_UINT_MAX ( (Tcl_WideUInt) ~ (Tcl_NarrowUInt) 0 )
#define NARROW_INT_MAX ( (Tcl_NarrowUInt) (NARROW_UINT_MAX/2) )
#define NARROW_UINT_BITS ( 8 * sizeof( Tcl_NarrowUInt ) )
#define NARROW_UINT_PER_INT \
    ( ( sizeof( int ) + sizeof( Tcl_NarrowUInt ) - 1 ) \
      / sizeof( Tcl_NarrowUInt ) )
#define NARROW_UINT_PER_WIDE_INT \
    ( ( sizeof( Tcl_WideUInt ) + sizeof( Tcl_NarrowUInt ) - 1 ) \
      / sizeof( Tcl_NarrowUInt ) )
#define BASE ( (Tcl_WideInt) 1 << NARROW_UINT_BITS )

#define NewBigInt(n) \
    ( (BigInt*) ( ckalloc( sizeof( BigInt ) \
                  + ((n)-1) * sizeof( Tcl_NarrowUInt ) ) ) )

/*
 * Static functions defined in this file
 */

static BigInt* AddAbsValues( BigInt* z1, BigInt* z2 );
				/* Add the absolute values of two large 
				 * integers */
static BigInt* SubtractAbsValues( BigInt* z1, BigInt* z2 );
				/* Subtract the absolute values of two
				 * large integers */


/*
 *----------------------------------------------------------------------
 *
 * Tcl_BigIntFromInt --
 *
 *	Creates a big integer from a native integer.
 *
 * Results:
 *	Returns the big integer just created.
 *
 * This function creates a big integer from a native integer. Refer
 * to the user documentation for details.
 *
 *----------------------------------------------------------------------
 */

Tcl_BigInt
Tcl_BigIntFromInt( int intVal )	/* Value to place in the big integer */
{

    BigInt* retVal = NewBigInt(NARROW_UINT_PER_INT);
#if ( SIZEOF_INT > TCL_SIZEOF_NARROW_INT )
    size_t i;
#endif

    if ( intVal < 0 ) {
	intVal = -intVal;
	retVal->signum = 1;
    } else {
	retVal->signum = 0;
    }

#if ( SIZEOF_INT <= TCL_SIZEOF_NARROW_INT )

    /* Common case - an integer fits in a single word of retval->v */

    retVal->v[0] = intVal;
    retVal->len = 1;

#else

    /* Hairy case - we have to pack the integer into multiple words. */

    i = 0;
    do {
	retval -> v[ i++ ] = (Tcl_NarrowUInt) ( intVal & NARROW_UINT_MAX );
	intVal >>= NARROW_UINT_BITS;
    } while ( intVal != 0 && i < NARROW_UINT_PER_INT );

#endif

    return (Tcl_BigInt) retVal;

}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_BigIntFromWideInt --
 *
 *	Creates a big integer from a native integer.
 *
 * Results:
 *	Returns the big integer just created.
 *
 * This function creates a big integer from a native integer. Refer
 * to the user documentation for details.
 *
 *----------------------------------------------------------------------
 */

Tcl_BigInt
Tcl_BigIntFromWideInt( Tcl_WideInt intVal )
				/* Value to place in the big integer */
{
    BigInt* retVal = NewBigInt( NARROW_UINT_PER_WIDE_INT );
    size_t i;

    if ( intVal < 0 ) {
	intVal = -intVal;
	retVal->signum = 1;
    } else {
	retVal->signum = 0;
    }

    i = 0;
    do {
	retVal -> v[ i++ ] = (Tcl_NarrowUInt) ( intVal & NARROW_UINT_MAX );
	intVal >>= NARROW_UINT_BITS;
    } while ( intVal != 0 && i < NARROW_UINT_PER_WIDE_INT );

    return (Tcl_BigInt) retVal;

}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_CopyBigInt --
 *
 *	Make a deep copy of an arbitrary-precision integer.
 *
 * Results:
 *	Returns the copy.
 *
 *----------------------------------------------------------------------
 */

Tcl_BigInt
Tcl_CopyBigInt( Tcl_BigInt bigVal )
				/* Value to copy */
{
    BigInt* v = (BigInt*) bigVal;
    BigInt* retVal = NewBigInt( v->len );
    memcpy( retVal, v, sizeof( BigInt ) + v->len * sizeof( Tcl_NarrowUInt ) );
    return (Tcl_BigInt) retVal;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_FreeBigInt --
 *
 *	Frees an arbitrary-precision integer.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Integer is freed.
 *
 * The Tcl_FreeBigInt function frees an arbitrary-precision integer.
 * Refer to the user documentation for details.
 *
 *----------------------------------------------------------------------
 */

void
Tcl_FreeBigInt( Tcl_BigInt bigVal )
				/* Value to free */
{
    BigInt* val = (BigInt*) bigVal;
    ckfree( (void*) val );
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_BigIntIsEven, Tcl_BigIntIsOdd, Tcl_BigIntIsZero,
 * Tcl_BigIntIsNegative, Tcl_BigIntIsPositive, Tcl_BigIntIsUnit,
 * Tcl_BigIntIsOne, Tcl_BigIntIsMinusOne,
 * Tcl_BigIntIsInt, Tcl_BigIntIsWideInt --
 *
 *	Simple predicates applying to Tcl's arbitrary precision integers.
 *
 * Results:
 *	Return values of 1 or 0 according to whether the given condition
 *	is true.
 *
 * These procedures all do simple tests on a single big-integer value.
 * Refer to the user documentation for details.
 *
 *----------------------------------------------------------------------
 */

int
Tcl_BigIntIsEven( Tcl_BigInt bigVal )
				/* Value to test */
{
    BigInt* z = (BigInt*) bigVal;
    return ( (z->v[0] & 1) == 0 );
}

int
Tcl_BigIntIsOdd( Tcl_BigInt bigVal )
				/* Value to test */
{
    BigInt* z = (BigInt*) bigVal;
    return ( (z->v[0] & 1) != 0 );
}

int
Tcl_BigIntIsZero( Tcl_BigInt bigVal )
				/* Value to test */
{
    BigInt* z = (BigInt*) bigVal;
    return ( z->len == 1 && z->v[0] == 0 );
}

int
Tcl_BigIntIsNegative( Tcl_BigInt bigVal )
				/* Value to test */
{
    BigInt* z = (BigInt*) bigVal;
    return ( z->signum );
}

int
Tcl_BigIntIsPositive( Tcl_BigInt bigVal )
				/* Value to test */
{
    BigInt* z = (BigInt*) bigVal;
    return ( z->signum == 0 && ( z->len > 1 || z->v[0] != 0 ) );
}

int
Tcl_BigIntIsUnit( Tcl_BigInt bigVal )
				/* Value to test */
{
    BigInt* z = (BigInt*) bigVal;
    return ( z->len == 1 && z->v[0] == 1 );
}

int
Tcl_BigIntIsOne( Tcl_BigInt bigVal )
				/* Value to test */
{
    BigInt* z = (BigInt*) bigVal;
    return ( z->len == 1 && z->v[0] == 1 && !z->signum );
}

int
Tcl_BigIntIsMinusOne( Tcl_BigInt bigVal )
				/* Value to test */
{
    BigInt* z = (BigInt*) bigVal;
    return ( z->len == 1 && z->v[0] == 1 && z->signum );
}

int
Tcl_BigIntIsInt( Tcl_BigInt bigVal )
{
    /*
     * ASSUMPTION: This code assumes that the size of Tcl_NarrowUInt
     *             divides sizeof(int) evenly.  The assumption is tested
     *             in #ifdefs at the head of this file.
     */
    BigInt* z = (BigInt*) bigVal;
    int mostSigWord;
    if ( z->len > NARROW_UINT_PER_INT ) {
	return 0;
    }
    if ( z->len < NARROW_UINT_PER_INT ) {
	return 1;
    }
    mostSigWord = z->v[ NARROW_UINT_PER_INT - 1 ];
    if ( z->signum ) {
	return ( mostSigWord-1 <= NARROW_INT_MAX );
    } else {
	return ( mostSigWord <= NARROW_INT_MAX );
    }
}

int
Tcl_BigIntIsWideInt( Tcl_BigInt bigVal )
{
    BigInt* z = (BigInt*) bigVal;
    int mostSigWord;
    if ( z->len > NARROW_UINT_PER_WIDE_INT ) {
	return 0;
    }
    if ( z->len < NARROW_UINT_PER_WIDE_INT ) {
	return 1;
    }
    mostSigWord = z->v[ NARROW_UINT_PER_WIDE_INT - 1 ];
    if ( z->signum ) {
	return ( mostSigWord-1 <= NARROW_INT_MAX );
    } else {
	return ( mostSigWord <= NARROW_INT_MAX );
    }
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_GetIntFromBigInt, Tcl_GetWideIntFromBigInt --
 *
 *	Convert an arbitrary-precision integer to a native integer.
 *
 * Results:
 *	Returns the converted value.
 *
 * These two functions convert an arbitrary-precision integer to a
 * native one.  If the conversion results in an overflow, the
 * native integer will be the least significant bits of the
 * arbitrary-precision one.
 *
 *----------------------------------------------------------------------
 */

int
Tcl_GetIntFromBigInt( Tcl_BigInt bigVal )
{
    BigInt* z = (BigInt*) bigVal;
    unsigned int retval;
#if SIZEOF_INT <= TCL_SIZEOF_NARROW_INT
    retval = (unsigned int) (z->v[0]);
#else
    int i = z->len-1;
    if ( i >= NARROW_UINT_PER_INT ) {
	i = NARROW_UINT_PER_INT - 1;
    }
    retval = (unsigned int) (z->v[i--]);
    while ( i >= 0 ) {
	retval = ( retval << ( 8 * sizeof( Tcl_NarrowUInt ) ) )
	    | (unsigned int) (z->v[i--]);
    }
#endif
    if ( z->signum ) {
	return - (int) retval;
    } else {
	return (int) retval;
    }
}

Tcl_WideInt
Tcl_GetWideIntFromBigInt( Tcl_BigInt bigVal )
{
    BigInt* z = (BigInt*) bigVal;
    Tcl_WideUInt retval;
    int i = z->len-1;
    if ( i >= NARROW_UINT_PER_WIDE_INT ) {
	i = NARROW_UINT_PER_WIDE_INT - 1;
    }
    retval = (Tcl_WideUInt) (z->v[i--]);
    while ( i >= 0 ) {
	retval = ( retval << ( 8 * sizeof( Tcl_NarrowUInt ) ) )
	    | (Tcl_WideUInt) (z->v[i--]);
    }
    if ( z->signum ) {
	return - (Tcl_WideInt) retval;
    } else {
	return (Tcl_WideInt) retval;
    }
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_BigIntCompare --
 *
 *	Compares two Tcl_BigInt values to see which is larger.
 *
 * Results:
 *	Returns -1 if bigVal1 is smaller, 0 if they are equal, 1
 *	if bigVal2 is smaller.
 *
 *----------------------------------------------------------------------
 */

int
Tcl_CompareBigInt( Tcl_BigInt bigVal1, Tcl_BigInt bigVal2 )
{
    BigInt* z1 = (BigInt*) bigVal1;
    BigInt* z2 = (BigInt*) bigVal2;
    int sign;
    size_t i;

    /* Non-negative numbers are greater than negative ones. */

    if ( z1->signum < z2->signum ) {
	return 1;
    } else if ( z2->signum < z1->signum ) {
	return -1;
    }

    /* If both numbers are negative, reverse the sense of the comparison */

    if ( z2->signum ) {
	sign = -1;
    } else {
	sign = 1;
    }

    /* Longer numbers are greater than shorter ones. */

    if ( z1->len > z2->len ) {
	return sign;
    } else if ( z1->len < z2->len ) {
	return -sign;
    }

    /*
     * Compare the individual words of the numbers, most significant parts
     * first, and return when one number does not equal the other. */

    for ( i = z2->len - 1; i >= 0; --i ) {
	Tcl_NarrowUInt part1 = z1->v[i];
	Tcl_NarrowUInt part2 = z2->v[i];
	if ( part1 > part2 ) {
	    return sign;
	} else if ( part1 < part2 ) {
	    return -sign;
	}
    }

    /* Two numbers are equal. */

    return 0;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_AddBigInt --
 *
 *	Adds together two large integers.
 *
 * Results:
 *	Returns the sum.
 *
 *----------------------------------------------------------------------
 */

Tcl_BigInt
Tcl_AddBigInt( Tcl_BigInt bigVal1, Tcl_BigInt bigVal2 )
{
    BigInt* z1 = (BigInt*) bigVal1;
    BigInt* z2 = (BigInt*) bigVal2;
    BigInt* retVal;

    /* 
     * Add numbers of opposite sign by subtracting the absolute
     * value of the negative number from the positive one.
     */

    if (z1->signum && !z2->signum) {
	retVal = SubtractAbsValues( z2, z1 );
    } else if (z2->signum && !z1->signum) {
	retVal = SubtractAbsValues( z1, z2 );
    } else {

	/* 
	 * Add numbers of like sign by adding their absolute values
	 * and correcting the sign.
	 */
	
	retVal = AddAbsValues( z1, z2 );
	retVal->signum = z1->signum; 
    }

    return (Tcl_BigInt) retVal;

}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_SubtractBigInt --
 *
 *	Subtracts one large integer from another.
 *
 * Results:
 *	Returns the difference (bigVal1-bigVal2).
 *
 *----------------------------------------------------------------------
 */

Tcl_BigInt
Tcl_SubtractBigInt( Tcl_BigInt bigVal1, Tcl_BigInt bigVal2 )
{
    BigInt* z1 = (BigInt*) bigVal1;
    BigInt* z2 = (BigInt*) bigVal2;
    BigInt* retVal;

    if ( z1->signum != z2->signum ) {
	/* 
	 * Subtracting two numbers of opposite sign is done by adding
	 * their absolute values.
	 */
	retVal = AddAbsValues( z1, z2 );
	retVal->signum = z1->signum;
    } else {
	/* 
	 * Subtracting two numbers of like sign is done by subtracting
	 * absolute values and correcting the sign.
	 */
	retVal = SubtractAbsValues( z1, z2 );
	if ( z1->signum && ( retVal->len > 1 || retVal->v[0] != 0 ) ) {
	    retVal->signum = ! (retVal->signum);
	}
    }
    return (Tcl_BigInt) retVal;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_MultiplyBigIntByNarrow --
 *
 *	Multiply a Tcl_BigInt by a small constant.
 *
 * Results:
 *	Returns the product 
 *
 *----------------------------------------------------------------------
 */

Tcl_BigInt
Tcl_MultiplyBigIntByNarrowInt( Tcl_BigInt bigVal, Tcl_NarrowInt smallVal )
{
    BigInt* z = (BigInt*) bigVal;
    BigInt* result;
    Tcl_NarrowUInt x;
    Tcl_WideUInt carry;
    size_t i;

    if ( smallVal == 0 
	 || ( z->len == 1 && z->v[0] == 0 ) ) {

	/* Multiplication by zero */

	result = NewBigInt(1);
	result->signum = 0;
	result->v[0] = 0;

    } else if ( smallVal == 1 || smallVal == -1 ) {

	/* Multiplication by unity. */

	result = (BigInt*) Tcl_CopyBigInt( bigVal );
	result->signum ^= ( smallVal < 0 );

    } else {

	/* Multiplication by something else. */

	result = NewBigInt( z->len + 1 );
	if ( smallVal < 0 ) {
	    result->signum = -z->signum;
	    x = - smallVal;
	} else {
	    result->signum = z->signum;
	    x = smallVal;
	}
	carry = 0;
	for ( i = 0; i < z->len; ++i ) {
	    carry = carry + x * z->v[i];
	    result->v[i] = (Tcl_NarrowUInt) ( carry & NARROW_UINT_MAX );
	    carry >>= NARROW_UINT_BITS;
	}
	if ( carry ) {
	    result->v[i++] = (Tcl_NarrowUInt) carry;
	}
	result->len = i;
    }
    return (Tcl_BigInt) result;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ShiftBigInt
 *
 *	Shift a large integer, multiplying it by a power of 2.
 *
 * Results:
 *	Returns bigVal * 2**intVal
 *
 * As is implied by the formula, intVal>0 shifts to the left,
 * intVal<0 shifts to the right, intVal==0 does nothing.
 *
 *----------------------------------------------------------------------
 */

Tcl_BigInt
Tcl_ShiftBigInt( Tcl_BigInt bigVal, int intVal )
{
    BigInt* z = (BigInt*) bigVal;
    BigInt* result;
    Tcl_WideUInt carry;
    Tcl_NarrowUInt *p, *q;
    size_t i;

    /* 
     * Compute the length of the shift, in words and bits.  Also
     * compute the word size minus the distance to shift.
     */

    size_t words = ( -intVal ) / NARROW_UINT_BITS;
    int bits = ( -intVal ) % NARROW_UINT_BITS;
    int notbits = NARROW_UINT_BITS - bits;

    if ( intVal == 0
	 || ( z->len == 0 && z->v[0] == 0 ) ) {

	/* 
	 * Shift by zero is the identity.  Shifting zero by anything
	 * is zero, hence is also an identity. Handle either case
	 * by copying the input value.
	 */
	return Tcl_CopyBigInt( bigVal );
	
    }

    /* A negative shift value is a right shift. */

    if ( intVal < 0 ) {

	size_t l = z->len - words;
	Tcl_WideUInt lowmask = (((Tcl_WideUInt) 1) << bits) - 1;

	if ( l <= 0 ) {

	    /* 
	     * Shifting a positive number right by more than the length of the
	     * number yields zero. Shifting a negative number right by more 
	     * than the length of the number yields -1. */

	    result = NewBigInt( 1 );
	    result->signum = z->signum;
	    result->v[0] = z->signum;
	    return (Tcl_BigInt) result;

	}

	/* 
	 * To preserve truncate-toward-minus-infinity semantics, if the
	 * number being shifted is negative, we determine whether a one
	 * bit has been shifted out anywhere, and increment the magnitude
	 * of the result if it has.
	 */

	carry = z->v[0] >> bits;
	if ( z->signum ) {
	    for ( i = 0; i < words; ++i ) {
		if ( z->v[i] != 0 ) {
		    ++carry;
		    break;
		}
	    }
	    if ( i >= words
		 && ( z->v[i] & lowmask ) != 0  ) {
		++carry;
	    }
	}

	if ( bits == 0 && carry == 0 ) {

	    /* 
	     * The simple case is shifting by a multiple of the word size
	     * without awkward carries.
	     */
	    
	    result = NewBigInt( l );
	    result->signum = z->signum;
	    memcpy( result->v, z->v, l * sizeof( Tcl_NarrowUInt ) );
	    return (Tcl_BigInt) result;
	    
	} else {

	    /*
	     * General case - shift right.  'carry' at this point
	     * contains the least significant 'bits' bits of the result.
	     * It may exceed that number of bits by one if we are
	     * shifting a negative number.
	     * Words 1 through (l-1) of the input argument will be each
	     * shifted onto this result.
	     */

	    result = NewBigInt( l + 1 );
	    result->signum = z->signum;

	    p = z->v + 1;
	    q = result->v;
	    for ( i = l; i > 0; --i ) {
		carry += *p++ << notbits;
		*q++ = (Tcl_NarrowUInt) ( carry & NARROW_UINT_MAX );
		carry >>= NARROW_UINT_BITS;
	    }
	    if ( carry ) {
		*q++ = (Tcl_NarrowUInt) ( carry & NARROW_UINT_MAX );
	    }

	}

    } else {

	/* Shift left */

	size_t l = z->len + words;
	if ( bits == 0 ) {

	    /* Easy case - shift left a multiple of the word size. */

	    result = NewBigInt( l );
	    result->signum = z->signum;
	    memset( result->v, 0, words );
	    memcpy( result->v + words, z->v, z->len );
	    return (Tcl_BigInt) result;

	} else {

	    /* Shift left by something other than a multiple of a word */

	    result = NewBigInt( l + 1 );
	    carry = 0;
	    p = z->v;
	    q = result->v;
	    for ( i = z->len; i > 0; --i ) {
		carry |= ( *p++ << bits );
		*q++ = (Tcl_NarrowUInt) ( carry & NARROW_UINT_MAX );
		carry >>= NARROW_UINT_BITS;
	    }
	    *q++ = (Tcl_NarrowUInt) carry;

	}	    
    }

    /*
     * The shift may have left a zero in the most significant
     * word (or even the most significant two words).
     * Find the most significant non-zero word to assign the
     * length of the result.
     */
    
    --q;
    while ( q > result->v && *q == 0 ) {
	--q;
    }
    result->len = q + 1 - result->v;
	
    return (Tcl_BigInt) result;
}

/*
 *----------------------------------------------------------------------
 *
 * AddAbsValues --
 *
 *	Adds the absolute values of two arbitrary-precision integers.
 *
 * Results:
 *	Returns an arbitrary-precision integer whose value is the
 *	sum of the absolute values of the arguments.
 *
 *----------------------------------------------------------------------
 */

static BigInt* 
AddAbsValues( BigInt* z1, BigInt* z2 )
{

    BigInt* retVal;
    Tcl_NarrowUInt *p1, *p2, *pd;
    Tcl_WideUInt carry;
    size_t len1, len2;

    /* Determine which argument is the longer */

    if ( z2->len > z1->len ) {
	retVal = NewBigInt( z2->len + 1 );
	p1 = z1->v;
	p2 = z2->v;
	len1 = z1->len;
	len2 = z2->len - z1->len;
    } else {
	retVal = NewBigInt( z1->len + 1 );
	p1 = z1->v;
	p2 = z2->v;
	len1 = z2->len;
	len2 = z1->len - z2->len;
    }

    retVal->signum = 0;
    carry = 0;
    pd = retVal->v;

    /* Add the two arguments */

    while ( len1-- ) {
	carry += (Tcl_WideUInt) (*p1++) + (Tcl_WideUInt) (*p2++);
	*pd++ = (Tcl_NarrowUInt) (carry & NARROW_UINT_MAX);
	carry >>= NARROW_UINT_BITS;
    }

    /* Propagate carries */

    while ( len2-- ) {
	carry += (Tcl_WideUInt) (*p2++);
	*pd++ = (Tcl_NarrowUInt) (carry & NARROW_UINT_MAX);
	carry >>= NARROW_UINT_BITS;
    }

    /* 
     * If carrying into the most significant word, store the carry;
     * otherwise, correct the length.
     */

    if ( carry ) {
	*pd = (Tcl_NarrowUInt) carry;
    } else {
	--( retVal->len );
    }

    return retVal;
    
}

/*
 *----------------------------------------------------------------------
 *
 * SubtractAbsValues --
 *
 *	Subtracts the absolute value of the large integer z2
 *	from the absolute value of the large integer z1.
 *
 * Results:
 *	Returns the difference.
 *
 *----------------------------------------------------------------------
 */

static BigInt*
SubtractAbsValues( BigInt* z1, BigInt* z2 )
{
    BigInt* retVal;
    size_t len1, len2, t;
    int bigger2;
    Tcl_NarrowUInt *p1, *p2, *pd;
    Tcl_WideUInt carry;
    
    /*
     * We need to compute the size of the result before we can
     * allocate it.  If one operand is longer than the other, the
     * size of the result will be the size of the longer operand.
     * If the operands are of equal length, we compare them,
     * most significant word first, and stop when we find words that
     * are different.  The size of the result will be one more than
     * the position of the first word that differs.
     */

    len1 = z1->len;
    len2 = z2->len;
    if ( len1 != len2 ) {
	bigger2 = (len1 < len2 );
    } else {
	p1 = z1->v + ( len1 - 1 );
	p2 = z2->v + ( len2 - 1 );
	while ( ( len1 != 0 ) && ( *p1 == *p2 ) ) {
	    p1--;
	    p2--;
	    len1--;
	}
	if ( len1 == 0 ) {
	    retVal = NewBigInt(1);
	    retVal->signum = 0;
	    retVal->v[0] = 0;
	    return retVal;
	}
	len2 = len1;
	bigger2 = ( *p1 < *p2 );
    }

    /* 
     * At this point, len1 and len2 have the effective lengths of
     * operands 1 and 2, respectively. bigger2 is true if the result
     * will be negative, and indicates that the absolute value of the
     * result will be computed by reversing the sense of the subtraction
     * (subtracting z1 from z2).
     */

    if ( bigger2 ) {
	retVal = NewBigInt( len2 );
	p1 = z2->v;
	p2 = z1->v;
	len2 = (len2 - len1);
    } else {
	retVal = NewBigInt( len1 );
	p1 = z1->v;
	p2 = z2->v;
	t = (len1 - len2);
	len1 = len2;
	len2 = t;
    }
    retVal->signum = bigger2;
    pd = retVal->v;
    carry = 1;
    
    /*
     * Subtract the two operands.  
     */

    while ( len1-- ) {
	carry = carry + NARROW_UINT_MAX + *p1++ - *p2++;
	*pd++ = (Tcl_NarrowUInt) ( carry & NARROW_UINT_MAX );
	carry >>= NARROW_UINT_BITS;
    }
    while ( len2-- ) {
	carry = carry + NARROW_UINT_MAX + *p1++;
	*pd++ = (Tcl_NarrowUInt) ( carry & NARROW_UINT_MAX );
	carry >>= NARROW_UINT_BITS;
    }

    /* Clean up any zeroes at the more significant end */

    while ( pd > retVal->v ) {
	if ( *pd != 0 ) break;
	--pd;
    }
    retVal->len = pd + 1 - retVal->v;

    return retVal;
    
}