biginteger.cc 4.37 KB
Newer Older
Antoine Lucas's avatar
import  
Antoine Lucas committed
1
2
3
4
5
6
7
8
9
10
/*! \file biginteger.cc
 *  \brief C function for class biginteger & bigmod
 *
 *  \version 1
 *
 *  \date Created: 27/10/04
 *  \date Last modified: Time-stamp: <2008-02-17 21:42:37 antoine>
 *
 *  \author Immanuel Scholz
 *
11
 *  \note Licence: GPL (>= 2)
Antoine Lucas's avatar
import  
Antoine Lucas committed
12
13
 */

14
#include <cstdio>
Antoine Lucas's avatar
import  
Antoine Lucas committed
15
16
#include <iostream>

17
18
#include "biginteger.h"

Antoine Lucas's avatar
import  
Antoine Lucas committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using std::string;

biginteger::biginteger(const char* raw)
{
  mpz_init(value);
  na = true;
  const int* r = (int*)(raw);
  if (r[0]>0)
    {
      mpz_import(value, r[0], 1, sizeof(int) , 0, 0, (void*)&(r[2] ));
      if(r[1]==-1)
	mpz_neg(value,value);
      na = false;
    }
  else
    mpz_set_si(value, 0);

  //std::cout << "lu: "<<  this->str(10) << std::endl;
}

  /**
   * Create a biginteger from a value. Remember to free the
   * parameter's mpz_t if you allocated them by yourself -
   * biginteger will copy the value.
   */
biginteger::biginteger(const mpz_t value_)
    : na(false)
{
  mpz_init_set(value, value_);
}

/*
 * Convert to string in base b; b from 2 to 36
 */

string biginteger::str(int b) const
{
  if (isNA())
    return "NA";

  // possible minus sign, size of number + '\0'
  char* buf = new char[mpz_sizeinbase(value, b)+2];
  mpz_get_str(buf, b, value);
  string s = buf;
  delete [] buf;
  return s;
}


/**
 * \brief export mpz to R raw value
 */
int biginteger::as_raw(char* raw) const
{
  int totals = raw_size() ;
  memset(raw, 0, totals );

  int* r = (int*)raw;
  r[0] = totals/sizeof(int) - 2;

  if (!isNA())
    {
      r[1] = (int) mpz_sgn(value);
      mpz_export(&r[2], 0, 1, sizeof(int), 0, 0, value);
    }


  return totals;
}

/**
 * \brief export mpz to R raw value
 *
 * return number of byte used (if na => 2*sizeofint, else
 * the two int + size of the big integer)
 *
 * \note IF size of big integer exceed value of one int => cannot store value
 */
int as_raw(char* raw,mpz_t value, bool na)
{
  int numb = 8*sizeof(int);
  int totals = sizeof(int);
  if(!na)
    totals =  sizeof(int) * (2 + (mpz_sizeinbase(value,2)+numb-1) / numb);
  memset(raw, 0, totals );

  int* r = (int*)raw;
  r[0] = totals/sizeof(int) - 2;

  if (!na)
    {
      r[1] = (int) mpz_sgn(value);
      mpz_export(&r[2], 0, 1, sizeof(int), 0, 0, value);
    }

  return totals;

}


// number of int used in R by on biginteger
size_t biginteger::raw_size() const
{
  if (isNA())
    return sizeof(int);

  int numb = 8*sizeof(int);
  return sizeof(int) * (2 + (mpz_sizeinbase(value,2)+numb-1) / numb);

  //  return (sizeof(int) * ( 2 + (mpz_sizeinbase(value,2)/(8*sizeof(int)))  ) );
}

void biginteger::swap(biginteger& other)
{
  mpz_swap(value, other.value);
  bool n = na;
  na = other.na;
  other.na = n;
}


biginteger & biginteger::operator= (const biginteger& rhs)
{
  if(this != &rhs)
    {
      mpz_set(value,rhs.getValueTemp());
      na = rhs.na;
    }
  return(*this);
}


// comparison
bool operator!=(const biginteger& rhs, const biginteger& lhs)
{
  if(rhs.isNA() || lhs.isNA())
    return(false); // SHOULD RETURN NA

  return(mpz_cmp(rhs.getValueTemp(),lhs.getValueTemp())!=0);
}



// comparison
bool operator<(const biginteger& rhs, const biginteger& lhs)
{
  if(rhs.isNA() || lhs.isNA())
    return(false); // SHOULD RETURN NA

  return(mpz_cmp(rhs.getValueTemp(),lhs.getValueTemp())<0);
}



// comparison
bool operator>(const biginteger& rhs, const biginteger& lhs)
{
  if(rhs.isNA() || lhs.isNA())
    return(false); // SHOULD RETURN NA

  return(mpz_cmp(rhs.getValueTemp(),lhs.getValueTemp())>0);
}



// addition
biginteger operator* (const biginteger& rhs, const biginteger& lhs)
{
  // one of them NA: return NA
  if(rhs.isNA() || lhs.isNA())
    return(biginteger());

  mpz_t result;
  mpz_init(result);
  mpz_t_sentry val_s(result);
  mpz_mul(result,rhs.getValueTemp(),lhs.getValueTemp());
  return(biginteger(result));

}

// substraction
biginteger operator- (const biginteger& rhs, const biginteger& lhs)
{
  // one of them NA: return NA
  if(rhs.isNA() || lhs.isNA())
    return(biginteger());

  mpz_t result;
  mpz_init(result);
  mpz_t_sentry val_s(result);
  mpz_sub(result,rhs.getValueTemp(),lhs.getValueTemp());
  return(biginteger(result));
}

// modulus
biginteger operator% (const biginteger& rhs, const biginteger& lhs)
{
  // one of them NA: return NA
  if(rhs.isNA() || lhs.isNA())
    return(biginteger());

  mpz_t result;
  mpz_init(result);
  mpz_t_sentry val_s(result);
  mpz_mod(result,rhs.getValueTemp(),lhs.getValueTemp());
  return(biginteger(result));
}