Tizen 2.1 base
[external/gmp.git] / mpn / generic / gcd_lehmer.c
1 /* gcd_lehmer.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 2003, 2004, 2005, 2008 Free Software Foundation, Inc.
8
9 This file is part of the GNU MP Library.
10
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
15
16 The GNU MP Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19 License for more details.
20
21 You should have received a copy of the GNU Lesser General Public License
22 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
23
24 #include "gmp.h"
25 #include "gmp-impl.h"
26 #include "longlong.h"
27
28 /* Use binary algorithm to compute G <-- GCD (U, V) for usize, vsize == 2.
29    Both U and V must be odd. */
30 static inline mp_size_t
31 gcd_2 (mp_ptr gp, mp_srcptr up, mp_srcptr vp)
32 {
33   mp_limb_t u0, u1, v0, v1;
34   mp_size_t gn;
35
36   u0 = up[0];
37   u1 = up[1];
38   v0 = vp[0];
39   v1 = vp[1];
40
41   ASSERT (u0 & 1);
42   ASSERT (v0 & 1);
43
44   /* Check for u0 != v0 needed to ensure that argument to
45    * count_trailing_zeros is non-zero. */
46   while (u1 != v1 && u0 != v0)
47     {
48       unsigned long int r;
49       if (u1 > v1)
50         {
51           u1 -= v1 + (u0 < v0);
52           u0 = (u0 - v0) & GMP_NUMB_MASK;
53           count_trailing_zeros (r, u0);
54           u0 = ((u1 << (GMP_NUMB_BITS - r)) & GMP_NUMB_MASK) | (u0 >> r);
55           u1 >>= r;
56         }
57       else  /* u1 < v1.  */
58         {
59           v1 -= u1 + (v0 < u0);
60           v0 = (v0 - u0) & GMP_NUMB_MASK;
61           count_trailing_zeros (r, v0);
62           v0 = ((v1 << (GMP_NUMB_BITS - r)) & GMP_NUMB_MASK) | (v0 >> r);
63           v1 >>= r;
64         }
65     }
66
67   gp[0] = u0, gp[1] = u1, gn = 1 + (u1 != 0);
68
69   /* If U == V == GCD, done.  Otherwise, compute GCD (V, |U - V|).  */
70   if (u1 == v1 && u0 == v0)
71     return gn;
72
73   v0 = (u0 == v0) ? ((u1 > v1) ? u1-v1 : v1-u1) : ((u0 > v0) ? u0-v0 : v0-u0);
74   gp[0] = mpn_gcd_1 (gp, gn, v0);
75
76   return 1;
77 }
78
79 /* Temporary storage: n */
80 mp_size_t
81 mpn_gcd_lehmer_n (mp_ptr gp, mp_ptr ap, mp_ptr bp, mp_size_t n, mp_ptr tp)
82 {
83   /* Relax this requirement, and normalize at the start? Must disallow
84      A = B = 0, though. */
85   ASSERT(ap[n-1] > 0 || bp[n-1] > 0);
86
87   while (n > 2)
88     {
89       struct hgcd_matrix1 M;
90       mp_limb_t ah, al, bh, bl;
91       mp_limb_t mask;
92
93       mask = ap[n-1] | bp[n-1];
94       ASSERT (mask > 0);
95
96       if (mask & GMP_NUMB_HIGHBIT)
97         {
98           ah = ap[n-1]; al = ap[n-2];
99           bh = bp[n-1]; bl = bp[n-2];
100         }
101       else
102         {
103           int shift;
104
105           count_leading_zeros (shift, mask);
106           ah = MPN_EXTRACT_NUMB (shift, ap[n-1], ap[n-2]);
107           al = MPN_EXTRACT_NUMB (shift, ap[n-2], ap[n-3]);
108           bh = MPN_EXTRACT_NUMB (shift, bp[n-1], bp[n-2]);
109           bl = MPN_EXTRACT_NUMB (shift, bp[n-2], bp[n-3]);
110         }
111
112       /* Try an mpn_nhgcd2 step */
113       if (mpn_hgcd2 (ah, al, bh, bl, &M))
114         {
115           n = mpn_hgcd_mul_matrix1_inverse_vector (&M, tp, ap, bp, n);
116           MP_PTR_SWAP (ap, tp);
117         }
118       else
119         {
120           /* mpn_hgcd2 has failed. Then either one of a or b is very
121              small, or the difference is very small. Perform one
122              subtraction followed by one division. */
123           mp_size_t gn;
124
125           /* Temporary storage n */
126           n = mpn_gcd_subdiv_step (gp, &gn, ap, bp, n, tp);
127           if (n == 0)
128             return gn;
129         }
130     }
131
132   if (n == 1)
133     {
134       *gp = mpn_gcd_1(ap, 1, bp[0]);
135       return 1;
136     }
137
138   /* Due to the calling convention for mpn_gcd, at most one can be
139      even. */
140
141   if (! (ap[0] & 1))
142     MP_PTR_SWAP (ap, bp);
143
144   ASSERT (ap[0] & 1);
145
146   if (bp[0] == 0)
147     {
148       *gp = mpn_gcd_1 (ap, 2, bp[1]);
149       return 1;
150     }
151   else if (! (bp[0] & 1))
152     {
153       int r;
154       count_trailing_zeros (r, bp[0]);
155       bp[0] = ((bp[1] << (GMP_NUMB_BITS - r)) & GMP_NUMB_MASK) | (bp[0] >> r);
156       bp[1] >>= r;
157     }
158
159   return gcd_2(gp, ap, bp);
160 }