1 /* glplib02.c (64-bit 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 /***********************************************************************
31 * xlset - expand integer to long integer
36 * glp_long xlset(int x);
40 * The routine xlset returns x expanded to long integer. */
44 t.lo = x, t.hi = (x >= 0 ? 0 : -1);
48 /***********************************************************************
51 * xlneg - negate long integer
56 * glp_long xlneg(glp_long x);
60 * The routine xlneg returns the difference 0 - x. */
62 glp_long xlneg(glp_long x)
64 x.lo = - x.lo, x.hi = ~x.hi;
70 /***********************************************************************
73 * xladd - add long integers
78 * glp_long xladd(glp_long x, glp_long y);
82 * The routine xladd returns the sum x + y. */
84 glp_long xladd(glp_long x, glp_long y)
85 { if ((unsigned int)x.lo <= 0xFFFFFFFF - (unsigned int)y.lo)
86 x.lo += y.lo, x.hi += y.hi;
88 x.lo += y.lo, x.hi += y.hi + 1;
92 /***********************************************************************
95 * xlsub - subtract long integers
100 * glp_long xlsub(glp_long x, glp_long y);
104 * The routine xlsub returns the difference x - y. */
106 glp_long xlsub(glp_long x, glp_long y)
111 /***********************************************************************
114 * xlcmp - compare long integers
118 * #include "glplib.h"
119 * int xlcmp(glp_long x, glp_long y);
123 * The routine xlcmp returns the sign of the difference x - y. */
125 int xlcmp(glp_long x, glp_long y)
126 { if (x.hi >= 0 && y.hi < 0) return +1;
127 if (x.hi < 0 && y.hi >= 0) return -1;
128 if ((unsigned int)x.hi < (unsigned int)y.hi) return -1;
129 if ((unsigned int)x.hi > (unsigned int)y.hi) return +1;
130 if ((unsigned int)x.lo < (unsigned int)y.lo) return -1;
131 if ((unsigned int)x.lo > (unsigned int)y.lo) return +1;
135 /***********************************************************************
138 * xlmul - multiply long integers
142 * #include "glplib.h"
143 * glp_long xlmul(glp_long x, glp_long y);
147 * The routine xlmul returns the product x * y. */
149 glp_long xlmul(glp_long x, glp_long y)
150 { unsigned short xx[8], yy[4];
151 xx[4] = (unsigned short)x.lo;
152 xx[5] = (unsigned short)(x.lo >> 16);
153 xx[6] = (unsigned short)x.hi;
154 xx[7] = (unsigned short)(x.hi >> 16);
155 yy[0] = (unsigned short)y.lo;
156 yy[1] = (unsigned short)(y.lo >> 16);
157 yy[2] = (unsigned short)y.hi;
158 yy[3] = (unsigned short)(y.hi >> 16);
159 bigmul(4, 4, xx, yy);
160 x.lo = (unsigned int)xx[0] | ((unsigned int)xx[1] << 16);
161 x.hi = (unsigned int)xx[2] | ((unsigned int)xx[3] << 16);
165 /***********************************************************************
168 * xldiv - divide long integers
172 * #include "glplib.h"
173 * glp_ldiv xldiv(glp_long x, glp_long y);
177 * The routine xldiv returns a structure of type glp_ldiv containing
178 * members quot (the quotient) and rem (the remainder), both of type
181 glp_ldiv xldiv(glp_long x, glp_long y)
184 unsigned short xx[8], yy[4];
190 if (sx) x = xlneg(x);
192 if (sy) y = xlneg(y);
193 /* compute x div y and x mod y */
194 xx[0] = (unsigned short)x.lo;
195 xx[1] = (unsigned short)(x.lo >> 16);
196 xx[2] = (unsigned short)x.hi;
197 xx[3] = (unsigned short)(x.hi >> 16);
198 yy[0] = (unsigned short)y.lo;
199 yy[1] = (unsigned short)(y.lo >> 16);
200 yy[2] = (unsigned short)y.hi;
201 yy[3] = (unsigned short)(y.hi >> 16);
211 xerror("xldiv: divide by zero\n");
212 bigdiv(4 - m, m, xx, yy);
213 /* remainder in x[0], x[1], ..., x[m-1] */
214 t.rem.lo = (unsigned int)xx[0], t.rem.hi = 0;
215 if (m >= 2) t.rem.lo |= (unsigned int)xx[1] << 16;
216 if (m >= 3) t.rem.hi = (unsigned int)xx[2];
217 if (m >= 4) t.rem.hi |= (unsigned int)xx[3] << 16;
218 if (sx) t.rem = xlneg(t.rem);
219 /* quotient in x[m], x[m+1], ..., x[4] */
220 t.quot.lo = (unsigned int)xx[m], t.quot.hi = 0;
221 if (m <= 3) t.quot.lo |= (unsigned int)xx[m+1] << 16;
222 if (m <= 2) t.quot.hi = (unsigned int)xx[m+2];
223 if (m <= 1) t.quot.hi |= (unsigned int)xx[m+3] << 16;
224 if (sx ^ sy) t.quot = xlneg(t.quot);
228 /***********************************************************************
231 * xltod - convert long integer to double
235 * #include "glplib.h"
236 * double xltod(glp_long x);
240 * The routine xltod returns x converted to double. */
242 double xltod(glp_long x)
247 s = -1.0, x = xlneg(x);
249 z = 4294967296.0 * (double)x.hi + (double)(unsigned int)x.lo;
251 { xassert(x.hi == 0x80000000 && x.lo == 0x00000000);
252 z = 9223372036854775808.0; /* 2^63 */
257 char *xltoa(glp_long x, char *s)
258 { /* convert long integer to character string */
259 static const char *d = "0123456789";
265 neg = 1, x = xlneg(x);
268 while (!(x.hi == 0 && x.lo == 0))
269 { t = xldiv(x, xlset(10));
270 xassert(0 <= t.rem.lo && t.rem.lo <= 9);
271 s[len++] = d[t.rem.lo];
274 if (len == 0) s[len++] = d[0];
275 if (neg) s[len++] = '-';
280 strcpy(s, "-9223372036854775808"); /* -2^63 */
284 /**********************************************************************/
289 #define N_TEST 1000000
290 /* number of tests */
292 static glp_long myrand(RNG *rand)
295 k = rng_unif_rand(rand, 4);
296 xassert(0 <= k && k <= 3);
297 x.lo = rng_unif_rand(rand, 65536);
298 if (k == 1 || k == 3)
300 x.lo += rng_unif_rand(rand, 65536);
305 x.hi = rng_unif_rand(rand, 65536);
308 x.hi += rng_unif_rand(rand, 65536);
310 if (rng_unif_rand(rand, 2)) x = xlneg(x);
319 rand = rng_create_rand();
320 for (test = 1; test <= N_TEST; test++)
323 if (y.lo == 0 && y.hi == 0) y.lo = 1;
324 /* z.quot := x div y, z.rem := x mod y */
326 /* x must be equal to y * z.quot + z.rem */
327 xassert(xlcmp(x, xladd(xlmul(y, z.quot), z.rem)) == 0);
329 xprintf("%d tests successfully passed\n", N_TEST);
330 rng_delete_rand(rand);