Coverage Report

Created: 2026-02-09 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gmp/mpn/hgcd2.c
Line
Count
Source
1
/* hgcd2.c
2
3
   THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES.  IT IS ONLY
4
   SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
5
   GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
6
7
Copyright 1996, 1998, 2000-2004, 2008, 2012, 2019 Free Software Foundation,
8
Inc.
9
10
This file is part of the GNU MP Library.
11
12
The GNU MP Library is free software; you can redistribute it and/or modify
13
it under the terms of either:
14
15
  * the GNU Lesser General Public License as published by the Free
16
    Software Foundation; either version 3 of the License, or (at your
17
    option) any later version.
18
19
or
20
21
  * the GNU General Public License as published by the Free Software
22
    Foundation; either version 2 of the License, or (at your option) any
23
    later version.
24
25
or both in parallel, as here.
26
27
The GNU MP Library is distributed in the hope that it will be useful, but
28
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
29
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
30
for more details.
31
32
You should have received copies of the GNU General Public License and the
33
GNU Lesser General Public License along with the GNU MP Library.  If not,
34
see https://www.gnu.org/licenses/.  */
35
36
#include "gmp-impl.h"
37
#include "longlong.h"
38
39
#include "mpn/generic/hgcd2-div.h"
40
41
#if GMP_NAIL_BITS != 0
42
#error Nails not implemented
43
#endif
44
45
/* Reduces a,b until |a-b| (almost) fits in one limb + 1 bit. Constructs
46
   matrix M. Returns 1 if we make progress, i.e. can perform at least
47
   one subtraction. Otherwise returns zero. */
48
49
/* FIXME: Possible optimizations:
50
51
   The div2 function starts with checking the most significant bit of
52
   the numerator. We can maintained normalized operands here, call
53
   hgcd with normalized operands only, which should make the code
54
   simpler and possibly faster.
55
56
   Experiment with table lookups on the most significant bits.
57
58
   This function is also a candidate for assembler implementation.
59
*/
60
int
61
mpn_hgcd2 (mp_limb_t ah, mp_limb_t al, mp_limb_t bh, mp_limb_t bl,
62
     struct hgcd_matrix1 *M)
63
591k
{
64
591k
  mp_limb_t u00, u01, u10, u11;
65
66
591k
  if (ah < 2 || bh < 2)
67
1.61k
    return 0;
68
69
589k
  if (ah > bh || (ah == bh && al > bl))
70
333k
    {
71
333k
      sub_ddmmss (ah, al, ah, al, bh, bl);
72
333k
      if (ah < 2)
73
2.99k
  return 0;
74
75
330k
      u00 = u01 = u11 = 1;
76
330k
      u10 = 0;
77
330k
    }
78
256k
  else
79
256k
    {
80
256k
      sub_ddmmss (bh, bl, bh, bl, ah, al);
81
256k
      if (bh < 2)
82
3.76k
  return 0;
83
84
252k
      u00 = u10 = u11 = 1;
85
252k
      u01 = 0;
86
252k
    }
87
88
583k
  if (ah < bh)
89
334k
    goto subtract_a;
90
91
248k
  for (;;)
92
5.53M
    {
93
5.53M
      ASSERT (ah >= bh);
94
5.53M
      if (ah == bh)
95
680
  goto done;
96
97
5.53M
      if (ah < (CNST_LIMB(1) << (GMP_LIMB_BITS / 2)))
98
314k
  {
99
314k
    ah = (ah << (GMP_LIMB_BITS / 2) ) + (al >> (GMP_LIMB_BITS / 2));
100
314k
    bh = (bh << (GMP_LIMB_BITS / 2) ) + (bl >> (GMP_LIMB_BITS / 2));
101
102
314k
    break;
103
314k
  }
104
105
      /* Subtract a -= q b, and multiply M from the right by (1 q ; 0
106
   1), affecting the second column of M. */
107
5.22M
      ASSERT (ah > bh);
108
5.22M
      sub_ddmmss (ah, al, ah, al, bh, bl);
109
110
5.22M
      if (ah < 2)
111
777
  goto done;
112
113
5.22M
      if (ah <= bh)
114
2.07M
  {
115
    /* Use q = 1 */
116
2.07M
    u01 += u00;
117
2.07M
    u11 += u10;
118
2.07M
  }
119
3.14M
      else
120
3.14M
  {
121
3.14M
    mp_limb_t r[2];
122
3.14M
    mp_limb_t q = div2 (r, ah, al, bh, bl);
123
3.14M
    al = r[0]; ah = r[1];
124
3.14M
    if (ah < 2)
125
1.61k
      {
126
        /* A is too small, but q is correct. */
127
1.61k
        u01 += q * u00;
128
1.61k
        u11 += q * u10;
129
1.61k
        goto done;
130
1.61k
      }
131
3.13M
    q++;
132
3.13M
    u01 += q * u00;
133
3.13M
    u11 += q * u10;
134
3.13M
  }
135
5.55M
    subtract_a:
136
5.55M
      ASSERT (bh >= ah);
137
5.55M
      if (ah == bh)
138
678
  goto done;
139
140
5.55M
      if (bh < (CNST_LIMB(1) << (GMP_LIMB_BITS / 2)))
141
262k
  {
142
262k
    ah = (ah << (GMP_LIMB_BITS / 2) ) + (al >> (GMP_LIMB_BITS / 2));
143
262k
    bh = (bh << (GMP_LIMB_BITS / 2) ) + (bl >> (GMP_LIMB_BITS / 2));
144
145
262k
    goto subtract_a1;
146
262k
  }
147
148
      /* Subtract b -= q a, and multiply M from the right by (1 0 ; q
149
   1), affecting the first column of M. */
150
5.29M
      sub_ddmmss (bh, bl, bh, bl, ah, al);
151
152
5.29M
      if (bh < 2)
153
635
  goto done;
154
155
5.28M
      if (bh <= ah)
156
2.08M
  {
157
    /* Use q = 1 */
158
2.08M
    u00 += u01;
159
2.08M
    u10 += u11;
160
2.08M
  }
161
3.20M
      else
162
3.20M
  {
163
3.20M
    mp_limb_t r[2];
164
3.20M
    mp_limb_t q = div2 (r, bh, bl, ah, al);
165
3.20M
    bl = r[0]; bh = r[1];
166
3.20M
    if (bh < 2)
167
1.57k
      {
168
        /* B is too small, but q is correct. */
169
1.57k
        u00 += q * u01;
170
1.57k
        u10 += q * u11;
171
1.57k
        goto done;
172
1.57k
      }
173
3.20M
    q++;
174
3.20M
    u00 += q * u01;
175
3.20M
    u10 += q * u11;
176
3.20M
  }
177
5.28M
    }
178
179
  /* NOTE: Since we discard the least significant half limb, we don't get a
180
     truly maximal M (corresponding to |a - b| < 2^{GMP_LIMB_BITS +1}). */
181
  /* Single precision loop */
182
314k
  for (;;)
183
5.04M
    {
184
5.04M
      ASSERT (ah >= bh);
185
186
5.04M
      ah -= bh;
187
5.04M
      if (ah < (CNST_LIMB (1) << (GMP_LIMB_BITS / 2 + 1)))
188
175k
  break;
189
190
4.86M
      if (ah <= bh)
191
2.18M
  {
192
    /* Use q = 1 */
193
2.18M
    u01 += u00;
194
2.18M
    u11 += u10;
195
2.18M
  }
196
2.67M
      else
197
2.67M
  {
198
2.67M
    mp_double_limb_t rq = div1 (ah, bh);
199
2.67M
    mp_limb_t q = rq.d1;
200
2.67M
    ah = rq.d0;
201
202
2.67M
    if (ah < (CNST_LIMB(1) << (GMP_LIMB_BITS / 2 + 1)))
203
145k
      {
204
        /* A is too small, but q is correct. */
205
145k
        u01 += q * u00;
206
145k
        u11 += q * u10;
207
145k
        break;
208
145k
      }
209
2.53M
    q++;
210
2.53M
    u01 += q * u00;
211
2.53M
    u11 += q * u10;
212
2.53M
  }
213
4.98M
    subtract_a1:
214
4.98M
      ASSERT (bh >= ah);
215
216
4.98M
      bh -= ah;
217
4.98M
      if (bh < (CNST_LIMB (1) << (GMP_LIMB_BITS / 2 + 1)))
218
110k
  break;
219
220
4.87M
      if (bh <= ah)
221
1.91M
  {
222
    /* Use q = 1 */
223
1.91M
    u00 += u01;
224
1.91M
    u10 += u11;
225
1.91M
  }
226
2.96M
      else
227
2.96M
  {
228
2.96M
    mp_double_limb_t rq = div1 (bh, ah);
229
2.96M
    mp_limb_t q = rq.d1;
230
2.96M
    bh = rq.d0;
231
232
2.96M
    if (bh < (CNST_LIMB(1) << (GMP_LIMB_BITS / 2 + 1)))
233
145k
      {
234
        /* B is too small, but q is correct. */
235
145k
        u00 += q * u01;
236
145k
        u10 += q * u11;
237
145k
        break;
238
145k
      }
239
2.81M
    q++;
240
2.81M
    u00 += q * u01;
241
2.81M
    u10 += q * u11;
242
2.81M
  }
243
4.87M
    }
244
245
583k
 done:
246
583k
  M->u[0][0] = u00; M->u[0][1] = u01;
247
583k
  M->u[1][0] = u10; M->u[1][1] = u11;
248
249
583k
  return 1;
250
314k
}
251
252
/* Sets (r;b) = (a;b) M, with M = (u00, u01; u10, u11). Vector must
253
 * have space for n + 1 limbs. Uses three buffers to avoid a copy*/
254
mp_size_t
255
mpn_hgcd_mul_matrix1_vector (const struct hgcd_matrix1 *M,
256
           mp_ptr rp, mp_srcptr ap, mp_ptr bp, mp_size_t n)
257
583k
{
258
583k
  mp_limb_t ah, bh;
259
260
  /* Compute (r,b) <-- (u00 a + u10 b, u01 a + u11 b) as
261
262
     r  = u00 * a
263
     r += u10 * b
264
     b *= u11
265
     b += u01 * a
266
  */
267
268
#if HAVE_NATIVE_mpn_addaddmul_1msb0
269
  ah = mpn_addaddmul_1msb0 (rp, ap, bp, n, M->u[0][0], M->u[1][0]);
270
  bh = mpn_addaddmul_1msb0 (bp, bp, ap, n, M->u[1][1], M->u[0][1]);
271
#else
272
583k
  ah =     mpn_mul_1 (rp, ap, n, M->u[0][0]);
273
583k
  ah += mpn_addmul_1 (rp, bp, n, M->u[1][0]);
274
275
583k
  bh =     mpn_mul_1 (bp, bp, n, M->u[1][1]);
276
583k
  bh += mpn_addmul_1 (bp, ap, n, M->u[0][1]);
277
583k
#endif
278
583k
  rp[n] = ah;
279
583k
  bp[n] = bh;
280
281
583k
  n += (ah | bh) > 0;
282
583k
  return n;
283
583k
}