remove extra division in mpq/aors.c
Marc Glisse
marc.glisse at inria.fr
Sun Feb 27 11:14:43 CET 2011
Just to make my earlier remarks more constructive, 2 versions of what I
was suggesting are attached.
--
Marc Glisse
-------------- next part --------------
*** gmp.98add4b37ac1/mpz/divegcd.c 2011-02-27 11:08:28.455139952 +0100
--- /data/repos/gmp/mpz/divegcd.c 2011-02-27 11:08:09.418916311 +0100
***************
*** 103,112 ****
--- 103,120 ----
if (SIZ(d) == 1)
{
mp_limb_t dl = PTR(d)[0];
int twos;
+ count_trailing_zeros (twos, dl);
+ if (twos != 0)
+ {
+ dl >>= twos;
+ mpz_tdiv_q_2exp (q, a, twos);
+ a=q;
+ }
+
if (dl == 1)
{
if (q != a)
mpz_set (q, a);
return;
***************
*** 124,157 ****
mpz_divexact_by5 (q, a);
return;
}
#endif
! count_trailing_zeros (twos, dl);
! dl >>= twos;
! mpz_tdiv_q_2exp (q, a, twos);
!
! if (dl == 1)
! {
! return;
! }
! #if GMP_NUMB_BITS % 2 == 0
! if (dl == 3)
! {
! mpz_divexact_by3 (q, q);
! return;
! }
! #endif
! #if GMP_NUMB_BITS % 4 == 0
! if (dl == 5)
! {
! mpz_divexact_by5 (q, q);
! return;
! }
! #endif
!
! mpz_divexact_ui (q, q, dl);
return;
}
mpz_divexact (q, a, d);
}
--- 132,142 ----
mpz_divexact_by5 (q, a);
return;
}
#endif
! mpz_divexact_ui (q, a, dl);
return;
}
mpz_divexact (q, a, d);
}
-------------- next part --------------
*** gmp.98add4b37ac1/mpz/divegcd.c 2011-02-27 11:09:30.266945592 +0100
--- /data/repos/gmp/mpz/divegcd.c 2011-02-27 11:09:12.754883638 +0100
***************
*** 42,54 ****
Future: This could change to an mpn_divexact_gcd, possibly partly
inlined, if/when the relevant mpq functions change to an mpn based
implementation. */
- #if GMP_NUMB_BITS % 2 == 0
static void
! mpz_divexact_by3 (mpz_ptr q, mpz_srcptr a)
{
mp_size_t size = SIZ(a);
if (size == 0)
{
SIZ(q) = 0;
--- 42,53 ----
Future: This could change to an mpn_divexact_gcd, possibly partly
inlined, if/when the relevant mpq functions change to an mpn based
implementation. */
static void
! mpz_divexact_by_limb (mpz_ptr q, mpz_srcptr a, mp_limb_t di)
{
mp_size_t size = SIZ(a);
if (size == 0)
{
SIZ(q) = 0;
***************
*** 60,101 ****
mp_ptr qp;
MPZ_REALLOC (q, abs_size);
qp = PTR(q);
! mpn_bdiv_dbm1 (qp, PTR(a), abs_size, GMP_NUMB_MASK / 3);
abs_size -= (qp[abs_size-1] == 0);
SIZ(q) = (size>0 ? abs_size : -abs_size);
}
}
- #endif
- #if GMP_NUMB_BITS % 4 == 0
- static void
- mpz_divexact_by5 (mpz_ptr q, mpz_srcptr a)
- {
- mp_size_t size = SIZ(a);
- if (size == 0)
- {
- SIZ(q) = 0;
- return;
- }
- else
- {
- mp_size_t abs_size = ABS(size);
- mp_ptr qp;
-
- MPZ_REALLOC (q, abs_size);
-
- qp = PTR(q);
- mpn_bdiv_dbm1 (qp, PTR(a), abs_size, GMP_NUMB_MASK / 5);
-
- abs_size -= (qp[abs_size-1] == 0);
- SIZ(q) = (size>0 ? abs_size : -abs_size);
- }
- }
- #endif
void
mpz_divexact_gcd (mpz_ptr q, mpz_srcptr a, mpz_srcptr d)
{
ASSERT (mpz_sgn (d) > 0);
--- 59,74 ----
mp_ptr qp;
MPZ_REALLOC (q, abs_size);
qp = PTR(q);
! mpn_bdiv_dbm1 (qp, PTR(a), abs_size, di);
abs_size -= (qp[abs_size-1] == 0);
SIZ(q) = (size>0 ? abs_size : -abs_size);
}
}
void
mpz_divexact_gcd (mpz_ptr q, mpz_srcptr a, mpz_srcptr d)
{
ASSERT (mpz_sgn (d) > 0);
***************
*** 103,157 ****
if (SIZ(d) == 1)
{
mp_limb_t dl = PTR(d)[0];
int twos;
! if (dl == 1)
! {
! if (q != a)
! mpz_set (q, a);
! return;
! }
! #if GMP_NUMB_BITS % 2 == 0
! if (dl == 3)
! {
! mpz_divexact_by3 (q, a);
! return;
! }
! #endif
! #if GMP_NUMB_BITS % 4 == 0
! if (dl == 5)
{
! mpz_divexact_by5 (q, a);
! return;
}
- #endif
-
- count_trailing_zeros (twos, dl);
- dl >>= twos;
- mpz_tdiv_q_2exp (q, a, twos);
if (dl == 1)
{
return;
}
! #if GMP_NUMB_BITS % 2 == 0
! if (dl == 3)
! {
! mpz_divexact_by3 (q, q);
! return;
! }
! #endif
! #if GMP_NUMB_BITS % 4 == 0
! if (dl == 5)
{
! mpz_divexact_by5 (q, q);
return;
}
- #endif
! mpz_divexact_ui (q, q, dl);
return;
}
mpz_divexact (q, a, d);
}
--- 76,106 ----
if (SIZ(d) == 1)
{
mp_limb_t dl = PTR(d)[0];
int twos;
! count_trailing_zeros (twos, dl);
! if (twos != 0)
{
! dl >>= twos;
! mpz_tdiv_q_2exp (q, a, twos);
! a=q;
}
if (dl == 1)
{
+ if (q != a)
+ mpz_set (q, a);
return;
}
! if (GMP_NUMB_MASK % dl == 0)
{
! mpz_divexact_by_limb (q, a, GMP_NUMB_MASK / dl);
return;
}
! mpz_divexact_ui (q, a, dl);
return;
}
mpz_divexact (q, a, d);
}
More information about the gmp-discuss
mailing list