ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Wide.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File Wide.h -------------------------------*/
3/*--------------------------------------------------------------------------*/
34/*--------------------------------------------------------------------------*/
35/*----------------------------- DEFINITIONS --------------------------------*/
36/*--------------------------------------------------------------------------*/
37
38#ifndef __ADS_Wide
39 #define __ADS_Wide
40/* self-identification: #endif constant */
41
42/*--------------------------------------------------------------------------*/
43/*------------------------------ INCLUDES ----------------------------------*/
44/*--------------------------------------------------------------------------*/
45
46#include "ADS/Portable.h"
47
48#include <bit>
49#include <compare>
50#include <concepts>
51#include <cstdint>
52#include <type_traits>
53
54#if defined( ADS_WITH_BOOST )
55 #include <boost/multiprecision/cpp_int.hpp>
56#endif
57
58/*--------------------------------------------------------------------------*/
59/*--------------------------- NAMESPACE ------------------------------------*/
60/*--------------------------------------------------------------------------*/
61
62namespace ADS
63{
64
66namespace detail
67{
68
69/*--------------------------------------------------------------------------*/
70
75[[gnu::always_inline]] constexpr std::uint64_t
76wideProduct( std::uint64_t a , std::uint64_t b , std::uint64_t & hi )
77{
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 ) );
84 }
85#endif
86 const WordPair p = wideProductHalves( a , b );
87 hi = p.hi;
88 return( p.lo );
89 }
90
113constexpr std::uint64_t wideDivide( std::uint64_t u1 , std::uint64_t u0 ,
114 std::uint64_t v , std::uint64_t & r )
115{
116 constexpr std::uint64_t B = std::uint64_t( 1 ) << 32 , M = B - 1;
117 const int s = std::countl_zero( v );
118 if( s > 0 ) {
119 v <<= s;
120 u1 = ( u1 << s ) | ( u0 >> ( 64 - s ) );
121 u0 <<= s;
122 }
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 ) ) ) {
129 --qh;
130 rh += v1;
131 if( rh >= B )
132 break;
133 }
134 q[ j ] = qh;
135 u1 = ( u1 << 32 ) + d - qh * v; // modulo 2^64, below v
136 }
137 r = u1 >> s;
138 return( ( q[ 1 ] << 32 ) | q[ 0 ] );
139 }
140
141/*--------------------------------------------------------------------------*/
142
146struct Int128
147{
148 std::uint64_t lo = 0;
149 std::uint64_t hi = 0;
150
151 constexpr Int128( void ) = default;
152
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 ) {
157 }
158
159 explicit operator long double( void ) const {
160 return( ( long double )( std::int64_t( hi ) ) * 18446744073709551616.0L +
161 ( long double )( lo ) );
162 }
163
164 friend constexpr Int128 operator+( Int128 a , Int128 b ) {
165 Int128 r;
166 r.lo = a.lo + b.lo;
167 r.hi = a.hi + b.hi + ( r.lo < a.lo ? 1 : 0 );
168 return( r );
169 }
170
171 friend constexpr Int128 operator-( Int128 a , Int128 b ) {
172 Int128 r;
173 r.lo = a.lo - b.lo;
174 r.hi = a.hi - b.hi - ( a.lo < b.lo ? 1 : 0 );
175 return( r );
176 }
177
178 friend constexpr Int128 operator*( Int128 a , Int128 b ) {
179 Int128 r;
180 r.lo = wideProduct( a.lo , b.lo , r.hi );
181 r.hi += a.lo * b.hi + a.hi * b.lo;
182 return( r );
183 }
184
185 friend constexpr bool operator==( Int128 a , Int128 b ) {
186 return( ( a.lo == b.lo ) && ( a.hi == b.hi ) );
187 }
188
189 friend constexpr std::strong_ordering operator<=>( Int128 a , Int128 b ) {
190 if( a.hi != b.hi )
191 return( std::int64_t( a.hi ) <=> std::int64_t( b.hi ) );
192 return( a.lo <=> b.lo );
193 }
194 };
195
196/*--------------------------------------------------------------------------*/
197
198} // end( namespace detail )
200
201/*--------------------------------------------------------------------------*/
202
203} // end( namespace ADS )
204
205/*--------------------------------------------------------------------------*/
206
207#endif /* Wide.h included */
208
209/*--------------------------------------------------------------------------*/
210/*---------------------------- End File Wide.h -----------------------------*/
211/*--------------------------------------------------------------------------*/
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
Definition ADT.h:96