forked from alpertron/calculators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
division.c
321 lines (308 loc) · 10.7 KB
/
division.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
This file is part of Alpertron Calculators.
Copyright 2015 Dario Alejandro Alpern
Alpertron Calculators is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Alpertron Calculators is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Alpertron Calculators. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bignbr.h"
#include "expression.h"
#include <math.h>
extern limb approxInv[MAX_LEN];
extern limb adjustedArgument[MAX_LEN];
extern limb arrAux[MAX_LEN];
extern int bitLengthCycle[20];
// This routine uses Newton iteration: if x is an approximate inverse square root of N,
// a better approximation is: x(3-Nxx)/2. After the inverse square root is computed,
// the square root is found just by multiplying by N.
// The argument is multiplied by a power of 4 so the most significant limb is
// between LIMB_RANGE/4 and LIMB_RANGE - 1 and there is an even number of limbs.
// At the end of the calculation, the result is divided by the power of 2.
// All computations are done in little-endian notation.
// Find power of 2 that divides the number.
// output: pNbrLimbs = pointer to number of limbs
// pPower2 = pointer to power of 2.
static void MultiplyBigNbrByMinPowerOf2(int *pPower2, limb *number, int len, limb *dest)
{
limb mostSignficLimb, oldLimb, newLimb;
int index2, mask, shLeft;
limb *ptrDest;
shLeft = 0;
mostSignficLimb.x = (number + len - 1)->x;
for (mask = LIMB_RANGE/2; mask > 0; mask >>= 1)
{
if ((mostSignficLimb.x & mask) != 0)
{
break;
}
shLeft++;
}
ptrDest = dest;
// Multiply number by this power.
oldLimb.x = 0;
for (index2 = len; index2 > 0; index2--)
{
newLimb.x = ptrDest->x;
(ptrDest++)->x = ((newLimb.x << shLeft) |
(oldLimb.x >> (BITS_PER_GROUP - shLeft))) & MAX_VALUE_LIMB;
oldLimb.x = newLimb.x;
}
ptrDest->x = oldLimb.x >> (BITS_PER_GROUP - shLeft);
*pPower2 = shLeft;
}
// After computing the number of limbs of the results, this routine finds the inverse
// of the divisor and then multiplies it by the dividend using nbrLimbs+1 limbs.
// After that, the quotient is adjusted.
enum eExprErr BigIntDivide(BigInteger *pDividend, BigInteger *pDivisor, BigInteger *pQuotient)
{
double inverse;
limb oldLimb, newLimb;
int nbrLimbs, nbrLimbsDividend, nbrLimbsDivisor;
// Check whether the divisor is zero.
if (pDivisor->limbs[0].x == 0 && pDivisor->nbrLimbs == 1)
{ // Indicate overflow if divisor is zero.
return EXPR_NUMBER_TOO_HIGH;
}
// Get number of limbs of quotient.
nbrLimbsDividend = pDividend->nbrLimbs;
nbrLimbsDivisor = pDivisor->nbrLimbs;
nbrLimbs = nbrLimbsDividend - nbrLimbsDivisor;
if (nbrLimbs < 0)
{ // Absolute value of dividend is less than absolute value of divisor.
pQuotient->limbs[0].x = 0;
pQuotient->nbrLimbs = 1;
pQuotient->sign = SIGN_POSITIVE;
return EXPR_OK;
}
if (nbrLimbs == 0)
{ // Both divisor and dividend have the same number of limbs.
for (nbrLimbs = nbrLimbsDividend - 1; nbrLimbs > 0; nbrLimbs--)
{
if (pDividend->limbs[nbrLimbs].x != pDivisor->limbs[nbrLimbs].x)
{
break;
}
}
if (pDividend->limbs[nbrLimbs].x < pDivisor->limbs[nbrLimbs].x)
{ // Dividend is less than divisor, so quotient is zero.
pQuotient->limbs[0].x = 0;
pQuotient->nbrLimbs = 1;
pQuotient->sign = SIGN_POSITIVE;
return EXPR_OK;
}
}
if (nbrLimbsDividend == 1)
{ // If dividend is small, perform the division directly.
pQuotient->limbs[0].x = pDividend->limbs[0].x / pDivisor->limbs[0].x;
pQuotient->nbrLimbs = 1;
}
else if (nbrLimbsDivisor == 1)
{ // Divisor is small: use divide by int.
// Sign of quotient is determined later.
if (pQuotient != pDividend)
{
CopyBigInt(pQuotient, pDividend);
}
subtractdivide(pQuotient, 0, pDivisor->limbs[0].x);
}
else
{
int index;
int bitLength;
int bitLengthNbrCycles;
int idx;
int nbrLimbsQuotient;
int power2;
limb *ptrDest;
limb *ptrDivisor, *ptrDividend, *ptrQuotient, *ptrQuot;
nbrLimbs += 3; // Use this number of limbs for intermediate calculations.
if (nbrLimbs > nbrLimbsDivisor)
{
memset(&adjustedArgument[0], 0, (nbrLimbs - nbrLimbsDivisor)*sizeof(limb));
memcpy(&adjustedArgument[nbrLimbs - nbrLimbsDivisor], &pDivisor->limbs[0], nbrLimbsDivisor*sizeof(limb));
}
else
{
memcpy(&adjustedArgument[0], &pDivisor->limbs[nbrLimbsDivisor - nbrLimbs], nbrLimbs*sizeof(limb));
}
MultiplyBigNbrByMinPowerOf2(&power2, adjustedArgument, nbrLimbs, adjustedArgument);
// Initialize approximate inverse.
inverse = MAX_VALUE_LIMB / ((double)adjustedArgument[nbrLimbs - 1].x + 1);
approxInv[nbrLimbs-1].x = 1;
if (inverse <= 1)
{
approxInv[nbrLimbs - 2].x = 0;
}
else
{
approxInv[nbrLimbs - 2].x = (int)floor((inverse - 1)*MAX_VALUE_LIMB);
}
// Perform Newton approximation loop.
// Get bit length of each cycle.
bitLengthNbrCycles = 0;
bitLength = nbrLimbs*BITS_PER_GROUP;
while (bitLength >= BITS_PER_GROUP)
{
bitLengthCycle[bitLengthNbrCycles++] = bitLength;
bitLength = (bitLength + 1) >> 1;
}
// Each loop increments precision.
// Use Newton iteration: x_{n+1} = x_n(2 - x_n)
while (--bitLengthNbrCycles >= 0)
{
limb *ptrArrAux;
int limbLength;
bitLength = bitLengthCycle[bitLengthNbrCycles];
limbLength = (bitLength + 3 * (BITS_PER_GROUP)-1) / BITS_PER_GROUP;
if (limbLength > nbrLimbs)
{
limbLength = nbrLimbs;
}
// Compute x(2-Nx).
// Multiply by divisor.
multiply(&approxInv[nbrLimbs-limbLength], &adjustedArgument[nbrLimbs - limbLength], arrAux, limbLength, NULL);
// Subtract arrAux from 2.
ptrArrAux = &arrAux[limbLength];
for (idx = limbLength - 1; idx > 0; idx--)
{
ptrArrAux->x = MAX_VALUE_LIMB - ptrArrAux->x;
ptrArrAux++;
}
ptrArrAux->x = 1 - ptrArrAux->x;
// Multiply arrAux by approxInv.
multiply(&arrAux[limbLength], &approxInv[nbrLimbs - limbLength], approxInv, limbLength, NULL);
memmove(&approxInv[nbrLimbs - limbLength], &approxInv[limbLength - 1], limbLength*sizeof(limb));
}
// Multiply approxInv by argument to obtain the quotient.
if (nbrLimbsDividend >= nbrLimbs)
{
multiply(&pDividend->limbs[nbrLimbsDividend - nbrLimbs], approxInv, approxInv, nbrLimbs, NULL);
}
else
{
memset(arrAux, 0, (nbrLimbs - nbrLimbsDividend)*sizeof(limb));
memcpy(&arrAux[nbrLimbs - nbrLimbsDividend], pDividend->limbs, nbrLimbsDividend*sizeof(limb));
multiply(arrAux, approxInv, approxInv, nbrLimbs, NULL);
} // approxInv holds the quotient.
// Shift left quotient power2 bits into result.
ptrDest = &approxInv[nbrLimbs - 1];
oldLimb.x = 0;
for (index = nbrLimbs; index >= 0; index--)
{
newLimb.x = ptrDest->x;
(ptrDest++)->x = ((newLimb.x << power2) |
(oldLimb.x >> (BITS_PER_GROUP - power2))) & MAX_VALUE_LIMB;
oldLimb.x = newLimb.x;
}
// Determine number of limbs of quotient.
nbrLimbsQuotient = nbrLimbsDividend - nbrLimbsDivisor;
ptrDivisor = &pDivisor->limbs[nbrLimbsDivisor - 1];
ptrDividend = &pDividend->limbs[nbrLimbsDividend - 1];
for (idx = nbrLimbsDivisor - 1; idx > 0; idx--)
{
if (ptrDividend->x != ptrDivisor->x)
{
break;
}
ptrDividend--;
ptrDivisor--;
}
if (ptrDividend->x >= ptrDivisor->x)
{
nbrLimbsQuotient++;
}
ptrQuotient = &approxInv[2 * nbrLimbs - nbrLimbsQuotient];
if (approxInv[2 * nbrLimbs - 1].x == 0)
{ // Most significant byte is zero, so it is not part of the quotient.
ptrQuotient--;
}
ptrQuot = ptrQuotient;
if ((ptrQuotient - 1)->x > (7 << (BITS_PER_GROUP - 3)))
{ // Increment quotient.
for (idx = 0; idx <= nbrLimbsQuotient; idx++)
{
if ((++((ptrQuotient + idx)->x)) & MAX_INT_NBR)
{
break;
}
(ptrQuotient + idx)->x = 0;
}
if (idx >= nbrLimbsQuotient)
{ // Roll back on overflow.
for (idx = 0; idx < nbrLimbsQuotient; idx++)
{
if (--((ptrQuotient + idx)->x) >= 0)
{
break;
}
(ptrQuotient + idx)->x = MAX_VALUE_LIMB;
}
}
if (approxInv[2 * nbrLimbs - 1].x != 0)
{ // Most significant byte is not zero, so it is part of the quotient.
ptrQuot = &approxInv[2 * nbrLimbs - nbrLimbsQuotient];
}
// Test whether the quotient is correct.
// It is correct only if multiplied by the divisor, it is <= than the dividend.
if (nbrLimbsQuotient > nbrLimbsDivisor)
{
memcpy(&approxInv[0], pDivisor->limbs, nbrLimbsDivisor * sizeof(limb));
memset(&approxInv[nbrLimbsDivisor], 0, (nbrLimbsQuotient - nbrLimbsDivisor) * sizeof(limb));
multiply(&approxInv[0], ptrQuot, arrAux, nbrLimbsQuotient, NULL);
}
else
{
memset(&approxInv[2 * nbrLimbs], 0, (nbrLimbsDivisor - nbrLimbsQuotient) * sizeof(limb));
multiply(pDivisor->limbs, ptrQuot, arrAux, nbrLimbsDivisor, NULL);
}
ptrDividend = &pDividend->limbs[pDividend->nbrLimbs - 1];
ptrDest = &arrAux[pDividend->nbrLimbs - 1];
for (idx = pDividend->nbrLimbs - 1; idx > 0; idx--)
{
if (ptrDividend->x != ptrDest->x)
{
break;
}
ptrDividend--;
ptrDest--;
}
if (ptrDividend->x < ptrDest->x)
{ // Decrement quotient.
ptrQuotient = ptrQuot;
for (idx = 0; idx < nbrLimbsQuotient; idx++)
{
if (--(ptrQuotient->x) >= 0)
{
break;
}
(ptrQuotient++)->x = MAX_VALUE_LIMB;
}
if (idx == nbrLimbsQuotient)
{
nbrLimbsQuotient--;
}
}
}
memcpy(&pQuotient->limbs[0], ptrQuot, nbrLimbsQuotient*sizeof(limb));
pQuotient->nbrLimbs = nbrLimbsQuotient;
}
if (pDividend->sign == pDivisor->sign || (pQuotient->limbs[0].x == 0 && pQuotient->nbrLimbs == 1))
{
pQuotient->sign = SIGN_POSITIVE;
}
else
{
pQuotient->sign = SIGN_NEGATIVE;
}
return EXPR_OK;
}