kernel/vga: Fix -Wundef.
[dragonfly.git] / contrib / gmp / mpq / aors.c
blob5e09de533d4acc55dc822518235b2528b52c5e6f
1 /* mpq_add, mpq_sub -- add or subtract rational numbers.
3 Copyright 1991, 1994, 1995, 1996, 1997, 2000, 2001, 2004, 2005 Free Software
4 Foundation, Inc.
6 This file is part of the GNU MP Library.
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.
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.
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/. */
21 #include "gmp.h"
22 #include "gmp-impl.h"
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))
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)))
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;
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);
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. */
50 mpz_gcd (gcd, &(op1->_mp_den), &(op2->_mp_den));
51 if (! MPZ_EQUAL_1_P (gcd))
53 mpz_t t;
55 mpz_divexact_gcd (tmp1, &(op2->_mp_den), gcd);
56 mpz_mul (tmp1, &(op1->_mp_num), tmp1);
58 mpz_divexact_gcd (tmp2, &(op1->_mp_den), gcd);
59 mpz_mul (tmp2, &(op2->_mp_num), tmp2);
61 MPZ_TMP_INIT (t, MAX (ABS (tmp1->_mp_size), ABS (tmp2->_mp_size)) + 1);
63 (*fun) (t, tmp1, tmp2);
64 mpz_divexact_gcd (tmp2, &(op1->_mp_den), gcd);
66 mpz_gcd (gcd, t, gcd);
67 if (MPZ_EQUAL_1_P (gcd))
69 mpz_set (&(rop->_mp_num), t);
70 mpz_mul (&(rop->_mp_den), &(op2->_mp_den), tmp2);
72 else
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);
79 else
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));
88 TMP_FREE;
92 void
93 mpq_add (mpq_ptr rop, mpq_srcptr op1, mpq_srcptr op2)
95 mpq_aors (rop, op1, op2, mpz_add);
98 void
99 mpq_sub (mpq_ptr rop, mpq_srcptr op1, mpq_srcptr op2)
101 mpq_aors (rop, op1, op2, mpz_sub);