diff options
author | David Walter Seikel | 2011-06-27 15:59:06 +1000 |
---|---|---|
committer | David Walter Seikel | 2011-06-27 15:59:06 +1000 |
commit | 92bd9b2503e7dd927f416cb9b17f169ea925fa56 (patch) | |
tree | f0f3776cfc0c6e6224db66f5678387c580095f16 /linden/indra/libgcrypt/libgcrypt-1.2.2/mpi/mpi-div.c | |
parent | Make use of the new packaging stuff from upstream. (diff) | |
download | meta-impy-92bd9b2503e7dd927f416cb9b17f169ea925fa56.zip meta-impy-92bd9b2503e7dd927f416cb9b17f169ea925fa56.tar.gz meta-impy-92bd9b2503e7dd927f416cb9b17f169ea925fa56.tar.bz2 meta-impy-92bd9b2503e7dd927f416cb9b17f169ea925fa56.tar.xz |
Clean up line endings, execute bits, and other bits of errant nonsense from Windows.
Diffstat (limited to 'linden/indra/libgcrypt/libgcrypt-1.2.2/mpi/mpi-div.c')
-rw-r--r--[-rwxr-xr-x] | linden/indra/libgcrypt/libgcrypt-1.2.2/mpi/mpi-div.c | 728 |
1 files changed, 364 insertions, 364 deletions
diff --git a/linden/indra/libgcrypt/libgcrypt-1.2.2/mpi/mpi-div.c b/linden/indra/libgcrypt/libgcrypt-1.2.2/mpi/mpi-div.c index a8bcab4..6172eb4 100755..100644 --- a/linden/indra/libgcrypt/libgcrypt-1.2.2/mpi/mpi-div.c +++ b/linden/indra/libgcrypt/libgcrypt-1.2.2/mpi/mpi-div.c | |||
@@ -1,364 +1,364 @@ | |||
1 | /* mpi-div.c - MPI functions | 1 | /* mpi-div.c - MPI functions |
2 | * Copyright (C) 1994, 1996, 1998, 2001, 2002, | 2 | * Copyright (C) 1994, 1996, 1998, 2001, 2002, |
3 | * 2003 Free Software Foundation, Inc. | 3 | * 2003 Free Software Foundation, Inc. |
4 | * | 4 | * |
5 | * This file is part of Libgcrypt. | 5 | * This file is part of Libgcrypt. |
6 | * | 6 | * |
7 | * Libgcrypt is free software; you can redistribute it and/or modify | 7 | * Libgcrypt is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU Lesser General Public License as | 8 | * it under the terms of the GNU Lesser General Public License as |
9 | * published by the Free Software Foundation; either version 2.1 of | 9 | * published by the Free Software Foundation; either version 2.1 of |
10 | * the License, or (at your option) any later version. | 10 | * the License, or (at your option) any later version. |
11 | * | 11 | * |
12 | * Libgcrypt is distributed in the hope that it will be useful, | 12 | * Libgcrypt is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU Lesser General Public License for more details. | 15 | * GNU Lesser General Public License for more details. |
16 | * | 16 | * |
17 | * You should have received a copy of the GNU Lesser General Public | 17 | * You should have received a copy of the GNU Lesser General Public |
18 | * License along with this program; if not, write to the Free Software | 18 | * License along with this program; if not, write to the Free Software |
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
20 | * | 20 | * |
21 | * Note: This code is heavily based on the GNU MP Library. | 21 | * Note: This code is heavily based on the GNU MP Library. |
22 | * Actually it's the same code with only minor changes in the | 22 | * Actually it's the same code with only minor changes in the |
23 | * way the data is stored; this is to support the abstraction | 23 | * way the data is stored; this is to support the abstraction |
24 | * of an optional secure memory allocation which may be used | 24 | * of an optional secure memory allocation which may be used |
25 | * to avoid revealing of sensitive data due to paging etc. | 25 | * to avoid revealing of sensitive data due to paging etc. |
26 | */ | 26 | */ |
27 | 27 | ||
28 | #include <config.h> | 28 | #include <config.h> |
29 | #include <stdio.h> | 29 | #include <stdio.h> |
30 | #include <stdlib.h> | 30 | #include <stdlib.h> |
31 | #include "mpi-internal.h" | 31 | #include "mpi-internal.h" |
32 | #include "longlong.h" | 32 | #include "longlong.h" |
33 | #include "g10lib.h" | 33 | #include "g10lib.h" |
34 | 34 | ||
35 | 35 | ||
36 | void | 36 | void |
37 | _gcry_mpi_fdiv_r( gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor ) | 37 | _gcry_mpi_fdiv_r( gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor ) |
38 | { | 38 | { |
39 | int divisor_sign = divisor->sign; | 39 | int divisor_sign = divisor->sign; |
40 | gcry_mpi_t temp_divisor = NULL; | 40 | gcry_mpi_t temp_divisor = NULL; |
41 | 41 | ||
42 | /* We need the original value of the divisor after the remainder has been | 42 | /* We need the original value of the divisor after the remainder has been |
43 | * preliminary calculated. We have to copy it to temporary space if it's | 43 | * preliminary calculated. We have to copy it to temporary space if it's |
44 | * the same variable as REM. */ | 44 | * the same variable as REM. */ |
45 | if( rem == divisor ) { | 45 | if( rem == divisor ) { |
46 | temp_divisor = mpi_copy( divisor ); | 46 | temp_divisor = mpi_copy( divisor ); |
47 | divisor = temp_divisor; | 47 | divisor = temp_divisor; |
48 | } | 48 | } |
49 | 49 | ||
50 | _gcry_mpi_tdiv_r( rem, dividend, divisor ); | 50 | _gcry_mpi_tdiv_r( rem, dividend, divisor ); |
51 | 51 | ||
52 | if( ((divisor_sign?1:0) ^ (dividend->sign?1:0)) && rem->nlimbs ) | 52 | if( ((divisor_sign?1:0) ^ (dividend->sign?1:0)) && rem->nlimbs ) |
53 | gcry_mpi_add( rem, rem, divisor); | 53 | gcry_mpi_add( rem, rem, divisor); |
54 | 54 | ||
55 | if( temp_divisor ) | 55 | if( temp_divisor ) |
56 | mpi_free(temp_divisor); | 56 | mpi_free(temp_divisor); |
57 | } | 57 | } |
58 | 58 | ||
59 | 59 | ||
60 | 60 | ||
61 | /**************** | 61 | /**************** |
62 | * Division rounding the quotient towards -infinity. | 62 | * Division rounding the quotient towards -infinity. |
63 | * The remainder gets the same sign as the denominator. | 63 | * The remainder gets the same sign as the denominator. |
64 | * rem is optional | 64 | * rem is optional |
65 | */ | 65 | */ |
66 | 66 | ||
67 | ulong | 67 | ulong |
68 | _gcry_mpi_fdiv_r_ui( gcry_mpi_t rem, gcry_mpi_t dividend, ulong divisor ) | 68 | _gcry_mpi_fdiv_r_ui( gcry_mpi_t rem, gcry_mpi_t dividend, ulong divisor ) |
69 | { | 69 | { |
70 | mpi_limb_t rlimb; | 70 | mpi_limb_t rlimb; |
71 | 71 | ||
72 | rlimb = _gcry_mpih_mod_1( dividend->d, dividend->nlimbs, divisor ); | 72 | rlimb = _gcry_mpih_mod_1( dividend->d, dividend->nlimbs, divisor ); |
73 | if( rlimb && dividend->sign ) | 73 | if( rlimb && dividend->sign ) |
74 | rlimb = divisor - rlimb; | 74 | rlimb = divisor - rlimb; |
75 | 75 | ||
76 | if( rem ) { | 76 | if( rem ) { |
77 | rem->d[0] = rlimb; | 77 | rem->d[0] = rlimb; |
78 | rem->nlimbs = rlimb? 1:0; | 78 | rem->nlimbs = rlimb? 1:0; |
79 | } | 79 | } |
80 | return rlimb; | 80 | return rlimb; |
81 | } | 81 | } |
82 | 82 | ||
83 | 83 | ||
84 | void | 84 | void |
85 | _gcry_mpi_fdiv_q( gcry_mpi_t quot, gcry_mpi_t dividend, gcry_mpi_t divisor ) | 85 | _gcry_mpi_fdiv_q( gcry_mpi_t quot, gcry_mpi_t dividend, gcry_mpi_t divisor ) |
86 | { | 86 | { |
87 | gcry_mpi_t tmp = mpi_alloc( mpi_get_nlimbs(quot) ); | 87 | gcry_mpi_t tmp = mpi_alloc( mpi_get_nlimbs(quot) ); |
88 | _gcry_mpi_fdiv_qr( quot, tmp, dividend, divisor); | 88 | _gcry_mpi_fdiv_qr( quot, tmp, dividend, divisor); |
89 | mpi_free(tmp); | 89 | mpi_free(tmp); |
90 | } | 90 | } |
91 | 91 | ||
92 | void | 92 | void |
93 | _gcry_mpi_fdiv_qr( gcry_mpi_t quot, gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor ) | 93 | _gcry_mpi_fdiv_qr( gcry_mpi_t quot, gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor ) |
94 | { | 94 | { |
95 | int divisor_sign = divisor->sign; | 95 | int divisor_sign = divisor->sign; |
96 | gcry_mpi_t temp_divisor = NULL; | 96 | gcry_mpi_t temp_divisor = NULL; |
97 | 97 | ||
98 | if( quot == divisor || rem == divisor ) { | 98 | if( quot == divisor || rem == divisor ) { |
99 | temp_divisor = mpi_copy( divisor ); | 99 | temp_divisor = mpi_copy( divisor ); |
100 | divisor = temp_divisor; | 100 | divisor = temp_divisor; |
101 | } | 101 | } |
102 | 102 | ||
103 | _gcry_mpi_tdiv_qr( quot, rem, dividend, divisor ); | 103 | _gcry_mpi_tdiv_qr( quot, rem, dividend, divisor ); |
104 | 104 | ||
105 | if( (divisor_sign ^ dividend->sign) && rem->nlimbs ) { | 105 | if( (divisor_sign ^ dividend->sign) && rem->nlimbs ) { |
106 | gcry_mpi_sub_ui( quot, quot, 1 ); | 106 | gcry_mpi_sub_ui( quot, quot, 1 ); |
107 | gcry_mpi_add( rem, rem, divisor); | 107 | gcry_mpi_add( rem, rem, divisor); |
108 | } | 108 | } |
109 | 109 | ||
110 | if( temp_divisor ) | 110 | if( temp_divisor ) |
111 | mpi_free(temp_divisor); | 111 | mpi_free(temp_divisor); |
112 | } | 112 | } |
113 | 113 | ||
114 | 114 | ||
115 | /* If den == quot, den needs temporary storage. | 115 | /* If den == quot, den needs temporary storage. |
116 | * If den == rem, den needs temporary storage. | 116 | * If den == rem, den needs temporary storage. |
117 | * If num == quot, num needs temporary storage. | 117 | * If num == quot, num needs temporary storage. |
118 | * If den has temporary storage, it can be normalized while being copied, | 118 | * If den has temporary storage, it can be normalized while being copied, |
119 | * i.e no extra storage should be allocated. | 119 | * i.e no extra storage should be allocated. |
120 | */ | 120 | */ |
121 | 121 | ||
122 | void | 122 | void |
123 | _gcry_mpi_tdiv_r( gcry_mpi_t rem, gcry_mpi_t num, gcry_mpi_t den) | 123 | _gcry_mpi_tdiv_r( gcry_mpi_t rem, gcry_mpi_t num, gcry_mpi_t den) |
124 | { | 124 | { |
125 | _gcry_mpi_tdiv_qr(NULL, rem, num, den ); | 125 | _gcry_mpi_tdiv_qr(NULL, rem, num, den ); |
126 | } | 126 | } |
127 | 127 | ||
128 | void | 128 | void |
129 | _gcry_mpi_tdiv_qr( gcry_mpi_t quot, gcry_mpi_t rem, gcry_mpi_t num, gcry_mpi_t den) | 129 | _gcry_mpi_tdiv_qr( gcry_mpi_t quot, gcry_mpi_t rem, gcry_mpi_t num, gcry_mpi_t den) |
130 | { | 130 | { |
131 | mpi_ptr_t np, dp; | 131 | mpi_ptr_t np, dp; |
132 | mpi_ptr_t qp, rp; | 132 | mpi_ptr_t qp, rp; |
133 | mpi_size_t nsize = num->nlimbs; | 133 | mpi_size_t nsize = num->nlimbs; |
134 | mpi_size_t dsize = den->nlimbs; | 134 | mpi_size_t dsize = den->nlimbs; |
135 | mpi_size_t qsize, rsize; | 135 | mpi_size_t qsize, rsize; |
136 | mpi_size_t sign_remainder = num->sign; | 136 | mpi_size_t sign_remainder = num->sign; |
137 | mpi_size_t sign_quotient = num->sign ^ den->sign; | 137 | mpi_size_t sign_quotient = num->sign ^ den->sign; |
138 | unsigned normalization_steps; | 138 | unsigned normalization_steps; |
139 | mpi_limb_t q_limb; | 139 | mpi_limb_t q_limb; |
140 | mpi_ptr_t marker[5]; | 140 | mpi_ptr_t marker[5]; |
141 | unsigned int marker_nlimbs[5]; | 141 | unsigned int marker_nlimbs[5]; |
142 | int markidx=0; | 142 | int markidx=0; |
143 | 143 | ||
144 | /* Ensure space is enough for quotient and remainder. | 144 | /* Ensure space is enough for quotient and remainder. |
145 | * We need space for an extra limb in the remainder, because it's | 145 | * We need space for an extra limb in the remainder, because it's |
146 | * up-shifted (normalized) below. */ | 146 | * up-shifted (normalized) below. */ |
147 | rsize = nsize + 1; | 147 | rsize = nsize + 1; |
148 | mpi_resize( rem, rsize); | 148 | mpi_resize( rem, rsize); |
149 | 149 | ||
150 | qsize = rsize - dsize; /* qsize cannot be bigger than this. */ | 150 | qsize = rsize - dsize; /* qsize cannot be bigger than this. */ |
151 | if( qsize <= 0 ) { | 151 | if( qsize <= 0 ) { |
152 | if( num != rem ) { | 152 | if( num != rem ) { |
153 | rem->nlimbs = num->nlimbs; | 153 | rem->nlimbs = num->nlimbs; |
154 | rem->sign = num->sign; | 154 | rem->sign = num->sign; |
155 | MPN_COPY(rem->d, num->d, nsize); | 155 | MPN_COPY(rem->d, num->d, nsize); |
156 | } | 156 | } |
157 | if( quot ) { | 157 | if( quot ) { |
158 | /* This needs to follow the assignment to rem, in case the | 158 | /* This needs to follow the assignment to rem, in case the |
159 | * numerator and quotient are the same. */ | 159 | * numerator and quotient are the same. */ |
160 | quot->nlimbs = 0; | 160 | quot->nlimbs = 0; |
161 | quot->sign = 0; | 161 | quot->sign = 0; |
162 | } | 162 | } |
163 | return; | 163 | return; |
164 | } | 164 | } |
165 | 165 | ||
166 | if( quot ) | 166 | if( quot ) |
167 | mpi_resize( quot, qsize); | 167 | mpi_resize( quot, qsize); |
168 | 168 | ||
169 | /* Read pointers here, when reallocation is finished. */ | 169 | /* Read pointers here, when reallocation is finished. */ |
170 | np = num->d; | 170 | np = num->d; |
171 | dp = den->d; | 171 | dp = den->d; |
172 | rp = rem->d; | 172 | rp = rem->d; |
173 | 173 | ||
174 | /* Optimize division by a single-limb divisor. */ | 174 | /* Optimize division by a single-limb divisor. */ |
175 | if( dsize == 1 ) { | 175 | if( dsize == 1 ) { |
176 | mpi_limb_t rlimb; | 176 | mpi_limb_t rlimb; |
177 | if( quot ) { | 177 | if( quot ) { |
178 | qp = quot->d; | 178 | qp = quot->d; |
179 | rlimb = _gcry_mpih_divmod_1( qp, np, nsize, dp[0] ); | 179 | rlimb = _gcry_mpih_divmod_1( qp, np, nsize, dp[0] ); |
180 | qsize -= qp[qsize - 1] == 0; | 180 | qsize -= qp[qsize - 1] == 0; |
181 | quot->nlimbs = qsize; | 181 | quot->nlimbs = qsize; |
182 | quot->sign = sign_quotient; | 182 | quot->sign = sign_quotient; |
183 | } | 183 | } |
184 | else | 184 | else |
185 | rlimb = _gcry_mpih_mod_1( np, nsize, dp[0] ); | 185 | rlimb = _gcry_mpih_mod_1( np, nsize, dp[0] ); |
186 | rp[0] = rlimb; | 186 | rp[0] = rlimb; |
187 | rsize = rlimb != 0?1:0; | 187 | rsize = rlimb != 0?1:0; |
188 | rem->nlimbs = rsize; | 188 | rem->nlimbs = rsize; |
189 | rem->sign = sign_remainder; | 189 | rem->sign = sign_remainder; |
190 | return; | 190 | return; |
191 | } | 191 | } |
192 | 192 | ||
193 | 193 | ||
194 | if( quot ) { | 194 | if( quot ) { |
195 | qp = quot->d; | 195 | qp = quot->d; |
196 | /* Make sure QP and NP point to different objects. Otherwise the | 196 | /* Make sure QP and NP point to different objects. Otherwise the |
197 | * numerator would be gradually overwritten by the quotient limbs. */ | 197 | * numerator would be gradually overwritten by the quotient limbs. */ |
198 | if(qp == np) { /* Copy NP object to temporary space. */ | 198 | if(qp == np) { /* Copy NP object to temporary space. */ |
199 | marker_nlimbs[markidx] = nsize; | 199 | marker_nlimbs[markidx] = nsize; |
200 | np = marker[markidx++] = mpi_alloc_limb_space(nsize, | 200 | np = marker[markidx++] = mpi_alloc_limb_space(nsize, |
201 | mpi_is_secure(quot)); | 201 | mpi_is_secure(quot)); |
202 | MPN_COPY(np, qp, nsize); | 202 | MPN_COPY(np, qp, nsize); |
203 | } | 203 | } |
204 | } | 204 | } |
205 | else /* Put quotient at top of remainder. */ | 205 | else /* Put quotient at top of remainder. */ |
206 | qp = rp + dsize; | 206 | qp = rp + dsize; |
207 | 207 | ||
208 | count_leading_zeros( normalization_steps, dp[dsize - 1] ); | 208 | count_leading_zeros( normalization_steps, dp[dsize - 1] ); |
209 | 209 | ||
210 | /* Normalize the denominator, i.e. make its most significant bit set by | 210 | /* Normalize the denominator, i.e. make its most significant bit set by |
211 | * shifting it NORMALIZATION_STEPS bits to the left. Also shift the | 211 | * shifting it NORMALIZATION_STEPS bits to the left. Also shift the |
212 | * numerator the same number of steps (to keep the quotient the same!). | 212 | * numerator the same number of steps (to keep the quotient the same!). |
213 | */ | 213 | */ |
214 | if( normalization_steps ) { | 214 | if( normalization_steps ) { |
215 | mpi_ptr_t tp; | 215 | mpi_ptr_t tp; |
216 | mpi_limb_t nlimb; | 216 | mpi_limb_t nlimb; |
217 | 217 | ||
218 | /* Shift up the denominator setting the most significant bit of | 218 | /* Shift up the denominator setting the most significant bit of |
219 | * the most significant word. Use temporary storage not to clobber | 219 | * the most significant word. Use temporary storage not to clobber |
220 | * the original contents of the denominator. */ | 220 | * the original contents of the denominator. */ |
221 | marker_nlimbs[markidx] = dsize; | 221 | marker_nlimbs[markidx] = dsize; |
222 | tp = marker[markidx++] = mpi_alloc_limb_space(dsize,mpi_is_secure(den)); | 222 | tp = marker[markidx++] = mpi_alloc_limb_space(dsize,mpi_is_secure(den)); |
223 | _gcry_mpih_lshift( tp, dp, dsize, normalization_steps ); | 223 | _gcry_mpih_lshift( tp, dp, dsize, normalization_steps ); |
224 | dp = tp; | 224 | dp = tp; |
225 | 225 | ||
226 | /* Shift up the numerator, possibly introducing a new most | 226 | /* Shift up the numerator, possibly introducing a new most |
227 | * significant word. Move the shifted numerator in the remainder | 227 | * significant word. Move the shifted numerator in the remainder |
228 | * meanwhile. */ | 228 | * meanwhile. */ |
229 | nlimb = _gcry_mpih_lshift(rp, np, nsize, normalization_steps); | 229 | nlimb = _gcry_mpih_lshift(rp, np, nsize, normalization_steps); |
230 | if( nlimb ) { | 230 | if( nlimb ) { |
231 | rp[nsize] = nlimb; | 231 | rp[nsize] = nlimb; |
232 | rsize = nsize + 1; | 232 | rsize = nsize + 1; |
233 | } | 233 | } |
234 | else | 234 | else |
235 | rsize = nsize; | 235 | rsize = nsize; |
236 | } | 236 | } |
237 | else { | 237 | else { |
238 | /* The denominator is already normalized, as required. Copy it to | 238 | /* The denominator is already normalized, as required. Copy it to |
239 | * temporary space if it overlaps with the quotient or remainder. */ | 239 | * temporary space if it overlaps with the quotient or remainder. */ |
240 | if( dp == rp || (quot && (dp == qp))) { | 240 | if( dp == rp || (quot && (dp == qp))) { |
241 | mpi_ptr_t tp; | 241 | mpi_ptr_t tp; |
242 | 242 | ||
243 | marker_nlimbs[markidx] = dsize; | 243 | marker_nlimbs[markidx] = dsize; |
244 | tp = marker[markidx++] = mpi_alloc_limb_space(dsize, | 244 | tp = marker[markidx++] = mpi_alloc_limb_space(dsize, |
245 | mpi_is_secure(den)); | 245 | mpi_is_secure(den)); |
246 | MPN_COPY( tp, dp, dsize ); | 246 | MPN_COPY( tp, dp, dsize ); |
247 | dp = tp; | 247 | dp = tp; |
248 | } | 248 | } |
249 | 249 | ||
250 | /* Move the numerator to the remainder. */ | 250 | /* Move the numerator to the remainder. */ |
251 | if( rp != np ) | 251 | if( rp != np ) |
252 | MPN_COPY(rp, np, nsize); | 252 | MPN_COPY(rp, np, nsize); |
253 | 253 | ||
254 | rsize = nsize; | 254 | rsize = nsize; |
255 | } | 255 | } |
256 | 256 | ||
257 | q_limb = _gcry_mpih_divrem( qp, 0, rp, rsize, dp, dsize ); | 257 | q_limb = _gcry_mpih_divrem( qp, 0, rp, rsize, dp, dsize ); |
258 | 258 | ||
259 | if( quot ) { | 259 | if( quot ) { |
260 | qsize = rsize - dsize; | 260 | qsize = rsize - dsize; |
261 | if(q_limb) { | 261 | if(q_limb) { |
262 | qp[qsize] = q_limb; | 262 | qp[qsize] = q_limb; |
263 | qsize += 1; | 263 | qsize += 1; |
264 | } | 264 | } |
265 | 265 | ||
266 | quot->nlimbs = qsize; | 266 | quot->nlimbs = qsize; |
267 | quot->sign = sign_quotient; | 267 | quot->sign = sign_quotient; |
268 | } | 268 | } |
269 | 269 | ||
270 | rsize = dsize; | 270 | rsize = dsize; |
271 | MPN_NORMALIZE (rp, rsize); | 271 | MPN_NORMALIZE (rp, rsize); |
272 | 272 | ||
273 | if( normalization_steps && rsize ) { | 273 | if( normalization_steps && rsize ) { |
274 | _gcry_mpih_rshift(rp, rp, rsize, normalization_steps); | 274 | _gcry_mpih_rshift(rp, rp, rsize, normalization_steps); |
275 | rsize -= rp[rsize - 1] == 0?1:0; | 275 | rsize -= rp[rsize - 1] == 0?1:0; |
276 | } | 276 | } |
277 | 277 | ||
278 | rem->nlimbs = rsize; | 278 | rem->nlimbs = rsize; |
279 | rem->sign = sign_remainder; | 279 | rem->sign = sign_remainder; |
280 | while( markidx ) | 280 | while( markidx ) |
281 | { | 281 | { |
282 | markidx--; | 282 | markidx--; |
283 | _gcry_mpi_free_limb_space (marker[markidx], marker_nlimbs[markidx]); | 283 | _gcry_mpi_free_limb_space (marker[markidx], marker_nlimbs[markidx]); |
284 | } | 284 | } |
285 | } | 285 | } |
286 | 286 | ||
287 | void | 287 | void |
288 | _gcry_mpi_tdiv_q_2exp( gcry_mpi_t w, gcry_mpi_t u, unsigned int count ) | 288 | _gcry_mpi_tdiv_q_2exp( gcry_mpi_t w, gcry_mpi_t u, unsigned int count ) |
289 | { | 289 | { |
290 | mpi_size_t usize, wsize; | 290 | mpi_size_t usize, wsize; |
291 | mpi_size_t limb_cnt; | 291 | mpi_size_t limb_cnt; |
292 | 292 | ||
293 | usize = u->nlimbs; | 293 | usize = u->nlimbs; |
294 | limb_cnt = count / BITS_PER_MPI_LIMB; | 294 | limb_cnt = count / BITS_PER_MPI_LIMB; |
295 | wsize = usize - limb_cnt; | 295 | wsize = usize - limb_cnt; |
296 | if( limb_cnt >= usize ) | 296 | if( limb_cnt >= usize ) |
297 | w->nlimbs = 0; | 297 | w->nlimbs = 0; |
298 | else { | 298 | else { |
299 | mpi_ptr_t wp; | 299 | mpi_ptr_t wp; |
300 | mpi_ptr_t up; | 300 | mpi_ptr_t up; |
301 | 301 | ||
302 | RESIZE_IF_NEEDED( w, wsize ); | 302 | RESIZE_IF_NEEDED( w, wsize ); |
303 | wp = w->d; | 303 | wp = w->d; |
304 | up = u->d; | 304 | up = u->d; |
305 | 305 | ||
306 | count %= BITS_PER_MPI_LIMB; | 306 | count %= BITS_PER_MPI_LIMB; |
307 | if( count ) { | 307 | if( count ) { |
308 | _gcry_mpih_rshift( wp, up + limb_cnt, wsize, count ); | 308 | _gcry_mpih_rshift( wp, up + limb_cnt, wsize, count ); |
309 | wsize -= !wp[wsize - 1]; | 309 | wsize -= !wp[wsize - 1]; |
310 | } | 310 | } |
311 | else { | 311 | else { |
312 | MPN_COPY_INCR( wp, up + limb_cnt, wsize); | 312 | MPN_COPY_INCR( wp, up + limb_cnt, wsize); |
313 | } | 313 | } |
314 | 314 | ||
315 | w->nlimbs = wsize; | 315 | w->nlimbs = wsize; |
316 | } | 316 | } |
317 | } | 317 | } |
318 | 318 | ||
319 | /**************** | 319 | /**************** |
320 | * Check whether dividend is divisible by divisor | 320 | * Check whether dividend is divisible by divisor |
321 | * (note: divisor must fit into a limb) | 321 | * (note: divisor must fit into a limb) |
322 | */ | 322 | */ |
323 | int | 323 | int |
324 | _gcry_mpi_divisible_ui(gcry_mpi_t dividend, ulong divisor ) | 324 | _gcry_mpi_divisible_ui(gcry_mpi_t dividend, ulong divisor ) |
325 | { | 325 | { |
326 | return !_gcry_mpih_mod_1( dividend->d, dividend->nlimbs, divisor ); | 326 | return !_gcry_mpih_mod_1( dividend->d, dividend->nlimbs, divisor ); |
327 | } | 327 | } |
328 | 328 | ||
329 | 329 | ||
330 | void | 330 | void |
331 | gcry_mpi_div (gcry_mpi_t quot, gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor, int round) | 331 | gcry_mpi_div (gcry_mpi_t quot, gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor, int round) |
332 | { | 332 | { |
333 | if (!round) | 333 | if (!round) |
334 | { | 334 | { |
335 | if (!rem) | 335 | if (!rem) |
336 | { | 336 | { |
337 | gcry_mpi_t tmp = mpi_alloc (mpi_get_nlimbs(quot)); | 337 | gcry_mpi_t tmp = mpi_alloc (mpi_get_nlimbs(quot)); |
338 | _gcry_mpi_tdiv_qr (quot, tmp, dividend, divisor); | 338 | _gcry_mpi_tdiv_qr (quot, tmp, dividend, divisor); |
339 | mpi_free (tmp); | 339 | mpi_free (tmp); |
340 | } | 340 | } |
341 | else | 341 | else |
342 | _gcry_mpi_tdiv_qr (quot, rem, dividend, divisor); | 342 | _gcry_mpi_tdiv_qr (quot, rem, dividend, divisor); |
343 | } | 343 | } |
344 | else if (round < 0) | 344 | else if (round < 0) |
345 | { | 345 | { |
346 | if (!rem) | 346 | if (!rem) |
347 | _gcry_mpi_fdiv_q (quot, dividend, divisor); | 347 | _gcry_mpi_fdiv_q (quot, dividend, divisor); |
348 | else if (!quot) | 348 | else if (!quot) |
349 | _gcry_mpi_fdiv_r (rem, dividend, divisor); | 349 | _gcry_mpi_fdiv_r (rem, dividend, divisor); |
350 | else | 350 | else |
351 | _gcry_mpi_fdiv_qr (quot, rem, dividend, divisor); | 351 | _gcry_mpi_fdiv_qr (quot, rem, dividend, divisor); |
352 | } | 352 | } |
353 | else | 353 | else |
354 | log_bug ("mpi rounding to ceiling not yet implemented\n"); | 354 | log_bug ("mpi rounding to ceiling not yet implemented\n"); |
355 | } | 355 | } |
356 | 356 | ||
357 | 357 | ||
358 | void | 358 | void |
359 | gcry_mpi_mod (gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor) | 359 | gcry_mpi_mod (gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor) |
360 | { | 360 | { |
361 | _gcry_mpi_fdiv_r (rem, dividend, divisor); | 361 | _gcry_mpi_fdiv_r (rem, dividend, divisor); |
362 | rem->sign = 0; | 362 | rem->sign = 0; |
363 | } | 363 | } |
364 | 364 | ||