alpar@1: /* glpgmp.h (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: #ifndef GLPGMP_H alpar@1: #define GLPGMP_H alpar@1: alpar@1: #ifdef HAVE_CONFIG_H alpar@1: #include alpar@1: #endif alpar@1: alpar@1: #ifdef HAVE_GMP /* use GNU MP bignum library */ alpar@1: alpar@1: #include alpar@1: alpar@1: #define gmp_pool_count _glp_gmp_pool_count alpar@1: #define gmp_free_mem _glp_gmp_free_mem alpar@1: alpar@1: int gmp_pool_count(void); alpar@1: void gmp_free_mem(void); alpar@1: alpar@1: #else /* use GLPK bignum module */ alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: // INTEGER NUMBERS alpar@1: // alpar@1: // Depending on its magnitude an integer number of arbitrary precision alpar@1: // is represented either in short format or in long format. alpar@1: // alpar@1: // Short format corresponds to the int type and allows representing alpar@1: // integer numbers in the range [-(2^31-1), +(2^31-1)]. Note that for alpar@1: // the most negative number of int type the short format is not used. alpar@1: // alpar@1: // In long format integer numbers are represented using the positional alpar@1: // system with the base (radix) 2^16 = 65536: alpar@1: // alpar@1: // x = (-1)^s sum{j in 0..n-1} d[j] * 65536^j, alpar@1: // alpar@1: // where x is the integer to be represented, s is its sign (+1 or -1), alpar@1: // d[j] are its digits (0 <= d[j] <= 65535). alpar@1: // alpar@1: // RATIONAL NUMBERS alpar@1: // alpar@1: // A rational number is represented as an irreducible fraction: alpar@1: // alpar@1: // p / q, alpar@1: // alpar@1: // where p (numerator) and q (denominator) are integer numbers (q > 0) alpar@1: // having no common divisors. */ alpar@1: alpar@1: struct mpz alpar@1: { /* integer number */ alpar@1: int val; alpar@1: /* if ptr is a null pointer, the number is in short format, and alpar@1: val is its value; otherwise, the number is in long format, and alpar@1: val is its sign (+1 or -1) */ alpar@1: struct mpz_seg *ptr; alpar@1: /* pointer to the linked list of the number segments ordered in alpar@1: ascending of powers of the base */ alpar@1: }; alpar@1: alpar@1: struct mpz_seg alpar@1: { /* integer number segment */ alpar@1: unsigned short d[6]; alpar@1: /* six digits of the number ordered in ascending of powers of the alpar@1: base */ alpar@1: struct mpz_seg *next; alpar@1: /* pointer to the next number segment */ alpar@1: }; alpar@1: alpar@1: struct mpq alpar@1: { /* rational number (p / q) */ alpar@1: struct mpz p; alpar@1: /* numerator */ alpar@1: struct mpz q; alpar@1: /* denominator */ alpar@1: }; alpar@1: alpar@1: typedef struct mpz *mpz_t; alpar@1: typedef struct mpq *mpq_t; alpar@1: alpar@1: #define gmp_get_atom _glp_gmp_get_atom alpar@1: #define gmp_free_atom _glp_gmp_free_atom alpar@1: #define gmp_pool_count _glp_gmp_pool_count alpar@1: #define gmp_get_work _glp_gmp_get_work alpar@1: #define gmp_free_mem _glp_gmp_free_mem alpar@1: alpar@1: #define _mpz_init _glp_mpz_init alpar@1: #define mpz_clear _glp_mpz_clear alpar@1: #define mpz_set _glp_mpz_set alpar@1: #define mpz_set_si _glp_mpz_set_si alpar@1: #define mpz_get_d _glp_mpz_get_d alpar@1: #define mpz_get_d_2exp _glp_mpz_get_d_2exp alpar@1: #define mpz_swap _glp_mpz_swap alpar@1: #define mpz_add _glp_mpz_add alpar@1: #define mpz_sub _glp_mpz_sub alpar@1: #define mpz_mul _glp_mpz_mul alpar@1: #define mpz_neg _glp_mpz_neg alpar@1: #define mpz_abs _glp_mpz_abs alpar@1: #define mpz_div _glp_mpz_div alpar@1: #define mpz_gcd _glp_mpz_gcd alpar@1: #define mpz_cmp _glp_mpz_cmp alpar@1: #define mpz_sgn _glp_mpz_sgn alpar@1: #define mpz_out_str _glp_mpz_out_str alpar@1: alpar@1: #define _mpq_init _glp_mpq_init alpar@1: #define mpq_clear _glp_mpq_clear alpar@1: #define mpq_canonicalize _glp_mpq_canonicalize alpar@1: #define mpq_set _glp_mpq_set alpar@1: #define mpq_set_si _glp_mpq_set_si alpar@1: #define mpq_get_d _glp_mpq_get_d alpar@1: #define mpq_set_d _glp_mpq_set_d alpar@1: #define mpq_add _glp_mpq_add alpar@1: #define mpq_sub _glp_mpq_sub alpar@1: #define mpq_mul _glp_mpq_mul alpar@1: #define mpq_div _glp_mpq_div alpar@1: #define mpq_neg _glp_mpq_neg alpar@1: #define mpq_abs _glp_mpq_abs alpar@1: #define mpq_cmp _glp_mpq_cmp alpar@1: #define mpq_sgn _glp_mpq_sgn alpar@1: #define mpq_out_str _glp_mpq_out_str alpar@1: alpar@1: void *gmp_get_atom(int size); alpar@1: void gmp_free_atom(void *ptr, int size); alpar@1: int gmp_pool_count(void); alpar@1: unsigned short *gmp_get_work(int size); alpar@1: void gmp_free_mem(void); alpar@1: alpar@1: mpz_t _mpz_init(void); alpar@1: #define mpz_init(x) (void)((x) = _mpz_init()) alpar@1: void mpz_clear(mpz_t x); alpar@1: void mpz_set(mpz_t z, mpz_t x); alpar@1: void mpz_set_si(mpz_t x, int val); alpar@1: double mpz_get_d(mpz_t x); alpar@1: double mpz_get_d_2exp(int *exp, mpz_t x); alpar@1: void mpz_swap(mpz_t x, mpz_t y); alpar@1: void mpz_add(mpz_t, mpz_t, mpz_t); alpar@1: void mpz_sub(mpz_t, mpz_t, mpz_t); alpar@1: void mpz_mul(mpz_t, mpz_t, mpz_t); alpar@1: void mpz_neg(mpz_t z, mpz_t x); alpar@1: void mpz_abs(mpz_t z, mpz_t x); alpar@1: void mpz_div(mpz_t q, mpz_t r, mpz_t x, mpz_t y); alpar@1: void mpz_gcd(mpz_t z, mpz_t x, mpz_t y); alpar@1: int mpz_cmp(mpz_t x, mpz_t y); alpar@1: int mpz_sgn(mpz_t x); alpar@1: int mpz_out_str(void *fp, int base, mpz_t x); alpar@1: alpar@1: mpq_t _mpq_init(void); alpar@1: #define mpq_init(x) (void)((x) = _mpq_init()) alpar@1: void mpq_clear(mpq_t x); alpar@1: void mpq_canonicalize(mpq_t x); alpar@1: void mpq_set(mpq_t z, mpq_t x); alpar@1: void mpq_set_si(mpq_t x, int p, unsigned int q); alpar@1: double mpq_get_d(mpq_t x); alpar@1: void mpq_set_d(mpq_t x, double val); alpar@1: void mpq_add(mpq_t z, mpq_t x, mpq_t y); alpar@1: void mpq_sub(mpq_t z, mpq_t x, mpq_t y); alpar@1: void mpq_mul(mpq_t z, mpq_t x, mpq_t y); alpar@1: void mpq_div(mpq_t z, mpq_t x, mpq_t y); alpar@1: void mpq_neg(mpq_t z, mpq_t x); alpar@1: void mpq_abs(mpq_t z, mpq_t x); alpar@1: int mpq_cmp(mpq_t x, mpq_t y); alpar@1: int mpq_sgn(mpq_t x); alpar@1: int mpq_out_str(void *fp, int base, mpq_t x); alpar@1: alpar@1: #endif alpar@1: alpar@1: #endif alpar@1: alpar@1: /* eof */