gcd_subdiv_step.c
上传用户:qaz666999
上传日期:2022-08-06
资源大小:2570k
文件大小:3k
源码类别:

数学计算

开发平台:

Unix_Linux

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