xref: /netbsd-src/external/lgpl3/gmp/dist/mpq/mul.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* mpq_mul -- multiply two rational numbers.
2 
3 Copyright 1991, 1994-1996, 2000-2002 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-impl.h"
32 
33 
34 void
mpq_mul(mpq_ptr prod,mpq_srcptr op1,mpq_srcptr op2)35 mpq_mul (mpq_ptr prod, mpq_srcptr op1, mpq_srcptr op2)
36 {
37   mpz_t gcd1, gcd2;
38   mpz_t tmp1, tmp2;
39   mp_size_t op1_num_size;
40   mp_size_t op1_den_size;
41   mp_size_t op2_num_size;
42   mp_size_t op2_den_size;
43   mp_size_t alloc;
44   TMP_DECL;
45 
46   if (op1 == op2)
47     {
48       /* No need for any GCDs when squaring. */
49       mpz_mul (mpq_numref (prod), mpq_numref (op1), mpq_numref (op1));
50       mpz_mul (mpq_denref (prod), mpq_denref (op1), mpq_denref (op1));
51       return;
52     }
53 
54   op1_num_size = ABSIZ(NUM(op1));
55   op1_den_size =   SIZ(DEN(op1));
56   op2_num_size = ABSIZ(NUM(op2));
57   op2_den_size =   SIZ(DEN(op2));
58 
59   if (op1_num_size == 0 || op2_num_size == 0)
60     {
61       /* We special case this to simplify allocation logic; gcd(0,x) = x
62 	 is a singular case for the allocations.  */
63       SIZ(NUM(prod)) = 0;
64       MPZ_NEWALLOC (DEN(prod), 1)[0] = 1;
65       SIZ(DEN(prod)) = 1;
66       return;
67     }
68 
69   TMP_MARK;
70 
71   alloc = MIN (op1_num_size, op2_den_size);
72   MPZ_TMP_INIT (gcd1, alloc);
73 
74   alloc = MIN (op2_num_size, op1_den_size);
75   MPZ_TMP_INIT (gcd2, alloc);
76 
77   alloc = MAX (op1_num_size, op2_den_size);
78   MPZ_TMP_INIT (tmp1, alloc);
79 
80   alloc = MAX (op2_num_size, op1_den_size);
81   MPZ_TMP_INIT (tmp2, alloc);
82 
83   /* PROD might be identical to either operand, so don't store the result there
84      until we are finished with the input operands.  We can overwrite the
85      numerator of PROD when we are finished with the numerators of OP1 and
86      OP2.  */
87 
88   mpz_gcd (gcd1, NUM(op1), DEN(op2));
89   mpz_gcd (gcd2, NUM(op2), DEN(op1));
90 
91   mpz_divexact_gcd (tmp1, NUM(op1), gcd1);
92   mpz_divexact_gcd (tmp2, NUM(op2), gcd2);
93 
94   mpz_mul (NUM(prod), tmp1, tmp2);
95 
96   mpz_divexact_gcd (tmp1, DEN(op2), gcd1);
97   mpz_divexact_gcd (tmp2, DEN(op1), gcd2);
98 
99   mpz_mul (DEN(prod), tmp1, tmp2);
100 
101   TMP_FREE;
102 }
103