src/glpgmp.h
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 06 Dec 2010 13:09:21 +0100
changeset 1 c445c931472f
permissions -rw-r--r--
Import glpk-4.45

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