Home | History | Annotate | Line # | Download | only in generic
pow_1.c revision 1.1
      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 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 it
     12 under the terms of the GNU Lesser General Public License as published by the
     13 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 or
     18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
     19 for more details.
     20 
     21 You should have received a copy of the GNU Lesser General Public License along
     22 with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
     23 
     24 
     25 #include "gmp.h"
     26 #include "gmp-impl.h"
     27 #include "longlong.h"
     28 
     29 mp_size_t
     30 mpn_pow_1 (mp_ptr rp, mp_srcptr bp, mp_size_t bn, mp_limb_t exp, mp_ptr tp)
     31 {
     32   mp_limb_t x;
     33   int cnt, i;
     34   mp_size_t rn;
     35   int par;
     36 
     37   ASSERT (bn >= 1);
     38   /* FIXME: Add operand overlap criteria */
     39 
     40   if (exp <= 1)
     41     {
     42       if (exp == 0)
     43 	{
     44 	  rp[0] = 1;
     45 	  return 1;
     46 	}
     47       else
     48 	{
     49 	  MPN_COPY (rp, bp, bn);
     50 	  return bn;
     51 	}
     52     }
     53 
     54   /* Count number of bits in exp, and compute where to put initial square in
     55      order to magically get results in the entry rp.  Use simple code,
     56      optimized for small exp.  For large exp, the bignum operations will take
     57      so much time that the slowness of this code will be negligible.  */
     58   par = 0;
     59   cnt = GMP_LIMB_BITS;
     60   for (x = exp; x != 0; x >>= 1)
     61     {
     62       par ^= x & 1;
     63       cnt--;
     64     }
     65   exp <<= cnt;
     66 
     67   if (bn == 1)
     68     {
     69       mp_limb_t bl = bp[0];
     70 
     71       if ((cnt & 1) != 0)
     72 	MP_PTR_SWAP (rp, tp);
     73 
     74       mpn_sqr (rp, bp, bn);
     75       rn = 2 * bn; rn -= rp[rn - 1] == 0;
     76 
     77       for (i = GMP_LIMB_BITS - cnt - 1;;)
     78 	{
     79 	  exp <<= 1;
     80 	  if ((exp & GMP_LIMB_HIGHBIT) != 0)
     81 	    {
     82 	      rp[rn] = mpn_mul_1 (rp, rp, rn, bl);
     83 	      rn += rp[rn] != 0;
     84 	    }
     85 
     86 	  if (--i == 0)
     87 	    break;
     88 
     89 	  mpn_sqr (tp, rp, rn);
     90 	  rn = 2 * rn; rn -= tp[rn - 1] == 0;
     91 	  MP_PTR_SWAP (rp, tp);
     92 	}
     93     }
     94   else
     95     {
     96       if (((par ^ cnt) & 1) == 0)
     97 	MP_PTR_SWAP (rp, tp);
     98 
     99       mpn_sqr (rp, bp, bn);
    100       rn = 2 * bn; rn -= rp[rn - 1] == 0;
    101 
    102       for (i = GMP_LIMB_BITS - cnt - 1;;)
    103 	{
    104 	  exp <<= 1;
    105 	  if ((exp & GMP_LIMB_HIGHBIT) != 0)
    106 	    {
    107 	      rn = rn + bn - (mpn_mul (tp, rp, rn, bp, bn) == 0);
    108 	      MP_PTR_SWAP (rp, tp);
    109 	    }
    110 
    111 	  if (--i == 0)
    112 	    break;
    113 
    114 	  mpn_sqr (tp, rp, rn);
    115 	  rn = 2 * rn; rn -= tp[rn - 1] == 0;
    116 	  MP_PTR_SWAP (rp, tp);
    117 	}
    118     }
    119 
    120   return rn;
    121 }
    122