beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / pow_1.c
blob2333206554ca22df3b3f3bc7ddcbcca14194588f
1 /* mpn_pow_1 -- Compute powers R = U^exp.
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.
7 Copyright 2002, 2014 Free Software Foundation, Inc.
9 This file is part of the GNU MP Library.
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of either:
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.
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.
24 or both in parallel, as here.
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.
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/. */
36 #include "gmp.h"
37 #include "gmp-impl.h"
38 #include "longlong.h"
40 mp_size_t
41 mpn_pow_1 (mp_ptr rp, mp_srcptr bp, mp_size_t bn, mp_limb_t exp, mp_ptr tp)
43 mp_limb_t x;
44 int cnt, i;
45 mp_size_t rn;
46 int par;
48 ASSERT (bn >= 1);
49 /* FIXME: Add operand overlap criteria */
51 if (exp <= 1)
53 if (exp == 0)
55 rp[0] = 1;
56 return 1;
58 else
60 MPN_COPY (rp, bp, bn);
61 return bn;
65 /* Count number of bits in exp, and compute where to put initial square in
66 order to magically get results in the entry rp. Use simple code,
67 optimized for small exp. For large exp, the bignum operations will take
68 so much time that the slowness of this code will be negligible. */
69 par = 0;
70 cnt = GMP_LIMB_BITS;
71 x = exp;
74 par ^= x;
75 cnt--;
76 x >>= 1;
77 } while (x != 0);
78 exp <<= cnt;
80 if (bn == 1)
82 mp_limb_t bl = bp[0];
84 if ((cnt & 1) != 0)
85 MP_PTR_SWAP (rp, tp);
87 mpn_sqr (rp, bp, bn);
88 rn = 2 * bn; rn -= rp[rn - 1] == 0;
90 for (i = GMP_LIMB_BITS - cnt - 1;;)
92 exp <<= 1;
93 if ((exp & GMP_LIMB_HIGHBIT) != 0)
95 rp[rn] = mpn_mul_1 (rp, rp, rn, bl);
96 rn += rp[rn] != 0;
99 if (--i == 0)
100 break;
102 mpn_sqr (tp, rp, rn);
103 rn = 2 * rn; rn -= tp[rn - 1] == 0;
104 MP_PTR_SWAP (rp, tp);
107 else
109 if (((par ^ cnt) & 1) == 0)
110 MP_PTR_SWAP (rp, tp);
112 mpn_sqr (rp, bp, bn);
113 rn = 2 * bn; rn -= rp[rn - 1] == 0;
115 for (i = GMP_LIMB_BITS - cnt - 1;;)
117 exp <<= 1;
118 if ((exp & GMP_LIMB_HIGHBIT) != 0)
120 rn = rn + bn - (mpn_mul (tp, rp, rn, bp, bn) == 0);
121 MP_PTR_SWAP (rp, tp);
124 if (--i == 0)
125 break;
127 mpn_sqr (tp, rp, rn);
128 rn = 2 * rn; rn -= tp[rn - 1] == 0;
129 MP_PTR_SWAP (rp, tp);
133 return rn;