aors.c revision 1.1.1.1 1 1.1 mrg /* mpq_add, mpq_sub -- add or subtract rational numbers.
2 1.1 mrg
3 1.1 mrg Copyright 1991, 1994, 1995, 1996, 1997, 2000, 2001, 2004, 2005 Free Software
4 1.1 mrg Foundation, Inc.
5 1.1 mrg
6 1.1 mrg This file is part of the GNU MP Library.
7 1.1 mrg
8 1.1 mrg The GNU MP Library is free software; you can redistribute it and/or modify
9 1.1 mrg it under the terms of the GNU Lesser General Public License as published by
10 1.1 mrg the Free Software Foundation; either version 3 of the License, or (at your
11 1.1 mrg option) any later version.
12 1.1 mrg
13 1.1 mrg The GNU MP Library is distributed in the hope that it will be useful, but
14 1.1 mrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 1.1 mrg or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 1.1 mrg License for more details.
17 1.1 mrg
18 1.1 mrg You should have received a copy of the GNU Lesser General Public License
19 1.1 mrg along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
20 1.1 mrg
21 1.1 mrg #include "gmp.h"
22 1.1 mrg #include "gmp-impl.h"
23 1.1 mrg
24 1.1 mrg
25 1.1 mrg static void __gmpq_aors __GMP_PROTO ((REGPARM_3_1 (mpq_ptr, mpq_srcptr, mpq_srcptr, void (*) __GMP_PROTO ((mpz_ptr, mpz_srcptr, mpz_srcptr))))) REGPARM_ATTR (1);
26 1.1 mrg #define mpq_aors(w,x,y,fun) __gmpq_aors (REGPARM_3_1 (w, x, y, fun))
27 1.1 mrg
28 1.1 mrg REGPARM_ATTR (1) static void
29 1.1 mrg mpq_aors (mpq_ptr rop, mpq_srcptr op1, mpq_srcptr op2,
30 1.1 mrg void (*fun) __GMP_PROTO ((mpz_ptr, mpz_srcptr, mpz_srcptr)))
31 1.1 mrg {
32 1.1 mrg mpz_t gcd;
33 1.1 mrg mpz_t tmp1, tmp2;
34 1.1 mrg mp_size_t op1_num_size = ABS (op1->_mp_num._mp_size);
35 1.1 mrg mp_size_t op1_den_size = op1->_mp_den._mp_size;
36 1.1 mrg mp_size_t op2_num_size = ABS (op2->_mp_num._mp_size);
37 1.1 mrg mp_size_t op2_den_size = op2->_mp_den._mp_size;
38 1.1 mrg TMP_DECL;
39 1.1 mrg
40 1.1 mrg TMP_MARK;
41 1.1 mrg MPZ_TMP_INIT (gcd, MIN (op1_den_size, op2_den_size));
42 1.1 mrg MPZ_TMP_INIT (tmp1, op1_num_size + op2_den_size);
43 1.1 mrg MPZ_TMP_INIT (tmp2, op2_num_size + op1_den_size);
44 1.1 mrg
45 1.1 mrg /* ROP might be identical to either operand, so don't store the
46 1.1 mrg result there until we are finished with the input operands. We
47 1.1 mrg dare to overwrite the numerator of ROP when we are finished
48 1.1 mrg with the numerators of OP1 and OP2. */
49 1.1 mrg
50 1.1 mrg mpz_gcd (gcd, &(op1->_mp_den), &(op2->_mp_den));
51 1.1 mrg if (! MPZ_EQUAL_1_P (gcd))
52 1.1 mrg {
53 1.1 mrg mpz_t t;
54 1.1 mrg
55 1.1 mrg mpz_divexact_gcd (tmp1, &(op2->_mp_den), gcd);
56 1.1 mrg mpz_mul (tmp1, &(op1->_mp_num), tmp1);
57 1.1 mrg
58 1.1 mrg mpz_divexact_gcd (tmp2, &(op1->_mp_den), gcd);
59 1.1 mrg mpz_mul (tmp2, &(op2->_mp_num), tmp2);
60 1.1 mrg
61 1.1 mrg MPZ_TMP_INIT (t, MAX (ABS (tmp1->_mp_size), ABS (tmp2->_mp_size)) + 1);
62 1.1 mrg
63 1.1 mrg (*fun) (t, tmp1, tmp2);
64 1.1 mrg mpz_divexact_gcd (tmp2, &(op1->_mp_den), gcd);
65 1.1 mrg
66 1.1 mrg mpz_gcd (gcd, t, gcd);
67 1.1 mrg if (MPZ_EQUAL_1_P (gcd))
68 1.1 mrg {
69 1.1 mrg mpz_set (&(rop->_mp_num), t);
70 1.1 mrg mpz_mul (&(rop->_mp_den), &(op2->_mp_den), tmp2);
71 1.1 mrg }
72 1.1 mrg else
73 1.1 mrg {
74 1.1 mrg mpz_divexact_gcd (&(rop->_mp_num), t, gcd);
75 1.1 mrg mpz_divexact_gcd (tmp1, &(op2->_mp_den), gcd);
76 1.1 mrg mpz_mul (&(rop->_mp_den), tmp1, tmp2);
77 1.1 mrg }
78 1.1 mrg }
79 1.1 mrg else
80 1.1 mrg {
81 1.1 mrg /* The common divisor is 1. This is the case (for random input) with
82 1.1 mrg probability 6/(pi**2), which is about 60.8%. */
83 1.1 mrg mpz_mul (tmp1, &(op1->_mp_num), &(op2->_mp_den));
84 1.1 mrg mpz_mul (tmp2, &(op2->_mp_num), &(op1->_mp_den));
85 1.1 mrg (*fun) (&(rop->_mp_num), tmp1, tmp2);
86 1.1 mrg mpz_mul (&(rop->_mp_den), &(op1->_mp_den), &(op2->_mp_den));
87 1.1 mrg }
88 1.1 mrg TMP_FREE;
89 1.1 mrg }
90 1.1 mrg
91 1.1 mrg
92 1.1 mrg void
93 1.1 mrg mpq_add (mpq_ptr rop, mpq_srcptr op1, mpq_srcptr op2)
94 1.1 mrg {
95 1.1 mrg mpq_aors (rop, op1, op2, mpz_add);
96 1.1 mrg }
97 1.1 mrg
98 1.1 mrg void
99 1.1 mrg mpq_sub (mpq_ptr rop, mpq_srcptr op1, mpq_srcptr op2)
100 1.1 mrg {
101 1.1 mrg mpq_aors (rop, op1, op2, mpz_sub);
102 1.1 mrg }
103