Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
Int128 Class Reference

#include <src/libslic3r/Int128.hpp>

Static Public Member Functions

static int sign_determinant_2x2_filtered (int64_t a11, int64_t a12, int64_t a21, int64_t a22)
 
static int compare_rationals_filtered (int64_t p1, int64_t q1, int64_t p2, int64_t q2)
 

Private Member Functions

 Int128 (int64_t lo=0)
 
 Int128 (const Int128 &val)
 
 Int128 (const int64_t &hi, const uint64_t &lo)
 
Int128operator= (const int64_t &val)
 
uint64_t lo () const
 
int64_t hi () const
 
int sign () const
 
bool operator== (const Int128 &val) const
 
bool operator!= (const Int128 &val) const
 
bool operator> (const Int128 &val) const
 
bool operator< (const Int128 &val) const
 
bool operator>= (const Int128 &val) const
 
bool operator<= (const Int128 &val) const
 
Int128operator+= (const Int128 &rhs)
 
Int128 operator+ (const Int128 &rhs) const
 
Int128operator-= (const Int128 &rhs)
 
Int128 operator- (const Int128 &rhs) const
 
Int128 operator- () const
 
 operator double () const
 

Static Private Member Functions

static Int128 multiply (int64_t lhs, int64_t rhs)
 
static int sign_determinant_2x2 (int64_t a11, int64_t a12, int64_t a21, int64_t a22)
 
static int compare_rationals (int64_t p1, int64_t q1, int64_t p2, int64_t q2)
 

Private Attributes

uint64_t m_lo
 
int64_t m_hi
 

Detailed Description

Constructor & Destructor Documentation

◆ Int128() [1/3]

Int128::Int128 ( int64_t  lo = 0)
inlineprivate
132: m_lo((uint64_t)lo), m_hi((lo < 0) ? -1 : 0) {}
uint64_t m_lo
Definition Int128.hpp:253
uint64_t lo() const
Definition Int128.hpp:143
int64_t m_hi
Definition Int128.hpp:254
unsigned __int64 uint64_t
Definition unistd.h:80

◆ Int128() [2/3]

Int128::Int128 ( const Int128 val)
inlineprivate
133: m_lo(val.m_lo), m_hi(val.m_hi) {}

◆ Int128() [3/3]

Int128::Int128 ( const int64_t hi,
const uint64_t lo 
)
inlineprivate
134: m_lo(lo), m_hi(hi) {}
int64_t hi() const
Definition Int128.hpp:144

Member Function Documentation

◆ compare_rationals()

static int Int128::compare_rationals ( int64_t  p1,
int64_t  q1,
int64_t  p2,
int64_t  q2 
)
inlinestaticprivate
246 {
247 int invert = ((q1 < 0) == (q2 < 0)) ? 1 : -1;
248 Int128 det = Int128::multiply(p1, q2) - Int128::multiply(p2, q1);
249 return det.sign() * invert;
250 }
Definition Int128.hpp:73
int sign() const
Definition Int128.hpp:145
static Int128 multiply(int64_t lhs, int64_t rhs)
Definition Int128.hpp:194

References multiply(), and sign().

Referenced by compare_rationals_filtered().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compare_rationals_filtered()

static int Int128::compare_rationals_filtered ( int64_t  p1,
int64_t  q1,
int64_t  p2,
int64_t  q2 
)
inlinestatic
285 {
286 // First try to calculate the determinant over the upper 31 bits.
287 // Round p1, p2, q1, q2 to 31 bits.
288 int invert = ((q1 < 0) == (q2 < 0)) ? 1 : -1;
289 int64_t q1s = (q1 + (1 << 31)) >> 32;
290 int64_t q2s = (q2 + (1 << 31)) >> 32;
291 if (q1s != 0 && q2s != 0) {
292 int64_t p1s = (p1 + (1 << 31)) >> 32;
293 int64_t p2s = (p2 + (1 << 31)) >> 32;
294 // Result fits 63 bits, it is an approximate of the determinant divided by 2^64.
295 int64_t det = p1s * q2s - p2s * q1s;
296 // Maximum absolute of the remainder of the exact determinant, divided by 2^64.
297 int64_t err = ((std::abs(p1s) + std::abs(q1s) + std::abs(p2s) + std::abs(q2s)) << 1) + 1;
298 assert(std::abs(det) <= err || ((det > 0) ? 1 : -1) * invert == compare_rationals(p1, q1, p2, q2));
299 if (std::abs(det) > err)
300 return ((det > 0) ? 1 : -1) * invert;
301 }
302 return sign_determinant_2x2(p1, q1, p2, q2) * invert;
303 }
static int sign_determinant_2x2(int64_t a11, int64_t a12, int64_t a21, int64_t a22)
Definition Int128.hpp:239
static int compare_rationals(int64_t p1, int64_t q1, int64_t p2, int64_t q2)
Definition Int128.hpp:245
__int64 int64_t
Definition unistd.h:76

References compare_rationals(), and sign_determinant_2x2().

+ Here is the call graph for this function:

◆ hi()

int64_t Int128::hi ( ) const
inlineprivate
144{ return m_hi; }

References m_hi.

◆ lo()

uint64_t Int128::lo ( ) const
inlineprivate
143{ return m_lo; }

References m_lo.

◆ multiply()

static Int128 Int128::multiply ( int64_t  lhs,
int64_t  rhs 
)
inlinestaticprivate
195 {
196#if defined(_MSC_VER) && defined(_WIN64)
197 // On Visual Studio 64bit, use the _mul128() intrinsic function.
198 Int128 result;
199 result.m_lo = (uint64_t)_mul128(lhs, rhs, &result.m_hi);
200 return result;
201#else
202 // This branch should only be executed in case there is neither __int16 type nor _mul128 intrinsic
203 // function available. This is mostly on 32bit operating systems.
204 // Use a pure C implementation of _mul128().
205
206 int negate = (lhs < 0) != (rhs < 0);
207
208 if (lhs < 0)
209 lhs = -lhs;
210 uint64_t int1Hi = uint64_t(lhs) >> 32;
211 uint64_t int1Lo = uint64_t(lhs & 0xFFFFFFFF);
212
213 if (rhs < 0)
214 rhs = -rhs;
215 uint64_t int2Hi = uint64_t(rhs) >> 32;
216 uint64_t int2Lo = uint64_t(rhs & 0xFFFFFFFF);
217
218 //because the high (sign) bits in both int1Hi & int2Hi have been zeroed,
219 //there's no risk of 64 bit overflow in the following assignment
220 //(ie: $7FFFFFFF*$FFFFFFFF + $7FFFFFFF*$FFFFFFFF < 64bits)
221 uint64_t a = int1Hi * int2Hi;
222 uint64_t b = int1Lo * int2Lo;
223 //Result = A shl 64 + C shl 32 + B ...
224 uint64_t c = int1Hi * int2Lo + int1Lo * int2Hi;
225
226 Int128 tmp;
227 tmp.m_hi = int64_t(a + (c >> 32));
228 tmp.m_lo = int64_t(c << 32);
229 tmp.m_lo += int64_t(b);
230 if (tmp.m_lo < b)
231 ++ tmp.m_hi;
232 if (negate)
233 tmp = - tmp;
234 return tmp;
235#endif
236 }

References m_hi, and m_lo.

Referenced by compare_rationals(), and sign_determinant_2x2().

+ Here is the caller graph for this function:

◆ operator double()

Int128::operator double ( ) const
inlineprivate
185 {
186 const double shift64 = 18446744073709551616.0; //2^64
187 return (m_hi < 0) ?
188 ((m_lo == 0) ?
189 (double)m_hi * shift64 :
190 -(double)(~m_lo + ~m_hi * shift64)) :
191 (double)(m_lo + m_hi * shift64);
192 }

References m_hi, and m_lo.

◆ operator!=()

bool Int128::operator!= ( const Int128 val) const
inlineprivate
148{ return ! (*this == val); }

◆ operator+()

Int128 Int128::operator+ ( const Int128 rhs) const
inlineprivate
163 {
164 Int128 result(*this);
165 result+= rhs;
166 return result;
167 }

◆ operator+=()

Int128 & Int128::operator+= ( const Int128 rhs)
inlineprivate
155 {
156 m_hi += rhs.m_hi;
157 m_lo += rhs.m_lo;
158 if (m_lo < rhs.m_lo) m_hi++;
159 return *this;
160 }

References m_hi, and m_lo.

◆ operator-() [1/2]

Int128 Int128::operator- ( ) const
inlineprivate
182{ return (m_lo == 0) ? Int128(-m_hi, 0) : Int128(~m_hi, ~m_lo + 1); }

References m_hi, and m_lo.

◆ operator-() [2/2]

Int128 Int128::operator- ( const Int128 rhs) const
inlineprivate
176 {
177 Int128 result(*this);
178 result -= rhs;
179 return result;
180 }

◆ operator-=()

Int128 & Int128::operator-= ( const Int128 rhs)
inlineprivate
170 {
171 *this += -rhs;
172 return *this;
173 }

◆ operator<()

bool Int128::operator< ( const Int128 val) const
inlineprivate
150{ return (m_hi == val.m_hi) ? m_lo < val.m_lo : m_hi < val.m_hi; }

References m_hi, and m_lo.

◆ operator<=()

bool Int128::operator<= ( const Int128 val) const
inlineprivate
152{ return ! (*this > val); }

◆ operator=()

Int128 & Int128::operator= ( const int64_t val)
inlineprivate
137 {
138 m_lo = (uint64_t)val;
139 m_hi = (val < 0) ? -1 : 0;
140 return *this;
141 }

References m_hi, and m_lo.

◆ operator==()

bool Int128::operator== ( const Int128 val) const
inlineprivate
147{ return m_hi == val.m_hi && m_lo == val.m_lo; }

References m_hi, and m_lo.

◆ operator>()

bool Int128::operator> ( const Int128 val) const
inlineprivate
149{ return (m_hi == val.m_hi) ? m_lo > val.m_lo : m_hi > val.m_hi; }

References m_hi, and m_lo.

◆ operator>=()

bool Int128::operator>= ( const Int128 val) const
inlineprivate
151{ return ! (*this < val); }

◆ sign()

int Int128::sign ( ) const
inlineprivate
145{ return (m_hi == 0) ? (m_lo > 0) : (m_hi > 0) - (m_hi < 0); }

References m_hi, and m_lo.

Referenced by compare_rationals().

+ Here is the caller graph for this function:

◆ sign_determinant_2x2()

static int Int128::sign_determinant_2x2 ( int64_t  a11,
int64_t  a12,
int64_t  a21,
int64_t  a22 
)
inlinestaticprivate
240 {
241 return (Int128::multiply(a11, a22) - Int128::multiply(a12, a21)).sign();
242 }

References multiply().

Referenced by compare_rationals_filtered(), and sign_determinant_2x2_filtered().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sign_determinant_2x2_filtered()

static int Int128::sign_determinant_2x2_filtered ( int64_t  a11,
int64_t  a12,
int64_t  a21,
int64_t  a22 
)
inlinestatic
266 {
267 // First try to calculate the determinant over the upper 31 bits.
268 // Round p1, p2, q1, q2 to 31 bits.
269 int64_t a11s = (a11 + (1 << 31)) >> 32;
270 int64_t a12s = (a12 + (1 << 31)) >> 32;
271 int64_t a21s = (a21 + (1 << 31)) >> 32;
272 int64_t a22s = (a22 + (1 << 31)) >> 32;
273 // Result fits 63 bits, it is an approximate of the determinant divided by 2^64.
274 int64_t det = a11s * a22s - a12s * a21s;
275 // Maximum absolute of the remainder of the exact determinant, divided by 2^64.
276 int64_t err = ((std::abs(a11s) + std::abs(a12s) + std::abs(a21s) + std::abs(a22s)) << 1) + 1;
277 assert(std::abs(det) <= err || ((det > 0) ? 1 : -1) == sign_determinant_2x2(a11, a12, a21, a22));
278 return (std::abs(det) > err) ?
279 ((det > 0) ? 1 : -1) :
280 sign_determinant_2x2(a11, a12, a21, a22);
281 }

References sign_determinant_2x2().

Referenced by Slic3r::int128::cross(), and Slic3r::int128::orient().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ m_hi

◆ m_lo


The documentation for this class was generated from the following file: