changed: Int::FromInt(const Int<argument_size> & p),

Int::FromInt(sint value) (it returns zero now)
         Int::operator=(uint i)
         Int::Int(uint i)
added:   Int::FromUInt(const UInt<argument_size> & p),
         Int::FromUInt(uint value)
         and appropriate constructors and assignment 
         operators as well
changed: Big::FromInt(Int<int_size> value),
added:   Big::FromUInt(UInt<int_size> value),
         Big::operator=(const UInt<int_size> & value)
         Big::Big(const UInt<int_size> & value)


git-svn-id: svn://ttmath.org/publicrep/ttmath/trunk@42 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2007-05-27 23:33:47 +00:00
parent bc9d528a75
commit d27cabec93
2 changed files with 189 additions and 49 deletions

View File

@ -1787,25 +1787,19 @@ public:
#endif
private:
/*!
a method for converting from 'Int<int_size>' to this class
an auxiliary method for converting from UInt and Int
we assume that there'll never be a carry here
(we have an exponent and the value in Big can be bigger than
that one from the UInt)
*/
template<uint int_size>
void FromInt(Int<int_size> value)
void FromUIntOrInt(const UInt<int_size> & value, sint compensation)
{
info = 0;
bool is_sign = false;
if( value.IsSign() )
{
value.ChangeSign();
is_sign = true;
}
uint minimum_size = (int_size < man)? int_size : man;
sint compensation = (sint)value.CompensationToLeft();
exponent = (sint(int_size)-sint(man)) * sint(TTMATH_BITS_PER_UINT) - compensation;
// copying the highest words
@ -1816,10 +1810,42 @@ public:
// setting the rest of mantissa.table into zero (if some has left)
for( ; i<=man ; ++i)
mantissa.table[man-i] = 0;
}
if( is_sign )
public:
/*!
a method for converting from 'UInt<int_size>' to this class
*/
template<uint int_size>
void FromUInt(UInt<int_size> value)
{
info = 0;
sint compensation = (sint)value.CompensationToLeft();
return FromUIntOrInt(value, compensation);
}
/*!
a method for converting from 'Int<int_size>' to this class
*/
template<uint int_size>
void FromInt(Int<int_size> value)
{
info = 0;
if( value.IsSign() )
{
value.ChangeSign();
SetSign();
}
sint compensation = (sint)value.CompensationToLeft();
return FromUIntOrInt(value, compensation);
}
@ -1845,6 +1871,28 @@ public:
}
/*!
an operator= for converting from 'UInt<int_size>' to this class
*/
template<uint int_size>
Big<exp,man> & operator=(const UInt<int_size> & value)
{
FromUInt(value);
return *this;
}
/*!
a constructor for converting from 'UInt<int_size>' to this class
*/
template<uint int_size>
Big(const UInt<int_size> & value)
{
FromUInt(value);
}
/*!
a default constructor
@ -1866,7 +1914,6 @@ public:
/*!
the default assignment operator
*/
Big<exp,man> & operator=(const Big<exp,man> & value)
{
info = value.info;

View File

@ -475,17 +475,14 @@ public:
* convertion methods
*
*/
private:
/*!
this method convert an UInt<another_size> type to this class
this operation has mainly sense if the value from p
can be held in this type
it returns a carry if the value 'p' is too big
an auxiliary method for converting both from UInt and Int
*/
template<uint argument_size>
uint FromInt(const Int<argument_size> & p)
uint FromUIntOrInt(const UInt<argument_size> & p, bool UInt_type)
{
uint min_size = (value_size < argument_size)? value_size : argument_size;
uint i;
@ -496,15 +493,25 @@ public:
if( value_size > argument_size )
{
// 'this' is longer than 'p'
uint fill = (p.table[argument_size-1] & TTMATH_UINT_HIGHEST_BIT)? TTMATH_UINT_MAX_VALUE : 0;
uint fill;
if( UInt_type )
fill = 0;
else
fill = (p.table[argument_size-1] & TTMATH_UINT_HIGHEST_BIT)?
TTMATH_UINT_MAX_VALUE : 0;
// 'this' is longer than 'p'
for( ; i<value_size ; ++i)
UInt<value_size>::table[i] = fill;
}
else
{
uint test = (UInt<value_size>::table[value_size-1] & TTMATH_UINT_HIGHEST_BIT)? TTMATH_UINT_MAX_VALUE : 0;
uint test = (UInt<value_size>::table[value_size-1] & TTMATH_UINT_HIGHEST_BIT)?
TTMATH_UINT_MAX_VALUE : 0;
if( UInt_type && test!=0 )
return 1;
for( ; i<argument_size ; ++i)
if( p.table[i] != test )
@ -514,11 +521,27 @@ public:
return 0;
}
public:
/*!
this method converts an Int<another_size> type into this class
this operation has mainly sense if the value from p
can be held in this type
it returns a carry if the value 'p' is too big
*/
template<uint argument_size>
uint FromInt(const Int<argument_size> & p)
{
return FromUIntOrInt(p, false);
}
/*!
this method converts the sint type into this class
*/
void FromInt(sint value)
uint FromInt(sint value)
{
uint fill = ( value<0 ) ? TTMATH_UINT_MAX_VALUE : 0;
@ -526,16 +549,46 @@ public:
UInt<value_size>::table[i] = fill;
UInt<value_size>::table[0] = uint(value);
// there'll never be a carry here
return 0;
}
/*!
this operator converts an UInt<another_size> type to this class
it doesn't return a carry
this method converts UInt<another_size> into this class
*/
template<uint argument_size>
Int<value_size> & operator=(const Int<argument_size> & p)
uint FromUInt(const UInt<argument_size> & p)
{
return FromUIntOrInt(p, true);
}
/*!
this method converts the uint type into this class
*/
uint FromUInt(uint value)
{
for(uint i=1 ; i<value_size ; ++i)
UInt<value_size>::table[i] = 0;
UInt<value_size>::table[0] = value;
// there can be a carry here when the size of this value is equal one word
// and the 'value' has the highest bit set
if( value_size==1 && (value & TTMATH_UINT_HIGHEST_BIT)!=0 )
return 1;
return 0;
}
// converting from Int
/*!
the default assignment operator
*/
Int<value_size> & operator=(const Int<value_size> & p)
{
FromInt(p);
@ -544,9 +597,12 @@ public:
/*!
the default assignment operator
this operator converts an Int<another_size> type to this class
it doesn't return a carry
*/
Int<value_size> & operator=(const Int<value_size> & p)
template<uint argument_size>
Int<value_size> & operator=(const Int<argument_size> & p)
{
FromInt(p);
@ -575,11 +631,47 @@ public:
/*!
this method converts the uint type to this class
the copy constructor
*/
Int(const Int<value_size> & u)
{
FromInt(u);
}
/*!
a constructor for copying from another types
*/
template<uint argument_size>
Int(const Int<argument_size> & u)
{
// look that 'size' we still set as 'value_size' and not as u.value_size
FromInt(u);
}
// converting from UInt
/*!
this operator converts an UInt<another_size> type to this class
it doesn't return a carry
*/
template<uint argument_size>
Int<value_size> & operator=(const UInt<argument_size> & p)
{
FromUInt(p);
return *this;
}
/*!
this method converts the Uint type to this class
*/
Int<value_size> & operator=(uint i)
{
UInt<value_size>::FromUInt(i);
FromUInt(i);
return *this;
}
@ -590,10 +682,23 @@ public:
*/
Int(uint i)
{
UInt<value_size>::FromUInt(i);
FromUInt(i);
}
/*!
a constructor for copying from another types
*/
template<uint argument_size>
Int(const UInt<argument_size> & u)
{
// look that 'size' we still set as 'value_size' and not as u.value_size
FromUInt(u);
}
//
#ifdef TTMATH_PLATFORM64
/*!
@ -630,7 +735,7 @@ public:
*/
Int<value_size> & operator=(unsigned int i)
{
UInt<value_size>::FromUInt(uint(i));
FromUInt(uint(i));
return *this;
}
@ -644,7 +749,7 @@ public:
*/
Int(unsigned int i)
{
UInt<value_size>::FromUInt(uint(i));
FromUInt(uint(i));
}
#endif
@ -678,18 +783,6 @@ public:
}
/*!
the copy constructor
*/
template<uint argument_size>
Int(const Int<argument_size> & u) : UInt<value_size>::size(value_size)
{
// look that 'size' we still set as 'value_size' and not as u.value_size
operator=(u);
}
/*!
the destructor
*/