Home | History | Annotate | Line # | Download | only in generic
pow_1.c revision 1.1.1.3
      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-impl.h"
     37 #include "longlong.h"
     38 
     39 mp_size_t
     40 mpn_pow_1 (mp_ptr rp, mp_srcptr bp, mp_size_t bn, mp_limb_t exp, mp_ptr tp)
     41 {
     42   mp_limb_t x;
     43   int cnt, i;
     44   mp_size_t rn;
     45   int par;
     46 
     47   ASSERT (bn >= 1);
     48   /* FIXME: Add operand overlap criteria */
     49 
     50   if (exp <= 1)
     51     {
     52       if (exp == 0)
     53 	{
     54 	  rp[0] = 1;
     55 	  return 1;
     56 	}
     57       else
     58 	{
     59 	  MPN_COPY (rp, bp, bn);
     60 	  return bn;
     61 	}
     62     }
     63 
     64   /* Count number of bits in exp, and compute where to put initial square in
     65      order to magically get results in the entry rp.  Use simple code,
     66      optimized for small exp.  For large exp, the bignum operations will take
     67      so much time that the slowness of this code will be negligible.  */
     68   par = 0;
     69   cnt = GMP_LIMB_BITS;
     70   x = exp;
     71   do
     72     {
     73       par ^= x;
     74       cnt--;
     75       x >>= 1;
     76     } while (x != 0);
     77   exp <<= cnt;
     78 
     79   if (bn == 1)
     80     {
     81       mp_limb_t rl, rh, bl = bp[0];
     82 
     83       if ((cnt & 1) != 0)
     84 	MP_PTR_SWAP (rp, tp);
     85 
     86       umul_ppmm (rh, rl, bl, bl << GMP_NAIL_BITS);
     87       rp[0] = rl >> GMP_NAIL_BITS;
     88       rp[1] = rh;
     89       rn = 1 + (rh != 0);
     90 
     91       for (i = GMP_LIMB_BITS - cnt - 1;;)
     92 	{
     93 	  exp <<= 1;
     94 	  if ((exp & GMP_LIMB_HIGHBIT) != 0)
     95 	    {
     96 	      rp[rn] = rh = mpn_mul_1 (rp, rp, rn, bl);
     97 	      rn += rh != 0;
     98 	    }
     99 
    100 	  if (--i == 0)
    101 	    break;
    102 
    103 	  mpn_sqr (tp, rp, rn);
    104 	  rn = 2 * rn; rn -= tp[rn - 1] == 0;
    105 	  MP_PTR_SWAP (rp, tp);
    106 	}
    107     }
    108   else
    109     {
    110       if (((par ^ cnt) & 1) == 0)
    111 	MP_PTR_SWAP (rp, tp);
    112 
    113       mpn_sqr (rp, bp, bn);
    114       rn = 2 * bn; rn -= rp[rn - 1] == 0;
    115 
    116       for (i = GMP_LIMB_BITS - cnt - 1;;)
    117 	{
    118 	  exp <<= 1;
    119 	  if ((exp & GMP_LIMB_HIGHBIT) != 0)
    120 	    {
    121 	      rn = rn + bn - (mpn_mul (tp, rp, rn, bp, bn) == 0);
    122 	      MP_PTR_SWAP (rp, tp);
    123 	    }
    124 
    125 	  if (--i == 0)
    126 	    break;
    127 
    128 	  mpn_sqr (tp, rp, rn);
    129 	  rn = 2 * rn; rn -= tp[rn - 1] == 0;
    130 	  MP_PTR_SWAP (rp, tp);
    131 	}
    132     }
    133 
    134   return rn;
    135 }
    136