static uint128 sigma1_sum(const uint64 n) { uint32 v = isqrt(n); uint128 ret = 0; for (uint32 i = 1; i <= v; ++i) { uint64 t = n / i; ret += uint128(t) * (uint64(2) * i + t + 1); } ret -= uint128(v) * v * (v + 1); return ret >> 1; }
FastDivision<uint64>::FastDivision(uint64 n) { // n should be < 2^63. if (n == 1) { shamt = 0; magic_num = 1; } else { shamt = 127 - clz(n - 1); magic_num = div128_64_small((uint128(1) << shamt) + n - 1, n); } mod = n; }
real128& real128::operator /= ( const real128& o ) { try { FC_ASSERT( o.fixed > uint128(0), "Divide by Zero" ); fc::bigint self(fixed); fc::bigint other(o.fixed); self *= FC_REAL128_PRECISION; self /= other; fixed = self; return *this; } FC_CAPTURE_AND_RETHROW( (*this)(o) ) }
uint128 RippleAddress::getSeed() const { switch (nVersion) { case VER_NONE: throw std::runtime_error("unset source - getSeed"); case VER_FAMILY_SEED: return uint128(vchData); default: throw std::runtime_error(str(boost::format("bad source: %d") % int(nVersion))); } }
uint128 STObject::getFieldH128 (SField::ref field) const { const SerializedType* rf = peekAtPField (field); if (!rf) throw std::runtime_error ("Field not found"); SerializedTypeID id = rf->getSType (); if (id == STI_NOTPRESENT) return uint128 (); // optional field not present const STHash128* cf = dynamic_cast<const STHash128*> (rf); if (!cf) throw std::runtime_error ("Wrong field type"); return cf->getValue (); }
uint128::operator std::string ()const { if(*this == 0) { return "0"; } // at worst it will be size digits (base 2) so make our buffer // that plus room for null terminator static char sz [128 + 1]; sz[sizeof(sz) - 1] = '\0'; uint128 ii(*this); int i = 128 - 1; while (ii != 0 && i) { uint128 remainder; divide(ii, uint128(10), ii, remainder); sz [--i] = "0123456789abcdefghijklmnopqrstuvwxyz"[remainder.to_integer()]; } return &sz[i]; }
friend uint128 operator << ( const uint128& l, const uint128& r ) { return uint128(l)<<=r; }
uint128::operator bigint()const { auto tmp = uint128( bswap_64( hi ), bswap_64( lo ) ); bigint bi( (char*)&tmp, sizeof(tmp) ); return bi; }
uint128::uint128( const fc::bigint& bi ) { *this = uint128( std::string(bi) ); // TODO: optimize this... }
friend uint128 operator + ( const uint128& l, const uint128& r ) { return uint128(l)+=r; }
inline constexpr uint64 operator / (uint64 n, FastDivision<uint64> d) { return (uint128(n) * d.magic_num) >> d.shamt; }
DYND_CUDA_HOST_DEVICE inline uint128 operator-(uint64_t rhs) const { uint64_t lo = m_lo + ~rhs + 1; return uint128(m_hi + 0xffffffffffffffffULL + (lo < m_lo), lo); }
friend uint128 operator - ( const uint128& l, const uint128& r ) { return uint128(l)-=r; }
DYND_CUDA_HOST_DEVICE inline uint128 operator+(uint32_t rhs) const { uint64_t lo = m_lo + static_cast<uint64_t>(rhs); return uint128(m_hi + (lo < m_lo), lo); }
DYND_CUDA_HOST_DEVICE inline uint128 operator-(const uint128 &rhs) const { uint64_t lo = m_lo + ~rhs.m_lo + 1; return uint128(m_hi + ~rhs.m_hi + (lo < m_lo), lo); }
friend uint128 operator * ( const uint128& l, const uint128& r ) { return uint128(l)*=r; }
DYND_CUDA_HOST_DEVICE inline uint128 operator+(uint64_t rhs) const { uint64_t lo = m_lo + rhs; return uint128(m_hi + (lo < m_lo), lo); }
friend uint128 operator ^ ( const uint128& l, const uint128& r ) { return uint128(l)^=r; }
friend uint128 operator & ( const uint128& l, const uint128& r ) { return uint128(l)&=r; }
friend uint128 operator | ( const uint128& l, const uint128& r ) { return uint128(l)=(r); }
friend uint128 operator % ( const uint128& l, const uint128& r ) { return uint128(l)%=r; }
friend uint128 operator / ( const uint128& l, const uint128& r ) { return uint128(l)/=r; }