xref: /dragonfly/contrib/gmp/mpn/generic/gcd_1.c (revision 73e0051e)
1 /* mpn_gcd_1 -- mpn and limb greatest common divisor.
2 
3 Copyright 1994, 1996, 2000, 2001 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library.
6 
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15 License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
19 
20 #include "gmp.h"
21 #include "gmp-impl.h"
22 #include "longlong.h"
23 
24 
25 /* Does not work for U == 0 or V == 0.  It would be tough to make it work for
26    V == 0 since gcd(x,0) = x, and U does not generally fit in an mp_limb_t.
27 
28    The threshold for doing u%v when size==1 will vary by CPU according to
29    the speed of a division and the code generated for the main loop.  Any
30    tuning for this is left to a CPU specific implementation.  */
31 
32 mp_limb_t
33 mpn_gcd_1 (mp_srcptr up, mp_size_t size, mp_limb_t vlimb)
34 {
35   mp_limb_t      ulimb;
36   unsigned long  zero_bits, u_low_zero_bits;
37 
38   ASSERT (size >= 1);
39   ASSERT (vlimb != 0);
40   ASSERT_MPN_NONZERO_P (up, size);
41 
42   ulimb = up[0];
43 
44   /* Need vlimb odd for modexact, want it odd to get common zeros. */
45   count_trailing_zeros (zero_bits, vlimb);
46   vlimb >>= zero_bits;
47 
48   if (size > 1)
49     {
50       /* Must get common zeros before the mod reduction.  If ulimb==0 then
51 	 vlimb already gives the common zeros.  */
52       if (ulimb != 0)
53 	{
54 	  count_trailing_zeros (u_low_zero_bits, ulimb);
55 	  zero_bits = MIN (zero_bits, u_low_zero_bits);
56 	}
57 
58       ulimb = MPN_MOD_OR_MODEXACT_1_ODD (up, size, vlimb);
59       if (ulimb == 0)
60 	goto done;
61 
62       goto strip_u_maybe;
63     }
64 
65   /* size==1, so up[0]!=0 */
66   count_trailing_zeros (u_low_zero_bits, ulimb);
67   ulimb >>= u_low_zero_bits;
68   zero_bits = MIN (zero_bits, u_low_zero_bits);
69 
70   /* make u bigger */
71   if (vlimb > ulimb)
72     MP_LIMB_T_SWAP (ulimb, vlimb);
73 
74   /* if u is much bigger than v, reduce using a division rather than
75      chipping away at it bit-by-bit */
76   if ((ulimb >> 16) > vlimb)
77     {
78       ulimb %= vlimb;
79       if (ulimb == 0)
80 	goto done;
81       goto strip_u_maybe;
82     }
83 
84   while (ulimb != vlimb)
85     {
86       ASSERT (ulimb & 1);
87       ASSERT (vlimb & 1);
88 
89       if (ulimb > vlimb)
90 	{
91 	  ulimb -= vlimb;
92 	  do
93 	    {
94 	      ulimb >>= 1;
95 	      ASSERT (ulimb != 0);
96 	    strip_u_maybe:
97 	      ;
98 	    }
99 	  while ((ulimb & 1) == 0);
100 	}
101       else /*  vlimb > ulimb.  */
102 	{
103 	  vlimb -= ulimb;
104 	  do
105 	    {
106 	      vlimb >>= 1;
107 	      ASSERT (vlimb != 0);
108 	    }
109 	  while ((vlimb & 1) == 0);
110 	}
111     }
112 
113  done:
114   return vlimb << zero_bits;
115 }
116