1/* mpq_div -- divide two rational numbers.
2
3Copyright 1991, 1994, 1995, 1996, 2000, 2001 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 3 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
19
20#include "gmp.h"
21#include "gmp-impl.h"
22
23
24void
25mpq_div (mpq_ptr quot, mpq_srcptr op1, mpq_srcptr op2)
26{
27  mpz_t gcd1, gcd2;
28  mpz_t tmp1, tmp2;
29  mpz_t numtmp;
30  mp_size_t op1_num_size;
31  mp_size_t op1_den_size;
32  mp_size_t op2_num_size;
33  mp_size_t op2_den_size;
34  mp_size_t alloc;
35  TMP_DECL;
36
37  op1_num_size = ABS (op1->_mp_num._mp_size);
38  op1_den_size =      op1->_mp_den._mp_size;
39  op2_num_size = ABS (op2->_mp_num._mp_size);
40  op2_den_size =      op2->_mp_den._mp_size;
41
42  if (op2_num_size == 0)
43    DIVIDE_BY_ZERO;
44
45  if (op1_num_size == 0)
46    {
47      /* We special case this to simplify allocation logic; gcd(0,x) = x
48	 is a singular case for the allocations.  */
49      quot->_mp_num._mp_size = 0;
50      quot->_mp_den._mp_d[0] = 1;
51      quot->_mp_den._mp_size = 1;
52      return;
53    }
54
55  TMP_MARK;
56
57  alloc = MIN (op1_num_size, op2_num_size);
58  MPZ_TMP_INIT (gcd1, alloc);
59
60  alloc = MIN (op1_den_size, op2_den_size);
61  MPZ_TMP_INIT (gcd2, alloc);
62
63  alloc = MAX (op1_num_size, op2_num_size);
64  MPZ_TMP_INIT (tmp1, alloc);
65
66  alloc = MAX (op1_den_size, op2_den_size);
67  MPZ_TMP_INIT (tmp2, alloc);
68
69  alloc = op1_num_size + op2_den_size;
70  MPZ_TMP_INIT (numtmp, alloc);
71
72  /* QUOT might be identical to either operand, so don't store the result there
73     until we are finished with the input operands.  We can overwrite the
74     numerator of QUOT when we are finished with the numerators of OP1 and
75     OP2.  */
76
77  mpz_gcd (gcd1, &(op1->_mp_num), &(op2->_mp_num));
78  mpz_gcd (gcd2, &(op2->_mp_den), &(op1->_mp_den));
79
80  mpz_divexact_gcd (tmp1, &(op1->_mp_num), gcd1);
81  mpz_divexact_gcd (tmp2, &(op2->_mp_den), gcd2);
82
83  mpz_mul (numtmp, tmp1, tmp2);
84
85  mpz_divexact_gcd (tmp1, &(op2->_mp_num), gcd1);
86  mpz_divexact_gcd (tmp2, &(op1->_mp_den), gcd2);
87
88  mpz_mul (&(quot->_mp_den), tmp1, tmp2);
89
90  /* We needed to go via NUMTMP to take care of QUOT being the same as OP2.
91     Now move NUMTMP to QUOT->_mp_num.  */
92  mpz_set (&(quot->_mp_num), numtmp);
93
94  /* Keep the denominator positive.  */
95  if (quot->_mp_den._mp_size < 0)
96    {
97      quot->_mp_den._mp_size = -quot->_mp_den._mp_size;
98      quot->_mp_num._mp_size = -quot->_mp_num._mp_size;
99    }
100
101  TMP_FREE;
102}
103