pow_1.c
Upload User: qaz666999
Upload Date: 2022-08-06
Package Size: 2570k
Code Size: 3k
Category:

Algorithm

Development Platform:

Unix_Linux

  1. /* mpn_pow_1 -- Compute powers R = U^exp.
  2.    THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
  3.    CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
  4.    FUTURE GNU MP RELEASES.
  5. Copyright 2002 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 it
  8. under the terms of the GNU Lesser General Public License as published by the
  9. 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 or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
  14. for more details.
  15. You should have received a copy of the GNU Lesser General Public License along
  16. 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. mp_size_t
  21. mpn_pow_1 (mp_ptr rp, mp_srcptr bp, mp_size_t bn, mp_limb_t exp, mp_ptr tp)
  22. {
  23.   mp_limb_t x;
  24.   int cnt, i;
  25.   mp_size_t rn;
  26.   int par;
  27.   ASSERT (bn >= 1);
  28.   /* FIXME: Add operand overlap criteria */
  29.   if (exp <= 1)
  30.     {
  31.       if (exp == 0)
  32. {
  33.   rp[0] = 1;
  34.   return 1;
  35. }
  36.       else
  37. {
  38.   MPN_COPY (rp, bp, bn);
  39.   return bn;
  40. }
  41.     }
  42.   /* Count number of bits in exp, and compute where to put initial square in
  43.      order to magically get results in the entry rp.  Use simple code,
  44.      optimized for small exp.  For large exp, the bignum operations will take
  45.      so much time that the slowness of this code will be negligible.  */
  46.   par = 0;
  47.   cnt = GMP_LIMB_BITS;
  48.   for (x = exp; x != 0; x >>= 1)
  49.     {
  50.       par ^= x & 1;
  51.       cnt--;
  52.     }
  53.   exp <<= cnt;
  54.   if (bn == 1)
  55.     {
  56.       mp_limb_t bl = bp[0];
  57.       if ((cnt & 1) != 0)
  58. MP_PTR_SWAP (rp, tp);
  59.       mpn_sqr (rp, bp, bn);
  60.       rn = 2 * bn; rn -= rp[rn - 1] == 0;
  61.       for (i = GMP_LIMB_BITS - cnt - 1;;)
  62. {
  63.   exp <<= 1;
  64.   if ((exp & GMP_LIMB_HIGHBIT) != 0)
  65.     {
  66.       rp[rn] = mpn_mul_1 (rp, rp, rn, bl);
  67.       rn += rp[rn] != 0;
  68.     }
  69.   if (--i == 0)
  70.     break;
  71.   mpn_sqr (tp, rp, rn);
  72.   rn = 2 * rn; rn -= tp[rn - 1] == 0;
  73.   MP_PTR_SWAP (rp, tp);
  74. }
  75.     }
  76.   else
  77.     {
  78.       if (((par ^ cnt) & 1) == 0)
  79. MP_PTR_SWAP (rp, tp);
  80.       mpn_sqr (rp, bp, bn);
  81.       rn = 2 * bn; rn -= rp[rn - 1] == 0;
  82.       for (i = GMP_LIMB_BITS - cnt - 1;;)
  83. {
  84.   exp <<= 1;
  85.   if ((exp & GMP_LIMB_HIGHBIT) != 0)
  86.     {
  87.       rn = rn + bn - (mpn_mul (tp, rp, rn, bp, bn) == 0);
  88.       MP_PTR_SWAP (rp, tp);
  89.     }
  90.   if (--i == 0)
  91.     break;
  92.   mpn_sqr (tp, rp, rn);
  93.   rn = 2 * rn; rn -= tp[rn - 1] == 0;
  94.   MP_PTR_SWAP (rp, tp);
  95. }
  96.     }
  97.   return rn;
  98. }