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