Home | History | Annotate | Line # | Download | only in generic
gcd_subdiv_step.c revision 1.1.1.1
      1 /* gcd_subdiv_step.c.
      2 
      3    THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES.  IT IS ONLY
      4    SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
      5    GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
      6 
      7 Copyright 2003, 2004, 2005, 2008 Free Software Foundation, Inc.
      8 
      9 This file is part of the GNU MP Library.
     10 
     11 The GNU MP Library is free software; you can redistribute it and/or modify
     12 it under the terms of the GNU Lesser General Public License as published by
     13 the Free Software Foundation; either version 3 of the License, or (at your
     14 option) any later version.
     15 
     16 The GNU MP Library is distributed in the hope that it will be useful, but
     17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     18 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     19 License for more details.
     20 
     21 You should have received a copy of the GNU Lesser General Public License
     22 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
     23 
     24 #include "gmp.h"
     25 #include "gmp-impl.h"
     26 #include "longlong.h"
     27 
     28 /* Used when mpn_hgcd or mpn_hgcd2 has failed. Then either one of a or
     29    b is small, or the difference is small. Perform one subtraction
     30    followed by one division. If the gcd is found, stores it in gp and
     31    *gn, and returns zero. Otherwise, compute the reduced a and b, and
     32    return the new size. */
     33 
     34 /* FIXME: Check when the smaller number is a single limb, and invoke
     35  * mpn_gcd_1. */
     36 mp_size_t
     37 mpn_gcd_subdiv_step (mp_ptr gp, mp_size_t *gn,
     38 		     mp_ptr ap, mp_ptr bp, mp_size_t n, mp_ptr tp)
     39 {
     40   mp_size_t an, bn;
     41 
     42   ASSERT (n > 0);
     43   ASSERT (ap[n-1] > 0 || bp[n-1] > 0);
     44 
     45   an = bn = n;
     46   MPN_NORMALIZE (ap, an);
     47   MPN_NORMALIZE (bp, bn);
     48 
     49   if (UNLIKELY (an == 0))
     50     {
     51     return_b:
     52       MPN_COPY (gp, bp, bn);
     53       *gn = bn;
     54       return 0;
     55     }
     56   else if (UNLIKELY (bn == 0))
     57     {
     58     return_a:
     59       MPN_COPY (gp, ap, an);
     60       *gn = an;
     61       return 0;
     62     }
     63 
     64   /* Arrange so that a > b, subtract an -= bn, and maintain
     65      normalization. */
     66   if (an < bn)
     67     MPN_PTR_SWAP (ap, an, bp, bn);
     68   else if (an == bn)
     69     {
     70       int c;
     71       MPN_CMP (c, ap, bp, an);
     72       if (UNLIKELY (c == 0))
     73 	goto return_a;
     74       else if (c < 0)
     75 	MP_PTR_SWAP (ap, bp);
     76     }
     77 
     78   ASSERT_NOCARRY (mpn_sub (ap, ap, an, bp, bn));
     79   MPN_NORMALIZE (ap, an);
     80   ASSERT (an > 0);
     81 
     82   /* Arrange so that a > b, and divide a = q b + r */
     83   /* FIXME: an < bn happens when we have cancellation. If that is the
     84      common case, then we could reverse the roles of a and b to avoid
     85      the swap. */
     86   if (an < bn)
     87     MPN_PTR_SWAP (ap, an, bp, bn);
     88   else if (an == bn)
     89     {
     90       int c;
     91       MPN_CMP (c, ap, bp, an);
     92       if (UNLIKELY (c == 0))
     93 	goto return_a;
     94       else if (c < 0)
     95 	MP_PTR_SWAP (ap, bp);
     96     }
     97 
     98   mpn_tdiv_qr (tp, ap, 0, ap, an, bp, bn);
     99 
    100   if (mpn_zero_p (ap, bn))
    101     goto return_b;
    102 
    103   return bn;
    104 }
    105