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 #ifndef GCD_1_METHOD 25 #define GCD_1_METHOD 2 26 #endif 27 28 #define USE_ZEROTAB 0 29 30 #if USE_ZEROTAB 31 static const unsigned char zerotab[16] = { 32 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 33 }; 34 #endif 35 36 /* Does not work for U == 0 or V == 0. It would be tough to make it work for 37 V == 0 since gcd(x,0) = x, and U does not generally fit in an mp_limb_t. 38 39 The threshold for doing u%v when size==1 will vary by CPU according to 40 the speed of a division and the code generated for the main loop. Any 41 tuning for this is left to a CPU specific implementation. */ 42 43 mp_limb_t 44 mpn_gcd_1 (mp_srcptr up, mp_size_t size, mp_limb_t vlimb) 45 { 46 mp_limb_t ulimb; 47 unsigned long zero_bits, u_low_zero_bits; 48 49 ASSERT (size >= 1); 50 ASSERT (vlimb != 0); 51 ASSERT_MPN_NONZERO_P (up, size); 52 53 ulimb = up[0]; 54 55 /* Need vlimb odd for modexact, want it odd to get common zeros. */ 56 count_trailing_zeros (zero_bits, vlimb); 57 vlimb >>= zero_bits; 58 59 if (size > 1) 60 { 61 /* Must get common zeros before the mod reduction. If ulimb==0 then 62 vlimb already gives the common zeros. */ 63 if (ulimb != 0) 64 { 65 count_trailing_zeros (u_low_zero_bits, ulimb); 66 zero_bits = MIN (zero_bits, u_low_zero_bits); 67 } 68 69 ulimb = MPN_MOD_OR_MODEXACT_1_ODD (up, size, vlimb); 70 if (ulimb == 0) 71 goto done; 72 73 goto strip_u_maybe; 74 } 75 76 /* size==1, so up[0]!=0 */ 77 count_trailing_zeros (u_low_zero_bits, ulimb); 78 ulimb >>= u_low_zero_bits; 79 zero_bits = MIN (zero_bits, u_low_zero_bits); 80 81 /* make u bigger */ 82 if (vlimb > ulimb) 83 MP_LIMB_T_SWAP (ulimb, vlimb); 84 85 /* if u is much bigger than v, reduce using a division rather than 86 chipping away at it bit-by-bit */ 87 if ((ulimb >> 16) > vlimb) 88 { 89 ulimb %= vlimb; 90 if (ulimb == 0) 91 goto done; 92 goto strip_u_maybe; 93 } 94 95 ASSERT (ulimb & 1); 96 ASSERT (vlimb & 1); 97 98 #if GCD_1_METHOD == 1 99 while (ulimb != vlimb) 100 { 101 ASSERT (ulimb & 1); 102 ASSERT (vlimb & 1); 103 104 if (ulimb > vlimb) 105 { 106 ulimb -= vlimb; 107 do 108 { 109 ulimb >>= 1; 110 ASSERT (ulimb != 0); 111 strip_u_maybe: 112 ; 113 } 114 while ((ulimb & 1) == 0); 115 } 116 else /* vlimb > ulimb. */ 117 { 118 vlimb -= ulimb; 119 do 120 { 121 vlimb >>= 1; 122 ASSERT (vlimb != 0); 123 } 124 while ((vlimb & 1) == 0); 125 } 126 } 127 #else 128 # if GCD_1_METHOD == 2 129 130 ulimb >>= 1; 131 vlimb >>= 1; 132 133 while (ulimb != vlimb) 134 { 135 int c; 136 mp_limb_t t = ulimb - vlimb; 137 mp_limb_t vgtu = LIMB_HIGHBIT_TO_MASK (t); 138 139 /* v <-- min (u, v) */ 140 vlimb += (vgtu & t); 141 142 /* u <-- |u - v| */ 143 ulimb = (t ^ vgtu) - vgtu; 144 145 #if USE_ZEROTAB 146 /* Number of trailing zeros is the same no matter if we look at 147 * t or ulimb, but using t gives more parallelism. */ 148 c = zerotab[t & 15]; 149 150 while (UNLIKELY (c == 4)) 151 { 152 ulimb >>= 4; 153 if (0) 154 strip_u_maybe: 155 vlimb >>= 1; 156 157 c = zerotab[ulimb & 15]; 158 } 159 #else 160 if (0) 161 { 162 strip_u_maybe: 163 vlimb >>= 1; 164 t = ulimb; 165 } 166 count_trailing_zeros (c, t); 167 #endif 168 ulimb >>= (c + 1); 169 } 170 171 vlimb = (vlimb << 1) | 1; 172 # else 173 # error Unknown GCD_1_METHOD 174 # endif 175 #endif 176 177 done: 178 return vlimb << zero_bits; 179 } 180