Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
uint128.hpp
Go to the documentation of this file.
1#pragma once
2#include <limits>
3#include <stdint.h>
4#include <string>
5
7#include <fc/crypto/city.hpp>
8
9#ifdef _MSC_VER
10 #pragma warning (push)
11 #pragma warning (disable : 4244)
12#endif
13
14namespace fc
15{
16 class bigint;
21 class uint128
22 {
23
24
25 public:
26 uint128():hi(0),lo(0){}
28 uint128( int32_t l ):hi( -(l<0) ),lo(l){}
29 uint128( int64_t l ):hi( -(l<0) ),lo(l){}
31 uint128( const std::string& s );
33 :hi(_h),lo(_l){}
34 uint128( const fc::bigint& bi );
35 explicit uint128( unsigned __int128 i ):hi( i >> 64 ), lo(i){ }
36
37 operator std::string()const;
38 operator fc::bigint()const;
39
40 explicit operator unsigned __int128()const {
41 unsigned __int128 result(hi);
42 result <<= 64;
43 return result | lo;
44 }
45
46 bool operator == ( const uint128& o )const{ return hi == o.hi && lo == o.lo; }
47 bool operator != ( const uint128& o )const{ return hi != o.hi || lo != o.lo; }
48 bool operator < ( const uint128& o )const { return (hi == o.hi) ? lo < o.lo : hi < o.hi; }
49 bool operator < ( const int64_t& o )const { return *this < uint128(o); }
50 bool operator !()const { return !(hi !=0 || lo != 0); }
51 uint128 operator -()const { return ++uint128( ~hi, ~lo ); }
52 uint128 operator ~()const { return uint128( ~hi, ~lo ); }
53
54 uint128& operator++() { hi += (++lo == 0); return *this; }
55 uint128& operator--() { hi -= (lo-- == 0); return *this; }
56 uint128 operator++(int) { auto tmp = *this; ++(*this); return tmp; }
57 uint128 operator--(int) { auto tmp = *this; --(*this); return tmp; }
58
59 uint128& operator |= ( const uint128& u ) { hi |= u.hi; lo |= u.lo; return *this; }
60 uint128& operator &= ( const uint128& u ) { hi &= u.hi; lo &= u.lo; return *this; }
61 uint128& operator ^= ( const uint128& u ) { hi ^= u.hi; lo ^= u.lo; return *this; }
62 uint128& operator <<= ( const uint128& u );
63 uint128& operator >>= ( const uint128& u );
64
65 uint128& operator += ( const uint128& u ) { const uint64_t old = lo; lo += u.lo; hi += u.hi + (lo < old); return *this; }
66 uint128& operator -= ( const uint128& u ) { return *this += -u; }
67 uint128& operator *= ( const uint128& u );
68 uint128& operator /= ( const uint128& u );
69 uint128& operator %= ( const uint128& u );
70
71
72 friend uint128 operator + ( const uint128& l, const uint128& r ) { return uint128(l)+=r; }
73 friend uint128 operator - ( const uint128& l, const uint128& r ) { return uint128(l)-=r; }
74 friend uint128 operator * ( const uint128& l, const uint128& r ) { return uint128(l)*=r; }
75 friend uint128 operator / ( const uint128& l, const uint128& r ) { return uint128(l)/=r; }
76 friend uint128 operator % ( const uint128& l, const uint128& r ) { return uint128(l)%=r; }
77 friend uint128 operator | ( const uint128& l, const uint128& r ) { return uint128(l)|=(r); }
78 friend uint128 operator & ( const uint128& l, const uint128& r ) { return uint128(l)&=r; }
79 friend uint128 operator ^ ( const uint128& l, const uint128& r ) { return uint128(l)^=r; }
80 friend uint128 operator << ( const uint128& l, const uint128& r ) { return uint128(l)<<=r; }
81 friend uint128 operator >> ( const uint128& l, const uint128& r ) { return uint128(l)>>=r; }
82 friend bool operator > ( const uint128& l, const uint128& r ) { return r < l; }
83 friend bool operator > ( const uint128& l, const int64_t& r ) { return uint128(r) < l; }
84 friend bool operator > ( const int64_t& l, const uint128& r ) { return r < uint128(l); }
85
86 friend bool operator >= ( const uint128& l, const uint128& r ) { return l == r || l > r; }
87 friend bool operator >= ( const uint128& l, const int64_t& r ) { return l >= uint128(r); }
88 friend bool operator >= ( const int64_t& l, const uint128& r ) { return uint128(l) >= r; }
89 friend bool operator <= ( const uint128& l, const uint128& r ) { return l == r || l < r; }
90 friend bool operator <= ( const uint128& l, const int64_t& r ) { return l <= uint128(r); }
91 friend bool operator <= ( const int64_t& l, const uint128& r ) { return uint128(l) <= r; }
92
93 friend std::size_t hash_value( const uint128& v ) { return city_hash_size_t((const char*)&v, sizeof(v)); }
94
96 {
97 FC_ASSERT( hi == 0 );
98 uint32_t lo32 = (uint32_t) lo;
99 FC_ASSERT( lo == lo32 );
100 return lo32;
101 }
103 {
104 FC_ASSERT( hi == 0 );
105 return lo;
106 }
107 uint32_t low_32_bits()const { return (uint32_t) lo; }
108 uint64_t low_bits()const { return lo; }
109 uint64_t high_bits()const { return hi; }
110
112 const uint64_t max64 = std::numeric_limits<uint64_t>::max();
113 return uint128( max64, max64 );
114 }
115
116 static void full_product( const uint128& a, const uint128& b, uint128& result_hi, uint128& result_lo );
117
118 uint8_t popcount() const;
119
120 // fields must be public for serialization
123 };
124 static_assert( sizeof(uint128) == 2*sizeof(uint64_t), "validate packing assumptions" );
125
126 class variant;
127
128 void to_variant( const uint128& var, variant& vo );
129 void from_variant( const variant& var, uint128& vo );
130// void to_variant( const unsigned __int128& var, variant& vo );
131// void from_variant( const variant& var, unsigned __int128& vo );
132
133 namespace raw
134 {
135 template<typename Stream>
136 inline void pack( Stream& s, const uint128& u ) { s.write( (char*)&u, sizeof(u) ); }
137 template<typename Stream>
138 inline void unpack( Stream& s, uint128& u ) { s.read( (char*)&u, sizeof(u) ); }
139 }
140
141 size_t city_hash_size_t(const char *buf, size_t len);
142} // namespace fc
143
144namespace std
145{
146 template<>
147 struct hash<fc::uint128>
148 {
149 size_t operator()( const fc::uint128& s )const
150 {
151 return fc::city_hash_size_t((char*)&s, sizeof(s));
152 }
153 };
154}
155
156FC_REFLECT( fc::uint128, (hi)(lo) )
157
158#ifdef _MSC_VER
159 #pragma warning (pop)
160#endif
const mie::Vuint & r
Definition bn.cpp:28
an implementation of 128 bit unsigned integer
Definition uint128.hpp:22
uint64_t high_bits() const
Definition uint128.hpp:109
uint128 & operator--()
Definition uint128.hpp:55
uint128 & operator++()
Definition uint128.hpp:54
uint128(uint64_t _h, uint64_t _l)
Definition uint128.hpp:32
bool operator!=(const uint128 &o) const
Definition uint128.hpp:47
static uint128 max_value()
Definition uint128.hpp:111
bool operator!() const
Definition uint128.hpp:50
uint128 & operator>>=(const uint128 &u)
Definition uint128.cpp:189
uint128(uint64_t l)
Definition uint128.hpp:30
uint128 operator-() const
Definition uint128.hpp:51
friend bool operator<=(const uint128 &l, const uint128 &r)
Definition uint128.hpp:89
uint32_t to_integer() const
Definition uint128.hpp:95
uint128 & operator|=(const uint128 &u)
Definition uint128.hpp:59
uint128(int32_t l)
Definition uint128.hpp:28
uint64_t to_uint64() const
Definition uint128.hpp:102
uint128 & operator-=(const uint128 &u)
Definition uint128.hpp:66
uint128 & operator%=(const uint128 &u)
Definition uint128.cpp:250
friend uint128 operator+(const uint128 &l, const uint128 &r)
Definition uint128.hpp:72
uint128 & operator^=(const uint128 &u)
Definition uint128.hpp:61
uint128 operator++(int)
Definition uint128.hpp:56
uint128(unsigned __int128 i)
Definition uint128.hpp:35
static void full_product(const uint128 &a, const uint128 &b, uint128 &result_hi, uint128 &result_lo)
Definition uint128.cpp:297
uint128(int64_t l)
Definition uint128.hpp:29
friend uint128 operator|(const uint128 &l, const uint128 &r)
Definition uint128.hpp:77
uint128 & operator/=(const uint128 &u)
Definition uint128.cpp:224
friend uint128 operator*(const uint128 &l, const uint128 &r)
Definition uint128.hpp:74
uint128 & operator*=(const uint128 &u)
Definition uint128.cpp:257
friend uint128 operator&(const uint128 &l, const uint128 &r)
Definition uint128.hpp:78
friend uint128 operator^(const uint128 &l, const uint128 &r)
Definition uint128.hpp:79
uint8_t popcount() const
Definition uint128.cpp:372
friend uint128 operator>>(const uint128 &l, const uint128 &r)
Definition uint128.hpp:81
friend bool operator>=(const uint128 &l, const uint128 &r)
Definition uint128.hpp:86
uint128 operator--(int)
Definition uint128.hpp:57
uint64_t lo
Definition uint128.hpp:122
uint128(uint32_t l)
Definition uint128.hpp:27
uint64_t low_bits() const
Definition uint128.hpp:108
friend bool operator>(const uint128 &l, const uint128 &r)
Definition uint128.hpp:82
uint128 & operator&=(const uint128 &u)
Definition uint128.hpp:60
uint128 operator~() const
Definition uint128.hpp:52
bool operator<(const uint128 &o) const
Definition uint128.hpp:48
bool operator==(const uint128 &o) const
Definition uint128.hpp:46
uint64_t hi
Definition uint128.hpp:121
friend uint128 operator%(const uint128 &l, const uint128 &r)
Definition uint128.hpp:76
uint128 & operator+=(const uint128 &u)
Definition uint128.hpp:65
friend uint128 operator/(const uint128 &l, const uint128 &r)
Definition uint128.hpp:75
friend std::size_t hash_value(const uint128 &v)
Definition uint128.hpp:93
uint32_t low_32_bits() const
Definition uint128.hpp:107
friend uint128 operator<<(const uint128 &l, const uint128 &r)
Definition uint128.hpp:80
uint128 & operator<<=(const uint128 &u)
Definition uint128.cpp:154
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
Concept for reading and writing characters.
Defines exception's used by fc.
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
void unpack(Stream &s, std::deque< T > &value)
Definition raw.hpp:540
void pack(Stream &s, const std::deque< T > &value)
Definition raw.hpp:531
namespace sysio::chain
Definition authority.cpp:3
size_t city_hash_size_t(const char *buf, size_t len)
Definition city.hpp:62
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
Definition name.hpp:106
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition reflect.hpp:311
signed __int64 int64_t
Definition stdint.h:135
unsigned int uint32_t
Definition stdint.h:126
signed int int32_t
Definition stdint.h:123
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
size_t operator()(const fc::uint128 &s) const
Definition uint128.hpp:149
char * s
size_t len
uint8_t buf[2048]
int l