glpgmp.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* glpgmp.h (bignum arithmetic) */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
  6. * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
  7. * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
  8. * E-mail: <mao@gnu.org>.
  9. *
  10. * GLPK is free software: you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  18. * License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************/
  23. #ifndef GLPGMP_H
  24. #define GLPGMP_H
  25. #ifdef HAVE_CONFIG_H
  26. #include <config.h>
  27. #endif
  28. #ifdef HAVE_GMP /* use GNU MP bignum library */
  29. #include <gmp.h>
  30. #define gmp_pool_count _glp_gmp_pool_count
  31. #define gmp_free_mem _glp_gmp_free_mem
  32. int gmp_pool_count(void);
  33. void gmp_free_mem(void);
  34. #else /* use GLPK bignum module */
  35. /*----------------------------------------------------------------------
  36. // INTEGER NUMBERS
  37. //
  38. // Depending on its magnitude an integer number of arbitrary precision
  39. // is represented either in short format or in long format.
  40. //
  41. // Short format corresponds to the int type and allows representing
  42. // integer numbers in the range [-(2^31-1), +(2^31-1)]. Note that for
  43. // the most negative number of int type the short format is not used.
  44. //
  45. // In long format integer numbers are represented using the positional
  46. // system with the base (radix) 2^16 = 65536:
  47. //
  48. // x = (-1)^s sum{j in 0..n-1} d[j] * 65536^j,
  49. //
  50. // where x is the integer to be represented, s is its sign (+1 or -1),
  51. // d[j] are its digits (0 <= d[j] <= 65535).
  52. //
  53. // RATIONAL NUMBERS
  54. //
  55. // A rational number is represented as an irreducible fraction:
  56. //
  57. // p / q,
  58. //
  59. // where p (numerator) and q (denominator) are integer numbers (q > 0)
  60. // having no common divisors. */
  61. struct mpz
  62. { /* integer number */
  63. int val;
  64. /* if ptr is a null pointer, the number is in short format, and
  65. val is its value; otherwise, the number is in long format, and
  66. val is its sign (+1 or -1) */
  67. struct mpz_seg *ptr;
  68. /* pointer to the linked list of the number segments ordered in
  69. ascending of powers of the base */
  70. };
  71. struct mpz_seg
  72. { /* integer number segment */
  73. unsigned short d[6];
  74. /* six digits of the number ordered in ascending of powers of the
  75. base */
  76. struct mpz_seg *next;
  77. /* pointer to the next number segment */
  78. };
  79. struct mpq
  80. { /* rational number (p / q) */
  81. struct mpz p;
  82. /* numerator */
  83. struct mpz q;
  84. /* denominator */
  85. };
  86. typedef struct mpz *mpz_t;
  87. typedef struct mpq *mpq_t;
  88. #define gmp_get_atom _glp_gmp_get_atom
  89. #define gmp_free_atom _glp_gmp_free_atom
  90. #define gmp_pool_count _glp_gmp_pool_count
  91. #define gmp_get_work _glp_gmp_get_work
  92. #define gmp_free_mem _glp_gmp_free_mem
  93. #define _mpz_init _glp_mpz_init
  94. #define mpz_clear _glp_mpz_clear
  95. #define mpz_set _glp_mpz_set
  96. #define mpz_set_si _glp_mpz_set_si
  97. #define mpz_get_d _glp_mpz_get_d
  98. #define mpz_get_d_2exp _glp_mpz_get_d_2exp
  99. #define mpz_swap _glp_mpz_swap
  100. #define mpz_add _glp_mpz_add
  101. #define mpz_sub _glp_mpz_sub
  102. #define mpz_mul _glp_mpz_mul
  103. #define mpz_neg _glp_mpz_neg
  104. #define mpz_abs _glp_mpz_abs
  105. #define mpz_div _glp_mpz_div
  106. #define mpz_gcd _glp_mpz_gcd
  107. #define mpz_cmp _glp_mpz_cmp
  108. #define mpz_sgn _glp_mpz_sgn
  109. #define mpz_out_str _glp_mpz_out_str
  110. #define _mpq_init _glp_mpq_init
  111. #define mpq_clear _glp_mpq_clear
  112. #define mpq_canonicalize _glp_mpq_canonicalize
  113. #define mpq_set _glp_mpq_set
  114. #define mpq_set_si _glp_mpq_set_si
  115. #define mpq_get_d _glp_mpq_get_d
  116. #define mpq_set_d _glp_mpq_set_d
  117. #define mpq_add _glp_mpq_add
  118. #define mpq_sub _glp_mpq_sub
  119. #define mpq_mul _glp_mpq_mul
  120. #define mpq_div _glp_mpq_div
  121. #define mpq_neg _glp_mpq_neg
  122. #define mpq_abs _glp_mpq_abs
  123. #define mpq_cmp _glp_mpq_cmp
  124. #define mpq_sgn _glp_mpq_sgn
  125. #define mpq_out_str _glp_mpq_out_str
  126. void *gmp_get_atom(int size);
  127. void gmp_free_atom(void *ptr, int size);
  128. int gmp_pool_count(void);
  129. unsigned short *gmp_get_work(int size);
  130. void gmp_free_mem(void);
  131. mpz_t _mpz_init(void);
  132. #define mpz_init(x) (void)((x) = _mpz_init())
  133. void mpz_clear(mpz_t x);
  134. void mpz_set(mpz_t z, mpz_t x);
  135. void mpz_set_si(mpz_t x, int val);
  136. double mpz_get_d(mpz_t x);
  137. double mpz_get_d_2exp(int *exp, mpz_t x);
  138. void mpz_swap(mpz_t x, mpz_t y);
  139. void mpz_add(mpz_t, mpz_t, mpz_t);
  140. void mpz_sub(mpz_t, mpz_t, mpz_t);
  141. void mpz_mul(mpz_t, mpz_t, mpz_t);
  142. void mpz_neg(mpz_t z, mpz_t x);
  143. void mpz_abs(mpz_t z, mpz_t x);
  144. void mpz_div(mpz_t q, mpz_t r, mpz_t x, mpz_t y);
  145. void mpz_gcd(mpz_t z, mpz_t x, mpz_t y);
  146. int mpz_cmp(mpz_t x, mpz_t y);
  147. int mpz_sgn(mpz_t x);
  148. int mpz_out_str(void *fp, int base, mpz_t x);
  149. mpq_t _mpq_init(void);
  150. #define mpq_init(x) (void)((x) = _mpq_init())
  151. void mpq_clear(mpq_t x);
  152. void mpq_canonicalize(mpq_t x);
  153. void mpq_set(mpq_t z, mpq_t x);
  154. void mpq_set_si(mpq_t x, int p, unsigned int q);
  155. double mpq_get_d(mpq_t x);
  156. void mpq_set_d(mpq_t x, double val);
  157. void mpq_add(mpq_t z, mpq_t x, mpq_t y);
  158. void mpq_sub(mpq_t z, mpq_t x, mpq_t y);
  159. void mpq_mul(mpq_t z, mpq_t x, mpq_t y);
  160. void mpq_div(mpq_t z, mpq_t x, mpq_t y);
  161. void mpq_neg(mpq_t z, mpq_t x);
  162. void mpq_abs(mpq_t z, mpq_t x);
  163. int mpq_cmp(mpq_t x, mpq_t y);
  164. int mpq_sgn(mpq_t x);
  165. int mpq_out_str(void *fp, int base, mpq_t x);
  166. #endif
  167. #endif
  168. /* eof */