summaryrefslogtreecommitdiff
path: root/mpz/tdiv_r.c
diff options
context:
space:
mode:
authortege <tege@gmplib.org>2000-04-14 19:30:02 +0200
committertege <tege@gmplib.org>2000-04-14 19:30:02 +0200
commit589b9fedd17ff50ac3e689c1f7ac4471fce542ae (patch)
tree51eceeef739364a5c0e462a12d7e15f9718775ef /mpz/tdiv_r.c
parent04e8a73b252c16ac734884b3258809ae53985318 (diff)
downloadgmp-589b9fedd17ff50ac3e689c1f7ac4471fce542ae.tar.gz
Rewrite using mpn_tdiv_qr.
Diffstat (limited to 'mpz/tdiv_r.c')
-rw-r--r--mpz/tdiv_r.c65
1 files changed, 63 insertions, 2 deletions
diff --git a/mpz/tdiv_r.c b/mpz/tdiv_r.c
index 813a0d4d7..956562a16 100644
--- a/mpz/tdiv_r.c
+++ b/mpz/tdiv_r.c
@@ -32,6 +32,67 @@ mpz_tdiv_r (rem, num, den)
mpz_srcptr num;
mpz_srcptr den;
#endif
+{
+ mp_size_t ql;
+ mp_size_t ns, ds, nl, dl;
+ mp_ptr np, dp, qp, rp;
+ TMP_DECL (marker)
-#undef COMPUTE_QUOTIENT
-#include "dmincl.c"
+ ns = SIZ (num);
+ ds = SIZ (den);
+ nl = ABS (ns);
+ dl = ABS (ds);
+ ql = nl - dl + 1;
+
+ if (dl == 0)
+ DIVIDE_BY_ZERO;
+
+ MPZ_REALLOC (rem, dl);
+
+ if (ql <= 0)
+ {
+ if (num != rem)
+ {
+ mp_ptr np, rp;
+ np = PTR (num);
+ rp = PTR (rem);
+ MPN_COPY (rp, np, nl);
+ SIZ (rem) = SIZ (num);
+ }
+ return;
+ }
+
+ TMP_MARK (marker);
+ qp = (mp_ptr) TMP_ALLOC (ql * BYTES_PER_MP_LIMB);
+ rp = PTR (rem);
+ np = PTR (num);
+ dp = PTR (den);
+
+ /* FIXME: We should think about how to handle the temporary allocation.
+ Perhaps mpn_tdiv_qr should handle it, since it anyway often need to
+ allocate temp space. */
+
+ /* Copy denominator to temporary space if it overlaps with the remainder. */
+ if (dp == rp)
+ {
+ mp_ptr tp;
+ tp = (mp_ptr) TMP_ALLOC (dl * BYTES_PER_MP_LIMB);
+ MPN_COPY (tp, dp, dl);
+ dp = tp;
+ }
+ /* Copy numerator to temporary space if it overlaps with the remainder. */
+ if (np == rp)
+ {
+ mp_ptr tp;
+ tp = (mp_ptr) TMP_ALLOC (nl * BYTES_PER_MP_LIMB);
+ MPN_COPY (tp, np, nl);
+ np = tp;
+ }
+
+ mpn_tdiv_qr (qp, rp, 0L, np, nl, dp, dl);
+
+ MPN_NORMALIZE (rp, dl);
+
+ SIZ (rem) = ns >= 0 ? dl : -dl;
+ TMP_FREE (marker);
+}