github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpq/aors.c (about)

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