Coverage Report

Created: 2025-03-09 06:52

/src/gmp-6.2.1/mpn/powlo.c
Line
Count
Source
1
/* mpn_powlo -- Compute R = U^E mod B^n, where B is the limb base.
2
3
Copyright 2007-2009, 2012, 2015, 2016, 2018 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 either:
9
10
  * the GNU Lesser General Public License as published by the Free
11
    Software Foundation; either version 3 of the License, or (at your
12
    option) any later version.
13
14
or
15
16
  * the GNU General Public License as published by the Free Software
17
    Foundation; either version 2 of the License, or (at your option) any
18
    later version.
19
20
or both in parallel, as here.
21
22
The GNU MP Library is distributed in the hope that it will be useful, but
23
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25
for more details.
26
27
You should have received copies of the GNU General Public License and the
28
GNU Lesser General Public License along with the GNU MP Library.  If not,
29
see https://www.gnu.org/licenses/.  */
30
31
32
#include "gmp-impl.h"
33
#include "longlong.h"
34
35
36
#define getbit(p,bi) \
37
289k
  ((p[(bi - 1) / GMP_LIMB_BITS] >> (bi - 1) % GMP_LIMB_BITS) & 1)
38
39
static inline mp_limb_t
40
getbits (const mp_limb_t *p, mp_bitcnt_t bi, unsigned nbits)
41
99.2k
{
42
99.2k
  unsigned nbits_in_r;
43
99.2k
  mp_limb_t r;
44
99.2k
  mp_size_t i;
45
46
99.2k
  if (bi < nbits)
47
1.06k
    {
48
1.06k
      return p[0] & (((mp_limb_t) 1 << bi) - 1);
49
1.06k
    }
50
98.1k
  else
51
98.1k
    {
52
98.1k
      bi -= nbits;      /* bit index of low bit to extract */
53
98.1k
      i = bi / GMP_NUMB_BITS;   /* word index of low bit to extract */
54
98.1k
      bi %= GMP_NUMB_BITS;   /* bit index in low word */
55
98.1k
      r = p[i] >> bi;     /* extract (low) bits */
56
98.1k
      nbits_in_r = GMP_NUMB_BITS - bi;  /* number of bits now in r */
57
98.1k
      if (nbits_in_r < nbits)    /* did we get enough bits? */
58
8.59k
  r += p[i + 1] << nbits_in_r; /* prepend bits from higher word */
59
98.1k
      return r & (((mp_limb_t ) 1 << nbits) - 1);
60
98.1k
    }
61
99.2k
}
62
63
static inline unsigned
64
win_size (mp_bitcnt_t eb)
65
10.9k
{
66
10.9k
  unsigned k;
67
10.9k
  static mp_bitcnt_t x[] = {7,25,81,241,673,1793,4609,11521,28161,~(mp_bitcnt_t)0};
68
10.9k
  ASSERT (eb > 1);
69
13.4k
  for (k = 0; eb > x[k++];)
70
2.52k
    ;
71
10.9k
  return k;
72
10.9k
}
73
74
/* rp[n-1..0] = bp[n-1..0] ^ ep[en-1..0] mod B^n, B is the limb base.
75
   Requires that ep[en-1] is non-zero.
76
   Uses scratch space tp[3n-1..0], i.e., 3n words.  */
77
/* We only use n words in the scratch space, we should pass tp + n to
78
   mullo/sqrlo as a temporary area, it is needed. */
79
void
80
mpn_powlo (mp_ptr rp, mp_srcptr bp,
81
     mp_srcptr ep, mp_size_t en,
82
     mp_size_t n, mp_ptr tp)
83
10.9k
{
84
10.9k
  unsigned cnt;
85
10.9k
  mp_bitcnt_t ebi;
86
10.9k
  unsigned windowsize, this_windowsize;
87
10.9k
  mp_limb_t expbits;
88
10.9k
  mp_limb_t *pp;
89
10.9k
  long i;
90
10.9k
  int flipflop;
91
10.9k
  TMP_DECL;
92
93
10.9k
  ASSERT (en > 1 || (en == 1 && ep[0] > 1));
94
95
10.9k
  TMP_MARK;
96
97
10.9k
  MPN_SIZEINBASE_2EXP(ebi, ep, en, 1);
98
99
10.9k
  windowsize = win_size (ebi);
100
10.9k
  if (windowsize > 1)
101
1.64k
    {
102
1.64k
      mp_limb_t *this_pp, *last_pp;
103
1.64k
      ASSERT (windowsize < ebi);
104
105
1.64k
      pp = TMP_ALLOC_LIMBS ((n << (windowsize - 1)));
106
107
1.64k
      this_pp = pp;
108
109
1.64k
      MPN_COPY (this_pp, bp, n);
110
111
      /* Store b^2 in tp.  */
112
1.64k
      mpn_sqrlo (tp, bp, n);
113
114
      /* Precompute odd powers of b and put them in the temporary area at pp.  */
115
1.64k
      i = (1 << (windowsize - 1)) - 1;
116
1.64k
      do
117
13.7k
  {
118
13.7k
    last_pp = this_pp;
119
13.7k
    this_pp += n;
120
13.7k
    mpn_mullo_n (this_pp, last_pp, tp, n);
121
13.7k
  } while (--i != 0);
122
123
1.64k
      expbits = getbits (ep, ebi, windowsize);
124
125
      /* THINK: Should we initialise the case expbits % 4 == 0 with a mullo? */
126
1.64k
      count_trailing_zeros (cnt, expbits);
127
1.64k
      ebi -= windowsize;
128
1.64k
      ebi += cnt;
129
1.64k
      expbits >>= cnt;
130
131
1.64k
      MPN_COPY (rp, pp + n * (expbits >> 1), n);
132
1.64k
    }
133
9.30k
  else
134
9.30k
    {
135
9.30k
      pp = tp + n;
136
9.30k
      MPN_COPY (pp, bp, n);
137
9.30k
      MPN_COPY (rp, bp, n);
138
9.30k
      --ebi;
139
9.30k
    }
140
141
10.9k
  flipflop = 0;
142
143
10.9k
  do
144
101k
    {
145
289k
      while (getbit (ep, ebi) == 0)
146
191k
  {
147
191k
    mpn_sqrlo (tp, rp, n);
148
191k
    MP_PTR_SWAP (rp, tp);
149
191k
    flipflop = ! flipflop;
150
191k
    if (--ebi == 0)
151
3.80k
      goto done;
152
191k
  }
153
154
      /* The next bit of the exponent is 1.  Now extract the largest block of
155
   bits <= windowsize, and such that the least significant bit is 1.  */
156
157
97.5k
      expbits = getbits (ep, ebi, windowsize);
158
97.5k
      this_windowsize = MIN (windowsize, ebi);
159
97.5k
      ebi -= this_windowsize;
160
161
97.5k
      count_trailing_zeros (cnt, expbits);
162
97.5k
      this_windowsize -= cnt;
163
97.5k
      ebi += cnt;
164
97.5k
      expbits >>= cnt;
165
166
351k
      while (this_windowsize > 1)
167
253k
  {
168
253k
    mpn_sqrlo (tp, rp, n);
169
253k
    mpn_sqrlo (rp, tp, n);
170
253k
    this_windowsize -= 2;
171
253k
  }
172
173
97.5k
      if (this_windowsize != 0)
174
48.5k
  mpn_sqrlo (tp, rp, n);
175
49.0k
      else
176
49.0k
  {
177
49.0k
    MP_PTR_SWAP (rp, tp);
178
49.0k
    flipflop = ! flipflop;
179
49.0k
  }
180
181
97.5k
      mpn_mullo_n (rp, tp, pp + n * (expbits >> 1), n);
182
97.5k
    } while (ebi != 0);
183
184
10.9k
 done:
185
10.9k
  if (flipflop)
186
5.46k
    MPN_COPY (tp, rp, n);
187
10.9k
  TMP_FREE;
188
10.9k
}