lib/mpi: Extend the MPI library
[linux-2.6-block.git] / lib / mpi / mpi-div.c
CommitLineData
a8ea8bdd
TZ
1/* mpi-div.c - MPI functions
2 * Copyright (C) 1994, 1996, 1998, 2001, 2002,
3 * 2003 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Note: This code is heavily based on the GNU MP Library.
8 * Actually it's the same code with only minor changes in the
9 * way the data is stored; this is to support the abstraction
10 * of an optional secure memory allocation which may be used
11 * to avoid revealing of sensitive data due to paging etc.
12 */
13
14#include "mpi-internal.h"
15#include "longlong.h"
16
17void mpi_tdiv_qr(MPI quot, MPI rem, MPI num, MPI den);
18void mpi_fdiv_qr(MPI quot, MPI rem, MPI dividend, MPI divisor);
19
20void mpi_fdiv_r(MPI rem, MPI dividend, MPI divisor)
21{
22 int divisor_sign = divisor->sign;
23 MPI temp_divisor = NULL;
24
25 /* We need the original value of the divisor after the remainder has been
26 * preliminary calculated. We have to copy it to temporary space if it's
27 * the same variable as REM.
28 */
29 if (rem == divisor) {
30 temp_divisor = mpi_copy(divisor);
31 divisor = temp_divisor;
32 }
33
34 mpi_tdiv_r(rem, dividend, divisor);
35
36 if (((divisor_sign?1:0) ^ (dividend->sign?1:0)) && rem->nlimbs)
37 mpi_add(rem, rem, divisor);
38
39 if (temp_divisor)
40 mpi_free(temp_divisor);
41}
42
43void mpi_fdiv_q(MPI quot, MPI dividend, MPI divisor)
44{
45 MPI tmp = mpi_alloc(mpi_get_nlimbs(quot));
46 mpi_fdiv_qr(quot, tmp, dividend, divisor);
47 mpi_free(tmp);
48}
49
50void mpi_fdiv_qr(MPI quot, MPI rem, MPI dividend, MPI divisor)
51{
52 int divisor_sign = divisor->sign;
53 MPI temp_divisor = NULL;
54
55 if (quot == divisor || rem == divisor) {
56 temp_divisor = mpi_copy(divisor);
57 divisor = temp_divisor;
58 }
59
60 mpi_tdiv_qr(quot, rem, dividend, divisor);
61
62 if ((divisor_sign ^ dividend->sign) && rem->nlimbs) {
63 mpi_sub_ui(quot, quot, 1);
64 mpi_add(rem, rem, divisor);
65 }
66
67 if (temp_divisor)
68 mpi_free(temp_divisor);
69}
70
71/* If den == quot, den needs temporary storage.
72 * If den == rem, den needs temporary storage.
73 * If num == quot, num needs temporary storage.
74 * If den has temporary storage, it can be normalized while being copied,
75 * i.e no extra storage should be allocated.
76 */
77
78void mpi_tdiv_r(MPI rem, MPI num, MPI den)
79{
80 mpi_tdiv_qr(NULL, rem, num, den);
81}
82
83void mpi_tdiv_qr(MPI quot, MPI rem, MPI num, MPI den)
84{
85 mpi_ptr_t np, dp;
86 mpi_ptr_t qp, rp;
87 mpi_size_t nsize = num->nlimbs;
88 mpi_size_t dsize = den->nlimbs;
89 mpi_size_t qsize, rsize;
90 mpi_size_t sign_remainder = num->sign;
91 mpi_size_t sign_quotient = num->sign ^ den->sign;
92 unsigned int normalization_steps;
93 mpi_limb_t q_limb;
94 mpi_ptr_t marker[5];
95 unsigned int marker_nlimbs[5];
96 int markidx = 0;
97
98 /* Ensure space is enough for quotient and remainder.
99 * We need space for an extra limb in the remainder, because it's
100 * up-shifted (normalized) below.
101 */
102 rsize = nsize + 1;
103 mpi_resize(rem, rsize);
104
105 qsize = rsize - dsize; /* qsize cannot be bigger than this. */
106 if (qsize <= 0) {
107 if (num != rem) {
108 rem->nlimbs = num->nlimbs;
109 rem->sign = num->sign;
110 MPN_COPY(rem->d, num->d, nsize);
111 }
112 if (quot) {
113 /* This needs to follow the assignment to rem, in case the
114 * numerator and quotient are the same.
115 */
116 quot->nlimbs = 0;
117 quot->sign = 0;
118 }
119 return;
120 }
121
122 if (quot)
123 mpi_resize(quot, qsize);
124
125 /* Read pointers here, when reallocation is finished. */
126 np = num->d;
127 dp = den->d;
128 rp = rem->d;
129
130 /* Optimize division by a single-limb divisor. */
131 if (dsize == 1) {
132 mpi_limb_t rlimb;
133 if (quot) {
134 qp = quot->d;
135 rlimb = mpihelp_divmod_1(qp, np, nsize, dp[0]);
136 qsize -= qp[qsize - 1] == 0;
137 quot->nlimbs = qsize;
138 quot->sign = sign_quotient;
139 } else
140 rlimb = mpihelp_mod_1(np, nsize, dp[0]);
141 rp[0] = rlimb;
142 rsize = rlimb != 0?1:0;
143 rem->nlimbs = rsize;
144 rem->sign = sign_remainder;
145 return;
146 }
147
148
149 if (quot) {
150 qp = quot->d;
151 /* Make sure QP and NP point to different objects. Otherwise the
152 * numerator would be gradually overwritten by the quotient limbs.
153 */
154 if (qp == np) { /* Copy NP object to temporary space. */
155 marker_nlimbs[markidx] = nsize;
156 np = marker[markidx++] = mpi_alloc_limb_space(nsize);
157 MPN_COPY(np, qp, nsize);
158 }
159 } else /* Put quotient at top of remainder. */
160 qp = rp + dsize;
161
162 normalization_steps = count_leading_zeros(dp[dsize - 1]);
163
164 /* Normalize the denominator, i.e. make its most significant bit set by
165 * shifting it NORMALIZATION_STEPS bits to the left. Also shift the
166 * numerator the same number of steps (to keep the quotient the same!).
167 */
168 if (normalization_steps) {
169 mpi_ptr_t tp;
170 mpi_limb_t nlimb;
171
172 /* Shift up the denominator setting the most significant bit of
173 * the most significant word. Use temporary storage not to clobber
174 * the original contents of the denominator.
175 */
176 marker_nlimbs[markidx] = dsize;
177 tp = marker[markidx++] = mpi_alloc_limb_space(dsize);
178 mpihelp_lshift(tp, dp, dsize, normalization_steps);
179 dp = tp;
180
181 /* Shift up the numerator, possibly introducing a new most
182 * significant word. Move the shifted numerator in the remainder
183 * meanwhile.
184 */
185 nlimb = mpihelp_lshift(rp, np, nsize, normalization_steps);
186 if (nlimb) {
187 rp[nsize] = nlimb;
188 rsize = nsize + 1;
189 } else
190 rsize = nsize;
191 } else {
192 /* The denominator is already normalized, as required. Copy it to
193 * temporary space if it overlaps with the quotient or remainder.
194 */
195 if (dp == rp || (quot && (dp == qp))) {
196 mpi_ptr_t tp;
197
198 marker_nlimbs[markidx] = dsize;
199 tp = marker[markidx++] = mpi_alloc_limb_space(dsize);
200 MPN_COPY(tp, dp, dsize);
201 dp = tp;
202 }
203
204 /* Move the numerator to the remainder. */
205 if (rp != np)
206 MPN_COPY(rp, np, nsize);
207
208 rsize = nsize;
209 }
210
211 q_limb = mpihelp_divrem(qp, 0, rp, rsize, dp, dsize);
212
213 if (quot) {
214 qsize = rsize - dsize;
215 if (q_limb) {
216 qp[qsize] = q_limb;
217 qsize += 1;
218 }
219
220 quot->nlimbs = qsize;
221 quot->sign = sign_quotient;
222 }
223
224 rsize = dsize;
225 MPN_NORMALIZE(rp, rsize);
226
227 if (normalization_steps && rsize) {
228 mpihelp_rshift(rp, rp, rsize, normalization_steps);
229 rsize -= rp[rsize - 1] == 0?1:0;
230 }
231
232 rem->nlimbs = rsize;
233 rem->sign = sign_remainder;
234 while (markidx) {
235 markidx--;
236 mpi_free_limb_space(marker[markidx]);
237 }
238}