beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpq / div.c
blob3a6018a22685695dabcdd77e68e1d4c2831786dc
1 /* mpq_div -- divide two rational numbers.
3 Copyright 1991, 1994-1996, 2000, 2001, 2015 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
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.
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.
20 or both in parallel, as here.
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.
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/. */
31 #include "gmp.h"
32 #include "gmp-impl.h"
35 void
36 mpq_div (mpq_ptr quot, mpq_srcptr op1, mpq_srcptr op2)
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;
45 op2_size = SIZ(NUM(op2));
47 if (UNLIKELY (op2_size == 0))
48 DIVIDE_BY_ZERO;
50 if (UNLIKELY (quot == op2))
52 if (op1 == op2)
54 PTR(NUM(quot))[0] = 1;
55 SIZ(NUM(quot)) = 1;
56 PTR(DEN(quot))[0] = 1;
57 SIZ(DEN(quot)) = 1;
58 return;
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)
67 SIZ(NUM(quot)) = SIZ(DEN(quot));
68 SIZ(DEN(quot)) = op2_size;
70 else
72 SIZ(NUM(quot)) = - SIZ(DEN(quot));
73 SIZ(DEN(quot)) = - op2_size;
75 mpq_mul (quot, quot, op1);
76 return;
79 op1_size = ABSIZ(NUM(op1));
81 if (op1_size == 0)
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;
91 op2_size = ABS(op2_size);
93 TMP_MARK;
95 alloc = MIN (op1_size, op2_size);
96 MPZ_TMP_INIT (gcd1, alloc);
98 alloc = MAX (op1_size, op2_size);
99 MPZ_TMP_INIT (tmp1, alloc);
101 op2_size = SIZ(DEN(op2));
102 op1_size = SIZ(DEN(op1));
104 alloc = MIN (op1_size, op2_size);
105 MPZ_TMP_INIT (gcd2, alloc);
107 alloc = MAX (op1_size, op2_size);
108 MPZ_TMP_INIT (tmp2, alloc);
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. */
115 mpz_gcd (gcd1, NUM(op1), NUM(op2));
116 mpz_gcd (gcd2, DEN(op2), DEN(op1));
118 mpz_divexact_gcd (tmp1, NUM(op1), gcd1);
119 mpz_divexact_gcd (tmp2, DEN(op2), gcd2);
121 mpz_mul (NUM(quot), tmp1, tmp2);
123 mpz_divexact_gcd (tmp1, NUM(op2), gcd1);
124 mpz_divexact_gcd (tmp2, DEN(op1), gcd2);
126 mpz_mul (DEN(quot), tmp1, tmp2);
128 /* Keep the denominator positive. */
129 if (SIZ(DEN(quot)) < 0)
131 SIZ(DEN(quot)) = -SIZ(DEN(quot));
132 SIZ(NUM(quot)) = -SIZ(NUM(quot));
135 TMP_FREE;