5#include <MRPch/MRBindingMacros.h>
23constexpr bool cFitsFastInt128 = std::is_integral_v<T> || std::is_same_v<T, FastInt128>;
29[[nodiscard]]
inline constexpr std::uint64_t
addCarry64( std::uint64_t a, std::uint64_t b, std::uint64_t & carry )
noexcept
32 carry = std::uint64_t( t >> 64 );
33 return std::uint64_t( t );
37[[nodiscard]]
inline constexpr std::uint64_t
subBorrow64( std::uint64_t a, std::uint64_t b, std::uint64_t & borrow )
noexcept
40 borrow = std::uint64_t( t >> 64 ) & 1;
41 return std::uint64_t( t );
45[[nodiscard]]
inline constexpr std::uint64_t
signWord( std::uint64_t hi )
noexcept
47 return std::int64_t( hi ) < 0 ? ~std::uint64_t( 0 ) : 0;
52template <std::
size_t n, std::
size_t m>
53[[nodiscard]]
constexpr std::array<std::uint64_t, n + m>
mulWords(
54 const std::array<std::uint64_t, n> & a,
const std::array<std::uint64_t, m> & b )
noexcept
56 std::array<std::uint64_t, n + m> res = {};
57 for ( std::size_t i = 0; i < n; ++i )
65 std::uint64_t carry = 0;
66 for ( std::size_t j = 0; j < m; ++j )
70 res[i + j] = std::uint64_t( t );
71 carry = std::uint64_t( t >> 64 );
78 if ( std::int64_t( a[n - 1] ) < 0 )
80 std::uint64_t borrow = 0;
81 for ( std::size_t i = 0; i < m; ++i )
82 res[n + i] =
subBorrow64( res[n + i], b[i], borrow );
84 if ( std::int64_t( b[m - 1] ) < 0 )
86 std::uint64_t borrow = 0;
87 for ( std::size_t i = 0; i < n; ++i )
88 res[m + i] =
subBorrow64( res[m + i], a[i], borrow );
96constexpr int cMulBits = std::is_same_v<T, FastInt128> || ( std::is_unsigned_v<T> &&
sizeof( T ) >= 8 ) ? 128 : 64;
105 return std::array{ std::uint64_t( x ), std::uint64_t( x >> 64 ) };
108 return std::array{ std::uint64_t( std::int64_t( v ) ) };
123#ifdef MR_HAS_BUILTIN_INT128
130 const std::uint64_t w[2] = { std::uint64_t( u ), std::uint64_t( u >> 64 ) };
143 static_assert( nBits >= 192 && nBits % 64 == 0 );
149 std::array<std::uint64_t, numWords>
w = {};
154 template <typename T>
155 requires
detail::cFitsFastInt128<T>
159 w[0] = std::uint64_t( x );
160 w[1] = std::uint64_t( x >> 64 );
162 for (
int i = 2; i <
numWords; ++i )
168 requires ( mBits < nBits )
172 for (
int i = 0; i < m; ++i )
175 for (
int i = m; i <
numWords; ++i )
183 requires ( mBits > nBits )
186 for (
int i = 0; i <
numWords; ++i )
189 for (
int i =
numWords; i < FastInt<mBits>::numWords; ++i )
190 assert( v.
w[i] == s );
194 [[nodiscard]]
constexpr int sign() const noexcept
196 if ( std::int64_t(
w[
numWords - 1] ) < 0 )
198 std::uint64_t any = 0;
199 for (
int i = 0; i <
numWords; ++i )
201 return any != 0 ? 1 : 0;
206 std::uint64_t carry = 0;
207 for (
int i = 0; i <
numWords; ++i )
214 std::uint64_t borrow = 0;
215 for (
int i = 0; i <
numWords; ++i )
232 [[nodiscard]]
friend constexpr std::strong_ordering operator <=>(
const FastInt & a,
const FastInt & b )
noexcept
234 if (
const auto c = std::int64_t( a.w[
numWords - 1] ) <=> std::int64_t( b.w[
numWords - 1] ); c != std::strong_ordering::equal )
236 for (
int i =
numWords - 2; i >= 0; --i )
237 if (
const auto c = a.w[i] <=> b.w[i]; c != std::strong_ordering::equal )
239 return std::strong_ordering::equal;
258template <
int nBits,
int mBits>
266template <
int nBits,
typename T>
275template <
int nBits,
typename T>
289 template <typename T>
290 requires
detail::cFitsFastInt128<T>
293 [[nodiscard]]
constexpr explicit operator FastInt128() const noexcept {
return v_; }
306 res.
w =
detail::mulWords( std::array{ std::uint64_t( ua ), std::uint64_t( ua >> 64 ) },
307 std::array{ std::uint64_t( ub ), std::uint64_t( ub >> 64 ) } );
319#if !defined MR_PARSING_FOR_ANY_BINDINGS && !defined MR_COMPILING_ANY_BINDINGS
#define MRMESH_API
Definition MRMeshFwd.h:85
Definition MRFastInt.h:141
constexpr FastInt(const FastInt< mBits > &v) noexcept
Definition MRFastInt.h:184
constexpr int sign() const noexcept
-1, 0 or 1 if the value is negative, zero or positive respectively
Definition MRFastInt.h:194
constexpr FastInt(const FastInt< mBits > &v) noexcept
sign-extends a narrower value of this family
Definition MRFastInt.h:169
FastInt() noexcept=default
std::array< std::uint64_t, numWords > w
Definition MRFastInt.h:149
static constexpr int numWords
Definition MRFastInt.h:146
Int128Mul256() noexcept=default
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:477
bool operator==(const BitSet &a, const BitSet &b)
compare that two bit sets have the same set bits (they can be equal even if sizes are distinct but la...
TransformedMesh & operator-=(TransformedMesh &a, const TransformedMesh &b)
difference operation on two meshes
__int128_t FastInt128
Definition MRFastInt128.h:25
Color operator/(const Color &b, float a)
Definition MRColor.h:129
Color operator*(float a, const Color &b)
Definition MRColor.h:119
__uint128_t FastUInt128
Definition MRFastInt128.h:26
std::array< Vector3f, 3 > MR_BIND_IGNORE
Definition MRMeshBuilderTypes.h:13
TransformedMesh & operator+=(TransformedMesh &a, const TransformedMesh &b)
union operation on two meshes
Color operator+(const Color &a, const Color &b)
Definition MRColor.h:109
FastInt< 256 > FastInt256
Definition MRFastInt.h:250
MR_BIND_IGNORE double toDouble(FastInt128 v) noexcept
Definition MRFastInt.h:121
Vector3< Int128Mul256 > Vector3i128mul
Definition MRFastInt.h:321
Vector2< Int128Mul256 > Vector2i128mul
no bindings for the same reason as for Vector3i128fast
Definition MRFastInt.h:320
FastInt< 512 > FastInt512
Definition MRFastInt.h:251
FastInt< 1024 > FastInt1024
Definition MRFastInt.h:252
Definition MRFastInt.h:19
constexpr auto mulWordsOf(T v) noexcept
the words of a multiplier of FastInt, sign-extended if it is signed
Definition MRFastInt.h:100
constexpr std::uint64_t signWord(std::uint64_t hi) noexcept
all ones if the highest bit of the given word is set, and zeros otherwise
Definition MRFastInt.h:45
constexpr std::uint64_t addCarry64(std::uint64_t a, std::uint64_t b, std::uint64_t &carry) noexcept
returns a + b + carry modulo 2^64, and replaces carry with the carry-out (0 or 1)
Definition MRFastInt.h:29
double doubleFromWords(const std::uint64_t *w, int n) noexcept
constexpr int cMulBits
Definition MRFastInt.h:96
constexpr std::uint64_t subBorrow64(std::uint64_t a, std::uint64_t b, std::uint64_t &borrow) noexcept
returns a - b - borrow modulo 2^64, and replaces borrow with the borrow-out (0 or 1)
Definition MRFastInt.h:37
constexpr std::array< std::uint64_t, n+m > mulWords(const std::array< std::uint64_t, n > &a, const std::array< std::uint64_t, m > &b) noexcept
std::size_t and not int, to be deducible from std::array
Definition MRFastInt.h:53
constexpr bool cFitsFastInt128
the integer types that FastInt128 represents exactly, and which the classes below accept
Definition MRFastInt.h:23
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRVector2.h:29
Definition MRVector3.h:33