Annotation of OpenXM_contrib/gmp/mpz/tdiv_q.c, Revision 1.1.1.3
1.1 maekawa 1: /* mpz_tdiv_q -- divide two integers and produce a quotient.
2:
1.1.1.3 ! ohara 3: Copyright 1991, 1993, 1994, 1996, 2000, 2001 Free Software Foundation, Inc.
1.1 maekawa 4:
5: This file is part of the GNU MP Library.
6:
7: The GNU MP Library is free software; you can redistribute it and/or modify
1.1.1.2 maekawa 8: it under the terms of the GNU Lesser General Public License as published by
9: the Free Software Foundation; either version 2.1 of the License, or (at your
1.1 maekawa 10: option) any later version.
11:
12: The GNU MP Library is distributed in the hope that it will be useful, but
13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1.1.1.2 maekawa 14: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
1.1 maekawa 15: License for more details.
16:
1.1.1.2 maekawa 17: You should have received a copy of the GNU Lesser General Public License
1.1 maekawa 18: along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20: MA 02111-1307, USA. */
21:
22: #include "gmp.h"
23: #include "gmp-impl.h"
24: #include "longlong.h"
25:
26: void
27: mpz_tdiv_q (mpz_ptr quot, mpz_srcptr num, mpz_srcptr den)
28: {
1.1.1.2 maekawa 29: mp_size_t ql;
30: mp_size_t ns, ds, nl, dl;
31: mp_ptr np, dp, qp, rp;
1.1 maekawa 32: TMP_DECL (marker);
33:
1.1.1.2 maekawa 34: ns = SIZ (num);
35: ds = SIZ (den);
36: nl = ABS (ns);
37: dl = ABS (ds);
38: ql = nl - dl + 1;
1.1 maekawa 39:
1.1.1.2 maekawa 40: if (dl == 0)
41: DIVIDE_BY_ZERO;
1.1 maekawa 42:
1.1.1.2 maekawa 43: if (ql <= 0)
1.1 maekawa 44: {
1.1.1.2 maekawa 45: SIZ (quot) = 0;
1.1 maekawa 46: return;
47: }
48:
1.1.1.2 maekawa 49: MPZ_REALLOC (quot, ql);
1.1 maekawa 50:
51: TMP_MARK (marker);
1.1.1.2 maekawa 52: qp = PTR (quot);
53: rp = (mp_ptr) TMP_ALLOC (dl * BYTES_PER_MP_LIMB);
54: np = PTR (num);
55: dp = PTR (den);
56:
57: /* FIXME: We should think about how to handle the temporary allocation.
58: Perhaps mpn_tdiv_qr should handle it, since it anyway often needs to
59: allocate temp space. */
1.1 maekawa 60:
1.1.1.2 maekawa 61: /* Copy denominator to temporary space if it overlaps with the quotient. */
62: if (dp == qp)
1.1 maekawa 63: {
64: mp_ptr tp;
1.1.1.2 maekawa 65: tp = (mp_ptr) TMP_ALLOC (dl * BYTES_PER_MP_LIMB);
66: MPN_COPY (tp, dp, dl);
1.1 maekawa 67: dp = tp;
68: }
1.1.1.2 maekawa 69: /* Copy numerator to temporary space if it overlaps with the quotient. */
70: if (np == qp)
1.1 maekawa 71: {
1.1.1.2 maekawa 72: mp_ptr tp;
73: tp = (mp_ptr) TMP_ALLOC (nl * BYTES_PER_MP_LIMB);
74: MPN_COPY (tp, np, nl);
75: np = tp;
1.1 maekawa 76: }
77:
1.1.1.2 maekawa 78: mpn_tdiv_qr (qp, rp, 0L, np, nl, dp, dl);
1.1 maekawa 79:
1.1.1.2 maekawa 80: ql -= qp[ql - 1] == 0;
1.1 maekawa 81:
1.1.1.2 maekawa 82: SIZ (quot) = (ns ^ ds) >= 0 ? ql : -ql;
1.1 maekawa 83: TMP_FREE (marker);
84: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>