[BACK]Return to aors.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpq

Annotation of OpenXM_contrib/gmp/mpq/aors.c, Revision 1.1

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>