1 /* mpz_ui_pow_ui(res, base, exp) -- Set RES to BASE**EXP.
3 Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Library General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15 License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA. */
28 mpz_ui_pow_ui (mpz_ptr r, unsigned long int b, unsigned long int e)
30 mpz_ui_pow_ui (r, b, e)
42 /* Single out cases that give result == 0 or 1. These tests are here
43 to simplify the general code below, not to optimize. */
58 /* Estimate space requirements accurately. Using the code from the
59 `else' path would over-estimate space requirements wildly. */
60 float lb = __mp_bases[blimb].chars_per_bit_exactly;
61 rsize = 2 + ((mp_size_t) (e / lb) / BITS_PER_MP_LIMB);
65 /* Over-estimate space requirements somewhat. */
66 count_leading_zeros (cnt, blimb);
67 rsize = e - cnt * e / BITS_PER_MP_LIMB + 1;
72 /* The two areas are used to alternatingly hold the input and recieve the
73 product for mpn_mul. (This scheme is used to fulfill the requirements
74 of mpn_mul; that the product space may not be the same as any of the
76 rp = (mp_ptr) TMP_ALLOC (rsize * BYTES_PER_MP_LIMB);
77 tp = (mp_ptr) TMP_ALLOC (rsize * BYTES_PER_MP_LIMB);
81 count_leading_zeros (cnt, e);
83 for (i = BITS_PER_MP_LIMB - cnt - 2; i >= 0; i--)
85 mpn_mul_n (tp, rp, rp, rsize);
87 rsize -= tp[rsize - 1] == 0;
88 xp = tp; tp = rp; rp = xp;
90 if ((e & ((mp_limb_t) 1 << i)) != 0)
93 cy = mpn_mul_1 (tp, rp, rsize, blimb);
99 xp = tp; tp = rp; rp = xp;
103 /* Now then we know the exact space requirements, reallocate if
105 if (r->_mp_alloc < rsize)
106 _mpz_realloc (r, rsize);
108 MPN_COPY (r->_mp_d, rp, rsize);