-
-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathInt128.cpp
More file actions
133 lines (106 loc) · 5.03 KB
/
Int128.cpp
File metadata and controls
133 lines (106 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*******************************************************************************
* *
* Author : Angus Johnson *
* Version : 5.1.6 *
* Date : 23 May 2013 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2013 *
* *
* License: *
* Use, modification & distribution is subject to Boost Software License Ver 1.*
* http://www.boost.org/LICENSE_1_0.txt *
* *
* Attributions: *
* The code in this library is an extension of Bala Vatti's clipping algorithm:*
* "A generic solution to polygon clipping" *
* Communications of the ACM, Vol 35, Issue 7 (July 1992) pp 56-63. *
* http://portal.acm.org/citation.cfm?id=129906 *
* *
* Computer graphics and geometric modeling: implementation and algorithms *
* By Max K. Agoston *
* Springer; 1 edition (January 4, 2005) *
* http://books.google.com/books?q=vatti+clipping+agoston *
* *
* See also: *
* "Polygon Offsetting by Computing Winding Numbers" *
* Paper no. DETC2005-85513 pp. 565-575 *
* ASME 2005 International Design Engineering Technical Conferences *
* and Computers and Information in Engineering Conference (IDETC/CIE2005) *
* September 24-28, 2005, Long Beach, California, USA *
* http://www.me.berkeley.edu/~mcmains/pubs/DAC05OffsetPolygon.pdf *
* *
* This is a translation of the Delphi Clipper library and the naming style *
* used has retained a Delphi flavour. *
******************************************************************************/
#include "Int128.h"
using namespace ClipperLib;
Int128 Int128::operator/(const Int128 &rhs) const {
if (!rhs.lo && !rhs.hi) throw "Int128 operator/: divide by zero";
bool negate = (rhs.hi < 0) != (hi < 0);
Int128 dividend = *this;
Int128 divisor = rhs;
if (dividend.hi < 0) dividend = -dividend;
if (divisor.hi < 0) divisor = -divisor;
if (divisor < dividend) {
Int128 result = Int128(0);
Int128 cntr = Int128(1);
while (divisor.hi >= 0 && !(divisor > dividend)) {
divisor.hi <<= 1;
if ((int64_t)divisor.lo < 0) divisor.hi++;
divisor.lo <<= 1;
cntr.hi <<= 1;
if ((int64_t)cntr.lo < 0) cntr.hi++;
cntr.lo <<= 1;
}
divisor.lo >>= 1;
if ((divisor.hi & 1) == 1) divisor.lo |= 0x8000000000000000LL;
divisor.hi = (uint64_t)divisor.hi >> 1;
cntr.lo >>= 1;
if ((cntr.hi & 1) == 1) cntr.lo |= 0x8000000000000000LL;
cntr.hi >>= 1;
while (cntr.hi || cntr.lo) {
if (divisor <= dividend) {
dividend -= divisor;
result.hi |= cntr.hi;
result.lo |= cntr.lo;
}
divisor.lo >>= 1;
if ((divisor.hi & 1) == 1) divisor.lo |= 0x8000000000000000LL;
divisor.hi >>= 1;
cntr.lo >>= 1;
if ((cntr.hi & 1) == 1) cntr.lo |= 0x8000000000000000LL;
cntr.hi >>= 1;
}
if (negate) result = -result;
return result;
} else if (rhs.hi == this->hi && rhs.lo == this->lo) return Int128(1);
else return Int128(0);
}
double Int128::AsDouble() const {
const double shift64 = 18446744073709551616.0; // 2^64
if (hi < 0) {
if (lo == 0) return (double)hi * shift64;
else return -(double)(~lo + ~hi * shift64);
} else return (double)(lo + hi * shift64);
}
namespace ClipperLib {
Int128 Int128Mul(int64_t lhs, int64_t rhs) {
bool negate = (lhs < 0) != (rhs < 0);
if (lhs < 0) lhs = -lhs;
uint64_t int1Hi = uint64_t(lhs) >> 32;
uint64_t int1Lo = uint64_t(lhs & 0xFFFFFFFF);
if (rhs < 0) rhs = -rhs;
uint64_t int2Hi = uint64_t(rhs) >> 32;
uint64_t int2Lo = uint64_t(rhs & 0xFFFFFFFF);
uint64_t a = int1Hi * int2Hi;
uint64_t b = int1Lo * int2Lo;
uint64_t c = int1Hi * int2Lo + int1Lo * int2Hi;
Int128 tmp;
tmp.hi = int64_t(a + (c >> 32));
tmp.lo = int64_t(c << 32);
tmp.lo += int64_t(b);
if (tmp.lo < b) tmp.hi++;
if (negate) tmp = -tmp;
return tmp;
}
}