| 1 | /* glpgmp.h (bignum arithmetic) */ |
| 2 | |
| 3 | /*********************************************************************** |
| 4 | * This code is part of GLPK (GNU Linear Programming Kit). |
| 5 | * |
| 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: <[email protected]>. |
| 10 | * |
| 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. |
| 15 | * |
| 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. |
| 20 | * |
| 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 | ***********************************************************************/ |
| 24 | |
| 25 | #ifndef GLPGMP_H |
| 26 | #define GLPGMP_H |
| 27 | |
| 28 | #ifdef HAVE_CONFIG_H |
| 29 | #include <config.h> |
| 30 | #endif |
| 31 | |
| 32 | #ifdef HAVE_GMP /* use GNU MP bignum library */ |
| 33 | |
| 34 | #include <gmp.h> |
| 35 | |
| 36 | #define gmp_pool_count _glp_gmp_pool_count |
| 37 | #define gmp_free_mem _glp_gmp_free_mem |
| 38 | |
| 39 | int gmp_pool_count(void); |
| 40 | void gmp_free_mem(void); |
| 41 | |
| 42 | #else /* use GLPK bignum module */ |
| 43 | |
| 44 | /*---------------------------------------------------------------------- |
| 45 | // INTEGER NUMBERS |
| 46 | // |
| 47 | // Depending on its magnitude an integer number of arbitrary precision |
| 48 | // is represented either in short format or in long format. |
| 49 | // |
| 50 | // Short format corresponds to the int type and allows representing |
| 51 | // integer numbers in the range [-(2^31-1), +(2^31-1)]. Note that for |
| 52 | // the most negative number of int type the short format is not used. |
| 53 | // |
| 54 | // In long format integer numbers are represented using the positional |
| 55 | // system with the base (radix) 2^16 = 65536: |
| 56 | // |
| 57 | // x = (-1)^s sum{j in 0..n-1} d[j] * 65536^j, |
| 58 | // |
| 59 | // where x is the integer to be represented, s is its sign (+1 or -1), |
| 60 | // d[j] are its digits (0 <= d[j] <= 65535). |
| 61 | // |
| 62 | // RATIONAL NUMBERS |
| 63 | // |
| 64 | // A rational number is represented as an irreducible fraction: |
| 65 | // |
| 66 | // p / q, |
| 67 | // |
| 68 | // where p (numerator) and q (denominator) are integer numbers (q > 0) |
| 69 | // having no common divisors. */ |
| 70 | |
| 71 | struct mpz |
| 72 | { /* integer number */ |
| 73 | int val; |
| 74 | /* if ptr is a null pointer, the number is in short format, and |
| 75 | val is its value; otherwise, the number is in long format, and |
| 76 | val is its sign (+1 or -1) */ |
| 77 | struct mpz_seg *ptr; |
| 78 | /* pointer to the linked list of the number segments ordered in |
| 79 | ascending of powers of the base */ |
| 80 | }; |
| 81 | |
| 82 | struct mpz_seg |
| 83 | { /* integer number segment */ |
| 84 | unsigned short d[6]; |
| 85 | /* six digits of the number ordered in ascending of powers of the |
| 86 | base */ |
| 87 | struct mpz_seg *next; |
| 88 | /* pointer to the next number segment */ |
| 89 | }; |
| 90 | |
| 91 | struct mpq |
| 92 | { /* rational number (p / q) */ |
| 93 | struct mpz p; |
| 94 | /* numerator */ |
| 95 | struct mpz q; |
| 96 | /* denominator */ |
| 97 | }; |
| 98 | |
| 99 | typedef struct mpz *mpz_t; |
| 100 | typedef struct mpq *mpq_t; |
| 101 | |
| 102 | #define gmp_get_atom _glp_gmp_get_atom |
| 103 | #define gmp_free_atom _glp_gmp_free_atom |
| 104 | #define gmp_pool_count _glp_gmp_pool_count |
| 105 | #define gmp_get_work _glp_gmp_get_work |
| 106 | #define gmp_free_mem _glp_gmp_free_mem |
| 107 | |
| 108 | #define _mpz_init _glp_mpz_init |
| 109 | #define mpz_clear _glp_mpz_clear |
| 110 | #define mpz_set _glp_mpz_set |
| 111 | #define mpz_set_si _glp_mpz_set_si |
| 112 | #define mpz_get_d _glp_mpz_get_d |
| 113 | #define mpz_get_d_2exp _glp_mpz_get_d_2exp |
| 114 | #define mpz_swap _glp_mpz_swap |
| 115 | #define mpz_add _glp_mpz_add |
| 116 | #define mpz_sub _glp_mpz_sub |
| 117 | #define mpz_mul _glp_mpz_mul |
| 118 | #define mpz_neg _glp_mpz_neg |
| 119 | #define mpz_abs _glp_mpz_abs |
| 120 | #define mpz_div _glp_mpz_div |
| 121 | #define mpz_gcd _glp_mpz_gcd |
| 122 | #define mpz_cmp _glp_mpz_cmp |
| 123 | #define mpz_sgn _glp_mpz_sgn |
| 124 | #define mpz_out_str _glp_mpz_out_str |
| 125 | |
| 126 | #define _mpq_init _glp_mpq_init |
| 127 | #define mpq_clear _glp_mpq_clear |
| 128 | #define mpq_canonicalize _glp_mpq_canonicalize |
| 129 | #define mpq_set _glp_mpq_set |
| 130 | #define mpq_set_si _glp_mpq_set_si |
| 131 | #define mpq_get_d _glp_mpq_get_d |
| 132 | #define mpq_set_d _glp_mpq_set_d |
| 133 | #define mpq_add _glp_mpq_add |
| 134 | #define mpq_sub _glp_mpq_sub |
| 135 | #define mpq_mul _glp_mpq_mul |
| 136 | #define mpq_div _glp_mpq_div |
| 137 | #define mpq_neg _glp_mpq_neg |
| 138 | #define mpq_abs _glp_mpq_abs |
| 139 | #define mpq_cmp _glp_mpq_cmp |
| 140 | #define mpq_sgn _glp_mpq_sgn |
| 141 | #define mpq_out_str _glp_mpq_out_str |
| 142 | |
| 143 | void *gmp_get_atom(int size); |
| 144 | void gmp_free_atom(void *ptr, int size); |
| 145 | int gmp_pool_count(void); |
| 146 | unsigned short *gmp_get_work(int size); |
| 147 | void gmp_free_mem(void); |
| 148 | |
| 149 | mpz_t _mpz_init(void); |
| 150 | #define mpz_init(x) (void)((x) = _mpz_init()) |
| 151 | void mpz_clear(mpz_t x); |
| 152 | void mpz_set(mpz_t z, mpz_t x); |
| 153 | void mpz_set_si(mpz_t x, int val); |
| 154 | double mpz_get_d(mpz_t x); |
| 155 | double mpz_get_d_2exp(int *exp, mpz_t x); |
| 156 | void mpz_swap(mpz_t x, mpz_t y); |
| 157 | void mpz_add(mpz_t, mpz_t, mpz_t); |
| 158 | void mpz_sub(mpz_t, mpz_t, mpz_t); |
| 159 | void mpz_mul(mpz_t, mpz_t, mpz_t); |
| 160 | void mpz_neg(mpz_t z, mpz_t x); |
| 161 | void mpz_abs(mpz_t z, mpz_t x); |
| 162 | void mpz_div(mpz_t q, mpz_t r, mpz_t x, mpz_t y); |
| 163 | void mpz_gcd(mpz_t z, mpz_t x, mpz_t y); |
| 164 | int mpz_cmp(mpz_t x, mpz_t y); |
| 165 | int mpz_sgn(mpz_t x); |
| 166 | int mpz_out_str(void *fp, int base, mpz_t x); |
| 167 | |
| 168 | mpq_t _mpq_init(void); |
| 169 | #define mpq_init(x) (void)((x) = _mpq_init()) |
| 170 | void mpq_clear(mpq_t x); |
| 171 | void mpq_canonicalize(mpq_t x); |
| 172 | void mpq_set(mpq_t z, mpq_t x); |
| 173 | void mpq_set_si(mpq_t x, int p, unsigned int q); |
| 174 | double mpq_get_d(mpq_t x); |
| 175 | void mpq_set_d(mpq_t x, double val); |
| 176 | void mpq_add(mpq_t z, mpq_t x, mpq_t y); |
| 177 | void mpq_sub(mpq_t z, mpq_t x, mpq_t y); |
| 178 | void mpq_mul(mpq_t z, mpq_t x, mpq_t y); |
| 179 | void mpq_div(mpq_t z, mpq_t x, mpq_t y); |
| 180 | void mpq_neg(mpq_t z, mpq_t x); |
| 181 | void mpq_abs(mpq_t z, mpq_t x); |
| 182 | int mpq_cmp(mpq_t x, mpq_t y); |
| 183 | int mpq_sgn(mpq_t x); |
| 184 | int mpq_out_str(void *fp, int base, mpq_t x); |
| 185 | |
| 186 | #endif |
| 187 | |
| 188 | #endif |
| 189 | |
| 190 | /* eof */ |