xref: /netbsd-src/external/lgpl3/gmp/dist/mpq/div.c (revision 5dd36a3bc8bf2a9dec29ceb6349550414570c447)
1 /* mpq_div -- divide two rational numbers.
2 
3 Copyright 1991, 1994-1996, 2000, 2001, 2015 Free Software Foundation, Inc.
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
8 it under the terms of either:
9 
10   * the GNU Lesser General Public License as published by the Free
11     Software Foundation; either version 3 of the License, or (at your
12     option) any later version.
13 
14 or
15 
16   * the GNU General Public License as published by the Free Software
17     Foundation; either version 2 of the License, or (at your option) any
18     later version.
19 
20 or both in parallel, as here.
21 
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25 for more details.
26 
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library.  If not,
29 see https://www.gnu.org/licenses/.  */
30 
31 #include "gmp.h"
32 #include "gmp-impl.h"
33 
34 
35 void
36 mpq_div (mpq_ptr quot, mpq_srcptr op1, mpq_srcptr op2)
37 {
38   mpz_t gcd1, gcd2;
39   mpz_t tmp1, tmp2;
40   mp_size_t op1_size;
41   mp_size_t op2_size;
42   mp_size_t alloc;
43   TMP_DECL;
44 
45   op2_size = SIZ(NUM(op2));
46 
47   if (UNLIKELY (op2_size == 0))
48     DIVIDE_BY_ZERO;
49 
50   if (UNLIKELY (quot == op2))
51     {
52       if (op1 == op2)
53 	{
54 	  PTR(NUM(quot))[0] = 1;
55 	  SIZ(NUM(quot)) = 1;
56 	  PTR(DEN(quot))[0] = 1;
57 	  SIZ(DEN(quot)) = 1;
58 	  return;
59 	}
60 
61       /* We checked for op1 == op2: we are not in the x=x/x case.
62 	 We compute x=y/x by computing x=inv(x)*y */
63       MPN_PTR_SWAP (PTR(NUM(quot)), ALLOC(NUM(quot)),
64 		    PTR(DEN(quot)), ALLOC(DEN(quot)));
65       if (op2_size > 0)
66 	{
67 	  SIZ(NUM(quot)) = SIZ(DEN(quot));
68 	  SIZ(DEN(quot)) = op2_size;
69 	}
70       else
71 	{
72 	  SIZ(NUM(quot)) = - SIZ(DEN(quot));
73 	  SIZ(DEN(quot)) = - op2_size;
74 	}
75       mpq_mul (quot, quot, op1);
76       return;
77     }
78 
79   op1_size = ABSIZ(NUM(op1));
80 
81   if (op1_size == 0)
82     {
83       /* We special case this to simplify allocation logic; gcd(0,x) = x
84 	 is a singular case for the allocations.  */
85       SIZ(NUM(quot)) = 0;
86       PTR(DEN(quot))[0] = 1;
87       SIZ(DEN(quot)) = 1;
88       return;
89     }
90 
91   op2_size = ABS(op2_size);
92 
93   TMP_MARK;
94 
95   alloc = MIN (op1_size, op2_size);
96   MPZ_TMP_INIT (gcd1, alloc);
97 
98   alloc = MAX (op1_size, op2_size);
99   MPZ_TMP_INIT (tmp1, alloc);
100 
101   op2_size = SIZ(DEN(op2));
102   op1_size = SIZ(DEN(op1));
103 
104   alloc = MIN (op1_size, op2_size);
105   MPZ_TMP_INIT (gcd2, alloc);
106 
107   alloc = MAX (op1_size, op2_size);
108   MPZ_TMP_INIT (tmp2, alloc);
109 
110   /* QUOT might be identical to OP1, so don't store the result there
111      until we are finished with the input operand.  We can overwrite
112      the numerator of QUOT when we are finished with the numerator of
113      OP1. */
114 
115   mpz_gcd (gcd1, NUM(op1), NUM(op2));
116   mpz_gcd (gcd2, DEN(op2), DEN(op1));
117 
118   mpz_divexact_gcd (tmp1, NUM(op1), gcd1);
119   mpz_divexact_gcd (tmp2, DEN(op2), gcd2);
120 
121   mpz_mul (NUM(quot), tmp1, tmp2);
122 
123   mpz_divexact_gcd (tmp1, NUM(op2), gcd1);
124   mpz_divexact_gcd (tmp2, DEN(op1), gcd2);
125 
126   mpz_mul (DEN(quot), tmp1, tmp2);
127 
128   /* Keep the denominator positive.  */
129   if (SIZ(DEN(quot)) < 0)
130     {
131       SIZ(DEN(quot)) = -SIZ(DEN(quot));
132       SIZ(NUM(quot)) = -SIZ(NUM(quot));
133     }
134 
135   TMP_FREE;
136 }
137