alpar@1: /* glplib01.c (bignum arithmetic) */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * This code is part of GLPK (GNU Linear Programming Kit). alpar@1: * alpar@1: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@1: * 2009, 2010 Andrew Makhorin, Department for Applied Informatics, alpar@1: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@1: * E-mail: . alpar@1: * alpar@1: * GLPK is free software: you can redistribute it and/or modify it alpar@1: * under the terms of the GNU General Public License as published by alpar@1: * the Free Software Foundation, either version 3 of the License, or alpar@1: * (at your option) any later version. alpar@1: * alpar@1: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@1: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@1: * License for more details. alpar@1: * alpar@1: * You should have received a copy of the GNU General Public License alpar@1: * along with GLPK. If not, see . alpar@1: ***********************************************************************/ alpar@1: alpar@1: #include "glpenv.h" alpar@1: #include "glplib.h" alpar@1: alpar@1: /*********************************************************************** alpar@1: * Two routines below are intended to multiply and divide unsigned alpar@1: * integer numbers of arbitrary precision. alpar@1: * alpar@1: * The routines assume that an unsigned integer number is represented in alpar@1: * the positional numeral system with the base 2^16 = 65536, i.e. each alpar@1: * "digit" of the number is in the range [0, 65535] and represented as alpar@1: * a 16-bit value of the unsigned short type. In other words, a number x alpar@1: * has the following representation: alpar@1: * alpar@1: * n-1 alpar@1: * x = sum d[j] * 65536^j, alpar@1: * j=0 alpar@1: * alpar@1: * where n is the number of places (positions), and d[j] is j-th "digit" alpar@1: * of x, 0 <= d[j] <= 65535. alpar@1: ***********************************************************************/ alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * bigmul - multiply unsigned integer numbers of arbitrary precision alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * #include "glplib.h" alpar@1: * void bigmul(int n, int m, unsigned short x[], unsigned short y[]); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine bigmul multiplies unsigned integer numbers of arbitrary alpar@1: * precision. alpar@1: * alpar@1: * n is the number of digits of multiplicand, n >= 1; alpar@1: * alpar@1: * m is the number of digits of multiplier, m >= 1; alpar@1: * alpar@1: * x is an array containing digits of the multiplicand in elements alpar@1: * x[m], x[m+1], ..., x[n+m-1]. Contents of x[0], x[1], ..., x[m-1] are alpar@1: * ignored on entry. alpar@1: * alpar@1: * y is an array containing digits of the multiplier in elements y[0], alpar@1: * y[1], ..., y[m-1]. alpar@1: * alpar@1: * On exit digits of the product are stored in elements x[0], x[1], ..., alpar@1: * x[n+m-1]. The array y is not changed. */ alpar@1: alpar@1: void bigmul(int n, int m, unsigned short x[], unsigned short y[]) alpar@1: { int i, j; alpar@1: unsigned int t; alpar@1: xassert(n >= 1); alpar@1: xassert(m >= 1); alpar@1: for (j = 0; j < m; j++) x[j] = 0; alpar@1: for (i = 0; i < n; i++) alpar@1: { if (x[i+m]) alpar@1: { t = 0; alpar@1: for (j = 0; j < m; j++) alpar@1: { t += (unsigned int)x[i+m] * (unsigned int)y[j] + alpar@1: (unsigned int)x[i+j]; alpar@1: x[i+j] = (unsigned short)t; alpar@1: t >>= 16; alpar@1: } alpar@1: x[i+m] = (unsigned short)t; alpar@1: } alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * bigdiv - divide unsigned integer numbers of arbitrary precision alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * #include "glplib.h" alpar@1: * void bigdiv(int n, int m, unsigned short x[], unsigned short y[]); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine bigdiv divides one unsigned integer number of arbitrary alpar@1: * precision by another with the algorithm described in [1]. alpar@1: * alpar@1: * n is the difference between the number of digits of dividend and the alpar@1: * number of digits of divisor, n >= 0. alpar@1: * alpar@1: * m is the number of digits of divisor, m >= 1. alpar@1: * alpar@1: * x is an array containing digits of the dividend in elements x[0], alpar@1: * x[1], ..., x[n+m-1]. alpar@1: * alpar@1: * y is an array containing digits of the divisor in elements y[0], alpar@1: * y[1], ..., y[m-1]. The highest digit y[m-1] must be non-zero. alpar@1: * alpar@1: * On exit n+1 digits of the quotient are stored in elements x[m], alpar@1: * x[m+1], ..., x[n+m], and m digits of the remainder are stored in alpar@1: * elements x[0], x[1], ..., x[m-1]. The array y is changed but then alpar@1: * restored. alpar@1: * alpar@1: * REFERENCES alpar@1: * alpar@1: * 1. D. Knuth. The Art of Computer Programming. Vol. 2: Seminumerical alpar@1: * Algorithms. Stanford University, 1969. */ alpar@1: alpar@1: void bigdiv(int n, int m, unsigned short x[], unsigned short y[]) alpar@1: { int i, j; alpar@1: unsigned int t; alpar@1: unsigned short d, q, r; alpar@1: xassert(n >= 0); alpar@1: xassert(m >= 1); alpar@1: xassert(y[m-1] != 0); alpar@1: /* special case when divisor has the only digit */ alpar@1: if (m == 1) alpar@1: { d = 0; alpar@1: for (i = n; i >= 0; i--) alpar@1: { t = ((unsigned int)d << 16) + (unsigned int)x[i]; alpar@1: x[i+1] = (unsigned short)(t / y[0]); alpar@1: d = (unsigned short)(t % y[0]); alpar@1: } alpar@1: x[0] = d; alpar@1: goto done; alpar@1: } alpar@1: /* multiply dividend and divisor by a normalizing coefficient in alpar@1: order to provide the condition y[m-1] >= base / 2 */ alpar@1: d = (unsigned short)(0x10000 / ((unsigned int)y[m-1] + 1)); alpar@1: if (d == 1) alpar@1: x[n+m] = 0; alpar@1: else alpar@1: { t = 0; alpar@1: for (i = 0; i < n+m; i++) alpar@1: { t += (unsigned int)x[i] * (unsigned int)d; alpar@1: x[i] = (unsigned short)t; alpar@1: t >>= 16; alpar@1: } alpar@1: x[n+m] = (unsigned short)t; alpar@1: t = 0; alpar@1: for (j = 0; j < m; j++) alpar@1: { t += (unsigned int)y[j] * (unsigned int)d; alpar@1: y[j] = (unsigned short)t; alpar@1: t >>= 16; alpar@1: } alpar@1: } alpar@1: /* main loop */ alpar@1: for (i = n; i >= 0; i--) alpar@1: { /* estimate and correct the current digit of quotient */ alpar@1: if (x[i+m] < y[m-1]) alpar@1: { t = ((unsigned int)x[i+m] << 16) + (unsigned int)x[i+m-1]; alpar@1: q = (unsigned short)(t / (unsigned int)y[m-1]); alpar@1: r = (unsigned short)(t % (unsigned int)y[m-1]); alpar@1: if (q == 0) goto putq; else goto test; alpar@1: } alpar@1: q = 0; alpar@1: r = x[i+m-1]; alpar@1: decr: q--; /* if q = 0 then q-- = 0xFFFF */ alpar@1: t = (unsigned int)r + (unsigned int)y[m-1]; alpar@1: r = (unsigned short)t; alpar@1: if (t > 0xFFFF) goto msub; alpar@1: test: t = (unsigned int)y[m-2] * (unsigned int)q; alpar@1: if ((unsigned short)(t >> 16) > r) goto decr; alpar@1: if ((unsigned short)(t >> 16) < r) goto msub; alpar@1: if ((unsigned short)t > x[i+m-2]) goto decr; alpar@1: msub: /* now subtract divisor multiplied by the current digit of alpar@1: quotient from the current dividend */ alpar@1: if (q == 0) goto putq; alpar@1: t = 0; alpar@1: for (j = 0; j < m; j++) alpar@1: { t += (unsigned int)y[j] * (unsigned int)q; alpar@1: if (x[i+j] < (unsigned short)t) t += 0x10000; alpar@1: x[i+j] -= (unsigned short)t; alpar@1: t >>= 16; alpar@1: } alpar@1: if (x[i+m] >= (unsigned short)t) goto putq; alpar@1: /* perform correcting addition, because the current digit of alpar@1: quotient is greater by one than its correct value */ alpar@1: q--; alpar@1: t = 0; alpar@1: for (j = 0; j < m; j++) alpar@1: { t += (unsigned int)x[i+j] + (unsigned int)y[j]; alpar@1: x[i+j] = (unsigned short)t; alpar@1: t >>= 16; alpar@1: } alpar@1: putq: /* store the current digit of quotient */ alpar@1: x[i+m] = q; alpar@1: } alpar@1: /* divide divisor and remainder by the normalizing coefficient in alpar@1: order to restore their original values */ alpar@1: if (d > 1) alpar@1: { t = 0; alpar@1: for (i = m-1; i >= 0; i--) alpar@1: { t = (t << 16) + (unsigned int)x[i]; alpar@1: x[i] = (unsigned short)(t / (unsigned int)d); alpar@1: t %= (unsigned int)d; alpar@1: } alpar@1: t = 0; alpar@1: for (j = m-1; j >= 0; j--) alpar@1: { t = (t << 16) + (unsigned int)y[j]; alpar@1: y[j] = (unsigned short)(t / (unsigned int)d); alpar@1: t %= (unsigned int)d; alpar@1: } alpar@1: } alpar@1: done: return; alpar@1: } alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: #if 0 alpar@1: #include alpar@1: #include alpar@1: #include alpar@1: #include "glprng.h" alpar@1: alpar@1: #define N_MAX 7 alpar@1: /* maximal number of digits in multiplicand */ alpar@1: alpar@1: #define M_MAX 5 alpar@1: /* maximal number of digits in multiplier */ alpar@1: alpar@1: #define N_TEST 1000000 alpar@1: /* number of tests */ alpar@1: alpar@1: int main(void) alpar@1: { RNG *rand; alpar@1: int d, j, n, m, test; alpar@1: unsigned short x[N_MAX], y[M_MAX], z[N_MAX+M_MAX]; alpar@1: rand = rng_create_rand(); alpar@1: for (test = 1; test <= N_TEST; test++) alpar@1: { /* x[0,...,n-1] := multiplicand */ alpar@1: n = 1 + rng_unif_rand(rand, N_MAX-1); alpar@1: assert(1 <= n && n <= N_MAX); alpar@1: for (j = 0; j < n; j++) alpar@1: { d = rng_unif_rand(rand, 65536); alpar@1: assert(0 <= d && d <= 65535); alpar@1: x[j] = (unsigned short)d; alpar@1: } alpar@1: /* y[0,...,m-1] := multiplier */ alpar@1: m = 1 + rng_unif_rand(rand, M_MAX-1); alpar@1: assert(1 <= m && m <= M_MAX); alpar@1: for (j = 0; j < m; j++) alpar@1: { d = rng_unif_rand(rand, 65536); alpar@1: assert(0 <= d && d <= 65535); alpar@1: y[j] = (unsigned short)d; alpar@1: } alpar@1: if (y[m-1] == 0) y[m-1] = 1; alpar@1: /* z[0,...,n+m-1] := x * y */ alpar@1: for (j = 0; j < n; j++) z[m+j] = x[j]; alpar@1: bigmul(n, m, z, y); alpar@1: /* z[0,...,m-1] := z mod y, z[m,...,n+m-1] := z div y */ alpar@1: bigdiv(n, m, z, y); alpar@1: /* z mod y must be 0 */ alpar@1: for (j = 0; j < m; j++) assert(z[j] == 0); alpar@1: /* z div y must be x */ alpar@1: for (j = 0; j < n; j++) assert(z[m+j] == x[j]); alpar@1: } alpar@1: fprintf(stderr, "%d tests successfully passed\n", N_TEST); alpar@1: rng_delete_rand(rand); alpar@1: return 0; alpar@1: } alpar@1: #endif alpar@1: alpar@1: /* eof */