[BACK]Return to tdiv_q.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpz

Annotation of OpenXM_contrib/gmp/mpz/tdiv_q.c, Revision 1.1.1.2

1.1       maekawa     1: /* mpz_tdiv_q -- divide two integers and produce a quotient.
                      2:
1.1.1.2 ! maekawa     3: Copyright (C) 1991, 1993, 1994, 1996, 2000 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: #if __STDC__
                     28: mpz_tdiv_q (mpz_ptr quot, mpz_srcptr num, mpz_srcptr den)
                     29: #else
                     30: mpz_tdiv_q (quot, num, den)
                     31:      mpz_ptr quot;
                     32:      mpz_srcptr num;
                     33:      mpz_srcptr den;
                     34: #endif
                     35: {
1.1.1.2 ! maekawa    36:   mp_size_t ql;
        !            37:   mp_size_t ns, ds, nl, dl;
        !            38:   mp_ptr np, dp, qp, rp;
1.1       maekawa    39:   TMP_DECL (marker);
                     40:
1.1.1.2 ! maekawa    41:   ns = SIZ (num);
        !            42:   ds = SIZ (den);
        !            43:   nl = ABS (ns);
        !            44:   dl = ABS (ds);
        !            45:   ql = nl - dl + 1;
1.1       maekawa    46:
1.1.1.2 ! maekawa    47:   if (dl == 0)
        !            48:     DIVIDE_BY_ZERO;
1.1       maekawa    49:
1.1.1.2 ! maekawa    50:   if (ql <= 0)
1.1       maekawa    51:     {
1.1.1.2 ! maekawa    52:       SIZ (quot) = 0;
1.1       maekawa    53:       return;
                     54:     }
                     55:
1.1.1.2 ! maekawa    56:   MPZ_REALLOC (quot, ql);
1.1       maekawa    57:
                     58:   TMP_MARK (marker);
1.1.1.2 ! maekawa    59:   qp = PTR (quot);
        !            60:   rp = (mp_ptr) TMP_ALLOC (dl * BYTES_PER_MP_LIMB);
        !            61:   np = PTR (num);
        !            62:   dp = PTR (den);
        !            63:
        !            64:   /* FIXME: We should think about how to handle the temporary allocation.
        !            65:      Perhaps mpn_tdiv_qr should handle it, since it anyway often needs to
        !            66:      allocate temp space.  */
1.1       maekawa    67:
1.1.1.2 ! maekawa    68:   /* Copy denominator to temporary space if it overlaps with the quotient.  */
        !            69:   if (dp == qp)
1.1       maekawa    70:     {
                     71:       mp_ptr tp;
1.1.1.2 ! maekawa    72:       tp = (mp_ptr) TMP_ALLOC (dl * BYTES_PER_MP_LIMB);
        !            73:       MPN_COPY (tp, dp, dl);
1.1       maekawa    74:       dp = tp;
                     75:     }
1.1.1.2 ! maekawa    76:   /* Copy numerator to temporary space if it overlaps with the quotient.  */
        !            77:   if (np == qp)
1.1       maekawa    78:     {
1.1.1.2 ! maekawa    79:       mp_ptr tp;
        !            80:       tp = (mp_ptr) TMP_ALLOC (nl * BYTES_PER_MP_LIMB);
        !            81:       MPN_COPY (tp, np, nl);
        !            82:       np = tp;
1.1       maekawa    83:     }
                     84:
1.1.1.2 ! maekawa    85:   mpn_tdiv_qr (qp, rp, 0L, np, nl, dp, dl);
1.1       maekawa    86:
1.1.1.2 ! maekawa    87:   ql -=  qp[ql - 1] == 0;
1.1       maekawa    88:
1.1.1.2 ! maekawa    89:   SIZ (quot) = (ns ^ ds) >= 0 ? ql : -ql;
1.1       maekawa    90:   TMP_FREE (marker);
                     91: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>