Coverage Report

Created: 2026-02-14 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gmp/mpn/toom42_mul.c
Line
Count
Source
1
/* mpn_toom42_mul -- Multiply {ap,an} and {bp,bn} where an is nominally twice
2
   as large as bn.  Or more accurately, (3/2)bn < an < 4bn.
3
4
   Contributed to the GNU project by Torbjorn Granlund.
5
   Additional improvements by Marco Bodrato.
6
7
   The idea of applying toom to unbalanced multiplication is due to Marco
8
   Bodrato and Alberto Zanoni.
9
10
   THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE.  IT IS ONLY
11
   SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
12
   GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
13
14
Copyright 2006-2008, 2012, 2014 Free Software Foundation, Inc.
15
16
This file is part of the GNU MP Library.
17
18
The GNU MP Library is free software; you can redistribute it and/or modify
19
it under the terms of either:
20
21
  * the GNU Lesser General Public License as published by the Free
22
    Software Foundation; either version 3 of the License, or (at your
23
    option) any later version.
24
25
or
26
27
  * the GNU General Public License as published by the Free Software
28
    Foundation; either version 2 of the License, or (at your option) any
29
    later version.
30
31
or both in parallel, as here.
32
33
The GNU MP Library is distributed in the hope that it will be useful, but
34
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
35
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
36
for more details.
37
38
You should have received copies of the GNU General Public License and the
39
GNU Lesser General Public License along with the GNU MP Library.  If not,
40
see https://www.gnu.org/licenses/.  */
41
42
43
#include "gmp-impl.h"
44
45
/* Evaluate in: -1, 0, +1, +2, +inf
46
47
  <-s-><--n--><--n--><--n-->
48
   ___ ______ ______ ______
49
  |a3_|___a2_|___a1_|___a0_|
50
         |_b1_|___b0_|
51
         <-t--><--n-->
52
53
  v0  =  a0             * b0      #   A(0)*B(0)
54
  v1  = (a0+ a1+ a2+ a3)*(b0+ b1) #   A(1)*B(1)      ah  <= 3  bh <= 1
55
  vm1 = (a0- a1+ a2- a3)*(b0- b1) #  A(-1)*B(-1)    |ah| <= 1  bh  = 0
56
  v2  = (a0+2a1+4a2+8a3)*(b0+2b1) #   A(2)*B(2)      ah  <= 14 bh <= 2
57
  vinf=              a3 *     b1  # A(inf)*B(inf)
58
*/
59
60
#define TOOM42_MUL_N_REC(p, a, b, n, ws)        \
61
11.4k
  do {                 \
62
11.4k
    mpn_mul_n (p, a, b, n);            \
63
11.4k
  } while (0)
64
65
void
66
mpn_toom42_mul (mp_ptr pp,
67
    mp_srcptr ap, mp_size_t an,
68
    mp_srcptr bp, mp_size_t bn,
69
    mp_ptr scratch)
70
2.86k
{
71
2.86k
  mp_size_t n, s, t;
72
2.86k
  int vm1_neg;
73
2.86k
  mp_limb_t cy, vinf0;
74
2.86k
  mp_ptr a0_a2;
75
2.86k
  mp_ptr as1, asm1, as2;
76
2.86k
  mp_ptr bs1, bsm1, bs2;
77
2.86k
  mp_ptr tmp;
78
2.86k
  TMP_DECL;
79
80
2.86k
#define a0  ap
81
2.86k
#define a1  (ap + n)
82
4.95k
#define a2  (ap + 2*n)
83
5.72k
#define a3  (ap + 3*n)
84
9.18k
#define b0  bp
85
13.0k
#define b1  (bp + n)
86
87
2.86k
  n = an >= 2 * bn ? (an + 3) >> 2 : (bn + 1) >> 1;
88
89
2.86k
  s = an - 3 * n;
90
2.86k
  t = bn - n;
91
92
2.86k
  ASSERT (0 < s && s <= n);
93
2.86k
  ASSERT (0 < t && t <= n);
94
95
2.86k
  TMP_MARK;
96
97
2.86k
  tmp = TMP_ALLOC_LIMBS (6 * n + 5);
98
2.86k
  as1  = tmp; tmp += n + 1;
99
2.86k
  asm1 = tmp; tmp += n + 1;
100
2.86k
  as2  = tmp; tmp += n + 1;
101
2.86k
  bs1  = tmp; tmp += n + 1;
102
2.86k
  bsm1 = tmp; tmp += n;
103
2.86k
  bs2  = tmp; tmp += n + 1;
104
105
2.86k
  a0_a2 = pp;
106
107
  /* Compute as1 and asm1.  */
108
2.86k
  vm1_neg = mpn_toom_eval_dgr3_pm1 (as1, asm1, ap, n, s, a0_a2) & 1;
109
110
  /* Compute as2.  */
111
#if HAVE_NATIVE_mpn_addlsh1_n
112
  cy  = mpn_addlsh1_n (as2, a2, a3, s);
113
  if (s != n)
114
    cy = mpn_add_1 (as2 + s, a2 + s, n - s, cy);
115
  cy = 2 * cy + mpn_addlsh1_n (as2, a1, as2, n);
116
  cy = 2 * cy + mpn_addlsh1_n (as2, a0, as2, n);
117
#else
118
2.86k
  cy  = mpn_lshift (as2, a3, s, 1);
119
2.86k
  cy += mpn_add_n (as2, a2, as2, s);
120
2.86k
  if (s != n)
121
2.09k
    cy = mpn_add_1 (as2 + s, a2 + s, n - s, cy);
122
2.86k
  cy = 2 * cy + mpn_lshift (as2, as2, n, 1);
123
2.86k
  cy += mpn_add_n (as2, a1, as2, n);
124
2.86k
  cy = 2 * cy + mpn_lshift (as2, as2, n, 1);
125
2.86k
  cy += mpn_add_n (as2, a0, as2, n);
126
2.86k
#endif
127
2.86k
  as2[n] = cy;
128
129
  /* Compute bs1 and bsm1.  */
130
2.86k
  if (t == n)
131
989
    {
132
#if HAVE_NATIVE_mpn_add_n_sub_n
133
      if (mpn_cmp (b0, b1, n) < 0)
134
  {
135
    cy = mpn_add_n_sub_n (bs1, bsm1, b1, b0, n);
136
    vm1_neg ^= 1;
137
  }
138
      else
139
  {
140
    cy = mpn_add_n_sub_n (bs1, bsm1, b0, b1, n);
141
  }
142
      bs1[n] = cy >> 1;
143
#else
144
989
      bs1[n] = mpn_add_n (bs1, b0, b1, n);
145
146
989
      if (mpn_cmp (b0, b1, n) < 0)
147
291
  {
148
291
    mpn_sub_n (bsm1, b1, b0, n);
149
291
    vm1_neg ^= 1;
150
291
  }
151
698
      else
152
698
  {
153
698
    mpn_sub_n (bsm1, b0, b1, n);
154
698
  }
155
989
#endif
156
989
    }
157
1.87k
  else
158
1.87k
    {
159
1.87k
      bs1[n] = mpn_add (bs1, b0, n, b1, t);
160
161
1.87k
      if (mpn_zero_p (b0 + t, n - t) && mpn_cmp (b0, b1, t) < 0)
162
342
  {
163
342
    mpn_sub_n (bsm1, b1, b0, t);
164
342
    MPN_ZERO (bsm1 + t, n - t);
165
342
    vm1_neg ^= 1;
166
342
  }
167
1.53k
      else
168
1.53k
  {
169
1.53k
    mpn_sub (bsm1, b0, n, b1, t);
170
1.53k
  }
171
1.87k
    }
172
173
  /* Compute bs2, recycling bs1. bs2=bs1+b1  */
174
2.86k
  mpn_add (bs2, bs1, n + 1, b1, t);
175
176
2.86k
  ASSERT (as1[n] <= 3);
177
2.86k
  ASSERT (bs1[n] <= 1);
178
2.86k
  ASSERT (asm1[n] <= 1);
179
/*ASSERT (bsm1[n] == 0);*/
180
2.86k
  ASSERT (as2[n] <= 14);
181
2.86k
  ASSERT (bs2[n] <= 2);
182
183
2.86k
#define v0    pp        /* 2n */
184
5.11k
#define v1    (pp + 2 * n)      /* 2n+1 */
185
5.72k
#define vinf  (pp + 4 * n)      /* s+t */
186
6.06k
#define vm1   scratch        /* 2n+1 */
187
2.86k
#define v2    (scratch + 2 * n + 1)    /* 2n+2 */
188
2.86k
#define scratch_out scratch + 4 * n + 4 /* Currently unused. */
189
190
  /* vm1, 2n+1 limbs */
191
2.86k
  TOOM42_MUL_N_REC (vm1, asm1, bsm1, n, scratch_out);
192
2.86k
  cy = 0;
193
2.86k
  if (asm1[n] != 0)
194
170
    cy = mpn_add_n (vm1 + n, vm1 + n, bsm1, n);
195
2.86k
  vm1[2 * n] = cy;
196
197
2.86k
  TOOM42_MUL_N_REC (v2, as2, bs2, n + 1, scratch_out);  /* v2, 2n+1 limbs */
198
199
  /* vinf, s+t limbs */
200
2.86k
  if (s > t)  mpn_mul (vinf, a3, s, b1, t);
201
2.05k
  else        mpn_mul (vinf, b1, t, a3, s);
202
203
2.86k
  vinf0 = vinf[0];        /* v1 overlaps with this */
204
205
  /* v1, 2n+1 limbs */
206
2.86k
  TOOM42_MUL_N_REC (v1, as1, bs1, n, scratch_out);
207
2.86k
  if (as1[n] == 1)
208
495
    {
209
495
      cy = mpn_add_n (v1 + n, v1 + n, bs1, n);
210
495
    }
211
2.36k
  else if (as1[n] == 2)
212
361
    {
213
#if HAVE_NATIVE_mpn_addlsh1_n_ip1
214
      cy = mpn_addlsh1_n_ip1 (v1 + n, bs1, n);
215
#else
216
361
      cy = mpn_addmul_1 (v1 + n, bs1, n, CNST_LIMB(2));
217
361
#endif
218
361
    }
219
2.00k
  else if (as1[n] == 3)
220
211
    {
221
211
      cy = mpn_addmul_1 (v1 + n, bs1, n, CNST_LIMB(3));
222
211
    }
223
1.79k
  else
224
1.79k
    cy = 0;
225
2.86k
  if (bs1[n] != 0)
226
344
    cy += as1[n] + mpn_add_n (v1 + n, v1 + n, as1, n);
227
2.86k
  v1[2 * n] = cy;
228
229
2.86k
  TOOM42_MUL_N_REC (v0, ap, bp, n, scratch_out);  /* v0, 2n limbs */
230
231
2.86k
  mpn_toom_interpolate_5pts (pp, v2, vm1, n, s + t, vm1_neg, vinf0);
232
233
2.86k
  TMP_FREE;
234
2.86k
}