Coverage Report

Created: 2025-03-09 06:52

/src/gmp-6.2.1/mpn/mulmod_bnm1.c
Line
Count
Source (jump to first uncovered line)
1
/* mulmod_bnm1.c -- multiplication mod B^n-1.
2
3
   Contributed to the GNU project by Niels Möller, Torbjorn Granlund and
4
   Marco Bodrato.
5
6
   THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES.  IT IS ONLY
7
   SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
8
   GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
9
10
Copyright 2009, 2010, 2012, 2013 Free Software Foundation, Inc.
11
12
This file is part of the GNU MP Library.
13
14
The GNU MP Library is free software; you can redistribute it and/or modify
15
it under the terms of either:
16
17
  * the GNU Lesser General Public License as published by the Free
18
    Software Foundation; either version 3 of the License, or (at your
19
    option) any later version.
20
21
or
22
23
  * the GNU General Public License as published by the Free Software
24
    Foundation; either version 2 of the License, or (at your option) any
25
    later version.
26
27
or both in parallel, as here.
28
29
The GNU MP Library is distributed in the hope that it will be useful, but
30
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
31
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
32
for more details.
33
34
You should have received copies of the GNU General Public License and the
35
GNU Lesser General Public License along with the GNU MP Library.  If not,
36
see https://www.gnu.org/licenses/.  */
37
38
39
#include "gmp-impl.h"
40
#include "longlong.h"
41
42
/* Inputs are {ap,rn} and {bp,rn}; output is {rp,rn}, computation is
43
   mod B^rn - 1, and values are semi-normalised; zero is represented
44
   as either 0 or B^n - 1.  Needs a scratch of 2rn limbs at tp.
45
   tp==rp is allowed. */
46
void
47
mpn_bc_mulmod_bnm1 (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t rn,
48
        mp_ptr tp)
49
3.19M
{
50
3.19M
  mp_limb_t cy;
51
52
3.19M
  ASSERT (0 < rn);
53
54
3.19M
  mpn_mul_n (tp, ap, bp, rn);
55
3.19M
  cy = mpn_add_n (rp, tp, tp + rn, rn);
56
  /* If cy == 1, then the value of rp is at most B^rn - 2, so there can
57
   * be no overflow when adding in the carry. */
58
3.19M
  MPN_INCR_U (rp, rn, cy);
59
3.19M
}
60
61
62
/* Inputs are {ap,rn+1} and {bp,rn+1}; output is {rp,rn+1}, in
63
   semi-normalised representation, computation is mod B^rn + 1. Needs
64
   a scratch area of 2rn + 2 limbs at tp; tp == rp is allowed.
65
   Output is normalised. */
66
static void
67
mpn_bc_mulmod_bnp1 (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t rn,
68
        mp_ptr tp)
69
8.64M
{
70
8.64M
  mp_limb_t cy;
71
72
8.64M
  ASSERT (0 < rn);
73
74
8.64M
  mpn_mul_n (tp, ap, bp, rn + 1);
75
8.64M
  ASSERT (tp[2*rn+1] == 0);
76
8.64M
  ASSERT (tp[2*rn] < GMP_NUMB_MAX);
77
8.64M
  cy = tp[2*rn] + mpn_sub_n (rp, tp, tp+rn, rn);
78
8.64M
  rp[rn] = 0;
79
8.64M
  MPN_INCR_U (rp, rn+1, cy);
80
8.64M
}
81
82
83
/* Computes {rp,MIN(rn,an+bn)} <- {ap,an}*{bp,bn} Mod(B^rn-1)
84
 *
85
 * The result is expected to be ZERO if and only if one of the operand
86
 * already is. Otherwise the class [0] Mod(B^rn-1) is represented by
87
 * B^rn-1. This should not be a problem if mulmod_bnm1 is used to
88
 * combine results and obtain a natural number when one knows in
89
 * advance that the final value is less than (B^rn-1).
90
 * Moreover it should not be a problem if mulmod_bnm1 is used to
91
 * compute the full product with an+bn <= rn, because this condition
92
 * implies (B^an-1)(B^bn-1) < (B^rn-1) .
93
 *
94
 * Requires 0 < bn <= an <= rn and an + bn > rn/2
95
 * Scratch need: rn + (need for recursive call OR rn + 4). This gives
96
 *
97
 * S(n) <= rn + MAX (rn + 4, S(n/2)) <= 2rn + 4
98
 */
99
void
100
mpn_mulmod_bnm1 (mp_ptr rp, mp_size_t rn, mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn, mp_ptr tp)
101
11.8M
{
102
11.8M
  ASSERT (0 < bn);
103
11.8M
  ASSERT (bn <= an);
104
11.8M
  ASSERT (an <= rn);
105
106
11.8M
  if ((rn & 1) != 0 || BELOW_THRESHOLD (rn, MULMOD_BNM1_THRESHOLD))
107
3.19M
    {
108
3.19M
      if (UNLIKELY (bn < rn))
109
0
  {
110
0
    if (UNLIKELY (an + bn <= rn))
111
0
      {
112
0
        mpn_mul (rp, ap, an, bp, bn);
113
0
      }
114
0
    else
115
0
      {
116
0
        mp_limb_t cy;
117
0
        mpn_mul (tp, ap, an, bp, bn);
118
0
        cy = mpn_add (rp, tp, rn, tp + rn, an + bn - rn);
119
0
        MPN_INCR_U (rp, rn, cy);
120
0
      }
121
0
  }
122
3.19M
      else
123
3.19M
  mpn_bc_mulmod_bnm1 (rp, ap, bp, rn, tp);
124
3.19M
    }
125
8.64M
  else
126
8.64M
    {
127
8.64M
      mp_size_t n;
128
8.64M
      mp_limb_t cy;
129
8.64M
      mp_limb_t hi;
130
131
8.64M
      n = rn >> 1;
132
133
      /* We need at least an + bn >= n, to be able to fit one of the
134
   recursive products at rp. Requiring strict inequality makes
135
   the code slightly simpler. If desired, we could avoid this
136
   restriction by initially halving rn as long as rn is even and
137
   an + bn <= rn/2. */
138
139
8.64M
      ASSERT (an + bn > n);
140
141
      /* Compute xm = a*b mod (B^n - 1), xp = a*b mod (B^n + 1)
142
   and crt together as
143
144
   x = -xp * B^n + (B^n + 1) * [ (xp + xm)/2 mod (B^n-1)]
145
      */
146
147
17.2M
#define a0 ap
148
17.2M
#define a1 (ap + n)
149
34.5M
#define b0 bp
150
17.2M
#define b1 (bp + n)
151
152
77.7M
#define xp  tp  /* 2n + 2 */
153
      /* am1  maybe in {xp, n} */
154
      /* bm1  maybe in {xp + n, n} */
155
51.8M
#define sp1 (tp + 2*n + 2)
156
      /* ap1  maybe in {sp1, n + 1} */
157
      /* bp1  maybe in {sp1 + n + 1, n + 1} */
158
159
8.64M
      {
160
8.64M
  mp_srcptr am1, bm1;
161
8.64M
  mp_size_t anm, bnm;
162
8.64M
  mp_ptr so;
163
164
8.64M
  bm1 = b0;
165
8.64M
  bnm = bn;
166
8.64M
  if (LIKELY (an > n))
167
8.64M
    {
168
8.64M
      am1 = xp;
169
8.64M
      cy = mpn_add (xp, a0, n, a1, an - n);
170
8.64M
      MPN_INCR_U (xp, n, cy);
171
8.64M
      anm = n;
172
8.64M
      so = xp + n;
173
8.64M
      if (LIKELY (bn > n))
174
8.64M
        {
175
8.64M
    bm1 = so;
176
8.64M
    cy = mpn_add (so, b0, n, b1, bn - n);
177
8.64M
    MPN_INCR_U (so, n, cy);
178
8.64M
    bnm = n;
179
8.64M
    so += n;
180
8.64M
        }
181
8.64M
    }
182
0
  else
183
0
    {
184
0
      so = xp;
185
0
      am1 = a0;
186
0
      anm = an;
187
0
    }
188
189
8.64M
  mpn_mulmod_bnm1 (rp, n, am1, anm, bm1, bnm, so);
190
8.64M
      }
191
192
0
      {
193
8.64M
  int       k;
194
8.64M
  mp_srcptr ap1, bp1;
195
8.64M
  mp_size_t anp, bnp;
196
197
8.64M
  bp1 = b0;
198
8.64M
  bnp = bn;
199
8.64M
  if (LIKELY (an > n)) {
200
8.64M
    ap1 = sp1;
201
8.64M
    cy = mpn_sub (sp1, a0, n, a1, an - n);
202
8.64M
    sp1[n] = 0;
203
8.64M
    MPN_INCR_U (sp1, n + 1, cy);
204
8.64M
    anp = n + ap1[n];
205
8.64M
    if (LIKELY (bn > n)) {
206
8.64M
      bp1 = sp1 + n + 1;
207
8.64M
      cy = mpn_sub (sp1 + n + 1, b0, n, b1, bn - n);
208
8.64M
      sp1[2*n+1] = 0;
209
8.64M
      MPN_INCR_U (sp1 + n + 1, n + 1, cy);
210
8.64M
      bnp = n + bp1[n];
211
8.64M
    }
212
8.64M
  } else {
213
0
    ap1 = a0;
214
0
    anp = an;
215
0
  }
216
217
8.64M
  if (BELOW_THRESHOLD (n, MUL_FFT_MODF_THRESHOLD))
218
8.64M
    k=0;
219
0
  else
220
0
    {
221
0
      int mask;
222
0
      k = mpn_fft_best_k (n, 0);
223
0
      mask = (1<<k) - 1;
224
0
      while (n & mask) {k--; mask >>=1;};
225
0
    }
226
8.64M
  if (k >= FFT_FIRST_K)
227
0
    xp[n] = mpn_mul_fft (xp, n, ap1, anp, bp1, bnp, k);
228
8.64M
  else if (UNLIKELY (bp1 == b0))
229
0
    {
230
0
      ASSERT (anp + bnp <= 2*n+1);
231
0
      ASSERT (anp + bnp > n);
232
0
      ASSERT (anp >= bnp);
233
0
      mpn_mul (xp, ap1, anp, bp1, bnp);
234
0
      anp = anp + bnp - n;
235
0
      ASSERT (anp <= n || xp[2*n]==0);
236
0
      anp-= anp > n;
237
0
      cy = mpn_sub (xp, xp, n, xp + n, anp);
238
0
      xp[n] = 0;
239
0
      MPN_INCR_U (xp, n+1, cy);
240
0
    }
241
8.64M
  else
242
8.64M
    mpn_bc_mulmod_bnp1 (xp, ap1, bp1, n, xp);
243
8.64M
      }
244
245
      /* Here the CRT recomposition begins.
246
247
   xm <- (xp + xm)/2 = (xp + xm)B^n/2 mod (B^n-1)
248
   Division by 2 is a bitwise rotation.
249
250
   Assumes xp normalised mod (B^n+1).
251
252
   The residue class [0] is represented by [B^n-1]; except when
253
   both input are ZERO.
254
      */
255
256
8.64M
#if HAVE_NATIVE_mpn_rsh1add_n || HAVE_NATIVE_mpn_rsh1add_nc
257
8.64M
#if HAVE_NATIVE_mpn_rsh1add_nc
258
8.64M
      cy = mpn_rsh1add_nc(rp, rp, xp, n, xp[n]); /* B^n = 1 */
259
8.64M
      hi = cy << (GMP_NUMB_BITS - 1);
260
8.64M
      cy = 0;
261
      /* next update of rp[n-1] will set cy = 1 only if rp[n-1]+=hi
262
   overflows, i.e. a further increment will not overflow again. */
263
#else /* ! _nc */
264
      cy = xp[n] + mpn_rsh1add_n(rp, rp, xp, n); /* B^n = 1 */
265
      hi = (cy<<(GMP_NUMB_BITS-1))&GMP_NUMB_MASK; /* (cy&1) << ... */
266
      cy >>= 1;
267
      /* cy = 1 only if xp[n] = 1 i.e. {xp,n} = ZERO, this implies that
268
   the rsh1add was a simple rshift: the top bit is 0. cy=1 => hi=0. */
269
#endif
270
8.64M
#if GMP_NAIL_BITS == 0
271
8.64M
      add_ssaaaa(cy, rp[n-1], cy, rp[n-1], 0, hi);
272
#else
273
      cy += (hi & rp[n-1]) >> (GMP_NUMB_BITS-1);
274
      rp[n-1] ^= hi;
275
#endif
276
#else /* ! HAVE_NATIVE_mpn_rsh1add_n */
277
#if HAVE_NATIVE_mpn_add_nc
278
      cy = mpn_add_nc(rp, rp, xp, n, xp[n]);
279
#else /* ! _nc */
280
      cy = xp[n] + mpn_add_n(rp, rp, xp, n); /* xp[n] == 1 implies {xp,n} == ZERO */
281
#endif
282
      cy += (rp[0]&1);
283
      mpn_rshift(rp, rp, n, 1);
284
      ASSERT (cy <= 2);
285
      hi = (cy<<(GMP_NUMB_BITS-1))&GMP_NUMB_MASK; /* (cy&1) << ... */
286
      cy >>= 1;
287
      /* We can have cy != 0 only if hi = 0... */
288
      ASSERT ((rp[n-1] & GMP_NUMB_HIGHBIT) == 0);
289
      rp[n-1] |= hi;
290
      /* ... rp[n-1] + cy can not overflow, the following INCR is correct. */
291
#endif
292
8.64M
      ASSERT (cy <= 1);
293
      /* Next increment can not overflow, read the previous comments about cy. */
294
8.64M
      ASSERT ((cy == 0) || ((rp[n-1] & GMP_NUMB_HIGHBIT) == 0));
295
8.64M
      MPN_INCR_U(rp, n, cy);
296
297
      /* Compute the highest half:
298
   ([(xp + xm)/2 mod (B^n-1)] - xp ) * B^n
299
       */
300
8.64M
      if (UNLIKELY (an + bn < rn))
301
0
  {
302
    /* Note that in this case, the only way the result can equal
303
       zero mod B^{rn} - 1 is if one of the inputs is zero, and
304
       then the output of both the recursive calls and this CRT
305
       reconstruction is zero, not B^{rn} - 1. Which is good,
306
       since the latter representation doesn't fit in the output
307
       area.*/
308
0
    cy = mpn_sub_n (rp + n, rp, xp, an + bn - n);
309
310
    /* FIXME: This subtraction of the high parts is not really
311
       necessary, we do it to get the carry out, and for sanity
312
       checking. */
313
0
    cy = xp[n] + mpn_sub_nc (xp + an + bn - n, rp + an + bn - n,
314
0
           xp + an + bn - n, rn - (an + bn), cy);
315
0
    ASSERT (an + bn == rn - 1 ||
316
0
      mpn_zero_p (xp + an + bn - n + 1, rn - 1 - (an + bn)));
317
0
    cy = mpn_sub_1 (rp, rp, an + bn, cy);
318
0
    ASSERT (cy == (xp + an + bn - n)[0]);
319
0
  }
320
8.64M
      else
321
8.64M
  {
322
8.64M
    cy = xp[n] + mpn_sub_n (rp + n, rp, xp, n);
323
    /* cy = 1 only if {xp,n+1} is not ZERO, i.e. {rp,n} is not ZERO.
324
       DECR will affect _at most_ the lowest n limbs. */
325
8.64M
    MPN_DECR_U (rp, 2*n, cy);
326
8.64M
  }
327
8.64M
#undef a0
328
8.64M
#undef a1
329
8.64M
#undef b0
330
8.64M
#undef b1
331
8.64M
#undef xp
332
8.64M
#undef sp1
333
8.64M
    }
334
11.8M
}
335
336
mp_size_t
337
mpn_mulmod_bnm1_next_size (mp_size_t n)
338
3.21M
{
339
3.21M
  mp_size_t nh;
340
341
3.21M
  if (BELOW_THRESHOLD (n,     MULMOD_BNM1_THRESHOLD))
342
18.8k
    return n;
343
3.19M
  if (BELOW_THRESHOLD (n, 4 * (MULMOD_BNM1_THRESHOLD - 1) + 1))
344
241
    return (n + (2-1)) & (-2);
345
3.19M
  if (BELOW_THRESHOLD (n, 8 * (MULMOD_BNM1_THRESHOLD - 1) + 1))
346
2.84M
    return (n + (4-1)) & (-4);
347
348
348k
  nh = (n + 1) >> 1;
349
350
348k
  if (BELOW_THRESHOLD (nh, MUL_FFT_MODF_THRESHOLD))
351
348k
    return (n + (8-1)) & (-8);
352
353
0
  return 2 * mpn_fft_next_size (nh, mpn_fft_best_k (nh, 0));
354
348k
}