@@ -76,16 +76,23 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
7676
7777/*
7878** IMHO, mpz_m{div,mod,divmod}() do the wrong things when the denominator < 0
79- ** I assume that this will be fixed in a future release
79+ ** This has been fixed with gmp release 2.0
8080*/
8181/*#define MPZ_MDIV_BUG fixed the (for me) nexessary parts in libgmp.a */
8282/*
8383** IMO, mpz_get_str() assumes a bit too large target space, if he doesn't
8484** allocate it himself
8585*/
86- #define MPZ_GET_STR_BUG
8786
8887#include "gmp.h"
88+ #include "gmp-impl.h"
89+
90+ #if __GNU_MP__ == 2
91+ #define GMP2
92+ #else
93+ #define MPZ_GET_STR_BUG
94+ #endif
95+
8996typedef struct {
9097 OB_HEAD
9198 MP_INT mpz ; /* the actual number */
@@ -117,7 +124,6 @@ newmpzobject()
117124} /* newmpzobject() */
118125
119126#ifdef MPZ_GET_STR_BUG
120- #include "gmp-impl.h"
121127#include "longlong.h"
122128#endif /* def MPZ_GET_STR_BUG */
123129
@@ -162,8 +168,13 @@ mpz_format(objp, base, withname)
162168 (int )mpz_sizeinbase (& mpzp -> mpz , base ));
163169#endif /* def MPZ_DEBUG */
164170#ifdef MPZ_GET_STR_BUG
171+ #ifdef GMP2
172+ i += ((size_t ) abs (mpzp -> mpz ._mp_size ) * BITS_PER_MP_LIMB
173+ * __mp_bases [base ].chars_per_bit_exactly ) + 1 ;
174+ #else
165175 i += ((size_t ) abs (mpzp -> mpz .size ) * BITS_PER_MP_LIMB
166176 * __mp_bases [base ].chars_per_bit_exactly ) + 1 ;
177+ #endif
167178#else /* def MPZ_GET_STR_BUG */
168179 i += (int )mpz_sizeinbase (& mpzp -> mpz , base );
169180#endif /* def MPZ_GET_STR_BUG else */
@@ -580,7 +591,6 @@ mpz_power(a, b, m)
580591{
581592 mpzobject * z ;
582593 int cmpres ;
583- long int longtmp1 , longtmp2 ;
584594
585595 if ((object * )m != Py_None )
586596 {
@@ -725,7 +735,7 @@ mpz_nonzero(v)
725735} /* mpz_nonzero() */
726736
727737static object *
728- mpz_invert (v )
738+ py_mpz_invert (v )
729739 mpzobject * v ;
730740{
731741 mpzobject * z ;
@@ -737,7 +747,7 @@ mpz_invert(v)
737747
738748 mpz_com (& z -> mpz , & v -> mpz );
739749 return (object * )z ;
740- } /* mpz_invert () */
750+ } /* py_mpz_invert () */
741751
742752static object *
743753mpz_lshift (a , b )
@@ -925,7 +935,7 @@ MPZ_mpz(self, args)
925935 mpz_init (& mplongdigit );
926936
927937 /* how we're gonna handle this? */
928- if (isnegative = ((i = ((longobject * )objp )-> ob_size ) < 0 ) )
938+ if (( isnegative = ((i = ((longobject * )objp )-> ob_size ) < 0 ) ) )
929939 i = - i ;
930940
931941 while (i -- ) {
@@ -1220,7 +1230,11 @@ mpz_divm(res, num, den, mod)
12201230 mpz_init_set (& d0 , den );
12211231 mpz_init_set (& d1 , mod );
12221232
1233+ #ifdef GMP2
1234+ while (d1 ._mp_size != 0 ) {
1235+ #else
12231236 while (d1 .size != 0 ) {
1237+ #endif
12241238 mpz_divmod (& q , & r , & d0 , & d1 );
12251239 mpz_set (& d0 , & d1 );
12261240 mpz_set (& d1 , & r );
@@ -1231,8 +1245,13 @@ mpz_divm(res, num, den, mod)
12311245 mpz_set (& s1 , & x );
12321246 }
12331247
1248+ #ifdef GMP2
1249+ if (d0 ._mp_size != 1 || d0 ._mp_d [0 ] != 1 )
1250+ res -> _mp_size = 0 ; /* trouble: the gcd != 1; set s to zero */
1251+ #else
12341252 if (d0 .size != 1 || d0 .d [0 ] != 1 )
12351253 res -> size = 0 ; /* trouble: the gcd != 1; set s to zero */
1254+ #endif
12361255 else {
12371256#ifdef MPZ_MDIV_BUG
12381257 /* watch out here! first check the signs, and then perform
@@ -1364,7 +1383,7 @@ mpz_long(self)
13641383
13651384 /* determine sign, and copy self to scratch var */
13661385 mpz_init_set (& mpzscratch , & self -> mpz );
1367- if (isnegative = (mpz_cmp_ui (& self -> mpz , (unsigned long int )0 ) < 0 ))
1386+ if (( isnegative = (mpz_cmp_ui (& self -> mpz , (unsigned long int )0 ) < 0 ) ))
13681387 mpz_neg (& mpzscratch , & mpzscratch );
13691388
13701389 /* let those bits come, let those bits go,
@@ -1440,7 +1459,7 @@ mpz_float(self)
14401459 i = (int )mpz_size (& self -> mpz );
14411460
14421461 /* determine sign, and copy abs(self) to scratch var */
1443- if (isnegative = (mpz_cmp_ui (& self -> mpz , (unsigned long int )0 ) < 0 )) {
1462+ if (( isnegative = (mpz_cmp_ui (& self -> mpz , (unsigned long int )0 ) < 0 ) )) {
14441463 mpz_init (& mpzscratch );
14451464 mpz_neg (& mpzscratch , & self -> mpz );
14461465 }
@@ -1635,7 +1654,7 @@ static number_methods mpz_as_number = {
16351654 UF mpz_positive , /*tp_positive*/
16361655 UF mpz_absolute , /*tp_absolute*/
16371656 IF mpz_nonzero , /*tp_nonzero*/
1638- UF mpz_invert , /*nb_invert*/
1657+ UF py_mpz_invert , /*nb_invert*/
16391658 BF mpz_lshift , /*nb_lshift*/
16401659 BF mpz_rshift , /*nb_rshift*/
16411660 BF mpz_andfunc , /*nb_and*/
0 commit comments