slow boob

This commit is contained in:
malloc 2018-03-22 22:27:13 -05:00
parent bebd2def31
commit e0f604bb04
3 changed files with 37 additions and 24 deletions

View file

@ -50,14 +50,29 @@ int main(int argc, char **argv) {
sosc::BigUInt a, b; sosc::BigUInt a, b;
a.Parse("ff0000"); a.Parse("368BEADA711E83274DAF974D1A1A10EB915023D016CFC8BFAB58E5D848CF8D45");
b.Parse("fff");
//assert(a - b == sosc::BigUInt("feff01")); //assert(a - b == sosc::BigUInt("feff01"));
//a += b; //auto d = sosc::BigUInt::DivideWithRemainder(a, b);
a *= b;
std::cout << a.ToString() << std::endl; /*for(int i = a.ByteCount() * 8 - 1; i >= 0; --i) {
std::cout << a.GetBit(i);
b.SetBit(i, a.GetBit(i));
}*/
//std::cout << sosc::BigUInt::GenerateRandomPrime(64).ToString();
//std::cout << a.IsProbablePrime();
for(int i = 0; i < 250; ++i)
a*a;
//std::cout << a.ToString();
/*std::cout << std::endl << std::endl
<< d.result.ToString()
<< std::endl
<< d.remainder.ToString();*/
return 0; return 0;
} }

View file

@ -159,7 +159,7 @@ sosc::division_t sosc::BigUInt::DivideWithRemainder
for(size_t i = num.ByteCount() * 8 - 1;; --i) { for(size_t i = num.ByteCount() * 8 - 1;; --i) {
remainder = remainder << 1; remainder = remainder << 1;
remainder.SetBit(0, num.GetBit(i)); remainder.SetBit(0, num.GetBit(i));
if(remainder >= denom) { if(remainder >= denom) {
remainder -= denom; remainder -= denom;
quotient.SetBit(i, true); quotient.SetBit(i, true);
@ -179,13 +179,17 @@ sosc::BigUInt sosc::BigUInt::ModPow
BigUInt x = exp; BigUInt x = exp;
BigUInt bpow = base; BigUInt bpow = base;
uint64_t iterations = 0;
while(!x.IsZero()) { while(!x.IsZero()) {
++iterations;
if(!x.IsEven()) if(!x.IsEven())
accum = (accum * bpow) % mod; accum = (accum * bpow) % mod;
x = x >> 1; x = x >> 1;
bpow = (bpow * bpow) % mod; bpow = (bpow * bpow) % mod;
} }
std::cout << std::endl << iterations << std::endl;
return accum; return accum;
} }
@ -323,14 +327,9 @@ bool sosc::BigUInt::operator == (const BigUInt& rhs) const {
if(this->UsedByteCount() != rhs.UsedByteCount()) if(this->UsedByteCount() != rhs.UsedByteCount())
return false; return false;
size_t cmp_range = std::max(this->ByteCount(), rhs.ByteCount()); for(size_t i = 0; i < this->UsedByteCount(); ++i)
for(size_t i = 0; i < cmp_range; ++i) { if(this->value[i] != rhs.value[i])
uint8_t a = i < this->ByteCount() ? this->value[i] : 0;
uint8_t b = i < rhs.ByteCount() ? rhs.value[i] : 0;
if(a != b)
return false; return false;
}
return true; return true;
} }
@ -342,20 +341,17 @@ bool sosc::BigUInt::operator != (const BigUInt& rhs) const {
bool sosc::BigUInt::operator > (const BigUInt& rhs) const { bool sosc::BigUInt::operator > (const BigUInt& rhs) const {
if(this->UsedByteCount() < rhs.UsedByteCount()) if(this->UsedByteCount() < rhs.UsedByteCount())
return false; return false;
if(this->UsedByteCount() > rhs.UsedByteCount())
return true;
size_t cmp_range = std::max(this->ByteCount(), rhs.ByteCount()); size_t msb = this->UsedByteCount() - 1;
for(size_t i = msb;; --i) {
for(size_t i = cmp_range - 1;; --i) { if(this->value[i] != rhs.value[i])
uint8_t a = i < this->ByteCount() ? this->value[i] : 0; return this->value[i] > rhs.value[i];
uint8_t b = i < rhs.ByteCount() ? rhs.value[i] : 0;
if(a > b)
return true;
if(i == 0) if(i == 0)
break; return false;
} }
return false;
} }
bool sosc::BigUInt::operator >= (const BigUInt& rhs) const { bool sosc::BigUInt::operator >= (const BigUInt& rhs) const {
@ -419,7 +415,7 @@ sosc::BigUInt sosc::BigUInt::operator << (const uint64_t& rhs) const {
} }
if(carry != 0) if(carry != 0)
this_v.push_back(carry); buffer.push_back(carry);
} }
sosc::BigUInt shifted; sosc::BigUInt shifted;

View file

@ -1,6 +1,8 @@
#ifndef SOSC_UTIL_BIGINT_H #ifndef SOSC_UTIL_BIGINT_H
#define SOSC_UTIL_BIGINT_H #define SOSC_UTIL_BIGINT_H
#include <iostream>
#include <cassert> #include <cassert>
#include <tuple> #include <tuple>
#include <string> #include <string>