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