[BACK]Return to ui_pow_ui.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpz

Annotation of OpenXM_contrib/gmp/mpz/ui_pow_ui.c, Revision 1.1

1.1     ! maekawa     1: /* mpz_ui_pow_ui(res, base, exp) -- Set RES to BASE**EXP.
        !             2:
        !             3: Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
        !             4:
        !             5: This file is part of the GNU MP Library.
        !             6:
        !             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.
        !            11:
        !            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.
        !            16:
        !            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. */
        !            21:
        !            22: #include "gmp.h"
        !            23: #include "gmp-impl.h"
        !            24: #include "longlong.h"
        !            25:
        !            26: void
        !            27: #if __STDC__
        !            28: mpz_ui_pow_ui (mpz_ptr r, unsigned long int b, unsigned long int e)
        !            29: #else
        !            30: mpz_ui_pow_ui (r, b, e)
        !            31:      mpz_ptr r;
        !            32:      unsigned long int b;
        !            33:      unsigned long int e;
        !            34: #endif
        !            35: {
        !            36:   mp_ptr rp, tp, xp;
        !            37:   mp_size_t rsize;
        !            38:   int cnt, i;
        !            39:   mp_limb_t blimb = b;
        !            40:   TMP_DECL (marker);
        !            41:
        !            42:   /* Single out cases that give result == 0 or 1.  These tests are here
        !            43:      to simplify the general code below, not to optimize.  */
        !            44:   if (e == 0)
        !            45:     {
        !            46:       r->_mp_d[0] = 1;
        !            47:       r->_mp_size = 1;
        !            48:       return;
        !            49:     }
        !            50:   if (blimb == 0)
        !            51:     {
        !            52:       r->_mp_size = 0;
        !            53:       return;
        !            54:     }
        !            55:
        !            56:   if (blimb < 0x100)
        !            57:     {
        !            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);
        !            62:     }
        !            63:   else
        !            64:     {
        !            65:       /* Over-estimate space requirements somewhat.  */
        !            66:       count_leading_zeros (cnt, blimb);
        !            67:       rsize = e - cnt * e / BITS_PER_MP_LIMB + 1;
        !            68:     }
        !            69:
        !            70:   TMP_MARK (marker);
        !            71:
        !            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
        !            75:      input operands.)  */
        !            76:   rp = (mp_ptr) TMP_ALLOC (rsize * BYTES_PER_MP_LIMB);
        !            77:   tp = (mp_ptr) TMP_ALLOC (rsize * BYTES_PER_MP_LIMB);
        !            78:
        !            79:   rp[0] = blimb;
        !            80:   rsize = 1;
        !            81:   count_leading_zeros (cnt, e);
        !            82:
        !            83:   for (i = BITS_PER_MP_LIMB - cnt - 2; i >= 0; i--)
        !            84:     {
        !            85:       mpn_mul_n (tp, rp, rp, rsize);
        !            86:       rsize = 2 * rsize;
        !            87:       rsize -= tp[rsize - 1] == 0;
        !            88:       xp = tp; tp = rp; rp = xp;
        !            89:
        !            90:       if ((e & ((mp_limb_t) 1 << i)) != 0)
        !            91:        {
        !            92:          mp_limb_t cy;
        !            93:          cy = mpn_mul_1 (tp, rp, rsize, blimb);
        !            94:          if (cy != 0)
        !            95:            {
        !            96:              tp[rsize] = cy;
        !            97:              rsize++;
        !            98:            }
        !            99:          xp = tp; tp = rp; rp = xp;
        !           100:        }
        !           101:     }
        !           102:
        !           103:   /* Now then we know the exact space requirements, reallocate if
        !           104:      necessary.  */
        !           105:   if (r->_mp_alloc < rsize)
        !           106:     _mpz_realloc (r, rsize);
        !           107:
        !           108:   MPN_COPY (r->_mp_d, rp, rsize);
        !           109:   r->_mp_size = rsize;
        !           110:   TMP_FREE (marker);
        !           111: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>