summaryrefslogtreecommitdiff
path: root/mpn/generic/gcd_subdiv_step.c
blob: 3db34073c93d2c5b51ece4a50666c1d33596f74f (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
/* gcd_subdiv_step.c.

   THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES.  IT IS ONLY
   SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
   GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.

Copyright 2003, 2004, 2005, 2008, 2010, 2011 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 <stdlib.h>		/* for NULL */

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

/* Used when mpn_hgcd or mpn_hgcd2 has failed. Then either one of a or
   b is small, or the difference is small. Perform one subtraction
   followed by one division. The normal case is to compute the reduced
   a and b, and return the new size.

   If s == 0 (used for gcd and gcdext), returns zero if the gcd is
   found.

   If s > 0, don't reduce to size <= s, and return zero if no
   reduction is possible (if either a, b or |a-b| is of size <= s). */

/* The hook function is called as

     hook(ctx, gp, gn, qp, qn, d)

   in the following cases:

   + If A = B at the start, G is the gcd, Q is NULL, d = -1.

   + If one input is zero at the start, G is the gcd, Q is NULL,
     d = 0 if A = G and d = 1 if B = G.

   Otherwise, if d = 0 we have just subtracted a multiple of A from B,
   and if d = 1 we have subtracted a multiple of B from A.

   + If A = B after subtraction, G is the gcd, Q is NULL.

   + If we get a zero remainder after division, G is the gcd, Q is the
     quotient.

   + Otherwise, G is NULL, Q is the quotient (often 1).

 */

mp_size_t
mpn_gcd_subdiv_step (mp_ptr ap, mp_ptr bp, mp_size_t n, mp_size_t s,
		     gcd_subdiv_step_hook *hook, void *ctx,
		     mp_ptr tp)
{
  static const mp_limb_t one = CNST_LIMB(1);
  mp_size_t an, bn, qn;

  int swapped;

  ASSERT (n > 0);
  ASSERT (ap[n-1] > 0 || bp[n-1] > 0);

  an = bn = n;
  MPN_NORMALIZE (ap, an);
  MPN_NORMALIZE (bp, bn);

  swapped = 0;

  /* Arrange so that a < b, subtract b -= a, and maintain
     normalization. */
  if (an == bn)
    {
      int c;
      MPN_CMP (c, ap, bp, an);
      if (UNLIKELY (c == 0))
	{
	  /* For gcdext, return the smallest of the two cofactors, so
	     pass d = -1. */
	  if (s == 0)
	    hook (ctx, ap, an, NULL, 0, -1);
	  return 0;
	}
      else if (c > 0)
	{
	  MP_PTR_SWAP (ap, bp);
	  swapped ^= 1;
	}
    }
  else
    {
      if (an > bn)
	{
	  MPN_PTR_SWAP (ap, an, bp, bn);
	  swapped ^= 1;
	}
    }
  if (an <= s)
    {
      if (s == 0)
	hook (ctx, bp, bn, NULL, 0, swapped ^ 1);
      return 0;
    }

  ASSERT_NOCARRY (mpn_sub (bp, bp, bn, ap, an));
  MPN_NORMALIZE (bp, bn);
  ASSERT (bn > 0);

  if (bn <= s)
    {
      /* Undo subtraction. */
      mp_limb_t cy = mpn_add (bp, ap, an, bp, bn);
      if (cy > 0)
	bp[an] = cy;
      return 0;
    }

  /* Arrange so that a < b */
  if (an == bn)
    {
      int c;
      MPN_CMP (c, ap, bp, an);
      if (UNLIKELY (c == 0))
	{
	  if (s > 0)
	    /* Just record subtraction and return */
	    hook (ctx, NULL, 0, &one, 1, swapped);
	  else
	    /* Found gcd. */
	    hook (ctx, bp, bn, NULL, 0, swapped);
	  return 0;
	}

      hook (ctx, NULL, 0, &one, 1, swapped);

      if (c > 0)
	{
	  MP_PTR_SWAP (ap, bp);
	  swapped ^= 1;
	}
    }
  else
    {
      hook (ctx, NULL, 0, &one, 1, swapped);

      if (an > bn)
	{
	  MPN_PTR_SWAP (ap, an, bp, bn);
	  swapped ^= 1;
	}
    }

  mpn_tdiv_qr (tp, bp, 0, bp, bn, ap, an);
  qn = bn - an + 1;
  bn = an;
  MPN_NORMALIZE (bp, bn);

  if (UNLIKELY (bn <= s))
    {
      if (s == 0)
	{
	  hook (ctx, ap, an, tp, qn, swapped);
	  return 0;
	}

      /* Quotient is one too large, so decrement it and add back A. */
      if (bn > 0)
	{
	  mp_limb_t cy = mpn_add (bp, ap, an, bp, bn);
	  if (cy)
	    bp[an++] = cy;
	}
      else
	MPN_COPY (bp, ap, an);

      MPN_DECR_U (tp, qn, 1);
    }

  hook (ctx, NULL, 0, tp, qn, swapped);
  return an;
}