@@ -225,7 +225,7 @@ mpz_manager<SYNCH>::sign_cell::sign_cell(mpz_manager& m, mpz const& a):
225225
226226template <bool SYNCH>
227227void mpz_manager<SYNCH>::del(mpz_manager<SYNCH>* m, mpz & a) {
228- if (!a. is_small ()) {
228+ if (a. has_ptr ()) {
229229 SASSERT (m);
230230 mpz::mpz_type* p = a.ptr ();
231231 m->deallocate (!a.is_external (), p);
@@ -272,7 +272,7 @@ void mpz_manager<SYNCH>::set_big_i64(mpz & c, int64_t v) {
272272 _v = v;
273273 }
274274#ifndef _MP_GMP
275- if (c. is_small ()) {
275+ if (!c. has_ptr ()) {
276276 c.set_ptr (allocate (m_init_cell_capacity), sign, false );
277277 } else {
278278 c.set_sign (sign ? -1 : 1 );
@@ -290,7 +290,7 @@ void mpz_manager<SYNCH>::set_big_i64(mpz & c, int64_t v) {
290290 c.ptr ()->m_size = digits (c)[1 ] == 0 ? 1 : 2 ;
291291 }
292292#else
293- if (c. is_small ()) {
293+ if (!c. has_ptr ()) {
294294 c.set_ptr (allocate (), false , false );
295295 }
296296 mpz_set_ui (*c.ptr (), static_cast <unsigned >(_v));
@@ -310,7 +310,7 @@ void mpz_manager<SYNCH>::set_big_i64(mpz & c, int64_t v) {
310310template <bool SYNCH>
311311void mpz_manager<SYNCH>::set_big_ui64(mpz & c, uint64_t v) {
312312#ifndef _MP_GMP
313- if (c. is_small ()) {
313+ if (!c. has_ptr ()) {
314314 c.set_ptr (allocate (m_init_cell_capacity), false , false ); // positive, owned
315315 } else {
316316 c.set_sign (1 ); // positive
@@ -328,7 +328,7 @@ void mpz_manager<SYNCH>::set_big_ui64(mpz & c, uint64_t v) {
328328 c.ptr ()->m_size = digits (c)[1 ] == 0 ? 1 : 2 ;
329329 }
330330#else
331- if (c. is_small ()) {
331+ if (!c. has_ptr ()) {
332332 c.set_ptr (allocate (), false , false ); // positive, owned
333333 }
334334 mpz_set_ui (*c.ptr (), static_cast <unsigned >(v));
@@ -344,7 +344,7 @@ void mpz_manager<SYNCH>::set_big_ui64(mpz & c, uint64_t v) {
344344
345345template <bool SYNCH>
346346mpz_manager<SYNCH>::ensure_mpz_t ::ensure_mpz_t (mpz const & a) {
347- if (is_small (a )) {
347+ if (!a. has_ptr ( )) {
348348 m_result = &m_local;
349349 mpz_init (m_local);
350350 mpz_set_si (m_local, a.value ());
@@ -377,15 +377,15 @@ void mpz_manager<SYNCH>::set(mpz_cell& src, mpz & a, int sign, unsigned sz) {
377377
378378 unsigned d = src.m_digits [0 ];
379379 int64_t val = sign < 0 ? -static_cast <int64_t >(d) : static_cast <int64_t >(d);
380- if (i == 1 && mpz::fits_in_small (val) && a. is_small ()) {
381- a. set (val);
380+ if (i == 1 && mpz::fits_in_small (val) && !a. has_ptr ()) {
381+ set (a, val);
382382 return ;
383383 }
384384
385385 set_digits (a, i, src.m_digits );
386386 a.set_sign (sign);
387387
388- SASSERT (!a. is_small ());
388+ SASSERT (a. has_ptr ());
389389}
390390#endif
391391
@@ -668,7 +668,7 @@ template<bool SYNCH>
668668void mpz_manager<SYNCH>::neg(mpz & a) {
669669 STRACE (mpz, tout << " [mpz] 0 - " << to_string (a) << " == " ;);
670670 if (is_small (a)) {
671- a. set (-a.value ());
671+ set (a, -a.value ());
672672 }
673673#ifndef _MP_GMP
674674 else {
@@ -687,7 +687,7 @@ void mpz_manager<SYNCH>::abs(mpz & a) {
687687 if (is_small (a)) {
688688 int64_t v = a.value ();
689689 if (v < 0 ) {
690- a. set (-v);
690+ set (a, -v);
691691 }
692692 }
693693 else {
@@ -947,7 +947,7 @@ void mpz_manager<SYNCH>::gcd(mpz const & a, mpz const & b, mpz & c) {
947947
948948 // reset least significant bit
949949 if (is_small (v))
950- v. set (v.value () & ~1 );
950+ set (v, v.value () & ~1 );
951951 else
952952 v.ptr ()->m_digits [0 ] &= ~static_cast <digit_t >(1 );
953953 k_v = power_of_two_multiple (v);
@@ -1079,7 +1079,7 @@ void mpz_manager<SYNCH>::gcd(mpz const & a, mpz const & b, mpz & c) {
10791079 }
10801080 }
10811081 sign_cell ca (*this , a1);
1082- SASSERT (! is_small (b1 ));
1082+ SASSERT (b1. has_ptr ( ));
10831083 a_sz = ca.cell ()->m_size ;
10841084 b_sz = b1.ptr ()->m_size ;
10851085 SASSERT (b_sz <= a_sz);
@@ -1145,7 +1145,7 @@ void mpz_manager<SYNCH>::gcd(mpz const & a, mpz const & b, mpz & c) {
11451145
11461146template <bool SYNCH>
11471147unsigned mpz_manager<SYNCH>::size_info(mpz const & a) {
1148- if (is_small (a ))
1148+ if (!a. has_ptr ( ))
11491149 return 1 ;
11501150#ifndef _MP_GMP
11511151 return a.ptr ()->m_size + 1 ;
@@ -1343,7 +1343,7 @@ void mpz_manager<SYNCH>::bitwise_or(mpz const & a, mpz const & b, mpz & c) {
13431343 SASSERT (is_nonneg (b));
13441344 TRACE (mpz, tout << " is_small(a): " << is_small (a) << " , is_small(b): " << is_small (b) << " \n " ;);
13451345 if (is_small (a) && is_small (b)) {
1346- c. set (a.value () | b.value ());
1346+ set (c, a.value () | b.value ());
13471347 }
13481348 else {
13491349#ifndef _MP_GMP
@@ -1388,7 +1388,7 @@ void mpz_manager<SYNCH>::bitwise_and(mpz const & a, mpz const & b, mpz & c) {
13881388 SASSERT (is_nonneg (a));
13891389 SASSERT (is_nonneg (b));
13901390 if (is_small (a) && is_small (b)) {
1391- c. set (a.value () & b.value ());
1391+ set (c, a.value () & b.value ());
13921392 }
13931393 else {
13941394#ifndef _MP_GMP
@@ -1535,16 +1535,13 @@ int mpz_manager<SYNCH>::big_compare(mpz const & a, mpz const & b) {
15351535
15361536template <bool SYNCH>
15371537bool mpz_manager<SYNCH>::is_uint64(mpz const & a) const {
1538- #ifndef _MP_GMP
15391538 if (is_small (a))
15401539 return a.value () >= 0 ;
1540+ #ifndef _MP_GMP
15411541 if (a.sign () < 0 )
15421542 return false ;
15431543 return size (a) <= (sizeof (digit_t ) == sizeof (uint64_t ) ? 1 : 2 );
15441544#else
1545- // GMP version
1546- if (is_small (a))
1547- return a.value () >= 0 ;
15481545 return is_nonneg (a) && mpz_cmp (*a.ptr (), m_uint64_max) <= 0 ;
15491546#endif
15501547}
@@ -1831,7 +1828,7 @@ void mpz_manager<SYNCH>::power(mpz const & a, unsigned p, mpz & b) {
18311828 if (is_small (a)) {
18321829 if (a.value () == 2 ) {
18331830 if (p < 8 * sizeof (int ) - 1 ) {
1834- b. set (1 << p);
1831+ set (b, 1 << p);
18351832 }
18361833 else {
18371834 unsigned sz = p/(8 * sizeof (digit_t )) + 1 ;
@@ -1927,9 +1924,9 @@ void mpz_manager<SYNCH>::ensure_capacity(mpz & a, unsigned capacity) {
19271924 if (capacity < m_init_cell_capacity)
19281925 capacity = m_init_cell_capacity;
19291926
1930- if (is_small (a )) {
1927+ if (!a. has_ptr ( )) {
19311928 int64_t val = a.value ();
1932- uint64_t abs_val = static_cast <uint64_t >(- val);
1929+ uint64_t abs_val = static_cast <uint64_t >(val < 0 ? -val : val);
19331930 allocate_if_needed (a, capacity);
19341931 if (sizeof (digit_t ) == sizeof (uint64_t )) {
19351932 a.ptr ()->m_digits [0 ] = static_cast <digit_t >(abs_val);
@@ -1974,10 +1971,10 @@ void mpz_manager<SYNCH>::machine_div2k(mpz & a, unsigned k) {
19741971 if (k < 64 ) {
19751972 int64_t twok = 1ull << ((int64_t )k);
19761973 int64_t val = a.value ();
1977- a. set (val/twok);
1974+ set (a, val/twok);
19781975 }
19791976 else {
1980- a. set (0 );
1977+ set (a, 0 );
19811978 }
19821979 return ;
19831980 }
@@ -2040,20 +2037,22 @@ template<bool SYNCH>
20402037void mpz_manager<SYNCH>::mul2k(mpz & a, unsigned k) {
20412038 if (k == 0 || is_zero (a))
20422039 return ;
2043- if (is_small (a) && k < 32 ) {
2044- set (a, a.value () * (static_cast <int64_t >(1 ) << k));
2040+
2041+ int64_t result;
2042+ if (is_small (a) && k < 64 && !mul_overflows (a.value (), static_cast <int64_t >(1 ) << k, result)) {
2043+ set (a, result);
20452044 return ;
20462045 }
20472046#ifndef _MP_GMP
20482047 TRACE (mpz_mul2k, tout << " mul2k\n a: " << to_string (a) << " \n k: " << k << " \n " ;);
20492048 unsigned word_shift = k / (8 * sizeof (digit_t ));
20502049 unsigned bit_shift = k % (8 * sizeof (digit_t ));
2051- unsigned old_sz = is_small (a ) ? 1 : a. ptr ()-> m_size ;
2050+ unsigned old_sz = a. has_ptr ( ) ? size (a) : ( sizeof ( int64_t ) / sizeof ( digit_t )) ;
20522051 unsigned new_sz = old_sz + word_shift + 1 ;
20532052 ensure_capacity (a, new_sz);
20542053 TRACE (mpz_mul2k, tout << " word_shift: " << word_shift << " \n bit_shift: " << bit_shift << " \n old_sz: " << old_sz << " \n new_sz: " << new_sz
20552054 << " \n a after ensure capacity:\n " << to_string (a) << " \n " ;);
2056- SASSERT (! is_small (a ));
2055+ SASSERT (a. has_ptr ( ));
20572056 mpz_cell * cell_a = a.ptr ();
20582057 old_sz = cell_a->m_size ;
20592058 digit_t * ds = cell_a->m_digits ;
0 commit comments