aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/glpk-4.65/src/misc/bignum.c
blob: 540dd9fdddfe60b7c5bd131e8977e5b1523584f2 (plain)
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
/* bignum.c (bignum arithmetic) */

/***********************************************************************
*  This code is part of GLPK (GNU Linear Programming Kit).
*
*  Copyright (C) 2006-2013 Andrew Makhorin, Department for Applied
*  Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
*  reserved. E-mail: <mao@gnu.org>.
*
*  GLPK 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.
*
*  GLPK 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 GLPK. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/

#include "env.h"
#include "bignum.h"

/***********************************************************************
*  Two routines below are intended to multiply and divide unsigned
*  integer numbers of arbitrary precision.
*
*  The routines assume that an unsigned integer number is represented in
*  the positional numeral system with the base 2^16 = 65536, i.e. each
*  "digit" of the number is in the range [0, 65535] and represented as
*  a 16-bit value of the unsigned short type. In other words, a number x
*  has the following representation:
*
*         n-1
*     x = sum d[j] * 65536^j,
*         j=0
*
*  where n is the number of places (positions), and d[j] is j-th "digit"
*  of x, 0 <= d[j] <= 65535.
***********************************************************************/

/***********************************************************************
*  NAME
*
*  bigmul - multiply unsigned integer numbers of arbitrary precision
*
*  SYNOPSIS
*
*  #include "bignum.h"
*  void bigmul(int n, int m, unsigned short x[], unsigned short y[]);
*
*  DESCRIPTION
*
*  The routine bigmul multiplies unsigned integer numbers of arbitrary
*  precision.
*
*  n is the number of digits of multiplicand, n >= 1;
*
*  m is the number of digits of multiplier, m >= 1;
*
*  x is an array containing digits of the multiplicand in elements
*  x[m], x[m+1], ..., x[n+m-1]. Contents of x[0], x[1], ..., x[m-1] are
*  ignored on entry.
*
*  y is an array containing digits of the multiplier in elements y[0],
*  y[1], ..., y[m-1].
*
*  On exit digits of the product are stored in elements x[0], x[1], ...,
*  x[n+m-1]. The array y is not changed. */

void bigmul(int n, int m, unsigned short x[], unsigned short y[])
{     int i, j;
      unsigned int t;
      xassert(n >= 1);
      xassert(m >= 1);
      for (j = 0; j < m; j++) x[j] = 0;
      for (i = 0; i < n; i++)
      {  if (x[i+m])
         {  t = 0;
            for (j = 0; j < m; j++)
            {  t += (unsigned int)x[i+m] * (unsigned int)y[j] +
                    (unsigned int)x[i+j];
               x[i+j] = (unsigned short)t;
               t >>= 16;
            }
            x[i+m] = (unsigned short)t;
         }
      }
      return;
}

/***********************************************************************
*  NAME
*
*  bigdiv - divide unsigned integer numbers of arbitrary precision
*
*  SYNOPSIS
*
*  #include "bignum.h"
*  void bigdiv(int n, int m, unsigned short x[], unsigned short y[]);
*
*  DESCRIPTION
*
*  The routine bigdiv divides one unsigned integer number of arbitrary
*  precision by another with the algorithm described in [1].
*
*  n is the difference between the number of digits of dividend and the
*  number of digits of divisor, n >= 0.
*
*  m is the number of digits of divisor, m >= 1.
*
*  x is an array containing digits of the dividend in elements x[0],
*  x[1], ..., x[n+m-1].
*
*  y is an array containing digits of the divisor in elements y[0],
*  y[1], ..., y[m-1]. The highest digit y[m-1] must be non-zero.
*
*  On exit n+1 digits of the quotient are stored in elements x[m],
*  x[m+1], ..., x[n+m], and m digits of the remainder are stored in
*  elements x[0], x[1], ..., x[m-1]. The array y is changed but then
*  restored.
*
*  REFERENCES
*
*  1. D. Knuth. The Art of Computer Programming. Vol. 2: Seminumerical
*  Algorithms. Stanford University, 1969. */

void bigdiv(int n, int m, unsigned short x[], unsigned short y[])
{     int i, j;
      unsigned int t;
      unsigned short d, q, r;
      xassert(n >= 0);
      xassert(m >= 1);
      xassert(y[m-1] != 0);
      /* special case when divisor has the only digit */
      if (m == 1)
      {  d = 0;
         for (i = n; i >= 0; i--)
         {  t = ((unsigned int)d << 16) + (unsigned int)x[i];
            x[i+1] = (unsigned short)(t / y[0]);
            d = (unsigned short)(t % y[0]);
         }
         x[0] = d;
         goto done;
      }
      /* multiply dividend and divisor by a normalizing coefficient in
       * order to provide the condition y[m-1] >= base / 2 */
      d = (unsigned short)(0x10000 / ((unsigned int)y[m-1] + 1));
      if (d == 1)
         x[n+m] = 0;
      else
      {  t = 0;
         for (i = 0; i < n+m; i++)
         {  t += (unsigned int)x[i] * (unsigned int)d;
            x[i] = (unsigned short)t;
            t >>= 16;
         }
         x[n+m] = (unsigned short)t;
         t = 0;
         for (j = 0; j < m; j++)
         {  t += (unsigned int)y[j] * (unsigned int)d;
            y[j] = (unsigned short)t;
            t >>= 16;
         }
      }
      /* main loop */
      for (i = n; i >= 0; i--)
      {  /* estimate and correct the current digit of quotient */
         if (x[i+m] < y[m-1])
         {  t = ((unsigned int)x[i+m] << 16) + (unsigned int)x[i+m-1];
            q = (unsigned short)(t / (unsigned int)y[m-1]);
            r = (unsigned short)(t % (unsigned int)y[m-1]);
            if (q == 0) goto putq; else goto test;
         }
         q = 0;
         r = x[i+m-1];
decr:    q--; /* if q = 0 then q-- = 0xFFFF */
         t = (unsigned int)r + (unsigned int)y[m-1];
         r = (unsigned short)t;
         if (t > 0xFFFF) goto msub;
test:    t = (unsigned int)y[m-2] * (unsigned int)q;
         if ((unsigned short)(t >> 16) > r) goto decr;
         if ((unsigned short)(t >> 16) < r) goto msub;
         if ((unsigned short)t > x[i+m-2]) goto decr;
msub:    /* now subtract divisor multiplied by the current digit of
          * quotient from the current dividend */
         if (q == 0) goto putq;
         t = 0;
         for (j = 0; j < m; j++)
         {  t += (unsigned int)y[j] * (unsigned int)q;
            if (x[i+j] < (unsigned short)t) t += 0x10000;
            x[i+j] -= (unsigned short)t;
            t >>= 16;
         }
         if (x[i+m] >= (unsigned short)t) goto putq;
         /* perform correcting addition, because the current digit of
          * quotient is greater by one than its correct value */
         q--;
         t = 0;
         for (j = 0; j < m; j++)
         {  t += (unsigned int)x[i+j] + (unsigned int)y[j];
            x[i+j] = (unsigned short)t;
            t >>= 16;
         }
putq:    /* store the current digit of quotient */
         x[i+m] = q;
      }
      /* divide divisor and remainder by the normalizing coefficient in
       * order to restore their original values */
      if (d > 1)
      {  t = 0;
         for (i = m-1; i >= 0; i--)
         {  t = (t << 16) + (unsigned int)x[i];
            x[i] = (unsigned short)(t / (unsigned int)d);
            t %= (unsigned int)d;
         }
         t = 0;
         for (j = m-1; j >= 0; j--)
         {  t = (t << 16) + (unsigned int)y[j];
            y[j] = (unsigned short)(t / (unsigned int)d);
            t %= (unsigned int)d;
         }
      }
done: return;
}

/**********************************************************************/

#ifdef GLP_TEST
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "rng.h"

#define N_MAX 7
/* maximal number of digits in multiplicand */

#define M_MAX 5
/* maximal number of digits in multiplier */

#define N_TEST 1000000
/* number of tests */

int main(void)
{     RNG *rand;
      int d, j, n, m, test;
      unsigned short x[N_MAX], y[M_MAX], z[N_MAX+M_MAX];
      rand = rng_create_rand();
      for (test = 1; test <= N_TEST; test++)
      {  /* x[0,...,n-1] := multiplicand */
         n = 1 + rng_unif_rand(rand, N_MAX-1);
         assert(1 <= n && n <= N_MAX);
         for (j = 0; j < n; j++)
         {  d = rng_unif_rand(rand, 65536);
            assert(0 <= d && d <= 65535);
            x[j] = (unsigned short)d;
         }
         /* y[0,...,m-1] := multiplier */
         m = 1 + rng_unif_rand(rand, M_MAX-1);
         assert(1 <= m && m <= M_MAX);
         for (j = 0; j < m; j++)
         {  d = rng_unif_rand(rand, 65536);
            assert(0 <= d && d <= 65535);
            y[j] = (unsigned short)d;
         }
         if (y[m-1] == 0) y[m-1] = 1;
         /* z[0,...,n+m-1] := x * y */
         for (j = 0; j < n; j++) z[m+j] = x[j];
         bigmul(n, m, z, y);
         /* z[0,...,m-1] := z mod y, z[m,...,n+m-1] := z div y */
         bigdiv(n, m, z, y);
         /* z mod y must be 0 */
         for (j = 0; j < m; j++) assert(z[j] == 0);
         /* z div y must be x */
         for (j = 0; j < n; j++) assert(z[m+j] == x[j]);
      }
      fprintf(stderr, "%d tests successfully passed\n", N_TEST);
      rng_delete_rand(rand);
      return 0;
}
#endif

/* eof */