10 /* Find the leftmost non-zero digit of X */
12 while (n >= 0 && !X[n])
17 int mul2(int *X, int n)
19 /* Multiply X by 2 and return its new length */
21 for (int i=0; i<=n; i++) {
31 int submul(int *X, int xn, int *Y, int yn, int z, int *T)
33 /* T = X-z*Y, returns -1 if it's <0; T and X can be the same array */
39 for (int i=0; i<=xn; i++) {
40 int y = z*((i <= yn) ? Y[i] : 0) + d;
43 int x = X[i] + (BASE-1-y) + c;
50 void divide(int *X0, int *Y0, int *Z)
52 /* A copy of X and Y will be needed to avoid overwriting inputs */
54 memcpy(X, X0, MAX * sizeof(*X));
55 memcpy(Y, Y0, MAX * sizeof(*Y));
57 /* Find the leftmost digit of X and Y */
58 int xn = first(X), yn = first(Y);
59 if (yn < 0) // Division by 0
62 /* While Y starts with <BASE/2, multiply both X and Y by 2 */
63 while (Y[yn] < BASE/2) {
68 /* Find the size of result and fill Z with zeroes */
69 for (int i=0; i<MAX; i++)
72 if (zn < 0) /* The result will obviously round to 0 */
75 /* The main division loop */
78 while (xn > 0 && !X[xn]) /* Fix the estimate of size of X */
81 if (xn-zn > yn) /* First 1 or 2 digits of X */
84 if (submul(X+zn, xn-zn, Y, yn, Z[zn], T) < 0)
86 submul(X+zn, xn-zn, Y, yn, Z[zn], X+zn);
93 int A[MAX] = { 6, 7, 5, 8, 4, 0, 1 };
94 int B[MAX] = { 4, 6 };
97 for (int i=MAX-1; i>=0; i--)