Skip to content

Commit c5ac2ac

Browse files
committed
py: Start to implement shl/shr for mpz. Fix return void.
1 parent f8a4fbb commit c5ac2ac

2 files changed

Lines changed: 84 additions & 40 deletions

File tree

py/mpz.c

Lines changed: 82 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,35 @@ int mpn_cmp(const mpz_dig_t *idig, uint ilen, const mpz_dig_t *jdig, uint jlen)
3535
return 0;
3636
}
3737

38+
/* computes i = j << n
39+
returns number of digits in i
40+
assumes enough memory in i; assumes normalised j
41+
can have i, j pointing to same memory
42+
*/
43+
/* unfinished
44+
uint mpn_shl(mpz_dig_t *idig, mpz_dig_t *jdig, uint jlen, uint n) {
45+
uint n_whole = n / DIG_SIZE;
46+
uint n_part = n % DIG_SIZE;
47+
48+
idig += jlen + n_whole + 1;
49+
50+
for (uint i = jlen; i > 0; --i, ++idig, ++jdig) {
51+
mpz_dbl_dig_t d = *jdig;
52+
if (i > 1) {
53+
d |= jdig[1] << DIG_SIZE;
54+
}
55+
d <<= n_part;
56+
*idig = d & DIG_MASK;
57+
}
58+
59+
if (idig[-1] == 0) {
60+
--jlen;
61+
}
62+
63+
return jlen;
64+
}
65+
*/
66+
3867
/* computes i = j >> n
3968
returns number of digits in i
4069
assumes enough memory in i; assumes normalised j
@@ -53,8 +82,9 @@ uint mpn_shr(mpz_dig_t *idig, mpz_dig_t *jdig, uint jlen, uint n) {
5382

5483
for (uint i = jlen; i > 0; --i, ++idig, ++jdig) {
5584
mpz_dbl_dig_t d = *jdig;
56-
if (i > 1)
85+
if (i > 1) {
5786
d |= jdig[1] << DIG_SIZE;
87+
}
5888
d >>= n_part;
5989
*idig = d & DIG_MASK;
6090
}
@@ -525,42 +555,6 @@ int mpz_cmp_sml_int(const mpz_t *z, machine_int_t sml_int) {
525555
return 0;
526556
}
527557

528-
/* not finished
529-
mpz_t *mpz_shl(mpz_t *dest, const mpz_t *lhs, int rhs)
530-
{
531-
if (dest != lhs)
532-
dest = mpz_set(dest, lhs);
533-
534-
if (dest.len == 0 || rhs == 0)
535-
return dest;
536-
537-
if (rhs < 0)
538-
return mpz_shr(dest, dest, -rhs);
539-
540-
printf("mpz_shl: not implemented\n");
541-
542-
return dest;
543-
}
544-
545-
mpz_t *mpz_shr(mpz_t *dest, const mpz_t *lhs, int rhs)
546-
{
547-
if (dest != lhs)
548-
dest = mpz_set(dest, lhs);
549-
550-
if (dest.len == 0 || rhs == 0)
551-
return dest;
552-
553-
if (rhs < 0)
554-
return mpz_shl(dest, dest, -rhs);
555-
556-
dest.len = mpn_shr(dest.len, dest.dig, rhs);
557-
dest.dig[dest.len .. dest->alloc] = 0;
558-
559-
return dest;
560-
}
561-
*/
562-
563-
564558
#if 0
565559
these functions are unused
566560

@@ -637,6 +631,51 @@ void mpz_neg_inpl(mpz_t *dest, const mpz_t *z) {
637631
dest->neg = 1 - dest->neg;
638632
}
639633

634+
#if 0
635+
not finished
636+
/* computes dest = lhs << rhs
637+
can have dest, lhs the same
638+
*/
639+
void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, machine_int_t rhs) {
640+
if (dest != lhs) {
641+
mpz_set(dest, lhs);
642+
}
643+
644+
if (dest.len == 0 || rhs == 0) {
645+
return dest;
646+
}
647+
648+
if (rhs < 0) {
649+
dest->len = mpn_shr(dest->len, dest->dig, -rhs);
650+
} else {
651+
dest->len = mpn_shl(dest->len, dest->dig, rhs);
652+
}
653+
654+
return dest;
655+
}
656+
657+
/* computes dest = lhs >> rhs
658+
can have dest, lhs the same
659+
*/
660+
void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, machine_int_t rhs) {
661+
if (dest != lhs) {
662+
mpz_set(dest, lhs);
663+
}
664+
665+
if (dest.len == 0 || rhs == 0) {
666+
return dest;
667+
}
668+
669+
if (rhs < 0) {
670+
dest->len = mpn_shl(dest->len, dest->dig, -rhs);
671+
} else {
672+
dest->len = mpn_shr(dest->len, dest->dig, rhs);
673+
}
674+
675+
return dest;
676+
}
677+
#endif
678+
640679
/* computes dest = lhs + rhs
641680
can have dest, lhs, rhs the same
642681
*/
@@ -692,7 +731,8 @@ void mpz_sub_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
692731
void mpz_mul_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
693732
{
694733
if (lhs->len == 0 || rhs->len == 0) {
695-
return mpz_set_from_int(dest, 0);
734+
mpz_set_from_int(dest, 0);
735+
return;
696736
}
697737

698738
mpz_t *temp = NULL;
@@ -723,11 +763,13 @@ void mpz_mul_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
723763
*/
724764
void mpz_pow_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
725765
if (lhs->len == 0 || rhs->neg != 0) {
726-
return mpz_set_from_int(dest, 0);
766+
mpz_set_from_int(dest, 0);
767+
return;
727768
}
728769

729770
if (rhs->len == 0) {
730-
return mpz_set_from_int(dest, 1);
771+
mpz_set_from_int(dest, 1);
772+
return;
731773
}
732774

733775
mpz_t *x = mpz_clone(lhs);

py/mpz.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ mpz_t *mpz_pow(const mpz_t *lhs, const mpz_t *rhs);
4646

4747
void mpz_abs_inpl(mpz_t *dest, const mpz_t *z);
4848
void mpz_neg_inpl(mpz_t *dest, const mpz_t *z);
49+
//void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, machine_int_t rhs);
50+
//void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, machine_int_t rhs);
4951
void mpz_add_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
5052
void mpz_sub_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
5153
void mpz_mul_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);

0 commit comments

Comments
 (0)