version 1.1, 2000/01/10 15:35:27 |
version 1.1.1.3, 2003/08/25 16:06:34 |
|
|
/* mpz_ui_pow_ui(res, base, exp) -- Set RES to BASE**EXP. |
/* mpz_ui_pow_ui -- ulong raised to ulong. |
|
|
Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc. |
Copyright 2001, 2002 Free Software Foundation, Inc. |
|
|
This file is part of the GNU MP Library. |
This file is part of the GNU MP Library. |
|
|
The GNU MP Library is free software; you can redistribute it and/or modify |
The GNU MP Library is free software; you can redistribute it and/or modify |
it under the terms of the GNU Library General Public License as published by |
it under the terms of the GNU Lesser General Public License as published by |
the Free Software Foundation; either version 2 of the License, or (at your |
the Free Software Foundation; either version 2.1 of the License, or (at your |
option) any later version. |
option) any later version. |
|
|
The GNU MP Library is distributed in the hope that it will be useful, but |
The GNU MP Library is distributed in the hope that it will be useful, but |
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
License for more details. |
License for more details. |
|
|
You should have received a copy of the GNU Library General Public License |
You should have received a copy of the GNU Lesser General Public License |
along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
MA 02111-1307, USA. */ |
MA 02111-1307, USA. */ |
|
|
#include "gmp.h" |
#include "gmp.h" |
#include "gmp-impl.h" |
#include "gmp-impl.h" |
#include "longlong.h" |
|
|
|
|
|
void |
void |
#if __STDC__ |
mpz_ui_pow_ui (mpz_ptr r, unsigned long b, unsigned long e) |
mpz_ui_pow_ui (mpz_ptr r, unsigned long int b, unsigned long int e) |
|
#else |
|
mpz_ui_pow_ui (r, b, e) |
|
mpz_ptr r; |
|
unsigned long int b; |
|
unsigned long int e; |
|
#endif |
|
{ |
{ |
mp_ptr rp, tp, xp; |
#if GMP_NAIL_BITS != 0 |
mp_size_t rsize; |
if (b > GMP_NUMB_MAX) |
int cnt, i; |
|
mp_limb_t blimb = b; |
|
TMP_DECL (marker); |
|
|
|
/* Single out cases that give result == 0 or 1. These tests are here |
|
to simplify the general code below, not to optimize. */ |
|
if (e == 0) |
|
{ |
{ |
r->_mp_d[0] = 1; |
mp_limb_t bb[2]; |
r->_mp_size = 1; |
bb[0] = b & GMP_NUMB_MASK; |
return; |
bb[1] = b >> GMP_NUMB_BITS; |
|
mpz_n_pow_ui (r, bb, (mp_size_t) 2, e); |
} |
} |
if (blimb == 0) |
|
{ |
|
r->_mp_size = 0; |
|
return; |
|
} |
|
|
|
if (blimb < 0x100) |
|
{ |
|
/* Estimate space requirements accurately. Using the code from the |
|
`else' path would over-estimate space requirements wildly. */ |
|
float lb = __mp_bases[blimb].chars_per_bit_exactly; |
|
rsize = 2 + ((mp_size_t) (e / lb) / BITS_PER_MP_LIMB); |
|
} |
|
else |
else |
|
#endif |
{ |
{ |
/* Over-estimate space requirements somewhat. */ |
#ifdef _LONG_LONG_LIMB |
count_leading_zeros (cnt, blimb); |
/* i386 gcc 2.95.3 doesn't recognise blimb can be eliminated when |
rsize = e - cnt * e / BITS_PER_MP_LIMB + 1; |
mp_limb_t is an unsigned long, so only use a separate blimb when |
|
necessary. */ |
|
mp_limb_t blimb = b; |
|
mpz_n_pow_ui (r, &blimb, (mp_size_t) (b != 0), e); |
|
#else |
|
mpz_n_pow_ui (r, &b, (mp_size_t) (b != 0), e); |
|
#endif |
} |
} |
|
|
TMP_MARK (marker); |
|
|
|
/* The two areas are used to alternatingly hold the input and recieve the |
|
product for mpn_mul. (This scheme is used to fulfill the requirements |
|
of mpn_mul; that the product space may not be the same as any of the |
|
input operands.) */ |
|
rp = (mp_ptr) TMP_ALLOC (rsize * BYTES_PER_MP_LIMB); |
|
tp = (mp_ptr) TMP_ALLOC (rsize * BYTES_PER_MP_LIMB); |
|
|
|
rp[0] = blimb; |
|
rsize = 1; |
|
count_leading_zeros (cnt, e); |
|
|
|
for (i = BITS_PER_MP_LIMB - cnt - 2; i >= 0; i--) |
|
{ |
|
mpn_mul_n (tp, rp, rp, rsize); |
|
rsize = 2 * rsize; |
|
rsize -= tp[rsize - 1] == 0; |
|
xp = tp; tp = rp; rp = xp; |
|
|
|
if ((e & ((mp_limb_t) 1 << i)) != 0) |
|
{ |
|
mp_limb_t cy; |
|
cy = mpn_mul_1 (tp, rp, rsize, blimb); |
|
if (cy != 0) |
|
{ |
|
tp[rsize] = cy; |
|
rsize++; |
|
} |
|
xp = tp; tp = rp; rp = xp; |
|
} |
|
} |
|
|
|
/* Now then we know the exact space requirements, reallocate if |
|
necessary. */ |
|
if (r->_mp_alloc < rsize) |
|
_mpz_realloc (r, rsize); |
|
|
|
MPN_COPY (r->_mp_d, rp, rsize); |
|
r->_mp_size = rsize; |
|
TMP_FREE (marker); |
|
} |
} |
|
|