00001 #ifndef LIBSHERPA_BIGNUM_HXX
00002 #define LIBSHERPA_BIGNUM_HXX
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <stdint.h>
00042 #include <stdlib.h>
00043 #include <vector>
00044 #include <iostream>
00045 #include <sstream>
00046
00047 namespace sherpa {
00048
00049 class BigNum {
00050 friend struct nvec;
00051
00052 bool negative;
00053 size_t nDigits;
00054
00055 union {
00056 uint32_t oneDigit;
00057 uint32_t *digits;
00058 };
00059
00062
00063
00064 uint64_t getDigit(size_t i) const
00065 {
00066 if (i >= nDigits)
00067 return 0;
00068 return (nDigits == 1) ? oneDigit : digits[i];
00069 }
00070
00071 uint32_t& theDigit(size_t i)
00072 {
00073 if (nDigits == 1)
00074 return oneDigit;
00075 return digits[i];
00076 }
00077
00078
00079 inline BigNum(size_t nDigits, uint32_t *digits, bool negative = false);
00080
00081 BigNum rshift_digits(size_t nDigits) const;
00082 BigNum lshift_digits(size_t nDigits) const;
00083 public:
00084 ~BigNum();
00085 BigNum()
00086 {
00087 negative = false;
00088 nDigits = 1;
00089 oneDigit = 0;
00090 }
00091
00092 inline BigNum(uint32_t u, bool neg)
00093 {
00094 negative = neg;
00095 nDigits = 1;
00096 oneDigit = u;
00097 }
00098
00099 BigNum(uint64_t u, bool neg);
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 inline BigNum(uint32_t u) {
00111 negative = false;
00112 nDigits = 1;
00113 oneDigit = u;
00114 }
00115 BigNum(uint64_t u);
00116
00117 inline BigNum(int32_t i)
00118 {
00119 negative = (i < 0);
00120 nDigits = 1;
00121 oneDigit = (i < 0) ? -i : i;
00122 }
00123 BigNum(int64_t i);
00124
00125 BigNum(const std::string& s, uint32_t radix = 0);
00126
00127 BigNum(const BigNum&);
00128
00129 BigNum operator+(const BigNum&) const;
00130 BigNum operator-(const BigNum&) const;
00131 BigNum operator*(const BigNum&) const;
00132 BigNum operator/(const BigNum&) const;
00133 BigNum operator%(const BigNum&) const;
00134
00135 inline BigNum& operator+=(const BigNum& other)
00136 {
00137 BigNum tmp = *this + other;
00138 *this = tmp;
00139 return *this;
00140 }
00141 inline BigNum& operator-=(const BigNum& other)
00142 {
00143 BigNum tmp = *this - other;
00144 *this = tmp;
00145 return *this;
00146 }
00147 inline BigNum& operator*=(const BigNum& other)
00148 {
00149 BigNum tmp = *this * other;
00150 *this = tmp;
00151 return *this;
00152 }
00153 inline BigNum& operator/=(const BigNum& other)
00154 {
00155 BigNum tmp = *this / other;
00156 *this = tmp;
00157 return *this;
00158 }
00159 inline BigNum& operator%=(const BigNum& other)
00160 {
00161 BigNum tmp = *this % other;
00162 *this = tmp;
00163 return *this;
00164 }
00165
00166 BigNum& operator=(const BigNum&);
00167
00168 BigNum abs() const;
00169 BigNum neg() const;
00170
00171 inline BigNum operator-() const
00172 {
00173 return this->neg();
00174 }
00175
00176 int cmp(const BigNum& other) const;
00177
00178 inline bool operator<(const BigNum& other) const
00179 {
00180 return cmp(other) < 0;
00181 }
00182
00183 inline bool operator<=(const BigNum& other) const
00184 {
00185 return cmp(other) <= 0;
00186 }
00187 inline bool operator>(const BigNum& other) const
00188 {
00189 return cmp(other) > 0;
00190 }
00191
00192 inline bool operator>=(const BigNum& other) const
00193 {
00194 return cmp(other) >= 0;
00195 }
00196
00197 inline bool operator==(const BigNum& other) const
00198 {
00199 return cmp(other) == 0;
00200 }
00201
00202 inline bool operator!=(const BigNum& other) const
00203 {
00204 return cmp(other) != 0;
00205 }
00206
00207 BigNum operator<<(size_t n);
00208 BigNum operator>>(size_t n);
00209
00210 inline BigNum& operator<<=(size_t n)
00211 {
00212 *this = *this << n;
00213 return *this;
00214 }
00215 inline BigNum& operator>>=(size_t n)
00216 {
00217 *this = *this >> n;
00218 return *this;
00219 }
00220
00221 std::string asString(uint32_t radix = 10) const;
00222
00223 void toStream(std::ostream& strm, uint32_t radix = 10) const;
00224 void fromStream(std::istream& strm, uint32_t radix = 10);
00225
00226 inline uint32_t as_uint32() const
00227 {
00228 return getDigit(0);
00229 }
00230
00231 inline uint64_t as_uint64() const
00232 {
00233 return (getDigit(1) << 32) | getDigit(0);
00234 }
00235 };
00236
00237
00238
00239 inline
00240 std::ostream& operator<<(std::ostream& strm, const sherpa::BigNum& bn)
00241 {
00242 if (strm.flags() & strm.hex)
00243 bn.toStream(strm, 16);
00244 else if (strm.flags() & strm.oct)
00245 bn.toStream(strm, 8);
00246 else
00247 bn.toStream(strm, 10);
00248
00249 return strm;
00250 }
00251
00252 inline
00253 std::istream& operator>>(std::istream& strm, sherpa::BigNum& bn)
00254 {
00255 if (strm.flags() & strm.hex)
00256 bn.fromStream(strm, 16);
00257 else if (strm.flags() & strm.oct)
00258 bn.fromStream(strm, 8);
00259 else
00260 bn.fromStream(strm, 10);
00261
00262 return strm;
00263 }
00264 }
00265
00266 #endif