github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpn/generic/pow_1.c (about)

     1  /* mpn_pow_1 -- Compute powers R = U^exp.
     2  
     3     THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
     4     CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
     5     FUTURE GNU MP RELEASES.
     6  
     7  Copyright 2002, 2014 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 either:
    13  
    14    * the GNU Lesser General Public License as published by the Free
    15      Software Foundation; either version 3 of the License, or (at your
    16      option) any later version.
    17  
    18  or
    19  
    20    * the GNU General Public License as published by the Free Software
    21      Foundation; either version 2 of the License, or (at your option) any
    22      later version.
    23  
    24  or both in parallel, as here.
    25  
    26  The GNU MP Library is distributed in the hope that it will be useful, but
    27  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    28  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    29  for more details.
    30  
    31  You should have received copies of the GNU General Public License and the
    32  GNU Lesser General Public License along with the GNU MP Library.  If not,
    33  see https://www.gnu.org/licenses/.  */
    34  
    35  
    36  #include "gmp.h"
    37  #include "gmp-impl.h"
    38  #include "longlong.h"
    39  
    40  mp_size_t
    41  mpn_pow_1 (mp_ptr rp, mp_srcptr bp, mp_size_t bn, mp_limb_t exp, mp_ptr tp)
    42  {
    43    mp_limb_t x;
    44    int cnt, i;
    45    mp_size_t rn;
    46    int par;
    47  
    48    ASSERT (bn >= 1);
    49    /* FIXME: Add operand overlap criteria */
    50  
    51    if (exp <= 1)
    52      {
    53        if (exp == 0)
    54  	{
    55  	  rp[0] = 1;
    56  	  return 1;
    57  	}
    58        else
    59  	{
    60  	  MPN_COPY (rp, bp, bn);
    61  	  return bn;
    62  	}
    63      }
    64  
    65    /* Count number of bits in exp, and compute where to put initial square in
    66       order to magically get results in the entry rp.  Use simple code,
    67       optimized for small exp.  For large exp, the bignum operations will take
    68       so much time that the slowness of this code will be negligible.  */
    69    par = 0;
    70    cnt = GMP_LIMB_BITS;
    71    x = exp;
    72    do
    73      {
    74        par ^= x;
    75        cnt--;
    76        x >>= 1;
    77      } while (x != 0);
    78    exp <<= cnt;
    79  
    80    if (bn == 1)
    81      {
    82        mp_limb_t bl = bp[0];
    83  
    84        if ((cnt & 1) != 0)
    85  	MP_PTR_SWAP (rp, tp);
    86  
    87        mpn_sqr (rp, bp, bn);
    88        rn = 2 * bn; rn -= rp[rn - 1] == 0;
    89  
    90        for (i = GMP_LIMB_BITS - cnt - 1;;)
    91  	{
    92  	  exp <<= 1;
    93  	  if ((exp & GMP_LIMB_HIGHBIT) != 0)
    94  	    {
    95  	      rp[rn] = mpn_mul_1 (rp, rp, rn, bl);
    96  	      rn += rp[rn] != 0;
    97  	    }
    98  
    99  	  if (--i == 0)
   100  	    break;
   101  
   102  	  mpn_sqr (tp, rp, rn);
   103  	  rn = 2 * rn; rn -= tp[rn - 1] == 0;
   104  	  MP_PTR_SWAP (rp, tp);
   105  	}
   106      }
   107    else
   108      {
   109        if (((par ^ cnt) & 1) == 0)
   110  	MP_PTR_SWAP (rp, tp);
   111  
   112        mpn_sqr (rp, bp, bn);
   113        rn = 2 * bn; rn -= rp[rn - 1] == 0;
   114  
   115        for (i = GMP_LIMB_BITS - cnt - 1;;)
   116  	{
   117  	  exp <<= 1;
   118  	  if ((exp & GMP_LIMB_HIGHBIT) != 0)
   119  	    {
   120  	      rn = rn + bn - (mpn_mul (tp, rp, rn, bp, bn) == 0);
   121  	      MP_PTR_SWAP (rp, tp);
   122  	    }
   123  
   124  	  if (--i == 0)
   125  	    break;
   126  
   127  	  mpn_sqr (tp, rp, rn);
   128  	  rn = 2 * rn; rn -= tp[rn - 1] == 0;
   129  	  MP_PTR_SWAP (rp, tp);
   130  	}
   131      }
   132  
   133    return rn;
   134  }