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