summaryrefslogtreecommitdiff
path: root/mpq/inv.c
diff options
context:
space:
mode:
Diffstat (limited to 'mpq/inv.c')
-rw-r--r--mpq/inv.c35
1 files changed, 15 insertions, 20 deletions
diff --git a/mpq/inv.c b/mpq/inv.c
index 73b844a46..0c6b5e8f1 100644
--- a/mpq/inv.c
+++ b/mpq/inv.c
@@ -1,7 +1,7 @@
/* mpq_inv(dest,src) -- invert a rational number, i.e. set DEST to SRC
with the numerator and denominator swapped.
-Copyright 1991, 1994, 1995, 2000, 2001 Free Software Foundation, Inc.
+Copyright 1991, 1994, 1995, 2000, 2001, 2012 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
@@ -22,44 +22,39 @@ along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
#include "gmp-impl.h"
void
-mpq_inv (MP_RAT *dest, const MP_RAT *src)
+mpq_inv (mpq_ptr dest, mpq_srcptr src)
{
mp_size_t num_size = SIZ(NUM(src));
mp_size_t den_size = SIZ(DEN(src));
- if (UNLIKELY (num_size == 0))
- DIVIDE_BY_ZERO;
-
if (num_size < 0)
{
num_size = -num_size;
den_size = -den_size;
}
+ else if (UNLIKELY (num_size == 0))
+ DIVIDE_BY_ZERO;
+
SIZ(DEN(dest)) = num_size;
SIZ(NUM(dest)) = den_size;
- /* If dest == src we may just swap the numerator and denominator, but
- we have to ensure the new denominator is positive. */
+ /* If dest == src we may just swap the numerator and denominator;
+ we ensured that the new denominator is positive. */
if (dest == src)
{
- mp_size_t alloc = ALLOC(NUM(dest));
- mp_ptr limb_ptr = PTR(NUM(dest));
-
- ALLOC(NUM(dest)) = ALLOC(DEN(dest));
- PTR(NUM(dest)) = PTR(DEN(dest));
-
- ALLOC(DEN(dest)) = alloc;
- PTR(DEN(dest)) = limb_ptr;
+ MP_PTR_SWAP (PTR(NUM(dest)), PTR(DEN(dest)));
+ MP_SIZE_T_SWAP (ALLOC(NUM(dest)), ALLOC(DEN(dest)));
}
else
{
- den_size = ABS (den_size);
+ mp_ptr dp;
- MPZ_REALLOC (NUM(dest), den_size);
- MPZ_REALLOC (DEN(dest), num_size);
+ den_size = ABS (den_size);
+ dp = MPZ_REALLOC (NUM(dest), den_size);
+ MPN_COPY (dp, PTR(DEN(src)), den_size);
- MPN_COPY (PTR(NUM(dest)), PTR(DEN(src)), den_size);
- MPN_COPY (PTR(DEN(dest)), PTR(NUM(src)), num_size);
+ dp = MPZ_REALLOC (DEN(dest), num_size);
+ MPN_COPY (dp, PTR(NUM(src)), num_size);
}
}