54#if defined( ADS_WITH_BOOST )
55 #include <boost/multiprecision/cpp_int.hpp>
75[[gnu::always_inline]]
constexpr std::uint64_t
76wideProduct( std::uint64_t a , std::uint64_t b , std::uint64_t & hi )
78#if defined( ADS_WITH_BOOST )
79 if( ! std::is_constant_evaluated() ) {
80 boost::multiprecision::uint128_t p;
81 boost::multiprecision::multiply( p , a , b );
82 hi =
static_cast< std::uint64_t
>( p >> 64 );
83 return(
static_cast< std::uint64_t
>( p ) );
86 const WordPair p = wideProductHalves( a , b );
113constexpr std::uint64_t wideDivide( std::uint64_t u1 , std::uint64_t u0 ,
114 std::uint64_t v , std::uint64_t & r )
116 constexpr std::uint64_t B = std::uint64_t( 1 ) << 32 , M = B - 1;
117 const int s = std::countl_zero( v );
120 u1 = ( u1 << s ) | ( u0 >> ( 64 - s ) );
123 const std::uint64_t v1 = v >> 32 , v0 = v & M;
124 std::uint64_t q[ 2 ] = { 0 , 0 };
125 for(
int j = 1 ; j >= 0 ; --j ) {
126 const std::uint64_t d = j ? ( u0 >> 32 ) : ( u0 & M );
127 std::uint64_t qh = u1 / v1 , rh = u1 - qh * v1;
128 while( ( qh >= B ) || ( qh * v0 > ( ( rh << 32 ) | d ) ) ) {
135 u1 = ( u1 << 32 ) + d - qh * v;
138 return( ( q[ 1 ] << 32 ) | q[ 0 ] );
148 std::uint64_t lo = 0;
149 std::uint64_t hi = 0;
151 constexpr Int128(
void ) =
default;
153 template< std::
integral I >
154 constexpr Int128( I v )
155 : lo( std::uint64_t( v ) ) ,
156 hi( ( std::is_signed_v< I > && ( v < 0 ) ) ? ~std::uint64_t( 0 ) : 0 ) {
159 explicit operator long double(
void )
const {
160 return( (
long double )( std::int64_t( hi ) ) * 18446744073709551616.0L +
161 (
long double )( lo ) );
164 friend constexpr Int128 operator+( Int128 a , Int128 b ) {
167 r.hi = a.hi + b.hi + ( r.lo < a.lo ? 1 : 0 );
171 friend constexpr Int128 operator-( Int128 a , Int128 b ) {
174 r.hi = a.hi - b.hi - ( a.lo < b.lo ? 1 : 0 );
178 friend constexpr Int128 operator*( Int128 a , Int128 b ) {
180 r.lo = wideProduct( a.lo , b.lo , r.hi );
181 r.hi += a.lo * b.hi + a.hi * b.lo;
185 friend constexpr bool operator==( Int128 a , Int128 b ) {
186 return( ( a.lo == b.lo ) && ( a.hi == b.hi ) );
189 friend constexpr std::strong_ordering operator<=>( Int128 a , Int128 b ) {
191 return( std::int64_t( a.hi ) <=> std::int64_t( b.hi ) );
192 return( a.lo <=> b.lo );
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...