1 /* mpn_gcd_1 -- mpn and limb greatest common divisor.
3 Copyright 1994, 1996, 2000, 2001, 2009, 2012 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
36 #define GCD_1_METHOD 2
43 #define MASK ((1 << MAXSHIFT) - 1)
44 static const unsigned char zerotab
[1 << MAXSHIFT
] =
47 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
49 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
53 /* Does not work for U == 0 or V == 0. It would be tough to make it work for
54 V == 0 since gcd(x,0) = x, and U does not generally fit in an mp_limb_t.
56 The threshold for doing u%v when size==1 will vary by CPU according to
57 the speed of a division and the code generated for the main loop. Any
58 tuning for this is left to a CPU specific implementation. */
61 mpn_gcd_1 (mp_srcptr up
, mp_size_t size
, mp_limb_t vlimb
)
64 unsigned long zero_bits
, u_low_zero_bits
;
68 ASSERT_MPN_NONZERO_P (up
, size
);
72 /* Need vlimb odd for modexact, want it odd to get common zeros. */
73 count_trailing_zeros (zero_bits
, vlimb
);
78 /* Must get common zeros before the mod reduction. If ulimb==0 then
79 vlimb already gives the common zeros. */
82 count_trailing_zeros (u_low_zero_bits
, ulimb
);
83 zero_bits
= MIN (zero_bits
, u_low_zero_bits
);
86 ulimb
= MPN_MOD_OR_MODEXACT_1_ODD (up
, size
, vlimb
);
93 /* size==1, so up[0]!=0 */
94 count_trailing_zeros (u_low_zero_bits
, ulimb
);
95 ulimb
>>= u_low_zero_bits
;
96 zero_bits
= MIN (zero_bits
, u_low_zero_bits
);
100 MP_LIMB_T_SWAP (ulimb
, vlimb
);
102 /* if u is much bigger than v, reduce using a division rather than
103 chipping away at it bit-by-bit */
104 if ((ulimb
>> 16) > vlimb
)
115 #if GCD_1_METHOD == 1
116 while (ulimb
!= vlimb
)
131 while ((ulimb
& 1) == 0);
133 else /* vlimb > ulimb. */
141 while ((vlimb
& 1) == 0);
145 # if GCD_1_METHOD == 2
150 while (ulimb
!= vlimb
)
157 vgtu
= LIMB_HIGHBIT_TO_MASK (t
);
159 /* v <-- min (u, v) */
163 ulimb
= (t
^ vgtu
) - vgtu
;
166 /* Number of trailing zeros is the same no matter if we look at
167 * t or ulimb, but using t gives more parallelism. */
168 c
= zerotab
[t
& MASK
];
170 while (UNLIKELY (c
== MAXSHIFT
))
177 c
= zerotab
[ulimb
& MASK
];
186 count_trailing_zeros (c
, t
);
191 vlimb
= (vlimb
<< 1) | 1;
193 # error Unknown GCD_1_METHOD
198 return vlimb
<< zero_bits
;