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
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
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/. */
41 mpn_pow_1 (mp_ptr rp
, mp_srcptr bp
, mp_size_t bn
, mp_limb_t exp
, mp_ptr tp
)
49 /* FIXME: Add operand overlap criteria */
60 MPN_COPY (rp
, bp
, 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. */
88 rn
= 2 * bn
; rn
-= rp
[rn
- 1] == 0;
90 for (i
= GMP_LIMB_BITS
- cnt
- 1;;)
93 if ((exp
& GMP_LIMB_HIGHBIT
) != 0)
95 rp
[rn
] = mpn_mul_1 (rp
, rp
, rn
, bl
);
102 mpn_sqr (tp
, rp
, rn
);
103 rn
= 2 * rn
; rn
-= tp
[rn
- 1] == 0;
104 MP_PTR_SWAP (rp
, tp
);
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;;)
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
);
127 mpn_sqr (tp
, rp
, rn
);
128 rn
= 2 * rn
; rn
-= tp
[rn
- 1] == 0;
129 MP_PTR_SWAP (rp
, tp
);