Coverage Report

Created: 2025-03-09 06:52

/src/gmp-6.2.1/mpn/pow_1.c
Line
Count
Source (jump to first uncovered line)
1
/* mpn_pow_1 -- Compute powers R = U^exp.
2
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.
6
7
Copyright 2002, 2014 Free Software Foundation, Inc.
8
9
This file is part of the GNU MP Library.
10
11
The GNU MP Library is free software; you can redistribute it and/or modify
12
it under the terms of either:
13
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.
17
18
or
19
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.
23
24
or both in parallel, as here.
25
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.
30
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/.  */
34
35
36
#include "gmp-impl.h"
37
#include "longlong.h"
38
39
mp_size_t
40
mpn_pow_1 (mp_ptr rp, mp_srcptr bp, mp_size_t bn, mp_limb_t exp, mp_ptr tp)
41
5.96k
{
42
5.96k
  mp_limb_t x;
43
5.96k
  int cnt, i;
44
5.96k
  mp_size_t rn;
45
5.96k
  int par;
46
47
5.96k
  ASSERT (bn >= 1);
48
  /* FIXME: Add operand overlap criteria */
49
50
5.96k
  if (exp <= 1)
51
0
    {
52
0
      if (exp == 0)
53
0
  {
54
0
    rp[0] = 1;
55
0
    return 1;
56
0
  }
57
0
      else
58
0
  {
59
0
    MPN_COPY (rp, bp, bn);
60
0
    return bn;
61
0
  }
62
0
    }
63
64
  /* Count number of bits in exp, and compute where to put initial square in
65
     order to magically get results in the entry rp.  Use simple code,
66
     optimized for small exp.  For large exp, the bignum operations will take
67
     so much time that the slowness of this code will be negligible.  */
68
5.96k
  par = 0;
69
5.96k
  cnt = GMP_LIMB_BITS;
70
5.96k
  x = exp;
71
5.96k
  do
72
14.2k
    {
73
14.2k
      par ^= x;
74
14.2k
      cnt--;
75
14.2k
      x >>= 1;
76
14.2k
    } while (x != 0);
77
5.96k
  exp <<= cnt;
78
79
5.96k
  if (bn == 1)
80
3.34k
    {
81
3.34k
      mp_limb_t rl, rh, bl = bp[0];
82
83
3.34k
      if ((cnt & 1) != 0)
84
13
  MP_PTR_SWAP (rp, tp);
85
86
3.34k
      umul_ppmm (rh, rl, bl, bl << GMP_NAIL_BITS);
87
3.34k
      rp[0] = rl >> GMP_NAIL_BITS;
88
3.34k
      rp[1] = rh;
89
3.34k
      rn = 1 + (rh != 0);
90
91
3.34k
      for (i = GMP_LIMB_BITS - cnt - 1;;)
92
4.72k
  {
93
4.72k
    exp <<= 1;
94
4.72k
    if ((exp & GMP_LIMB_HIGHBIT) != 0)
95
104
      {
96
104
        rp[rn] = rh = mpn_mul_1 (rp, rp, rn, bl);
97
104
        rn += rh != 0;
98
104
      }
99
100
4.72k
    if (--i == 0)
101
3.34k
      break;
102
103
1.38k
    mpn_sqr (tp, rp, rn);
104
1.38k
    rn = 2 * rn; rn -= tp[rn - 1] == 0;
105
1.38k
    MP_PTR_SWAP (rp, tp);
106
1.38k
  }
107
3.34k
    }
108
2.61k
  else
109
2.61k
    {
110
2.61k
      if (((par ^ cnt) & 1) == 0)
111
517
  MP_PTR_SWAP (rp, tp);
112
113
2.61k
      mpn_sqr (rp, bp, bn);
114
2.61k
      rn = 2 * bn; rn -= rp[rn - 1] == 0;
115
116
2.61k
      for (i = GMP_LIMB_BITS - cnt - 1;;)
117
3.60k
  {
118
3.60k
    exp <<= 1;
119
3.60k
    if ((exp & GMP_LIMB_HIGHBIT) != 0)
120
517
      {
121
517
        rn = rn + bn - (mpn_mul (tp, rp, rn, bp, bn) == 0);
122
517
        MP_PTR_SWAP (rp, tp);
123
517
      }
124
125
3.60k
    if (--i == 0)
126
2.61k
      break;
127
128
988
    mpn_sqr (tp, rp, rn);
129
988
    rn = 2 * rn; rn -= tp[rn - 1] == 0;
130
988
    MP_PTR_SWAP (rp, tp);
131
988
  }
132
2.61k
    }
133
134
5.96k
  return rn;
135
5.96k
}