summaryrefslogtreecommitdiff
path: root/gmp/mpn/generic/gcdext_1.c
blob: f1dd9ee9630976792f0b7383fe271a70e08f7116 (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
/* mpn_gcdext -- Extended Greatest Common Divisor.

Copyright 1996, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009 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 Lesser General Public License as published by
the Free Software Foundation; either version 3 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 Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */

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


/* FIXME: Takes two single-word limbs. It could be extended to a
 * function that accepts a bignum for the first input, and only
 * returns the first co-factor. */

mp_limb_t
mpn_gcdext_1 (mp_limb_signed_t *up, mp_limb_signed_t *vp,
	      mp_limb_t a, mp_limb_t b)
{
  /* Maintain

     a =  u0 A + v0 B
     b =  u1 A + v1 B

     where A, B are the original inputs.
  */
  mp_limb_signed_t u0 = 1;
  mp_limb_signed_t v0 = 0;
  mp_limb_signed_t u1 = 0;
  mp_limb_signed_t v1 = 1;

  ASSERT (a > 0);
  ASSERT (b > 0);

  if (a < b)
    goto divide_by_b;

  for (;;)
    {
      mp_limb_t q;

      q = a / b;
      a -= q * b;

      if (a == 0)
	{
	  *up = u1;
	  *vp = v1;
	  return b;
	}
      u0 -= q * u1;
      v0 -= q * v1;

    divide_by_b:
      q = b / a;
      b -= q * a;

      if (b == 0)
	{
	  *up = u0;
	  *vp = v0;
	  return a;
	}
      u1 -= q * u0;
      v1 -= q * v0;
    }
}