Skip to content

Commit 5d9b816

Browse files
committed
py: Fix bug in mpn_shl (multi-prec int shift left).
Before this patch, eg, 1 << 75 (or any large multiple of 15) was setting the MSB in the digits, which is outside the valid range of DIG_MASK.
1 parent 3ef9113 commit 5d9b816

1 file changed

Lines changed: 5 additions & 8 deletions

File tree

py/mpz.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ STATIC uint mpn_shl(mpz_dig_t *idig, mpz_dig_t *jdig, uint jlen, uint n) {
8989
mpz_dbl_dig_t d = 0;
9090
for (uint i = jlen; i > 0; i--, idig--, jdig--) {
9191
d |= *jdig;
92-
*idig = d >> (DIG_SIZE - n_part);
92+
*idig = (d >> (DIG_SIZE - n_part)) & DIG_MASK;
9393
d <<= DIG_SIZE;
9494
}
9595

9696
// store remaining bits
97-
*idig = d >> (DIG_SIZE - n_part);
97+
*idig = (d >> (DIG_SIZE - n_part)) & DIG_MASK;
9898
idig -= n_whole - 1;
9999
memset(idig, 0, (n_whole - 1) * sizeof(mpz_dig_t));
100100

@@ -1132,12 +1132,9 @@ mpz_t *mpz_gcd(const mpz_t *z1, const mpz_t *z2) {
11321132
lcm(0, 0) = 0
11331133
lcm(z, 0) = 0
11341134
*/
1135-
mpz_t *mpz_lcm(const mpz_t *z1, const mpz_t *z2)
1136-
{
1137-
// braces below are required for compilation to succeed with CL, see bug report
1138-
// https://connect.microsoft.com/VisualStudio/feedback/details/864169/compilation-error-when-braces-are-left-out-of-single-line-if-statement
1139-
if (z1->len == 0 || z2->len == 0) {
1140-
return mpz_zero();
1135+
mpz_t *mpz_lcm(const mpz_t *z1, const mpz_t *z2) {
1136+
if (z1->len == 0 || z2->len == 0) {
1137+
return mpz_zero();
11411138
}
11421139

11431140
mpz_t *gcd = mpz_gcd(z1, z2);

0 commit comments

Comments
 (0)