1 /* mpq_add, mpq_sub -- add or subtract rational numbers. 2 3 Copyright 1991, 1994, 1995, 1996, 1997, 2000, 2001, 2004, 2005 Free Software 4 Foundation, 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 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 #define mpq_aors(w,x,y,fun) __gmpq_aors (REGPARM_3_1 (w, x, y, fun)) 27 28 REGPARM_ATTR (1) static void 29 mpq_aors (mpq_ptr rop, mpq_srcptr op1, mpq_srcptr op2, 30 void (*fun) __GMP_PROTO ((mpz_ptr, mpz_srcptr, mpz_srcptr))) 31 { 32 mpz_t gcd; 33 mpz_t tmp1, tmp2; 34 mp_size_t op1_num_size = ABS (op1->_mp_num._mp_size); 35 mp_size_t op1_den_size = op1->_mp_den._mp_size; 36 mp_size_t op2_num_size = ABS (op2->_mp_num._mp_size); 37 mp_size_t op2_den_size = op2->_mp_den._mp_size; 38 TMP_DECL; 39 40 TMP_MARK; 41 MPZ_TMP_INIT (gcd, MIN (op1_den_size, op2_den_size)); 42 MPZ_TMP_INIT (tmp1, op1_num_size + op2_den_size); 43 MPZ_TMP_INIT (tmp2, op2_num_size + op1_den_size); 44 45 /* ROP might be identical to either operand, so don't store the 46 result there until we are finished with the input operands. We 47 dare to overwrite the numerator of ROP when we are finished 48 with the numerators of OP1 and OP2. */ 49 50 mpz_gcd (gcd, &(op1->_mp_den), &(op2->_mp_den)); 51 if (! MPZ_EQUAL_1_P (gcd)) 52 { 53 mpz_t t; 54 55 mpz_divexact_gcd (tmp1, &(op2->_mp_den), gcd); 56 mpz_mul (tmp1, &(op1->_mp_num), tmp1); 57 58 mpz_divexact_gcd (tmp2, &(op1->_mp_den), gcd); 59 mpz_mul (tmp2, &(op2->_mp_num), tmp2); 60 61 MPZ_TMP_INIT (t, MAX (ABS (tmp1->_mp_size), ABS (tmp2->_mp_size)) + 1); 62 63 (*fun) (t, tmp1, tmp2); 64 mpz_divexact_gcd (tmp2, &(op1->_mp_den), gcd); 65 66 mpz_gcd (gcd, t, gcd); 67 if (MPZ_EQUAL_1_P (gcd)) 68 { 69 mpz_set (&(rop->_mp_num), t); 70 mpz_mul (&(rop->_mp_den), &(op2->_mp_den), tmp2); 71 } 72 else 73 { 74 mpz_divexact_gcd (&(rop->_mp_num), t, gcd); 75 mpz_divexact_gcd (tmp1, &(op2->_mp_den), gcd); 76 mpz_mul (&(rop->_mp_den), tmp1, tmp2); 77 } 78 } 79 else 80 { 81 /* The common divisor is 1. This is the case (for random input) with 82 probability 6/(pi**2), which is about 60.8%. */ 83 mpz_mul (tmp1, &(op1->_mp_num), &(op2->_mp_den)); 84 mpz_mul (tmp2, &(op2->_mp_num), &(op1->_mp_den)); 85 (*fun) (&(rop->_mp_num), tmp1, tmp2); 86 mpz_mul (&(rop->_mp_den), &(op1->_mp_den), &(op2->_mp_den)); 87 } 88 TMP_FREE; 89 } 90 91 92 void 93 mpq_add (mpq_ptr rop, mpq_srcptr op1, mpq_srcptr op2) 94 { 95 mpq_aors (rop, op1, op2, mpz_add); 96 } 97 98 void 99 mpq_sub (mpq_ptr rop, mpq_srcptr op1, mpq_srcptr op2) 100 { 101 mpq_aors (rop, op1, op2, mpz_sub); 102 } 103