1 /* mpz_tdiv_q -- divide two integers and produce a quotient.
3 Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2005, 2010, 2012 Free Software
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of either:
11 * the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
17 * the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any
21 or both in parallel, as here.
23 The GNU MP Library is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 You should have received copies of the GNU General Public License and the
29 GNU Lesser General Public License along with the GNU MP Library. If not,
30 see https://www.gnu.org/licenses/. */
37 mpz_tdiv_q (mpz_ptr quot
, mpz_srcptr num
, mpz_srcptr den
)
40 mp_size_t ns
, ds
, nl
, dl
;
50 if (UNLIKELY (dl
== 0))
59 qp
= MPZ_REALLOC (quot
, ql
);
65 /* Copy denominator to temporary space if it overlaps with the quotient. */
69 tp
= TMP_ALLOC_LIMBS (dl
);
70 MPN_COPY (tp
, dp
, dl
);
73 /* Copy numerator to temporary space if it overlaps with the quotient. */
77 tp
= TMP_ALLOC_LIMBS (nl
+ 1);
78 MPN_COPY (tp
, np
, nl
);
79 /* Overlap dividend and scratch. */
80 mpn_div_q (qp
, tp
, nl
, dp
, dl
, tp
);
85 tp
= TMP_ALLOC_LIMBS (nl
+ 1);
86 mpn_div_q (qp
, np
, nl
, dp
, dl
, tp
);
89 ql
-= qp
[ql
- 1] == 0;
91 SIZ (quot
) = (ns
^ ds
) >= 0 ? ql
: -ql
;