1 /* mpz_divexact -- finds quotient when known that quot * den == num && den != 0.
3 Contributed to the GNU project by Niels Möller.
5 Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2005,
6 2006, 2007, 2009 Free Software Foundation, Inc.
8 This file is part of the GNU MP Library.
10 The GNU MP Library is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
15 The GNU MP Library is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
29 mpz_divexact (mpz_ptr quot
, mpz_srcptr num
, mpz_srcptr den
)
41 mpz_tdiv_r (rem
, num
, den
);
42 ASSERT (SIZ(rem
) == 0);
51 MPZ_REALLOC (quot
, qn
);
55 /* This special case avoids segfaults below when the function is
56 incorrectly called with |N| < |D|, N != 0. It also handles the
57 well-defined case N = 0. */
66 if (quot
== num
|| quot
== den
)
67 qp
= TMP_ALLOC_LIMBS (qn
);
72 mpn_divexact (qp
, np
, nn
, dp
, dn
);
73 MPN_NORMALIZE (qp
, qn
);
75 SIZ(quot
) = (SIZ(num
) ^ SIZ(den
)) >= 0 ? qn
: -qn
;
78 MPN_COPY (PTR(quot
), qp
, qn
);