From 27e224b012006b82e4f043ff556dcf7967441663 Mon Sep 17 00:00:00 2001 From: theoretical Date: Fri, 30 Jan 2015 12:59:43 -0500 Subject: [PATCH] uint128: Make to_integer(), to_uint64() assert magnitude - All places these are used already handle magnitude checks correctly - As a "bit-twiddling" method, low_bits() truncates - As a "semantics preserving type conversion" method, to_uint64() asserts - Add low_32_bits() as a "bit-twiddling" non-asserting to_integer() equivalent --- include/fc/uint128.hpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/include/fc/uint128.hpp b/include/fc/uint128.hpp index 08c3cc1..cbee51b 100644 --- a/include/fc/uint128.hpp +++ b/include/fc/uint128.hpp @@ -3,6 +3,8 @@ #include #include +#include + #ifdef _MSC_VER #pragma warning (push) #pragma warning (disable : 4244) @@ -74,8 +76,19 @@ namespace fc friend bool operator >= ( const uint128& l, const uint128& r ) { return l == r || l > r; } friend bool operator <= ( const uint128& l, const uint128& r ) { return l == r || l < r; } - uint32_t to_integer()const { return (uint32_t)lo; } - uint64_t to_uint64()const { return lo; } + uint32_t to_integer()const + { + FC_ASSERT( hi == 0 ); + uint32_t lo32 = (uint32_t) lo; + FC_ASSERT( lo == lo32 ); + return lo32; + } + uint64_t to_uint64()const + { + FC_ASSERT( hi == 0 ); + return lo; + } + uint32_t low_32_bits()const { return (uint32_t) lo; } uint64_t low_bits()const { return lo; } uint64_t high_bits()const { return hi; }