/usr/include/ITK-4.5/vnl/vnl_bignum.h is in libinsighttoolkit4-dev 4.5.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | // This is core/vnl/vnl_bignum.h
#ifndef vnl_bignum_h_
#define vnl_bignum_h_
//:
// \file
// \brief Infinite precision integers
//
// The vnl_bignum class implements near-infinite precision integers
// and arithmetic by using a dynamic bit vector. A
// vnl_bignum object will grow in size as necessary to hold its
// integer value. Implicit conversion to the system defined
// types: short, int, long, float, double and long double
// is supported by overloaded operator member functions.
// Addition and subtraction operators are performed by
// simple bitwise addition and subtraction on
// unsigned short boundaries with checks for carry flag propagation.
// The multiplication, division, and remainder operations
// utilize the algorithms from Knuth's Volume 2 of "The
// Art of Computer Programming". However, despite the use of
// these algorithms and inline member functions, arithmetic
// operations on vnl_bignum objects are considerably slower than
// the built-in integer types that use hardware integer arithmetic
// capabilities.
//
// The vnl_bignum class supports the parsing of character string
// representations of all the literal number formats, PLUS the
// strings "Infinity", "+Infinity" and "-Infinity". The following
// table shows an example of a character string
// representation on the left and a brief description of the
// interpreted meaning on the right:
//
// Character String Interpreted Meaning
// 1234 1234
// 1234l 1234
// 1234L 1234
// 1234u 1234
// 1234U 1234
// 1234ul 1234
// 1234UL 1234
// 01234 1234 in octal (leading 0)
// 0x1234 1234 in hexadecimal (leading 0x)
// 0X1234 1234 in hexadecimal (leading 0X)
// 123.4 123 (value truncated)
// 1.234e2 123 (exponent expanded/truncated)
// 1.234e-5 0 (truncated value less than 1)
// Infinity +Inf ("maxval", obeying all conventional arithmetic)
//
// \author
// Copyright (C) 1991 Texas Instruments Incorporated.
//
// Permission is granted to any individual or institution to use, copy, modify,
// and distribute this software, provided that this complete copyright and
// permission notice is maintained, intact, in all copies and supporting
// documentation.
//
// Texas Instruments Incorporated provides this software "as is" without
// express or implied warranty.
//
// \verbatim
// Modifications
// Peter Vanroose, 24 January 2002: ported to vnl from COOL
// Peter Vanroose, 7 September 2002: added "Infinity" (incl. all arithmetic)
// Ian Scott, 23 March 2004: made ++ and -- much more efficient.
// Peter Vanroose, March 2008: try to fix divide bug: partially succeeded
// Peter Vanroose, June 2009: finally fixed this long standing divide bug
// \endverbatim
#include <vcl_iostream.h>
#include <vcl_string.h>
class vnl_bignum;
// These are all auxiliary functions:
int magnitude_cmp(const vnl_bignum&, const vnl_bignum&);
void add(const vnl_bignum&, const vnl_bignum&, vnl_bignum&);
void subtract(const vnl_bignum&, const vnl_bignum&, vnl_bignum&);
void multiply_aux(const vnl_bignum&, unsigned short d, vnl_bignum&, unsigned short i);
unsigned short normalize(const vnl_bignum&, const vnl_bignum&, vnl_bignum&, vnl_bignum&);
void divide_aux(const vnl_bignum&, unsigned short, vnl_bignum&, unsigned short&);
unsigned short estimate_q_hat(const vnl_bignum&, const vnl_bignum&, unsigned short);
unsigned short multiply_subtract(vnl_bignum&, const vnl_bignum&, unsigned short, unsigned short);
void divide(const vnl_bignum&, const vnl_bignum&, vnl_bignum&, vnl_bignum&);
vnl_bignum left_shift(const vnl_bignum& b1, int l);
vnl_bignum right_shift(const vnl_bignum& b1, int l);
void decrement (vnl_bignum& bnum);
void increment (vnl_bignum& bnum);
//: formatted output
// \relatesalso vnl_bignum
vcl_ostream& operator<<(vcl_ostream& s, vnl_bignum const& r);
//: simple input
// \relatesalso vnl_bignum
vcl_istream& operator>>(vcl_istream& s, vnl_bignum& r);
//: Infinite precision integers
//
// The vnl_bignum class implements near-infinite precision integers
// and arithmetic by using a dynamic bit vector. A
// vnl_bignum object will grow in size as necessary to hold its
// integer value. Implicit conversion to the system defined
// types: short, int, long, float, double and long double
// is supported by overloaded operator member functions.
// Addition and subtraction operators are performed by
// simple bitwise addition and subtraction on
// unsigned short boundaries with checks for carry flag propagation.
// The multiplication, division, and remainder operations
// utilize the algorithms from Knuth's Volume 2 of "The
// Art of Computer Programming". However, despite the use of
// these algorithms and inline member functions, arithmetic
// operations on vnl_bignum objects are considerably slower than
// the built-in integer types that use hardware integer arithmetic
// capabilities.
//
// The vnl_bignum class supports the parsing of character string
// representations of all the literal number formats, PLUS the
// strings "Infinity", "+Infinity" and "-Infinity". The following
// table shows an example of a character string
// representation on the left and a brief description of the
// interpreted meaning on the right:
//
// Character String Interpreted Meaning
// 1234 1234
// 1234l 1234
// 1234L 1234
// 1234u 1234
// 1234U 1234
// 1234ul 1234
// 1234UL 1234
// 01234 1234 in octal (leading 0)
// 0x1234 1234 in hexadecimal (leading 0x)
// 0X1234 1234 in hexadecimal (leading 0X)
// 123.4 123 (value truncated)
// 1.234e2 123 (exponent expanded/truncated)
// 1.234e-5 0 (truncated value less than 1)
// Infinity +Inf ("maxval", obeying all conventional arithmetic)
//
class vnl_bignum
{
unsigned short count; // Number of data elements (never 0 except for "0")
int sign; // Sign of vnl_bignum (+1 or -1, nothing else!!)
unsigned short* data; // Pointer to data value
public:
vnl_bignum(); // Void constructor
vnl_bignum(long); // Long constructor
vnl_bignum(unsigned long); // Unsigned Long constructor
vnl_bignum(int); // Int constructor
vnl_bignum(unsigned int); // Unsigned Int constructor
vnl_bignum(float); // Float constructor
vnl_bignum(double); // Double constructor
vnl_bignum(long double); // Long Double constructor
vnl_bignum(vnl_bignum const&); // Copy constructor
vnl_bignum(const char*); // String constructor
~vnl_bignum(); // Destructor
operator short() const; // Implicit type conversion
operator int() const; // Implicit type conversion
operator long() const; // Implicit type conversion
operator float() const; // Implicit type conversion
operator double() const; // Implicit type conversion
operator long double() const; // Implicit type conversion
inline operator short() { return ((const vnl_bignum*)this)->operator short(); }
inline operator int() { return ((const vnl_bignum*)this)->operator int(); }
inline operator long() { return ((const vnl_bignum*)this)->operator long(); }
inline operator float() { return ((const vnl_bignum*)this)->operator float(); }
inline operator double() { return ((const vnl_bignum*)this)->operator double(); }
inline operator long double() { return ((const vnl_bignum*)this)->operator long double(); }
vnl_bignum operator-() const; // Unary minus operator
inline vnl_bignum operator+() const { return *this; } // Unary plus operator
vnl_bignum& operator=(const vnl_bignum&); // Assignment operator
vnl_bignum operator<<(int l) const; // Bit shift
vnl_bignum operator>>(int l) const; // Bit shift
vnl_bignum operator+(vnl_bignum const& r) const;
inline vnl_bignum& operator+=(vnl_bignum const& r) { return *this = operator+(r); }
inline vnl_bignum& operator-=(vnl_bignum const& r) { return *this = operator+(-r); }
vnl_bignum& operator*=(vnl_bignum const& r);
vnl_bignum& operator/=(vnl_bignum const& r);
vnl_bignum& operator%=(vnl_bignum const& r);
inline vnl_bignum& operator<<=(int l) { return *this = *this << l; }
inline vnl_bignum& operator>>=(int l) { return *this = *this >> l; }
//: prefix increment (++b)
vnl_bignum& operator++();
//: decrement
vnl_bignum& operator--();
//: postfix increment (b++)
inline vnl_bignum operator++(int) { vnl_bignum b=(*this); operator++(); return b; }
//: decrement
inline vnl_bignum operator--(int) { vnl_bignum b=(*this); operator--(); return b; }
bool operator==(vnl_bignum const&) const; // equality
bool operator< (vnl_bignum const&) const; // less than
inline bool operator!=(vnl_bignum const& r) const { return !operator==(r); }
inline bool operator> (vnl_bignum const& r) const { return r<(*this); }
inline bool operator<=(vnl_bignum const& r) const { return !operator>(r); }
inline bool operator>=(vnl_bignum const& r) const { return !operator<(r); }
inline bool operator==(long r) const { return operator==(vnl_bignum(r)); }
inline bool operator!=(long r) const { return !operator==(vnl_bignum(r)); }
inline bool operator< (long r) const { return operator<(vnl_bignum(r)); }
inline bool operator> (long r) const { return vnl_bignum(r) < (*this); }
inline bool operator<=(long r) const { return !operator>(vnl_bignum(r)); }
inline bool operator>=(long r) const { return !operator<(vnl_bignum(r)); }
inline bool operator==(int r) const { return operator==(long(r)); }
inline bool operator!=(int r) const { return !operator==(long(r)); }
inline bool operator< (int r) const { return operator<(long(r)); }
inline bool operator> (int r) const { return vnl_bignum(long(r)) < (*this); }
inline bool operator<=(int r) const { return !operator>(long(r)); }
inline bool operator>=(int r) const { return !operator<(long(r)); }
inline bool operator==(double r) const { return r == this->operator double(); }
inline bool operator!=(double r) const { return r != this->operator double(); }
inline bool operator< (double r) const { return r > this->operator double(); }
inline bool operator> (double r) const { return r < this->operator double(); }
inline bool operator<=(double r) const { return r >= this->operator double(); }
inline bool operator>=(double r) const { return r <= this->operator double(); }
inline bool operator==(long double r) const { return r == this->operator long double(); }
inline bool operator!=(long double r) const { return r != this->operator long double(); }
inline bool operator< (long double r) const { return r > this->operator long double(); }
inline bool operator> (long double r) const { return r < this->operator long double(); }
inline bool operator<=(long double r) const { return r >= this->operator long double(); }
inline bool operator>=(long double r) const { return r <= this->operator long double(); }
inline vnl_bignum abs() const { return operator<(0L) ? operator-() : *this; }
// "+/-Inf" is represented as: count=1, data[0]=0, sign=+/-1 :
inline bool is_infinity() const { return count==1 && data[0]==0; }
inline bool is_plus_infinity() const { return is_infinity() && sign==1; }
inline bool is_minus_infinity() const { return is_infinity() && sign==-1; }
void dump(vcl_ostream& = vcl_cout) const; // Dump contents of vnl_bignum
friend int magnitude_cmp(const vnl_bignum&, const vnl_bignum&);
friend void add(const vnl_bignum&, const vnl_bignum&, vnl_bignum&);
friend void subtract(const vnl_bignum&, const vnl_bignum&, vnl_bignum&);
friend void increment (vnl_bignum& bnum);
friend void decrement (vnl_bignum& bnum);
friend void multiply_aux(const vnl_bignum&, unsigned short, vnl_bignum&, unsigned short);
friend unsigned short normalize(const vnl_bignum&, const vnl_bignum&, vnl_bignum&, vnl_bignum&);
friend void divide_aux(const vnl_bignum&, unsigned short, vnl_bignum&, unsigned short&);
friend unsigned short estimate_q_hat(const vnl_bignum&, const vnl_bignum&, unsigned short);
friend unsigned short multiply_subtract(vnl_bignum&, const vnl_bignum&, unsigned short, unsigned short);
friend void divide(const vnl_bignum&, const vnl_bignum&, vnl_bignum&, vnl_bignum&);
friend vnl_bignum left_shift(const vnl_bignum& b1, int l);
friend vnl_bignum right_shift(const vnl_bignum& b1, int l);
friend vcl_ostream& operator<< (vcl_ostream&, const vnl_bignum&);
friend vcl_istream& operator>> (vcl_istream&, vnl_bignum&);
friend vcl_string& vnl_bignum_to_string (vcl_string& s, const vnl_bignum& b);
friend vnl_bignum& vnl_bignum_from_string (vnl_bignum& b, const vcl_string& s);
private:
void xtoBigNum(const char *s); // convert hex to vnl_bignum
int dtoBigNum(const char *s); // convert decimal to vnl_bignum
void otoBigNum(const char *s); // convert octal to vnl_bignum
void exptoBigNum(const char *s); // convert exponential to vnl_bignum
void resize(short); // Resize vnl_bignum data
vnl_bignum& trim(); // Trim vnl_bignum data
};
//: Convert the number to a decimal representation in a string.
// \relatesalso vnl_bignum
vcl_string& vnl_bignum_to_string (vcl_string& s, const vnl_bignum& b);
//: Convert the number from a decimal representation in a string.
// \relatesalso vnl_bignum
vnl_bignum& vnl_bignum_from_string (vnl_bignum& b, const vcl_string& s);
//: Returns the sum of two bignum numbers.
// \relatesalso vnl_bignum
inline vnl_bignum operator+(vnl_bignum const& r1, long r2) { return r1+vnl_bignum(r2); }
inline vnl_bignum operator+(vnl_bignum const& r1, int r2) { return r1+long(r2); }
inline vnl_bignum operator+(vnl_bignum const& r1, double r2) { return r1+vnl_bignum(r2); }
inline vnl_bignum operator+(vnl_bignum const& r1, long double r2) { return r1+vnl_bignum(r2); }
inline vnl_bignum operator+(long r2, vnl_bignum const& r1) { return r1 + r2; }
inline vnl_bignum operator+(int r2, vnl_bignum const& r1) { return r1 + r2; }
inline vnl_bignum operator+(double r2, vnl_bignum const& r1) { return r1 + r2; }
inline vnl_bignum operator+(long double r2, vnl_bignum const& r1) { return r1 + r2; }
//: Returns the difference of two bignum numbers.
// \relatesalso vnl_bignum
inline vnl_bignum operator-(vnl_bignum const& r1, vnl_bignum const& r2) { return r1 + (-r2); }
inline vnl_bignum operator-(vnl_bignum const& r1, long r2) { return r1 + (-r2); }
inline vnl_bignum operator-(vnl_bignum const& r1, int r2) { return r1 + (-r2); }
inline vnl_bignum operator-(vnl_bignum const& r1, double r2) { return r1 + (-r2); }
inline vnl_bignum operator-(vnl_bignum const& r1, long double r2) { return r1 + (-r2); }
inline vnl_bignum operator-(long r2, vnl_bignum const& r1) { return -(r1 + (-r2)); }
inline vnl_bignum operator-(int r2, vnl_bignum const& r1) { return -(r1 + (-r2)); }
inline vnl_bignum operator-(double r2, vnl_bignum const& r1) { return -(r1 + (-r2)); }
inline vnl_bignum operator-(long double r2, vnl_bignum const& r1) { return -(r1 + (-r2)); }
//: Returns the product of two bignum numbers.
// \relatesalso vnl_bignum
inline vnl_bignum operator*(vnl_bignum const& r1, vnl_bignum const& r2)
{
vnl_bignum result(r1); return result *= r2;
}
inline vnl_bignum operator*(vnl_bignum const& r1, long r2)
{
vnl_bignum result(r1); return result *= vnl_bignum(r2);
}
inline vnl_bignum operator*(vnl_bignum const& r1, int r2)
{
vnl_bignum result(r1); return result *= (long)r2;
}
inline vnl_bignum operator*(vnl_bignum const& r1, double r2)
{
vnl_bignum result(r1); return result *= (vnl_bignum)r2;
}
inline vnl_bignum operator*(vnl_bignum const& r1, long double r2)
{
vnl_bignum result(r1); return result *= (vnl_bignum)r2;
}
inline vnl_bignum operator*(long r2, vnl_bignum const& r1)
{
vnl_bignum result(r1); return result *= r2;
}
inline vnl_bignum operator*(int r2, vnl_bignum const& r1)
{
vnl_bignum result(r1); return result *= (long)r2;
}
inline vnl_bignum operator*(double r2, vnl_bignum const& r1)
{
vnl_bignum result(r1); return result *= (vnl_bignum)r2;
}
inline vnl_bignum operator*(long double r2, vnl_bignum const& r1)
{
vnl_bignum result(r1); return result *= (vnl_bignum)r2;
}
//: Returns the division of two bignum numbers.
// \relatesalso vnl_bignum
inline vnl_bignum operator/(vnl_bignum const& r1, vnl_bignum const& r2)
{
vnl_bignum result(r1); return result /= r2;
}
inline vnl_bignum operator/(vnl_bignum const& r1, long r2)
{
vnl_bignum result(r1); return result /= r2;
}
inline vnl_bignum operator/(vnl_bignum const& r1, int r2)
{
vnl_bignum result(r1); return result /= (long)r2;
}
inline vnl_bignum operator/(vnl_bignum const& r1, double r2)
{
vnl_bignum result(r1); return result /= (vnl_bignum)r2;
}
inline vnl_bignum operator/(vnl_bignum const& r1, long double r2)
{
vnl_bignum result(r1); return result /= (vnl_bignum)r2;
}
inline vnl_bignum operator/(long r1, vnl_bignum const& r2)
{
vnl_bignum result(r1); return result /= r2;
}
inline vnl_bignum operator/(int r1, vnl_bignum const& r2)
{
vnl_bignum result((long)r1); return result /= r2;
}
inline vnl_bignum operator/(double r1, vnl_bignum const& r2)
{
vnl_bignum result(r1); return result /= r2;
}
inline vnl_bignum operator/(long double r1, vnl_bignum const& r2)
{
vnl_bignum result(r1); return result /= r2;
}
//: Returns the remainder of r1 divided by r2.
// \relatesalso vnl_bignum
inline vnl_bignum operator%(vnl_bignum const& r1, vnl_bignum const& r2)
{
vnl_bignum result(r1); return result %= r2;
}
inline vnl_bignum operator%(vnl_bignum const& r1, long r2)
{
vnl_bignum result(r1); return result %= vnl_bignum(r2);
}
inline vnl_bignum operator%(vnl_bignum const& r1, int r2)
{
vnl_bignum result(r1); return result %= vnl_bignum((long)r2);
}
inline vnl_bignum operator%(long r1, vnl_bignum const& r2)
{
vnl_bignum result(r1); return result %= r2;
}
inline vnl_bignum operator%(int r1, vnl_bignum const& r2)
{
vnl_bignum result((long)r1); return result %= r2;
}
// Miscellaneous operators and functions
inline bool operator==(long r1, vnl_bignum const& r2) { return r2==r1; }
inline bool operator!=(long r1, vnl_bignum const& r2) { return r2!=r1; }
inline bool operator< (long r1, vnl_bignum const& r2) { return r2> r1; }
inline bool operator> (long r1, vnl_bignum const& r2) { return r2< r1; }
inline bool operator<=(long r1, vnl_bignum const& r2) { return r2>=r1; }
inline bool operator>=(long r1, vnl_bignum const& r2) { return r2<=r1; }
inline vnl_bignum vnl_math_abs(vnl_bignum const& x) { return x.abs(); }
inline vnl_bignum vnl_math_squared_magnitude(vnl_bignum const& x) { return x*x; }
inline vnl_bignum vnl_math_sqr(vnl_bignum const& x) { return x*x; }
inline bool vnl_math_isnan(vnl_bignum const& ) { return false; }
inline bool vnl_math_isfinite(vnl_bignum const& x) { return ! x.is_infinity(); }
#endif // vnl_bignum_h_
|