commented out #include that broke build on windows

This commit is contained in:
Vadim Kurland
2010-11-16 13:49:45 -08:00
parent 773def365f
commit 2e3d1f2f43
+153 -67
View File
@@ -13,6 +13,14 @@
* without express or implied warranty.
*/
/*
* Commented out the line that #includes <bits/ios_base.h>
* and ran through astyle:
* astyle --style=ansi --indent=spaces --convert-tabs uint128.h
*
* --vk 11/16/2010
*/
#ifndef UINT128_20050119_H_
#define UINT128_20050119_H_
@@ -20,7 +28,7 @@
#include <string>
#include <iostream>
#include <bits/ios_base.h>
// #include <bits/ios_base.h>
#include <stdint.h>
#include <limits>
@@ -49,7 +57,10 @@ public:
// constructors for all basic types
uint128() : lo(0), hi(0) {}
uint128(int value) : lo(static_cast<base_type>(value)), hi(0) { if(value < 0) hi = static_cast<base_type>(-1); }
uint128(int value) : lo(static_cast<base_type>(value)), hi(0)
{
if (value < 0) hi = static_cast<base_type>(-1);
}
uint128(unsigned int value) : lo(static_cast<base_type>(value)), hi(0) {}
uint128(float value) : lo(static_cast<base_type>(value)), hi(0) {}
uint128(double value) : lo(static_cast<base_type>(value)), hi(0) {}
@@ -57,10 +68,12 @@ public:
uint128(base_type value) : lo(value), hi(0) {}
uint128(const uint32_t value[4]): lo((uint64_t(value[2])<<32)|value[3]),hi((uint64_t(value[0])<<32)|value[1]) {}
uint128(const std::string &sz) : lo(0), hi(0) {
uint128(const std::string &sz) : lo(0), hi(0)
{
// do we have at least one character?
if (!sz.empty()) {
if (!sz.empty())
{
// make some reasonable assumptions
int radix = 10;
@@ -70,47 +83,69 @@ public:
// check for minus sign, i suppose technically this should only apply
// to base 10, but who says that -0x1 should be invalid?
if (*i == '-') {
if (*i == '-')
{
++i;
minus = true;
}
// check if there is radix changing prefix (0 or 0x)
if(i != sz.end()) {
if (*i == '0') {
if (i != sz.end())
{
if (*i == '0')
{
radix = 8;
++i;
if(i != sz.end()) {
if (*i == 'x') {
if (i != sz.end())
{
if (*i == 'x')
{
radix = 16;
++i;
}
}
}
while(i != sz.end()) {
while (i != sz.end())
{
unsigned int n;
const char ch = *i;
if(ch >= 'A' && ch <= 'Z') {
if(((ch - 'A') + 10) < radix) {
if (ch >= 'A' && ch <= 'Z')
{
if (((ch - 'A') + 10) < radix)
{
n = (ch - 'A') + 10;
} else {
}
else
{
break;
}
} else if(ch >= 'a' && ch <= 'z') {
if(((ch - 'a') + 10) < radix) {
}
else if (ch >= 'a' && ch <= 'z')
{
if (((ch - 'a') + 10) < radix)
{
n = (ch - 'a') + 10;
} else {
}
else
{
break;
}
} else if(ch >= '0' && ch <= '9') {
if((ch - '0') < radix) {
}
else if (ch >= '0' && ch <= '9')
{
if ((ch - '0') < radix)
{
n = (ch - '0');
} else {
}
else
{
break;
}
} else {
}
else
{
/* completely invalid character */
break;
}
@@ -123,14 +158,17 @@ public:
}
// if this was a negative number, do that two's compliment madness :-P
if (minus) {
if (minus)
{
*this = -*this;
}
}
}
Self &operator=(const Self &other) {
if(&other != this) {
Self &operator=(const Self &other)
{
if (&other != this)
{
lo = other.lo;
hi = other.hi;
}
@@ -139,57 +177,70 @@ public:
public: // comparison operators
bool operator==(const Self &o) const {
bool operator==(const Self &o) const
{
return hi == o.hi && lo == o.lo;
}
bool operator!=(const Self &o) const {
bool operator!=(const Self &o) const
{
return hi != o.hi || lo != o.lo;
}
bool operator<(const Self &o) const {
bool operator<(const Self &o) const
{
return (hi == o.hi) ? lo < o.lo : hi < o.hi;
}
bool operator>(const Self &o) const {
bool operator>(const Self &o) const
{
return (hi == o.hi) ? lo > o.lo : hi > o.hi;
}
bool operator<=(const Self &o) const {
bool operator<=(const Self &o) const
{
return *this < o || *this == 0;
}
bool operator>=(const Self &o) const {
bool operator>=(const Self &o) const
{
return *this > o || *this == 0;
}
public: // unary operators
bool operator!() const {
bool operator!() const
{
return !(hi != 0 || lo != 0);
}
Self operator-() const {
Self operator-() const
{
// standard 2's compliment negation
return ~Self(*this) + 1;
}
Self operator~() const {
Self operator~() const
{
Self t(*this);
t.lo = ~t.lo;
t.hi = ~t.hi;
return t;
}
Self &operator++() {
if (++lo == 0) {
Self &operator++()
{
if (++lo == 0)
{
++hi;
}
return *this;
}
Self &operator--() {
if (lo-- == 0) {
Self &operator--()
{
if (lo-- == 0)
{
--hi;
}
return *this;
@@ -197,33 +248,40 @@ public: // unary operators
public: // basic math operators
Self &operator+=(const Self &b) {
Self &operator+=(const Self &b)
{
const base_type old_lo = lo;
lo += b.lo;
hi += b.hi;
if(lo < old_lo) {
if (lo < old_lo)
{
++hi;
}
return *this;
}
Self &operator-=(const Self &b) {
Self &operator-=(const Self &b)
{
// it happens to be way easier to write it
// this way instead of make a subtraction algorithm
return *this += -b;
}
Self &operator*=(const Self &b) {
Self &operator*=(const Self &b)
{
// check for multiply by 0
// result is always 0 :-P
if (b == 0) {
if (b == 0)
{
hi = 0;
lo = 0;
} else if (b != 1) {
}
else if (b != 1)
{
// check we aren't multiplying by 1
@@ -233,8 +291,10 @@ public: // basic math operators
lo = 0;
hi = 0;
for (int i = 0; i < size; ++i) {
if ((t & 1) != 0) {
for (int i = 0; i < size; ++i)
{
if ((t & 1) != 0)
{
*this += (a << i);
}
@@ -245,53 +305,64 @@ public: // basic math operators
return *this;
}
Self &operator|=(const Self &b) {
Self &operator|=(const Self &b)
{
hi |= b.hi;
lo |= b.lo;
return *this;
}
Self &operator&=(const Self &b) {
Self &operator&=(const Self &b)
{
hi &= b.hi;
lo &= b.lo;
return *this;
}
Self &operator^=(const Self &b) {
Self &operator^=(const Self &b)
{
hi ^= b.hi;
lo ^= b.lo;
return *this;
}
Self &operator/=(const Self &b) {
Self &operator/=(const Self &b)
{
Self remainder;
__do_div(*this, b, *this, remainder);
return *this;
}
Self &operator%=(const Self &b) {
Self &operator%=(const Self &b)
{
Self quotient;
__do_div(*this, b, quotient, *this);
return *this;
}
Self &operator<<=(const Self& rhs) {
Self &operator<<=(const Self& rhs)
{
int n = rhs.to_integer();
if(n >= size) {
if (n >= size)
{
hi = 0;
lo = 0;
} else {
}
else
{
const int halfsize = size / 2;
if (n >= halfsize){
if (n >= halfsize)
{
n -= halfsize;
hi = lo;
lo = 0;
}
if (n != 0) {
if (n != 0)
{
// shift high half
hi <<= n;
@@ -308,23 +379,29 @@ public: // basic math operators
return *this;
}
Self &operator>>=(const Self& rhs) {
Self &operator>>=(const Self& rhs)
{
int n = rhs.to_integer();
if(n >= size) {
if (n >= size)
{
hi = 0;
lo = 0;
} else {
}
else
{
const int halfsize = size / 2;
if (n >= halfsize) {
if (n >= halfsize)
{
n -= halfsize;
lo = hi;
hi = 0;
}
if (n != 0) {
if (n != 0)
{
// shift low half
lo >>= n;
@@ -372,11 +449,13 @@ public: // basic math operators
public:
int to_integer() const {
int to_integer() const
{
return static_cast<int>(lo);
}
base_type to_base_type() const {
base_type to_base_type() const
{
return lo;
}
@@ -392,25 +471,32 @@ public:
private:
template <typename T>
static void __do_div(const T &numerator, const T &denominator, T &quotient, T &remainder) {
static void __do_div(const T &numerator, const T &denominator, T &quotient, T &remainder)
{
static const int bits = sizeof(T) * CHAR_BIT;
if(denominator == 0) {
if (denominator == 0)
{
throw std::domain_error("divide by zero");
} else {
}
else
{
T n = numerator;
T d = denominator;
T x = 1;
T answer = 0;
while((n >= d) && (((d >> (bits - 1)) & 1) == 0)) {
while ((n >= d) && (((d >> (bits - 1)) & 1) == 0))
{
x <<= 1;
d <<= 1;
}
while(x != 0) {
if(n >= d) {
while (x != 0)
{
if (n >= d)
{
n -= d;
answer |= x;
}