alpar@1: /* glpgmp.c */ 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: #define _GLPSTD_STDIO alpar@1: #include "glpdmp.h" alpar@1: #include "glpgmp.h" alpar@1: #define xfault xerror alpar@1: alpar@1: #ifdef HAVE_GMP /* use GNU MP bignum library */ alpar@1: alpar@1: int gmp_pool_count(void) { return 0; } alpar@1: alpar@1: void gmp_free_mem(void) { return; } alpar@1: alpar@1: #else /* use GLPK bignum module */ alpar@1: alpar@1: static DMP *gmp_pool = NULL; alpar@1: static int gmp_size = 0; alpar@1: static unsigned short *gmp_work = NULL; alpar@1: alpar@1: void *gmp_get_atom(int size) alpar@1: { if (gmp_pool == NULL) alpar@1: gmp_pool = dmp_create_pool(); alpar@1: return dmp_get_atom(gmp_pool, size); alpar@1: } alpar@1: alpar@1: void gmp_free_atom(void *ptr, int size) alpar@1: { xassert(gmp_pool != NULL); alpar@1: dmp_free_atom(gmp_pool, ptr, size); alpar@1: return; alpar@1: } alpar@1: alpar@1: int gmp_pool_count(void) alpar@1: { if (gmp_pool == NULL) alpar@1: return 0; alpar@1: else alpar@1: return dmp_in_use(gmp_pool).lo; alpar@1: } alpar@1: alpar@1: unsigned short *gmp_get_work(int size) alpar@1: { xassert(size > 0); alpar@1: if (gmp_size < size) alpar@1: { if (gmp_size == 0) alpar@1: { xassert(gmp_work == NULL); alpar@1: gmp_size = 100; alpar@1: } alpar@1: else alpar@1: { xassert(gmp_work != NULL); alpar@1: xfree(gmp_work); alpar@1: } alpar@1: while (gmp_size < size) gmp_size += gmp_size; alpar@1: gmp_work = xcalloc(gmp_size, sizeof(unsigned short)); alpar@1: } alpar@1: return gmp_work; alpar@1: } alpar@1: alpar@1: void gmp_free_mem(void) alpar@1: { if (gmp_pool != NULL) dmp_delete_pool(gmp_pool); alpar@1: if (gmp_work != NULL) xfree(gmp_work); alpar@1: gmp_pool = NULL; alpar@1: gmp_size = 0; alpar@1: gmp_work = NULL; alpar@1: return; alpar@1: } alpar@1: alpar@1: /*====================================================================*/ alpar@1: alpar@1: mpz_t _mpz_init(void) alpar@1: { /* initialize x, and set its value to 0 */ alpar@1: mpz_t x; alpar@1: x = gmp_get_atom(sizeof(struct mpz)); alpar@1: x->val = 0; alpar@1: x->ptr = NULL; alpar@1: return x; alpar@1: } alpar@1: alpar@1: void mpz_clear(mpz_t x) alpar@1: { /* free the space occupied by x */ alpar@1: mpz_set_si(x, 0); alpar@1: xassert(x->ptr == NULL); alpar@1: /* free the number descriptor */ alpar@1: gmp_free_atom(x, sizeof(struct mpz)); alpar@1: return; alpar@1: } alpar@1: alpar@1: void mpz_set(mpz_t z, mpz_t x) alpar@1: { /* set the value of z from x */ alpar@1: struct mpz_seg *e, *ee, *es; alpar@1: if (z != x) alpar@1: { mpz_set_si(z, 0); alpar@1: z->val = x->val; alpar@1: xassert(z->ptr == NULL); alpar@1: for (e = x->ptr, es = NULL; e != NULL; e = e->next) alpar@1: { ee = gmp_get_atom(sizeof(struct mpz_seg)); alpar@1: memcpy(ee->d, e->d, 12); alpar@1: ee->next = NULL; alpar@1: if (z->ptr == NULL) alpar@1: z->ptr = ee; alpar@1: else alpar@1: es->next = ee; alpar@1: es = ee; alpar@1: } alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: void mpz_set_si(mpz_t x, int val) alpar@1: { /* set the value of x to val */ alpar@1: struct mpz_seg *e; alpar@1: /* free existing segments, if any */ alpar@1: while (x->ptr != NULL) alpar@1: { e = x->ptr; alpar@1: x->ptr = e->next; alpar@1: gmp_free_atom(e, sizeof(struct mpz_seg)); alpar@1: } alpar@1: /* assign new value */ alpar@1: if (val == 0x80000000) alpar@1: { /* long format is needed */ alpar@1: x->val = -1; alpar@1: x->ptr = e = gmp_get_atom(sizeof(struct mpz_seg)); alpar@1: memset(e->d, 0, 12); alpar@1: e->d[1] = 0x8000; alpar@1: e->next = NULL; alpar@1: } alpar@1: else alpar@1: { /* short format is enough */ alpar@1: x->val = val; alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: double mpz_get_d(mpz_t x) alpar@1: { /* convert x to a double, truncating if necessary */ alpar@1: struct mpz_seg *e; alpar@1: int j; alpar@1: double val, deg; alpar@1: if (x->ptr == NULL) alpar@1: val = (double)x->val; alpar@1: else alpar@1: { xassert(x->val != 0); alpar@1: val = 0.0; alpar@1: deg = 1.0; alpar@1: for (e = x->ptr; e != NULL; e = e->next) alpar@1: { for (j = 0; j <= 5; j++) alpar@1: { val += deg * (double)((int)e->d[j]); alpar@1: deg *= 65536.0; alpar@1: } alpar@1: } alpar@1: if (x->val < 0) val = - val; alpar@1: } alpar@1: return val; alpar@1: } alpar@1: alpar@1: double mpz_get_d_2exp(int *exp, mpz_t x) alpar@1: { /* convert x to a double, truncating if necessary (i.e. rounding alpar@1: towards zero), and returning the exponent separately; alpar@1: the return value is in the range 0.5 <= |d| < 1 and the alpar@1: exponent is stored to *exp; d*2^exp is the (truncated) x value; alpar@1: if x is zero, the return is 0.0 and 0 is stored to *exp; alpar@1: this is similar to the standard C frexp function */ alpar@1: struct mpz_seg *e; alpar@1: int j, n, n1; alpar@1: double val; alpar@1: if (x->ptr == NULL) alpar@1: val = (double)x->val, n = 0; alpar@1: else alpar@1: { xassert(x->val != 0); alpar@1: val = 0.0, n = 0; alpar@1: for (e = x->ptr; e != NULL; e = e->next) alpar@1: { for (j = 0; j <= 5; j++) alpar@1: { val += (double)((int)e->d[j]); alpar@1: val /= 65536.0, n += 16; alpar@1: } alpar@1: } alpar@1: if (x->val < 0) val = - val; alpar@1: } alpar@1: val = frexp(val, &n1); alpar@1: *exp = n + n1; alpar@1: return val; alpar@1: } alpar@1: alpar@1: void mpz_swap(mpz_t x, mpz_t y) alpar@1: { /* swap the values x and y efficiently */ alpar@1: int val; alpar@1: void *ptr; alpar@1: val = x->val, ptr = x->ptr; alpar@1: x->val = y->val, x->ptr = y->ptr; alpar@1: y->val = val, y->ptr = ptr; alpar@1: return; alpar@1: } alpar@1: alpar@1: static void normalize(mpz_t x) alpar@1: { /* normalize integer x that includes removing non-significant alpar@1: (leading) zeros and converting to short format, if possible */ alpar@1: struct mpz_seg *es, *e; alpar@1: /* if the integer is in short format, it remains unchanged */ alpar@1: if (x->ptr == NULL) alpar@1: { xassert(x->val != 0x80000000); alpar@1: goto done; alpar@1: } alpar@1: xassert(x->val == +1 || x->val == -1); alpar@1: /* find the last (most significant) non-zero segment */ alpar@1: es = NULL; alpar@1: for (e = x->ptr; e != NULL; e = e->next) alpar@1: { if (e->d[0] || e->d[1] || e->d[2] || alpar@1: e->d[3] || e->d[4] || e->d[5]) es = e; alpar@1: } alpar@1: /* if all segments contain zeros, the integer is zero */ alpar@1: if (es == NULL) alpar@1: { mpz_set_si(x, 0); alpar@1: goto done; alpar@1: } alpar@1: /* remove non-significant (leading) zero segments */ alpar@1: while (es->next != NULL) alpar@1: { e = es->next; alpar@1: es->next = e->next; alpar@1: gmp_free_atom(e, sizeof(struct mpz_seg)); alpar@1: } alpar@1: /* convert the integer to short format, if possible */ alpar@1: e = x->ptr; alpar@1: if (e->next == NULL && e->d[1] <= 0x7FFF && alpar@1: !e->d[2] && !e->d[3] && !e->d[4] && !e->d[5]) alpar@1: { int val; alpar@1: val = (int)e->d[0] + ((int)e->d[1] << 16); alpar@1: if (x->val < 0) val = - val; alpar@1: mpz_set_si(x, val); alpar@1: } alpar@1: done: return; alpar@1: } alpar@1: alpar@1: void mpz_add(mpz_t z, mpz_t x, mpz_t y) alpar@1: { /* set z to x + y */ alpar@1: static struct mpz_seg zero = { { 0, 0, 0, 0, 0, 0 }, NULL }; alpar@1: struct mpz_seg dumx, dumy, *ex, *ey, *ez, *es, *ee; alpar@1: int k, sx, sy, sz; alpar@1: unsigned int t; alpar@1: /* if [x] = 0 then [z] = [y] */ alpar@1: if (x->val == 0) alpar@1: { xassert(x->ptr == NULL); alpar@1: mpz_set(z, y); alpar@1: goto done; alpar@1: } alpar@1: /* if [y] = 0 then [z] = [x] */ alpar@1: if (y->val == 0) alpar@1: { xassert(y->ptr == NULL); alpar@1: mpz_set(z, x); alpar@1: goto done; alpar@1: } alpar@1: /* special case when both [x] and [y] are in short format */ alpar@1: if (x->ptr == NULL && y->ptr == NULL) alpar@1: { int xval = x->val, yval = y->val, zval = x->val + y->val; alpar@1: xassert(xval != 0x80000000 && yval != 0x80000000); alpar@1: if (!(xval > 0 && yval > 0 && zval <= 0 || alpar@1: xval < 0 && yval < 0 && zval >= 0)) alpar@1: { mpz_set_si(z, zval); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: /* convert [x] to long format, if necessary */ alpar@1: if (x->ptr == NULL) alpar@1: { xassert(x->val != 0x80000000); alpar@1: if (x->val >= 0) alpar@1: { sx = +1; alpar@1: t = (unsigned int)(+ x->val); alpar@1: } alpar@1: else alpar@1: { sx = -1; alpar@1: t = (unsigned int)(- x->val); alpar@1: } alpar@1: ex = &dumx; alpar@1: ex->d[0] = (unsigned short)t; alpar@1: ex->d[1] = (unsigned short)(t >> 16); alpar@1: ex->d[2] = ex->d[3] = ex->d[4] = ex->d[5] = 0; alpar@1: ex->next = NULL; alpar@1: } alpar@1: else alpar@1: { sx = x->val; alpar@1: xassert(sx == +1 || sx == -1); alpar@1: ex = x->ptr; alpar@1: } alpar@1: /* convert [y] to long format, if necessary */ alpar@1: if (y->ptr == NULL) alpar@1: { xassert(y->val != 0x80000000); alpar@1: if (y->val >= 0) alpar@1: { sy = +1; alpar@1: t = (unsigned int)(+ y->val); alpar@1: } alpar@1: else alpar@1: { sy = -1; alpar@1: t = (unsigned int)(- y->val); alpar@1: } alpar@1: ey = &dumy; alpar@1: ey->d[0] = (unsigned short)t; alpar@1: ey->d[1] = (unsigned short)(t >> 16); alpar@1: ey->d[2] = ey->d[3] = ey->d[4] = ey->d[5] = 0; alpar@1: ey->next = NULL; alpar@1: } alpar@1: else alpar@1: { sy = y->val; alpar@1: xassert(sy == +1 || sy == -1); alpar@1: ey = y->ptr; alpar@1: } alpar@1: /* main fragment */ alpar@1: sz = sx; alpar@1: ez = es = NULL; alpar@1: if (sx > 0 && sy > 0 || sx < 0 && sy < 0) alpar@1: { /* [x] and [y] have identical signs -- addition */ alpar@1: t = 0; alpar@1: for (; ex || ey; ex = ex->next, ey = ey->next) alpar@1: { if (ex == NULL) ex = &zero; alpar@1: if (ey == NULL) ey = &zero; alpar@1: ee = gmp_get_atom(sizeof(struct mpz_seg)); alpar@1: for (k = 0; k <= 5; k++) alpar@1: { t += (unsigned int)ex->d[k]; alpar@1: t += (unsigned int)ey->d[k]; alpar@1: ee->d[k] = (unsigned short)t; alpar@1: t >>= 16; alpar@1: } alpar@1: ee->next = NULL; alpar@1: if (ez == NULL) alpar@1: ez = ee; alpar@1: else alpar@1: es->next = ee; alpar@1: es = ee; alpar@1: } alpar@1: if (t) alpar@1: { /* overflow -- one extra digit is needed */ alpar@1: ee = gmp_get_atom(sizeof(struct mpz_seg)); alpar@1: ee->d[0] = 1; alpar@1: ee->d[1] = ee->d[2] = ee->d[3] = ee->d[4] = ee->d[5] = 0; alpar@1: ee->next = NULL; alpar@1: xassert(es != NULL); alpar@1: es->next = ee; alpar@1: } alpar@1: } alpar@1: else alpar@1: { /* [x] and [y] have different signs -- subtraction */ alpar@1: t = 1; alpar@1: for (; ex || ey; ex = ex->next, ey = ey->next) alpar@1: { if (ex == NULL) ex = &zero; alpar@1: if (ey == NULL) ey = &zero; alpar@1: ee = gmp_get_atom(sizeof(struct mpz_seg)); alpar@1: for (k = 0; k <= 5; k++) alpar@1: { t += (unsigned int)ex->d[k]; alpar@1: t += (0xFFFF - (unsigned int)ey->d[k]); alpar@1: ee->d[k] = (unsigned short)t; alpar@1: t >>= 16; alpar@1: } alpar@1: ee->next = NULL; alpar@1: if (ez == NULL) alpar@1: ez = ee; alpar@1: else alpar@1: es->next = ee; alpar@1: es = ee; alpar@1: } alpar@1: if (!t) alpar@1: { /* |[x]| < |[y]| -- result in complement coding */ alpar@1: sz = - sz; alpar@1: t = 1; alpar@1: for (ee = ez; ee != NULL; ee = ee->next) alpar@1: for (k = 0; k <= 5; k++) alpar@1: { t += (0xFFFF - (unsigned int)ee->d[k]); alpar@1: ee->d[k] = (unsigned short)t; alpar@1: t >>= 16; alpar@1: } alpar@1: } alpar@1: } alpar@1: /* contruct and normalize result */ alpar@1: mpz_set_si(z, 0); alpar@1: z->val = sz; alpar@1: z->ptr = ez; alpar@1: normalize(z); alpar@1: done: return; alpar@1: } alpar@1: alpar@1: void mpz_sub(mpz_t z, mpz_t x, mpz_t y) alpar@1: { /* set z to x - y */ alpar@1: if (x == y) alpar@1: mpz_set_si(z, 0); alpar@1: else alpar@1: { y->val = - y->val; alpar@1: mpz_add(z, x, y); alpar@1: if (y != z) y->val = - y->val; alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: void mpz_mul(mpz_t z, mpz_t x, mpz_t y) alpar@1: { /* set z to x * y */ alpar@1: struct mpz_seg dumx, dumy, *ex, *ey, *es, *e; alpar@1: int sx, sy, k, nx, ny, n; alpar@1: unsigned int t; alpar@1: unsigned short *work, *wx, *wy; alpar@1: /* if [x] = 0 then [z] = 0 */ alpar@1: if (x->val == 0) alpar@1: { xassert(x->ptr == NULL); alpar@1: mpz_set_si(z, 0); alpar@1: goto done; alpar@1: } alpar@1: /* if [y] = 0 then [z] = 0 */ alpar@1: if (y->val == 0) alpar@1: { xassert(y->ptr == NULL); alpar@1: mpz_set_si(z, 0); alpar@1: goto done; alpar@1: } alpar@1: /* special case when both [x] and [y] are in short format */ alpar@1: if (x->ptr == NULL && y->ptr == NULL) alpar@1: { int xval = x->val, yval = y->val, sz = +1; alpar@1: xassert(xval != 0x80000000 && yval != 0x80000000); alpar@1: if (xval < 0) xval = - xval, sz = - sz; alpar@1: if (yval < 0) yval = - yval, sz = - sz; alpar@1: if (xval <= 0x7FFFFFFF / yval) alpar@1: { mpz_set_si(z, sz * (xval * yval)); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: /* convert [x] to long format, if necessary */ alpar@1: if (x->ptr == NULL) alpar@1: { xassert(x->val != 0x80000000); alpar@1: if (x->val >= 0) alpar@1: { sx = +1; alpar@1: t = (unsigned int)(+ x->val); alpar@1: } alpar@1: else alpar@1: { sx = -1; alpar@1: t = (unsigned int)(- x->val); alpar@1: } alpar@1: ex = &dumx; alpar@1: ex->d[0] = (unsigned short)t; alpar@1: ex->d[1] = (unsigned short)(t >> 16); alpar@1: ex->d[2] = ex->d[3] = ex->d[4] = ex->d[5] = 0; alpar@1: ex->next = NULL; alpar@1: } alpar@1: else alpar@1: { sx = x->val; alpar@1: xassert(sx == +1 || sx == -1); alpar@1: ex = x->ptr; alpar@1: } alpar@1: /* convert [y] to long format, if necessary */ alpar@1: if (y->ptr == NULL) alpar@1: { xassert(y->val != 0x80000000); alpar@1: if (y->val >= 0) alpar@1: { sy = +1; alpar@1: t = (unsigned int)(+ y->val); alpar@1: } alpar@1: else alpar@1: { sy = -1; alpar@1: t = (unsigned int)(- y->val); alpar@1: } alpar@1: ey = &dumy; alpar@1: ey->d[0] = (unsigned short)t; alpar@1: ey->d[1] = (unsigned short)(t >> 16); alpar@1: ey->d[2] = ey->d[3] = ey->d[4] = ey->d[5] = 0; alpar@1: ey->next = NULL; alpar@1: } alpar@1: else alpar@1: { sy = y->val; alpar@1: xassert(sy == +1 || sy == -1); alpar@1: ey = y->ptr; alpar@1: } alpar@1: /* determine the number of digits of [x] */ alpar@1: nx = n = 0; alpar@1: for (e = ex; e != NULL; e = e->next) alpar@1: for (k = 0; k <= 5; k++) alpar@1: { n++; alpar@1: if (e->d[k]) nx = n; alpar@1: } alpar@1: xassert(nx > 0); alpar@1: /* determine the number of digits of [y] */ alpar@1: ny = n = 0; alpar@1: for (e = ey; e != NULL; e = e->next) alpar@1: for (k = 0; k <= 5; k++) alpar@1: { n++; alpar@1: if (e->d[k]) ny = n; alpar@1: } alpar@1: xassert(ny > 0); alpar@1: /* we need working array containing at least nx+ny+ny places */ alpar@1: work = gmp_get_work(nx+ny+ny); alpar@1: /* load digits of [x] */ alpar@1: wx = &work[0]; alpar@1: for (n = 0; n < nx; n++) wx[ny+n] = 0; alpar@1: for (n = 0, e = ex; e != NULL; e = e->next) alpar@1: for (k = 0; k <= 5; k++, n++) alpar@1: if (e->d[k]) wx[ny+n] = e->d[k]; alpar@1: /* load digits of [y] */ alpar@1: wy = &work[nx+ny]; alpar@1: for (n = 0; n < ny; n++) wy[n] = 0; alpar@1: for (n = 0, e = ey; e != NULL; e = e->next) alpar@1: for (k = 0; k <= 5; k++, n++) alpar@1: if (e->d[k]) wy[n] = e->d[k]; alpar@1: /* compute [x] * [y] */ alpar@1: bigmul(nx, ny, wx, wy); alpar@1: /* construct and normalize result */ alpar@1: mpz_set_si(z, 0); alpar@1: z->val = sx * sy; alpar@1: es = NULL; alpar@1: k = 6; alpar@1: for (n = 0; n < nx+ny; n++) alpar@1: { if (k > 5) alpar@1: { e = gmp_get_atom(sizeof(struct mpz_seg)); alpar@1: e->d[0] = e->d[1] = e->d[2] = 0; alpar@1: e->d[3] = e->d[4] = e->d[5] = 0; alpar@1: e->next = NULL; alpar@1: if (z->ptr == NULL) alpar@1: z->ptr = e; alpar@1: else alpar@1: es->next = e; alpar@1: es = e; alpar@1: k = 0; alpar@1: } alpar@1: es->d[k++] = wx[n]; alpar@1: } alpar@1: normalize(z); alpar@1: done: return; alpar@1: } alpar@1: alpar@1: void mpz_neg(mpz_t z, mpz_t x) alpar@1: { /* set z to 0 - x */ alpar@1: mpz_set(z, x); alpar@1: z->val = - z->val; alpar@1: return; alpar@1: } alpar@1: alpar@1: void mpz_abs(mpz_t z, mpz_t x) alpar@1: { /* set z to the absolute value of x */ alpar@1: mpz_set(z, x); alpar@1: if (z->val < 0) z->val = - z->val; alpar@1: return; alpar@1: } alpar@1: alpar@1: void mpz_div(mpz_t q, mpz_t r, mpz_t x, mpz_t y) alpar@1: { /* divide x by y, forming quotient q and/or remainder r alpar@1: if q = NULL then quotient is not stored; if r = NULL then alpar@1: remainder is not stored alpar@1: the sign of quotient is determined as in algebra while the alpar@1: sign of remainder is the same as the sign of dividend: alpar@1: +26 : +7 = +3, remainder is +5 alpar@1: -26 : +7 = -3, remainder is -5 alpar@1: +26 : -7 = -3, remainder is +5 alpar@1: -26 : -7 = +3, remainder is -5 */ alpar@1: struct mpz_seg dumx, dumy, *ex, *ey, *es, *e; alpar@1: int sx, sy, k, nx, ny, n; alpar@1: unsigned int t; alpar@1: unsigned short *work, *wx, *wy; alpar@1: /* divide by zero is not allowed */ alpar@1: if (y->val == 0) alpar@1: { xassert(y->ptr == NULL); alpar@1: xfault("mpz_div: divide by zero not allowed\n"); alpar@1: } alpar@1: /* if [x] = 0 then [q] = [r] = 0 */ alpar@1: if (x->val == 0) alpar@1: { xassert(x->ptr == NULL); alpar@1: if (q != NULL) mpz_set_si(q, 0); alpar@1: if (r != NULL) mpz_set_si(r, 0); alpar@1: goto done; alpar@1: } alpar@1: /* special case when both [x] and [y] are in short format */ alpar@1: if (x->ptr == NULL && y->ptr == NULL) alpar@1: { int xval = x->val, yval = y->val; alpar@1: xassert(xval != 0x80000000 && yval != 0x80000000); alpar@1: if (q != NULL) mpz_set_si(q, xval / yval); alpar@1: if (r != NULL) mpz_set_si(r, xval % yval); alpar@1: goto done; alpar@1: } alpar@1: /* convert [x] to long format, if necessary */ alpar@1: if (x->ptr == NULL) alpar@1: { xassert(x->val != 0x80000000); alpar@1: if (x->val >= 0) alpar@1: { sx = +1; alpar@1: t = (unsigned int)(+ x->val); alpar@1: } alpar@1: else alpar@1: { sx = -1; alpar@1: t = (unsigned int)(- x->val); alpar@1: } alpar@1: ex = &dumx; alpar@1: ex->d[0] = (unsigned short)t; alpar@1: ex->d[1] = (unsigned short)(t >> 16); alpar@1: ex->d[2] = ex->d[3] = ex->d[4] = ex->d[5] = 0; alpar@1: ex->next = NULL; alpar@1: } alpar@1: else alpar@1: { sx = x->val; alpar@1: xassert(sx == +1 || sx == -1); alpar@1: ex = x->ptr; alpar@1: } alpar@1: /* convert [y] to long format, if necessary */ alpar@1: if (y->ptr == NULL) alpar@1: { xassert(y->val != 0x80000000); alpar@1: if (y->val >= 0) alpar@1: { sy = +1; alpar@1: t = (unsigned int)(+ y->val); alpar@1: } alpar@1: else alpar@1: { sy = -1; alpar@1: t = (unsigned int)(- y->val); alpar@1: } alpar@1: ey = &dumy; alpar@1: ey->d[0] = (unsigned short)t; alpar@1: ey->d[1] = (unsigned short)(t >> 16); alpar@1: ey->d[2] = ey->d[3] = ey->d[4] = ey->d[5] = 0; alpar@1: ey->next = NULL; alpar@1: } alpar@1: else alpar@1: { sy = y->val; alpar@1: xassert(sy == +1 || sy == -1); alpar@1: ey = y->ptr; alpar@1: } alpar@1: /* determine the number of digits of [x] */ alpar@1: nx = n = 0; alpar@1: for (e = ex; e != NULL; e = e->next) alpar@1: for (k = 0; k <= 5; k++) alpar@1: { n++; alpar@1: if (e->d[k]) nx = n; alpar@1: } alpar@1: xassert(nx > 0); alpar@1: /* determine the number of digits of [y] */ alpar@1: ny = n = 0; alpar@1: for (e = ey; e != NULL; e = e->next) alpar@1: for (k = 0; k <= 5; k++) alpar@1: { n++; alpar@1: if (e->d[k]) ny = n; alpar@1: } alpar@1: xassert(ny > 0); alpar@1: /* if nx < ny then [q] = 0 and [r] = [x] */ alpar@1: if (nx < ny) alpar@1: { if (r != NULL) mpz_set(r, x); alpar@1: if (q != NULL) mpz_set_si(q, 0); alpar@1: goto done; alpar@1: } alpar@1: /* we need working array containing at least nx+ny+1 places */ alpar@1: work = gmp_get_work(nx+ny+1); alpar@1: /* load digits of [x] */ alpar@1: wx = &work[0]; alpar@1: for (n = 0; n < nx; n++) wx[n] = 0; alpar@1: for (n = 0, e = ex; e != NULL; e = e->next) alpar@1: for (k = 0; k <= 5; k++, n++) alpar@1: if (e->d[k]) wx[n] = e->d[k]; alpar@1: /* load digits of [y] */ alpar@1: wy = &work[nx+1]; alpar@1: for (n = 0; n < ny; n++) wy[n] = 0; alpar@1: for (n = 0, e = ey; e != NULL; e = e->next) alpar@1: for (k = 0; k <= 5; k++, n++) alpar@1: if (e->d[k]) wy[n] = e->d[k]; alpar@1: /* compute quotient and remainder */ alpar@1: xassert(wy[ny-1] != 0); alpar@1: bigdiv(nx-ny, ny, wx, wy); alpar@1: /* construct and normalize quotient */ alpar@1: if (q != NULL) alpar@1: { mpz_set_si(q, 0); alpar@1: q->val = sx * sy; alpar@1: es = NULL; alpar@1: k = 6; alpar@1: for (n = ny; n <= nx; n++) alpar@1: { if (k > 5) alpar@1: { e = gmp_get_atom(sizeof(struct mpz_seg)); alpar@1: e->d[0] = e->d[1] = e->d[2] = 0; alpar@1: e->d[3] = e->d[4] = e->d[5] = 0; alpar@1: e->next = NULL; alpar@1: if (q->ptr == NULL) alpar@1: q->ptr = e; alpar@1: else alpar@1: es->next = e; alpar@1: es = e; alpar@1: k = 0; alpar@1: } alpar@1: es->d[k++] = wx[n]; alpar@1: } alpar@1: normalize(q); alpar@1: } alpar@1: /* construct and normalize remainder */ alpar@1: if (r != NULL) alpar@1: { mpz_set_si(r, 0); alpar@1: r->val = sx; alpar@1: es = NULL; alpar@1: k = 6; alpar@1: for (n = 0; n < ny; n++) alpar@1: { if (k > 5) alpar@1: { e = gmp_get_atom(sizeof(struct mpz_seg)); alpar@1: e->d[0] = e->d[1] = e->d[2] = 0; alpar@1: e->d[3] = e->d[4] = e->d[5] = 0; alpar@1: e->next = NULL; alpar@1: if (r->ptr == NULL) alpar@1: r->ptr = e; alpar@1: else alpar@1: es->next = e; alpar@1: es = e; alpar@1: k = 0; alpar@1: } alpar@1: es->d[k++] = wx[n]; alpar@1: } alpar@1: normalize(r); alpar@1: } alpar@1: done: return; alpar@1: } alpar@1: alpar@1: void mpz_gcd(mpz_t z, mpz_t x, mpz_t y) alpar@1: { /* set z to the greatest common divisor of x and y */ alpar@1: /* in case of arbitrary integers GCD(x, y) = GCD(|x|, |y|), and, alpar@1: in particular, GCD(0, 0) = 0 */ alpar@1: mpz_t u, v, r; alpar@1: mpz_init(u); alpar@1: mpz_init(v); alpar@1: mpz_init(r); alpar@1: mpz_abs(u, x); alpar@1: mpz_abs(v, y); alpar@1: while (mpz_sgn(v)) alpar@1: { mpz_div(NULL, r, u, v); alpar@1: mpz_set(u, v); alpar@1: mpz_set(v, r); alpar@1: } alpar@1: mpz_set(z, u); alpar@1: mpz_clear(u); alpar@1: mpz_clear(v); alpar@1: mpz_clear(r); alpar@1: return; alpar@1: } alpar@1: alpar@1: int mpz_cmp(mpz_t x, mpz_t y) alpar@1: { /* compare x and y; return a positive value if x > y, zero if alpar@1: x = y, or a nefative value if x < y */ alpar@1: static struct mpz_seg zero = { { 0, 0, 0, 0, 0, 0 }, NULL }; alpar@1: struct mpz_seg dumx, dumy, *ex, *ey; alpar@1: int cc, sx, sy, k; alpar@1: unsigned int t; alpar@1: if (x == y) alpar@1: { cc = 0; alpar@1: goto done; alpar@1: } alpar@1: /* special case when both [x] and [y] are in short format */ alpar@1: if (x->ptr == NULL && y->ptr == NULL) alpar@1: { int xval = x->val, yval = y->val; alpar@1: xassert(xval != 0x80000000 && yval != 0x80000000); alpar@1: cc = (xval > yval ? +1 : xval < yval ? -1 : 0); alpar@1: goto done; alpar@1: } alpar@1: /* special case when [x] and [y] have different signs */ alpar@1: if (x->val > 0 && y->val <= 0 || x->val == 0 && y->val < 0) alpar@1: { cc = +1; alpar@1: goto done; alpar@1: } alpar@1: if (x->val < 0 && y->val >= 0 || x->val == 0 && y->val > 0) alpar@1: { cc = -1; alpar@1: goto done; alpar@1: } alpar@1: /* convert [x] to long format, if necessary */ alpar@1: if (x->ptr == NULL) alpar@1: { xassert(x->val != 0x80000000); alpar@1: if (x->val >= 0) alpar@1: { sx = +1; alpar@1: t = (unsigned int)(+ x->val); alpar@1: } alpar@1: else alpar@1: { sx = -1; alpar@1: t = (unsigned int)(- x->val); alpar@1: } alpar@1: ex = &dumx; alpar@1: ex->d[0] = (unsigned short)t; alpar@1: ex->d[1] = (unsigned short)(t >> 16); alpar@1: ex->d[2] = ex->d[3] = ex->d[4] = ex->d[5] = 0; alpar@1: ex->next = NULL; alpar@1: } alpar@1: else alpar@1: { sx = x->val; alpar@1: xassert(sx == +1 || sx == -1); alpar@1: ex = x->ptr; alpar@1: } alpar@1: /* convert [y] to long format, if necessary */ alpar@1: if (y->ptr == NULL) alpar@1: { xassert(y->val != 0x80000000); alpar@1: if (y->val >= 0) alpar@1: { sy = +1; alpar@1: t = (unsigned int)(+ y->val); alpar@1: } alpar@1: else alpar@1: { sy = -1; alpar@1: t = (unsigned int)(- y->val); alpar@1: } alpar@1: ey = &dumy; alpar@1: ey->d[0] = (unsigned short)t; alpar@1: ey->d[1] = (unsigned short)(t >> 16); alpar@1: ey->d[2] = ey->d[3] = ey->d[4] = ey->d[5] = 0; alpar@1: ey->next = NULL; alpar@1: } alpar@1: else alpar@1: { sy = y->val; alpar@1: xassert(sy == +1 || sy == -1); alpar@1: ey = y->ptr; alpar@1: } alpar@1: /* main fragment */ alpar@1: xassert(sx > 0 && sy > 0 || sx < 0 && sy < 0); alpar@1: cc = 0; alpar@1: for (; ex || ey; ex = ex->next, ey = ey->next) alpar@1: { if (ex == NULL) ex = &zero; alpar@1: if (ey == NULL) ey = &zero; alpar@1: for (k = 0; k <= 5; k++) alpar@1: { if (ex->d[k] > ey->d[k]) cc = +1; alpar@1: if (ex->d[k] < ey->d[k]) cc = -1; alpar@1: } alpar@1: } alpar@1: if (sx < 0) cc = - cc; alpar@1: done: return cc; alpar@1: } alpar@1: alpar@1: int mpz_sgn(mpz_t x) alpar@1: { /* return +1 if x > 0, 0 if x = 0, and -1 if x < 0 */ alpar@1: int s; alpar@1: s = (x->val > 0 ? +1 : x->val < 0 ? -1 : 0); alpar@1: return s; alpar@1: } alpar@1: alpar@1: int mpz_out_str(void *_fp, int base, mpz_t x) alpar@1: { /* output x on stream fp, as a string in given base; the base alpar@1: may vary from 2 to 36; alpar@1: return the number of bytes written, or if an error occurred, alpar@1: return 0 */ alpar@1: FILE *fp = _fp; alpar@1: mpz_t b, y, r; alpar@1: int n, j, nwr = 0; alpar@1: unsigned char *d; alpar@1: static char *set = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; alpar@1: if (!(2 <= base && base <= 36)) alpar@1: xfault("mpz_out_str: base = %d; invalid base\n", base); alpar@1: mpz_init(b); alpar@1: mpz_set_si(b, base); alpar@1: mpz_init(y); alpar@1: mpz_init(r); alpar@1: /* determine the number of digits */ alpar@1: mpz_abs(y, x); alpar@1: for (n = 0; mpz_sgn(y) != 0; n++) alpar@1: mpz_div(y, NULL, y, b); alpar@1: if (n == 0) n = 1; alpar@1: /* compute the digits */ alpar@1: d = xmalloc(n); alpar@1: mpz_abs(y, x); alpar@1: for (j = 0; j < n; j++) alpar@1: { mpz_div(y, r, y, b); alpar@1: xassert(0 <= r->val && r->val < base && r->ptr == NULL); alpar@1: d[j] = (unsigned char)r->val; alpar@1: } alpar@1: /* output the integer to the stream */ alpar@1: if (fp == NULL) fp = stdout; alpar@1: if (mpz_sgn(x) < 0) alpar@1: fputc('-', fp), nwr++; alpar@1: for (j = n-1; j >= 0; j--) alpar@1: fputc(set[d[j]], fp), nwr++; alpar@1: if (ferror(fp)) nwr = 0; alpar@1: mpz_clear(b); alpar@1: mpz_clear(y); alpar@1: mpz_clear(r); alpar@1: xfree(d); alpar@1: return nwr; alpar@1: } alpar@1: alpar@1: /*====================================================================*/ alpar@1: alpar@1: mpq_t _mpq_init(void) alpar@1: { /* initialize x, and set its value to 0/1 */ alpar@1: mpq_t x; alpar@1: x = gmp_get_atom(sizeof(struct mpq)); alpar@1: x->p.val = 0; alpar@1: x->p.ptr = NULL; alpar@1: x->q.val = 1; alpar@1: x->q.ptr = NULL; alpar@1: return x; alpar@1: } alpar@1: alpar@1: void mpq_clear(mpq_t x) alpar@1: { /* free the space occupied by x */ alpar@1: mpz_set_si(&x->p, 0); alpar@1: xassert(x->p.ptr == NULL); alpar@1: mpz_set_si(&x->q, 0); alpar@1: xassert(x->q.ptr == NULL); alpar@1: /* free the number descriptor */ alpar@1: gmp_free_atom(x, sizeof(struct mpq)); alpar@1: return; alpar@1: } alpar@1: alpar@1: void mpq_canonicalize(mpq_t x) alpar@1: { /* remove any factors that are common to the numerator and alpar@1: denominator of x, and make the denominator positive */ alpar@1: mpz_t f; alpar@1: xassert(x->q.val != 0); alpar@1: if (x->q.val < 0) alpar@1: { mpz_neg(&x->p, &x->p); alpar@1: mpz_neg(&x->q, &x->q); alpar@1: } alpar@1: mpz_init(f); alpar@1: mpz_gcd(f, &x->p, &x->q); alpar@1: if (!(f->val == 1 && f->ptr == NULL)) alpar@1: { mpz_div(&x->p, NULL, &x->p, f); alpar@1: mpz_div(&x->q, NULL, &x->q, f); alpar@1: } alpar@1: mpz_clear(f); alpar@1: return; alpar@1: } alpar@1: alpar@1: void mpq_set(mpq_t z, mpq_t x) alpar@1: { /* set the value of z from x */ alpar@1: if (z != x) alpar@1: { mpz_set(&z->p, &x->p); alpar@1: mpz_set(&z->q, &x->q); alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: void mpq_set_si(mpq_t x, int p, unsigned int q) alpar@1: { /* set the value of x to p/q */ alpar@1: if (q == 0) alpar@1: xfault("mpq_set_si: zero denominator not allowed\n"); alpar@1: mpz_set_si(&x->p, p); alpar@1: xassert(q <= 0x7FFFFFFF); alpar@1: mpz_set_si(&x->q, q); alpar@1: return; alpar@1: } alpar@1: alpar@1: double mpq_get_d(mpq_t x) alpar@1: { /* convert x to a double, truncating if necessary */ alpar@1: int np, nq; alpar@1: double p, q; alpar@1: p = mpz_get_d_2exp(&np, &x->p); alpar@1: q = mpz_get_d_2exp(&nq, &x->q); alpar@1: return ldexp(p / q, np - nq); alpar@1: } alpar@1: alpar@1: void mpq_set_d(mpq_t x, double val) alpar@1: { /* set x to val; there is no rounding, the conversion is exact */ alpar@1: int s, n, d, j; alpar@1: double f; alpar@1: mpz_t temp; alpar@1: xassert(-DBL_MAX <= val && val <= +DBL_MAX); alpar@1: mpq_set_si(x, 0, 1); alpar@1: if (val > 0.0) alpar@1: s = +1; alpar@1: else if (val < 0.0) alpar@1: s = -1; alpar@1: else alpar@1: goto done; alpar@1: f = frexp(fabs(val), &n); alpar@1: /* |val| = f * 2^n, where 0.5 <= f < 1.0 */ alpar@1: mpz_init(temp); alpar@1: while (f != 0.0) alpar@1: { f *= 16.0, n -= 4; alpar@1: d = (int)f; alpar@1: xassert(0 <= d && d <= 15); alpar@1: f -= (double)d; alpar@1: /* x := 16 * x + d */ alpar@1: mpz_set_si(temp, 16); alpar@1: mpz_mul(&x->p, &x->p, temp); alpar@1: mpz_set_si(temp, d); alpar@1: mpz_add(&x->p, &x->p, temp); alpar@1: } alpar@1: mpz_clear(temp); alpar@1: /* x := x * 2^n */ alpar@1: if (n > 0) alpar@1: { for (j = 1; j <= n; j++) alpar@1: mpz_add(&x->p, &x->p, &x->p); alpar@1: } alpar@1: else if (n < 0) alpar@1: { for (j = 1; j <= -n; j++) alpar@1: mpz_add(&x->q, &x->q, &x->q); alpar@1: mpq_canonicalize(x); alpar@1: } alpar@1: if (s < 0) mpq_neg(x, x); alpar@1: done: return; alpar@1: } alpar@1: alpar@1: void mpq_add(mpq_t z, mpq_t x, mpq_t y) alpar@1: { /* set z to x + y */ alpar@1: mpz_t p, q; alpar@1: mpz_init(p); alpar@1: mpz_init(q); alpar@1: mpz_mul(p, &x->p, &y->q); alpar@1: mpz_mul(q, &x->q, &y->p); alpar@1: mpz_add(p, p, q); alpar@1: mpz_mul(q, &x->q, &y->q); alpar@1: mpz_set(&z->p, p); alpar@1: mpz_set(&z->q, q); alpar@1: mpz_clear(p); alpar@1: mpz_clear(q); alpar@1: mpq_canonicalize(z); alpar@1: return; alpar@1: } alpar@1: alpar@1: void mpq_sub(mpq_t z, mpq_t x, mpq_t y) alpar@1: { /* set z to x - y */ alpar@1: mpz_t p, q; alpar@1: mpz_init(p); alpar@1: mpz_init(q); alpar@1: mpz_mul(p, &x->p, &y->q); alpar@1: mpz_mul(q, &x->q, &y->p); alpar@1: mpz_sub(p, p, q); alpar@1: mpz_mul(q, &x->q, &y->q); alpar@1: mpz_set(&z->p, p); alpar@1: mpz_set(&z->q, q); alpar@1: mpz_clear(p); alpar@1: mpz_clear(q); alpar@1: mpq_canonicalize(z); alpar@1: return; alpar@1: } alpar@1: alpar@1: void mpq_mul(mpq_t z, mpq_t x, mpq_t y) alpar@1: { /* set z to x * y */ alpar@1: mpz_mul(&z->p, &x->p, &y->p); alpar@1: mpz_mul(&z->q, &x->q, &y->q); alpar@1: mpq_canonicalize(z); alpar@1: return; alpar@1: } alpar@1: alpar@1: void mpq_div(mpq_t z, mpq_t x, mpq_t y) alpar@1: { /* set z to x / y */ alpar@1: mpz_t p, q; alpar@1: if (mpq_sgn(y) == 0) alpar@1: xfault("mpq_div: zero divisor not allowed\n"); alpar@1: mpz_init(p); alpar@1: mpz_init(q); alpar@1: mpz_mul(p, &x->p, &y->q); alpar@1: mpz_mul(q, &x->q, &y->p); alpar@1: mpz_set(&z->p, p); alpar@1: mpz_set(&z->q, q); alpar@1: mpz_clear(p); alpar@1: mpz_clear(q); alpar@1: mpq_canonicalize(z); alpar@1: return; alpar@1: } alpar@1: alpar@1: void mpq_neg(mpq_t z, mpq_t x) alpar@1: { /* set z to 0 - x */ alpar@1: mpq_set(z, x); alpar@1: mpz_neg(&z->p, &z->p); alpar@1: return; alpar@1: } alpar@1: alpar@1: void mpq_abs(mpq_t z, mpq_t x) alpar@1: { /* set z to the absolute value of x */ alpar@1: mpq_set(z, x); alpar@1: mpz_abs(&z->p, &z->p); alpar@1: xassert(mpz_sgn(&x->q) > 0); alpar@1: return; alpar@1: } alpar@1: alpar@1: int mpq_cmp(mpq_t x, mpq_t y) alpar@1: { /* compare x and y; return a positive value if x > y, zero if alpar@1: x = y, or a nefative value if x < y */ alpar@1: mpq_t temp; alpar@1: int s; alpar@1: mpq_init(temp); alpar@1: mpq_sub(temp, x, y); alpar@1: s = mpq_sgn(temp); alpar@1: mpq_clear(temp); alpar@1: return s; alpar@1: } alpar@1: alpar@1: int mpq_sgn(mpq_t x) alpar@1: { /* return +1 if x > 0, 0 if x = 0, and -1 if x < 0 */ alpar@1: int s; alpar@1: s = mpz_sgn(&x->p); alpar@1: xassert(mpz_sgn(&x->q) > 0); alpar@1: return s; alpar@1: } alpar@1: alpar@1: int mpq_out_str(void *_fp, int base, mpq_t x) alpar@1: { /* output x on stream fp, as a string in given base; the base alpar@1: may vary from 2 to 36; output is in the form 'num/den' or if alpar@1: the denominator is 1 then just 'num'; alpar@1: if the parameter fp is a null pointer, stdout is assumed; alpar@1: return the number of bytes written, or if an error occurred, alpar@1: return 0 */ alpar@1: FILE *fp = _fp; alpar@1: int nwr; alpar@1: if (!(2 <= base && base <= 36)) alpar@1: xfault("mpq_out_str: base = %d; invalid base\n", base); alpar@1: if (fp == NULL) fp = stdout; alpar@1: nwr = mpz_out_str(fp, base, &x->p); alpar@1: if (x->q.val == 1 && x->q.ptr == NULL) alpar@1: ; alpar@1: else alpar@1: { fputc('/', fp), nwr++; alpar@1: nwr += mpz_out_str(fp, base, &x->q); alpar@1: } alpar@1: if (ferror(fp)) nwr = 0; alpar@1: return nwr; alpar@1: } alpar@1: alpar@1: #endif alpar@1: alpar@1: /* eof */