fixed: RclMoveAllWords() and RcrMoveAllWords() sometimes didn't return

the proper carry, (when 'bits' was greater than or equal to 'value_size')
        this had impact on Rcl() and Rcr(), they also returned the wrong carry


git-svn-id: svn://ttmath.org/publicrep/ttmath/trunk@85 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2008-10-31 20:43:08 +00:00
parent 712bfc9c3b
commit 1d81dc75ff
1 changed files with 34 additions and 34 deletions

View File

@ -762,7 +762,6 @@ public:
private: private:
public: ///// !!!!!!
#ifdef TTMATH_PLATFORM32 #ifdef TTMATH_PLATFORM32
@ -1195,34 +1194,37 @@ private:
this method moves only words this method moves only words
*/ */
void RclMoveAllWords( sint & all_words, uint & rest_bits, uint & last_c, void RclMoveAllWords(uint & rest_bits, uint & last_c, uint bits, uint c)
uint bits, uint c)
{ {
rest_bits = sint(bits % TTMATH_BITS_PER_UINT); rest_bits = sint(bits % TTMATH_BITS_PER_UINT);
all_words = sint(bits / TTMATH_BITS_PER_UINT); sint all_words = sint(bits / TTMATH_BITS_PER_UINT);
uint mask = ( c ) ? TTMATH_UINT_MAX_VALUE : 0;
if( all_words >= sint(value_size) ) if( all_words >= sint(value_size) )
{ {
if( all_words == value_size && rest_bits == 0 ) if( all_words == value_size && rest_bits == 0 )
last_c = table[0] & 1; last_c = table[0] & 1;
all_words = value_size; // not value_size - 1 // clearing
for(uint i = 0 ; i<value_size ; ++i)
table[i] = mask;
rest_bits = 0; rest_bits = 0;
} }
else
if( all_words > 0 ) if( all_words > 0 )
{ {
sint first; // 0 < all_words < value_size
sint second;
sint first, second;
last_c = table[value_size - all_words] & 1; // all_words is greater than 0 last_c = table[value_size - all_words] & 1; // all_words is greater than 0
// copying the first part of the value // copying the first part of the value
for(first = value_size-1, second=first-all_words ; second>=0 ; --first, --second) for(first = value_size-1, second=first-all_words ; second>=0 ; --first, --second)
table[first] = table[second]; table[first] = table[second];
// sets the rest bits of value into 'c' // setting the rest to 'c'
uint mask = c ? TTMATH_UINT_MAX_VALUE : 0;
for( ; first>=0 ; --first ) for( ; first>=0 ; --first )
table[first] = mask; table[first] = mask;
} }
@ -1243,14 +1245,13 @@ public:
uint Rcl(uint bits, uint c=0) uint Rcl(uint bits, uint c=0)
{ {
uint last_c = 0; uint last_c = 0;
sint all_words = 0;
uint rest_bits = bits; uint rest_bits = bits;
if( bits == 0 ) if( bits == 0 )
return 0; return 0;
if( bits >= TTMATH_BITS_PER_UINT ) if( bits >= TTMATH_BITS_PER_UINT )
RclMoveAllWords(all_words, rest_bits, last_c, bits, c); RclMoveAllWords(rest_bits, last_c, bits, c);
if( rest_bits == 0 ) if( rest_bits == 0 )
return last_c; return last_c;
@ -1282,35 +1283,36 @@ private:
this method moves only words this method moves only words
*/ */
void RcrMoveAllWords( sint & all_words, uint & rest_bits, uint & last_c, void RcrMoveAllWords(uint & rest_bits, uint & last_c, uint bits, uint c)
uint bits, uint c)
{ {
rest_bits = sint(bits % TTMATH_BITS_PER_UINT); rest_bits = sint(bits % TTMATH_BITS_PER_UINT);
all_words = sint(bits / TTMATH_BITS_PER_UINT); sint all_words = sint(bits / TTMATH_BITS_PER_UINT);
uint mask = c ? TTMATH_UINT_MAX_VALUE : 0;
if( all_words >= sint(value_size) ) if( all_words >= sint(value_size) )
{ {
if( all_words == value_size && rest_bits == 0 ) if( all_words == value_size && rest_bits == 0 )
last_c = (table[value_size-1] & TTMATH_UINT_HIGHEST_BIT) ? 1 : 0; last_c = (table[value_size-1] & TTMATH_UINT_HIGHEST_BIT) ? 1 : 0;
all_words = value_size; // not value_size - 1 // clearing
for(uint i = 0 ; i<value_size ; ++i)
table[i] = mask;
rest_bits = 0; rest_bits = 0;
} }
else if( all_words > 0 )
if( all_words > 0 )
{ {
uint first; // 0 < all_words < value_size
uint second;
uint first, second;
last_c = (table[all_words - 1] & TTMATH_UINT_HIGHEST_BIT) ? 1 : 0; // all_words is > 0 last_c = (table[all_words - 1] & TTMATH_UINT_HIGHEST_BIT) ? 1 : 0; // all_words is > 0
// copying the first part of the value // copying the first part of the value
for(first=0, second=all_words ; second<value_size ; ++first, ++second) for(first=0, second=all_words ; second<value_size ; ++first, ++second)
table[first] = table[second]; table[first] = table[second];
// sets the rest bits of value into 'c' // setting the rest to 'c'
uint mask = c ? TTMATH_UINT_MAX_VALUE : 0;
for( ; first<value_size ; ++first ) for( ; first<value_size ; ++first )
table[first] = mask; table[first] = mask;
} }
@ -1331,14 +1333,13 @@ public:
uint Rcr(uint bits, uint c=0) uint Rcr(uint bits, uint c=0)
{ {
uint last_c = 0; uint last_c = 0;
sint all_words = 0;
uint rest_bits = bits; uint rest_bits = bits;
if( bits == 0 ) if( bits == 0 )
return 0; return 0;
if( bits >= TTMATH_BITS_PER_UINT ) if( bits >= TTMATH_BITS_PER_UINT )
RcrMoveAllWords(all_words, rest_bits, last_c, bits, c); RcrMoveAllWords(rest_bits, last_c, bits, c);
if( rest_bits == 0 ) if( rest_bits == 0 )
return last_c; return last_c;
@ -3574,7 +3575,6 @@ public:
#ifdef TTMATH_PLATFORM64 #ifdef TTMATH_PLATFORM64
private: private:
public:
uint Rcl2_one(uint c); uint Rcl2_one(uint c);
uint Rcr2_one(uint c); uint Rcr2_one(uint c);
uint Rcl2(uint bits, uint c); uint Rcl2(uint bits, uint c);