summaryrefslogtreecommitdiffstats
path: root/contrib/libgmp/mpn/generic/gcdext.c
blob: 245e20a4d528435246d04005d8b4fe47d45c4b45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* mpn_gcdext -- Extended Greatest Common Divisor.

Copyright (C) 1996 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
License for more details.

You should have received a copy of the GNU Library General Public License
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include "gmp.h"
#include "gmp-impl.h"
#include "longlong.h"

#ifndef EXTEND
#define EXTEND 1
#endif

#if STAT
int arr[BITS_PER_MP_LIMB];
#endif

#define SGN(A) (((A) < 0) ? -1 : ((A) > 0))

/* Idea 1: After we have performed a full division, don't shift operands back,
	   but instead account for the extra factors-of-2 thus introduced.
   Idea 2: Simple generalization to use divide-and-conquer would give us an
	   algorithm that runs faster than O(n^2).
   Idea 3: The input numbers need less space as the computation progresses,
	   while the s0 and s1 variables need more space.  To save space, we
	   could make them share space, and have the latter variables grow
	   into the former.  */

/* Precondition: U >= V.  */

mp_size_t
#if EXTEND
#if __STDC__
mpn_gcdext (mp_ptr gp, mp_ptr s0p,
	    mp_ptr up, mp_size_t size, mp_ptr vp, mp_size_t vsize)
#else
mpn_gcdext (gp, s0p, up, size, vp, vsize)
     mp_ptr gp;
     mp_ptr s0p;
     mp_ptr up;
     mp_size_t size;
     mp_ptr vp;
     mp_size_t vsize;
#endif
#else
#if __STDC__
mpn_gcd (mp_ptr gp,
	 mp_ptr up, mp_size_t size, mp_ptr vp, mp_size_t vsize)
#else
mpn_gcd (gp, up, size, vp, vsize)
     mp_ptr gp;
     mp_ptr up;
     mp_size_t size;
     mp_ptr vp;
     mp_size_t vsize;
#endif
#endif
{
  mp_limb_t uh, vh;
  mp_limb_signed_t A, B, C, D;
  int cnt;
  mp_ptr tp, wp;
#if RECORD
  mp_limb_signed_t min = 0, max = 0;
#endif
#if EXTEND
  mp_ptr s1p;
  mp_ptr orig_s0p = s0p;
  mp_size_t ssize, orig_size = size;
  TMP_DECL (mark);

  TMP_MARK (mark);

  tp = (mp_ptr) TMP_ALLOC ((size + 1) * BYTES_PER_MP_LIMB);
  wp = (mp_ptr) TMP_ALLOC ((size + 1) * BYTES_PER_MP_LIMB);
  s1p = (mp_ptr) TMP_ALLOC (size * BYTES_PER_MP_LIMB);

  MPN_ZERO (s0p, size);
  MPN_ZERO (s1p, size);

  s0p[0] = 1;
  s1p[0] = 0;
  ssize = 1;
#endif

  if (size > vsize)
    {
      /* Normalize V (and shift up U the same amount).  */
      count_leading_zeros (cnt, vp[vsize - 1]);
      if (cnt != 0)
	{
	  mp_limb_t cy;
	  mpn_lshift (vp, vp, vsize, cnt);
	  cy = mpn_lshift (up, up, size, cnt);
	  up[size] = cy;
	  size += cy != 0;
	}

      mpn_divmod (up + vsize, up, size, vp, vsize);
#if EXTEND
      /* This is really what it boils down to in this case... */
      s0p[0] = 0;
      s1p[0] = 1;
#endif
      size = vsize;
      if (cnt != 0)
	{
	  mpn_rshift (up, up, size, cnt);
	  mpn_rshift (vp, vp, size, cnt);
	}
      {
	mp_ptr xp;
	xp = up; up = vp; vp = xp;
      }
    }

  for (;;)
    {
      /* Figure out exact size of V.  */
      vsize = size;
      MPN_NORMALIZE (vp, vsize);
      if (vsize <= 1)
	break;

      /* Make UH be the most significant limb of U, and make VH be
	 corresponding bits from V.  */
      uh = up[size - 1];
      vh = vp[size - 1];
      count_leading_zeros (cnt, uh);
      if (cnt != 0)
	{
	  uh = (uh << cnt) | (up[size - 2] >> (BITS_PER_MP_LIMB - cnt));
	  vh = (vh << cnt) | (vp[size - 2] >> (BITS_PER_MP_LIMB - cnt));
	}

#if 0
      /* For now, only handle BITS_PER_MP_LIMB-1 bits.  This makes
	 room for sign bit.  */
      uh >>= 1;
      vh >>= 1;
#endif
      A = 1;
      B = 0;
      C = 0;
      D = 1;

      for (;;)
	{
	  mp_limb_signed_t q, T;
	  if (vh + C == 0 || vh + D == 0)
	    break;

	  q = (uh + A) / (vh + C);
	  if (q != (uh + B) / (vh + D))
	    break;

	  T = A - q * C;
	  A = C;
	  C = T;
	  T = B - q * D;
	  B = D;
	  D = T;
	  T = uh - q * vh;
	  uh = vh;
	  vh = T;
	}

#if RECORD
      min = MIN (A, min);  min = MIN (B, min);
      min = MIN (C, min);  min = MIN (D, min);
      max = MAX (A, max);  max = MAX (B, max);
      max = MAX (C, max);  max = MAX (D, max);
#endif

      if (B == 0)
	{
	  mp_limb_t qh;
	  mp_size_t i;

	  /* This is quite rare.  I.e., optimize something else!  */

	  /* Normalize V (and shift up U the same amount).  */
	  count_leading_zeros (cnt, vp[vsize - 1]);
	  if (cnt != 0)
	    {
	      mp_limb_t cy;
	      mpn_lshift (vp, vp, vsize, cnt);
	      cy = mpn_lshift (up, up, size, cnt);
	      up[size] = cy;
	      size += cy != 0;
	    }

	  qh = mpn_divmod (up + vsize, up, size, vp, vsize);
#if EXTEND
	  MPN_COPY (tp, s0p, ssize);
	  for (i = 0; i < size - vsize; i++)
	    {
	      mp_limb_t cy;
	      cy = mpn_addmul_1 (tp + i, s1p, ssize, up[vsize + i]);
	      if (cy != 0)
		tp[ssize++] = cy;
	    }
	  if (qh != 0)
	    {
	      mp_limb_t cy;
	      abort ();
	      /* XXX since qh == 1, mpn_addmul_1 is overkill */
	      cy = mpn_addmul_1 (tp + size - vsize, s1p, ssize, qh);
	      if (cy != 0)
		tp[ssize++] = cy;
      	    }
#if 0
	  MPN_COPY (s0p, s1p, ssize); /* should be old ssize, kind of */
	  MPN_COPY (s1p, tp, ssize);
#else
	  {
	    mp_ptr xp;
	    xp = s0p; s0p = s1p; s1p = xp;
	    xp = s1p; s1p = tp; tp = xp;
	  }
#endif
#endif
	  size = vsize;
	  if (cnt != 0)
	    {
	      mpn_rshift (up, up, size, cnt);
	      mpn_rshift (vp, vp, size, cnt);
	    }

	  {
	    mp_ptr xp;
	    xp = up; up = vp; vp = xp;
	  }
	  MPN_NORMALIZE (up, size);
	}
      else
	{
	  /* T = U*A + V*B
	     W = U*C + V*D
	     U = T
	     V = W	   */

	  if (SGN(A) == SGN(B))	/* should be different sign */
	    abort ();
	  if (SGN(C) == SGN(D))	/* should be different sign */
	    abort ();
#if STAT
	  { mp_limb_t x;
	    x = ABS (A) | ABS (B) | ABS (C) | ABS (D);
	    count_leading_zeros (cnt, x);
	    arr[BITS_PER_MP_LIMB - cnt]++; }
#endif
	  if (A == 0)
	    {
	      if (B != 1) abort ();
	      MPN_COPY (tp, vp, size);
	    }
	  else
	    {
	      if (A < 0)
		{
		  mpn_mul_1 (tp, vp, size, B);
		  mpn_submul_1 (tp, up, size, -A);
		}
	      else
		{
		  mpn_mul_1 (tp, up, size, A);
		  mpn_submul_1 (tp, vp, size, -B);
		}
	    }
	  if (C < 0)
	    {
	      mpn_mul_1 (wp, vp, size, D);
	      mpn_submul_1 (wp, up, size, -C);
	    }
	  else
	    {
	      mpn_mul_1 (wp, up, size, C);
	      mpn_submul_1 (wp, vp, size, -D);
	    }

	  {
	    mp_ptr xp;
	    xp = tp; tp = up; up = xp;
	    xp = wp; wp = vp; vp = xp;
	  }

#if EXTEND
	  { mp_limb_t cy;
	  MPN_ZERO (tp, orig_size);
	  if (A == 0)
	    {
	      if (B != 1) abort ();
	      MPN_COPY (tp, s1p, ssize);
	    }
	  else
	    {
	      if (A < 0)
		{
		  cy = mpn_mul_1 (tp, s1p, ssize, B);
		  cy += mpn_addmul_1 (tp, s0p, ssize, -A);
		}
	      else
		{
		  cy = mpn_mul_1 (tp, s0p, ssize, A);
		  cy += mpn_addmul_1 (tp, s1p, ssize, -B);
		}
	      if (cy != 0)
		tp[ssize++] = cy;
	    }
	  MPN_ZERO (wp, orig_size);
	  if (C < 0)
	    {
	      cy = mpn_mul_1 (wp, s1p, ssize, D);
	      cy += mpn_addmul_1 (wp, s0p, ssize, -C);
	    }
	  else
	    {
	      cy = mpn_mul_1 (wp, s0p, ssize, C);
	      cy += mpn_addmul_1 (wp, s1p, ssize, -D);
	    }
	  if (cy != 0)
	    wp[ssize++] = cy;
	  }
	  {
	    mp_ptr xp;
	    xp = tp; tp = s0p; s0p = xp;
	    xp = wp; wp = s1p; s1p = xp;
	  }
#endif
#if 0	/* Is it a win to remove multiple zeros here? */
	  MPN_NORMALIZE (up, size);
#else
	  if (up[size - 1] == 0)
	    size--;
#endif
	}
    }

#if RECORD
  printf ("min: %ld\n", min);
  printf ("max: %ld\n", max);
#endif

  if (vsize == 0)
    {
      if (gp != up)
	MPN_COPY (gp, up, size);
#if EXTEND
      if (orig_s0p != s0p)
	MPN_COPY (orig_s0p, s0p, ssize);
#endif
      TMP_FREE (mark);
      return size;
    }
  else
    {
      mp_limb_t vl, ul, t;
#if EXTEND
      mp_limb_t cy;
      mp_size_t i;
#endif
      vl = vp[0];
#if EXTEND
      t = mpn_divmod_1 (wp, up, size, vl);
      MPN_COPY (tp, s0p, ssize);
      for (i = 0; i < size; i++)
	{
	  cy = mpn_addmul_1 (tp + i, s1p, ssize, wp[i]);
	  if (cy != 0)
	    tp[ssize++] = cy;
	}
#if 0
      MPN_COPY (s0p, s1p, ssize);
      MPN_COPY (s1p, tp, ssize);
#else
      {
	mp_ptr xp;
	xp = s0p; s0p = s1p; s1p = xp;
	xp = s1p; s1p = tp; tp = xp;
      }
#endif
#else
      t = mpn_mod_1 (up, size, vl);
#endif
      ul = vl;
      vl = t;
      while (vl != 0)
	{
	  mp_limb_t t;
#if EXTEND
	  mp_limb_t q, cy;
	  q = ul / vl;
	  t = ul - q*vl;

	  MPN_COPY (tp, s0p, ssize);
	  cy = mpn_addmul_1 (tp, s1p, ssize, q);
	  if (cy != 0)
	    tp[ssize++] = cy;
#if 0
	  MPN_COPY (s0p, s1p, ssize);
	  MPN_COPY (s1p, tp, ssize);
#else
	  {
	    mp_ptr xp;
	    xp = s0p; s0p = s1p; s1p = xp;
	    xp = s1p; s1p = tp; tp = xp;
	  }
#endif

#else
	  t = ul % vl;
#endif
	  ul = vl;
	  vl = t;
	}
      gp[0] = ul;
#if EXTEND
      if (orig_s0p != s0p)
	MPN_COPY (orig_s0p, s0p, ssize);
#endif
      TMP_FREE (mark);
      return 1;
    }
}
OpenPOWER on IntegriCloud