1 /* glplib01.c (bignum arithmetic) */
3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
7 * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
9 * E-mail: <mao@gnu.org>.
11 * GLPK is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 * License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
23 ***********************************************************************/
28 /***********************************************************************
29 * Two routines below are intended to multiply and divide unsigned
30 * integer numbers of arbitrary precision.
32 * The routines assume that an unsigned integer number is represented in
33 * the positional numeral system with the base 2^16 = 65536, i.e. each
34 * "digit" of the number is in the range [0, 65535] and represented as
35 * a 16-bit value of the unsigned short type. In other words, a number x
36 * has the following representation:
39 * x = sum d[j] * 65536^j,
42 * where n is the number of places (positions), and d[j] is j-th "digit"
43 * of x, 0 <= d[j] <= 65535.
44 ***********************************************************************/
46 /***********************************************************************
49 * bigmul - multiply unsigned integer numbers of arbitrary precision
54 * void bigmul(int n, int m, unsigned short x[], unsigned short y[]);
58 * The routine bigmul multiplies unsigned integer numbers of arbitrary
61 * n is the number of digits of multiplicand, n >= 1;
63 * m is the number of digits of multiplier, m >= 1;
65 * x is an array containing digits of the multiplicand in elements
66 * x[m], x[m+1], ..., x[n+m-1]. Contents of x[0], x[1], ..., x[m-1] are
69 * y is an array containing digits of the multiplier in elements y[0],
72 * On exit digits of the product are stored in elements x[0], x[1], ...,
73 * x[n+m-1]. The array y is not changed. */
75 void bigmul(int n, int m, unsigned short x[], unsigned short y[])
80 for (j = 0; j < m; j++) x[j] = 0;
81 for (i = 0; i < n; i++)
84 for (j = 0; j < m; j++)
85 { t += (unsigned int)x[i+m] * (unsigned int)y[j] +
87 x[i+j] = (unsigned short)t;
90 x[i+m] = (unsigned short)t;
96 /***********************************************************************
99 * bigdiv - divide unsigned integer numbers of arbitrary precision
103 * #include "glplib.h"
104 * void bigdiv(int n, int m, unsigned short x[], unsigned short y[]);
108 * The routine bigdiv divides one unsigned integer number of arbitrary
109 * precision by another with the algorithm described in [1].
111 * n is the difference between the number of digits of dividend and the
112 * number of digits of divisor, n >= 0.
114 * m is the number of digits of divisor, m >= 1.
116 * x is an array containing digits of the dividend in elements x[0],
117 * x[1], ..., x[n+m-1].
119 * y is an array containing digits of the divisor in elements y[0],
120 * y[1], ..., y[m-1]. The highest digit y[m-1] must be non-zero.
122 * On exit n+1 digits of the quotient are stored in elements x[m],
123 * x[m+1], ..., x[n+m], and m digits of the remainder are stored in
124 * elements x[0], x[1], ..., x[m-1]. The array y is changed but then
129 * 1. D. Knuth. The Art of Computer Programming. Vol. 2: Seminumerical
130 * Algorithms. Stanford University, 1969. */
132 void bigdiv(int n, int m, unsigned short x[], unsigned short y[])
135 unsigned short d, q, r;
138 xassert(y[m-1] != 0);
139 /* special case when divisor has the only digit */
142 for (i = n; i >= 0; i--)
143 { t = ((unsigned int)d << 16) + (unsigned int)x[i];
144 x[i+1] = (unsigned short)(t / y[0]);
145 d = (unsigned short)(t % y[0]);
150 /* multiply dividend and divisor by a normalizing coefficient in
151 order to provide the condition y[m-1] >= base / 2 */
152 d = (unsigned short)(0x10000 / ((unsigned int)y[m-1] + 1));
157 for (i = 0; i < n+m; i++)
158 { t += (unsigned int)x[i] * (unsigned int)d;
159 x[i] = (unsigned short)t;
162 x[n+m] = (unsigned short)t;
164 for (j = 0; j < m; j++)
165 { t += (unsigned int)y[j] * (unsigned int)d;
166 y[j] = (unsigned short)t;
171 for (i = n; i >= 0; i--)
172 { /* estimate and correct the current digit of quotient */
174 { t = ((unsigned int)x[i+m] << 16) + (unsigned int)x[i+m-1];
175 q = (unsigned short)(t / (unsigned int)y[m-1]);
176 r = (unsigned short)(t % (unsigned int)y[m-1]);
177 if (q == 0) goto putq; else goto test;
181 decr: q--; /* if q = 0 then q-- = 0xFFFF */
182 t = (unsigned int)r + (unsigned int)y[m-1];
183 r = (unsigned short)t;
184 if (t > 0xFFFF) goto msub;
185 test: t = (unsigned int)y[m-2] * (unsigned int)q;
186 if ((unsigned short)(t >> 16) > r) goto decr;
187 if ((unsigned short)(t >> 16) < r) goto msub;
188 if ((unsigned short)t > x[i+m-2]) goto decr;
189 msub: /* now subtract divisor multiplied by the current digit of
190 quotient from the current dividend */
191 if (q == 0) goto putq;
193 for (j = 0; j < m; j++)
194 { t += (unsigned int)y[j] * (unsigned int)q;
195 if (x[i+j] < (unsigned short)t) t += 0x10000;
196 x[i+j] -= (unsigned short)t;
199 if (x[i+m] >= (unsigned short)t) goto putq;
200 /* perform correcting addition, because the current digit of
201 quotient is greater by one than its correct value */
204 for (j = 0; j < m; j++)
205 { t += (unsigned int)x[i+j] + (unsigned int)y[j];
206 x[i+j] = (unsigned short)t;
209 putq: /* store the current digit of quotient */
212 /* divide divisor and remainder by the normalizing coefficient in
213 order to restore their original values */
216 for (i = m-1; i >= 0; i--)
217 { t = (t << 16) + (unsigned int)x[i];
218 x[i] = (unsigned short)(t / (unsigned int)d);
219 t %= (unsigned int)d;
222 for (j = m-1; j >= 0; j--)
223 { t = (t << 16) + (unsigned int)y[j];
224 y[j] = (unsigned short)(t / (unsigned int)d);
225 t %= (unsigned int)d;
231 /**********************************************************************/
240 /* maximal number of digits in multiplicand */
243 /* maximal number of digits in multiplier */
245 #define N_TEST 1000000
246 /* number of tests */
250 int d, j, n, m, test;
251 unsigned short x[N_MAX], y[M_MAX], z[N_MAX+M_MAX];
252 rand = rng_create_rand();
253 for (test = 1; test <= N_TEST; test++)
254 { /* x[0,...,n-1] := multiplicand */
255 n = 1 + rng_unif_rand(rand, N_MAX-1);
256 assert(1 <= n && n <= N_MAX);
257 for (j = 0; j < n; j++)
258 { d = rng_unif_rand(rand, 65536);
259 assert(0 <= d && d <= 65535);
260 x[j] = (unsigned short)d;
262 /* y[0,...,m-1] := multiplier */
263 m = 1 + rng_unif_rand(rand, M_MAX-1);
264 assert(1 <= m && m <= M_MAX);
265 for (j = 0; j < m; j++)
266 { d = rng_unif_rand(rand, 65536);
267 assert(0 <= d && d <= 65535);
268 y[j] = (unsigned short)d;
270 if (y[m-1] == 0) y[m-1] = 1;
271 /* z[0,...,n+m-1] := x * y */
272 for (j = 0; j < n; j++) z[m+j] = x[j];
274 /* z[0,...,m-1] := z mod y, z[m,...,n+m-1] := z div y */
276 /* z mod y must be 0 */
277 for (j = 0; j < m; j++) assert(z[j] == 0);
278 /* z div y must be x */
279 for (j = 0; j < n; j++) assert(z[m+j] == x[j]);
281 fprintf(stderr, "%d tests successfully passed\n", N_TEST);
282 rng_delete_rand(rand);