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:
parent
bc9d528a75
commit
d27cabec93
|
@ -1787,25 +1787,19 @@ public:
|
||||||
|
|
||||||
#endif
|
#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>
|
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;
|
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;
|
exponent = (sint(int_size)-sint(man)) * sint(TTMATH_BITS_PER_UINT) - compensation;
|
||||||
|
|
||||||
// copying the highest words
|
// copying the highest words
|
||||||
|
@ -1816,10 +1810,42 @@ public:
|
||||||
// setting the rest of mantissa.table into zero (if some has left)
|
// setting the rest of mantissa.table into zero (if some has left)
|
||||||
for( ; i<=man ; ++i)
|
for( ; i<=man ; ++i)
|
||||||
mantissa.table[man-i] = 0;
|
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();
|
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
|
a default constructor
|
||||||
|
|
||||||
|
@ -1866,7 +1914,6 @@ public:
|
||||||
/*!
|
/*!
|
||||||
the default assignment operator
|
the default assignment operator
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Big<exp,man> & operator=(const Big<exp,man> & value)
|
Big<exp,man> & operator=(const Big<exp,man> & value)
|
||||||
{
|
{
|
||||||
info = value.info;
|
info = value.info;
|
||||||
|
|
|
@ -475,17 +475,14 @@ public:
|
||||||
* convertion methods
|
* convertion methods
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
private:
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
this method convert an UInt<another_size> type to this class
|
an auxiliary method for converting both from UInt and Int
|
||||||
|
|
||||||
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>
|
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 min_size = (value_size < argument_size)? value_size : argument_size;
|
||||||
uint i;
|
uint i;
|
||||||
|
@ -496,15 +493,25 @@ public:
|
||||||
|
|
||||||
if( value_size > argument_size )
|
if( value_size > argument_size )
|
||||||
{
|
{
|
||||||
// 'this' is longer than 'p'
|
uint fill;
|
||||||
uint fill = (p.table[argument_size-1] & TTMATH_UINT_HIGHEST_BIT)? TTMATH_UINT_MAX_VALUE : 0;
|
|
||||||
|
|
||||||
|
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)
|
for( ; i<value_size ; ++i)
|
||||||
UInt<value_size>::table[i] = fill;
|
UInt<value_size>::table[i] = fill;
|
||||||
}
|
}
|
||||||
else
|
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)
|
for( ; i<argument_size ; ++i)
|
||||||
if( p.table[i] != test )
|
if( p.table[i] != test )
|
||||||
|
@ -514,11 +521,27 @@ public:
|
||||||
return 0;
|
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
|
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;
|
uint fill = ( value<0 ) ? TTMATH_UINT_MAX_VALUE : 0;
|
||||||
|
|
||||||
|
@ -526,16 +549,46 @@ public:
|
||||||
UInt<value_size>::table[i] = fill;
|
UInt<value_size>::table[i] = fill;
|
||||||
|
|
||||||
UInt<value_size>::table[0] = uint(value);
|
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
|
this method converts UInt<another_size> into this class
|
||||||
|
|
||||||
it doesn't return a carry
|
|
||||||
*/
|
*/
|
||||||
template<uint argument_size>
|
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);
|
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);
|
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)
|
Int<value_size> & operator=(uint i)
|
||||||
{
|
{
|
||||||
UInt<value_size>::FromUInt(i);
|
FromUInt(i);
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -590,10 +682,23 @@ public:
|
||||||
*/
|
*/
|
||||||
Int(uint i)
|
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
|
#ifdef TTMATH_PLATFORM64
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -630,7 +735,7 @@ public:
|
||||||
*/
|
*/
|
||||||
Int<value_size> & operator=(unsigned int i)
|
Int<value_size> & operator=(unsigned int i)
|
||||||
{
|
{
|
||||||
UInt<value_size>::FromUInt(uint(i));
|
FromUInt(uint(i));
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -644,7 +749,7 @@ public:
|
||||||
*/
|
*/
|
||||||
Int(unsigned int i)
|
Int(unsigned int i)
|
||||||
{
|
{
|
||||||
UInt<value_size>::FromUInt(uint(i));
|
FromUInt(uint(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#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
|
the destructor
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue