/src/tdengine/source/libs/decimal/src/detail/wideInteger.cpp
Line | Count | Source |
1 | | #include "wideInteger.h" |
2 | | #include "intx/int128.hpp" |
3 | | #include "intx/intx.hpp" |
4 | | |
5 | | |
6 | | const UInt128 uInt128Zero = {0, 0}; |
7 | | const uint64_t k1e18 = 1000000000000000000LL; |
8 | | const UInt128 uInt128_1e18 = {k1e18, 0}; |
9 | | const UInt128 uInt128One = {1, 0}; |
10 | | const UInt128 uInt128Two = {2, 0}; |
11 | | |
12 | 0 | void makeUInt128(uint128* pUint128, uint64_t high, uint64_t low) { |
13 | 0 | intx::uint128* pIntxUint = (intx::uint128*)pUint128; |
14 | 0 | pIntxUint->hi = high; |
15 | 0 | pIntxUint->lo = low; |
16 | 0 | } |
17 | | |
18 | 0 | uint64_t uInt128Hi(const UInt128* pInt) { |
19 | 0 | intx::uint128 *pIntUint = (intx::uint128*)pInt; |
20 | 0 | return pIntUint->hi; |
21 | 0 | } |
22 | | |
23 | 0 | uint64_t uInt128Lo(const UInt128* pInt) { |
24 | 0 | intx::uint128 *pIntUint = (intx::uint128*)pInt; |
25 | 0 | return pIntUint->lo; |
26 | 0 | } |
27 | | |
28 | 0 | void uInt128Add(UInt128* pLeft, const UInt128* pRight) { |
29 | 0 | intx::uint128 *pX = (intx::uint128*)pLeft; |
30 | 0 | const intx::uint128 *pY = (const intx::uint128*)pRight; |
31 | 0 | *pX += *pY; |
32 | 0 | } |
33 | 0 | void uInt128Subtract(UInt128* pLeft, const UInt128* pRight) { |
34 | 0 | intx::uint128 *pX = (intx::uint128*)pLeft; |
35 | 0 | const intx::uint128 *pY = (const intx::uint128*)pRight; |
36 | 0 | *pX -= *pY; |
37 | 0 | } |
38 | 0 | void uInt128Multiply(UInt128* pLeft, const UInt128* pRight) { |
39 | 0 | intx::uint128 *pX = (intx::uint128*)pLeft; |
40 | 0 | const intx::uint128 *pY = (const intx::uint128*)pRight; |
41 | 0 | *pX *= *pY; |
42 | | /* __uint128_t *px = (__uint128_t*)pLeft; |
43 | | const __uint128_t *py = (__uint128_t*)pRight; |
44 | | *px = *px * *py; */ |
45 | 0 | } |
46 | 0 | void uInt128Divide(UInt128* pLeft, const UInt128* pRight) { |
47 | 0 | intx::uint128 *pX = (intx::uint128*)pLeft; |
48 | 0 | const intx::uint128 *pY = (const intx::uint128*)pRight; |
49 | 0 | *pX /= *pY; |
50 | | /* __uint128_t *px = (__uint128_t*)pLeft; |
51 | | const __uint128_t *py = (__uint128_t*)pRight; |
52 | | *px = *px / *py; */ |
53 | 0 | } |
54 | 0 | void uInt128Mod(UInt128* pLeft, const UInt128* pRight) { |
55 | 0 | intx::uint128 *pX = (intx::uint128*)pLeft; |
56 | 0 | const intx::uint128 *pY = (const intx::uint128*)pRight; |
57 | 0 | *pX %= *pY; |
58 | | /* __uint128_t *px = (__uint128_t*)pLeft; |
59 | | const __uint128_t *py = (__uint128_t*)pRight; |
60 | | *px = *px % *py; */ |
61 | 0 | } |
62 | 0 | bool uInt128Lt(const UInt128* pLeft, const UInt128* pRight) { |
63 | 0 | const intx::uint128 *pX = (const intx::uint128*)pLeft; |
64 | 0 | const intx::uint128 *pY = (const intx::uint128*)pRight; |
65 | 0 | return *pX < *pY; |
66 | 0 | } |
67 | 0 | bool uInt128Gt(const UInt128* pLeft, const UInt128* pRight) { |
68 | 0 | const intx::uint128 *pX = (const intx::uint128*)pLeft; |
69 | 0 | const intx::uint128 *pY = (const intx::uint128*)pRight; |
70 | 0 | return *pX > *pY; |
71 | 0 | } |
72 | 0 | bool uInt128Eq(const UInt128* pLeft, const UInt128* pRight) { |
73 | 0 | const intx::uint128 *pX = (const intx::uint128*)pLeft; |
74 | 0 | const intx::uint128 *pY = (const intx::uint128*)pRight; |
75 | 0 | return *pX == *pY; |
76 | 0 | } |
77 | | |
78 | 0 | Int128 makeInt128(int64_t high, uint64_t low) { |
79 | 0 | Int128 int128 = {low, high}; |
80 | 0 | return int128; |
81 | 0 | } |
82 | 0 | int64_t int128Hi(const Int128* pUint128) { |
83 | 0 | return pUint128->high; |
84 | 0 | } |
85 | 0 | uint64_t int128Lo(const Int128* pUint128) { |
86 | 0 | return pUint128->low; |
87 | 0 | } |
88 | 0 | Int128 int128Abs(const Int128* pInt128) { |
89 | 0 | if (int128Lt(pInt128, &int128Zero)) { |
90 | 0 | return int128Negate(pInt128); |
91 | 0 | } |
92 | 0 | return *pInt128; |
93 | 0 | } |
94 | 0 | Int128 int128Negate(const Int128* pInt128) { |
95 | 0 | uint64_t low = ~pInt128->low + 1; |
96 | 0 | int64_t high = ~pInt128->high; |
97 | 0 | if (low == 0) high += 1; |
98 | 0 | return makeInt128(high, low); |
99 | 0 | } |
100 | 0 | Int128 int128Add(const Int128* pLeft, const Int128* pRight) { |
101 | 0 | intx::uint128 result = *(intx::uint128*)pLeft + *(intx::uint128*)pRight; |
102 | 0 | return *(Int128*)&result; |
103 | 0 | } |
104 | 0 | Int128 int128Subtract(const Int128* pLeft, const Int128* pRight) { |
105 | 0 | intx::uint128 result = *(intx::uint128*)pLeft - *(intx::uint128*)pRight; |
106 | 0 | return *(Int128*)&result; |
107 | 0 | } |
108 | 0 | Int128 int128Multiply(const Int128* pLeft, const Int128* pRight) { |
109 | 0 | intx::uint128 result = *(intx::uint128*)pLeft * *(intx::uint128*)pRight; |
110 | 0 | return *(Int128*)&result; |
111 | 0 | } |
112 | 0 | Int128 int128Divide(const Int128* pLeft, const Int128* pRight) { |
113 | 0 | intx::uint128 result = *(intx::uint128*)pLeft / *(intx::uint128*)pRight; |
114 | 0 | return *(Int128*)&result; |
115 | 0 | } |
116 | 0 | Int128 int128Mod(const Int128* pLeft, const Int128* pRight) { |
117 | 0 | intx::uint128 result = *(intx::uint128*)pLeft % *(intx::uint128*)pRight; |
118 | 0 | return *(Int128*)&result; |
119 | 0 | } |
120 | 0 | bool int128Lt(const Int128* pLeft, const Int128* pRight) { |
121 | 0 | return pLeft->high < pRight->high || (pLeft->high == pRight->high && pLeft->low < pRight->low); |
122 | 0 | } |
123 | 0 | bool int128Gt(const Int128* pLeft, const Int128* pRight) { |
124 | 0 | return int128Lt(pRight, pLeft); |
125 | 0 | } |
126 | 0 | bool int128Eq(const Int128* pLeft, const Int128* pRight) { |
127 | 0 | return pLeft->high == pRight->high && pLeft->low == pRight->low; |
128 | 0 | } |
129 | 0 | Int128 int128RightShift(const Int128* pLeft, int32_t shift) { |
130 | 0 | intx::uint128 result = *(intx::uint128*)pLeft >> shift; |
131 | 0 | return *(Int128*)&result; |
132 | 0 | } |
133 | | |
134 | | const Int128 int128Zero = {0, 0}; |
135 | | const Int128 int128One = {1, 0}; |
136 | | |
137 | 0 | UInt256 makeUint256(UInt128 high, UInt128 low) { |
138 | 0 | UInt256 uint256 = {high, low}; |
139 | 0 | return uint256; |
140 | 0 | } |
141 | 0 | uint128 uInt256Hi(const UInt256* pUint256) { |
142 | 0 | return pUint256->high; |
143 | 0 | } |
144 | 0 | uint128 uInt256Lo(const UInt256* pUint256) { |
145 | 0 | return pUint256->low; |
146 | 0 | } |
147 | 0 | UInt256 uInt256Add(const UInt256* pLeft, const UInt256* pRight) { |
148 | 0 | intx::uint256 result = *(intx::uint256*)pLeft + *(intx::uint256*)pRight; |
149 | 0 | return *(UInt256*)&result; |
150 | 0 | } |
151 | 0 | UInt256 uInt256Subtract(const UInt256* pLeft, const UInt256* pRight) { |
152 | 0 | intx::uint256 result = *(intx::uint256*)pLeft - *(intx::uint256*)pRight; |
153 | 0 | return *(UInt256*)&result; |
154 | 0 | } |
155 | 0 | UInt256 uInt256Multiply(const UInt256* pLeft, const UInt256* pRight) { |
156 | 0 | intx::uint256 result = *(intx::uint256*)pLeft * *(intx::uint256*)pRight; |
157 | 0 | return *(UInt256*)&result; |
158 | 0 | } |
159 | 0 | UInt256 uInt256Divide(const UInt256* pLeft, const UInt256* pRight) { |
160 | 0 | intx::uint256 result = *(intx::uint256*)pLeft / *(intx::uint256*)pRight; |
161 | 0 | return *(UInt256*)&result; |
162 | 0 | } |
163 | 0 | UInt256 uInt256Mod(const UInt256* pLeft, const UInt256* pRight) { |
164 | 0 | intx::uint256 result = *(intx::uint256*)pLeft % *(intx::uint256*)pRight; |
165 | 0 | return *(UInt256*)&result; |
166 | 0 | } |
167 | 0 | bool uInt256Lt(const UInt256* pLeft, const UInt256* pRight) { |
168 | 0 | return *(intx::uint256*)pLeft < *(intx::uint256*)pRight; |
169 | 0 | } |
170 | 0 | bool uInt256Gt(const UInt256* pLeft, const UInt256* pRight) { |
171 | 0 | return *(intx::uint256*)pLeft > *(intx::uint256*)pRight; |
172 | 0 | } |
173 | 0 | bool uInt256Eq(const UInt256* pLeft, const UInt256* pRight) { |
174 | 0 | return *(intx::uint256*)pLeft == *(intx::uint256*)pRight; |
175 | 0 | } |
176 | 0 | UInt256 uInt256RightShift(const UInt256* pLeft, int32_t shift) { |
177 | 0 | intx::uint256 result = *(intx::uint256*)pLeft >> shift; |
178 | 0 | return *(UInt256*)&result; |
179 | 0 | } |
180 | | |
181 | 0 | Int256 makeInt256(Int128 high, UInt128 low) { |
182 | 0 | Int256 int256 = {low, high}; |
183 | 0 | return int256; |
184 | 0 | } |
185 | 0 | Int128 int256Hi(const Int256* pUint256) { |
186 | 0 | return pUint256->high; |
187 | 0 | } |
188 | 0 | UInt128 int256Lo(const Int256* pUint256) { |
189 | 0 | return pUint256->low; |
190 | 0 | } |
191 | 0 | Int256 int256Abs(const Int256* pInt256) { |
192 | 0 | if (int256Lt(pInt256, &int256Zero)) { |
193 | 0 | return int256Negate(pInt256); |
194 | 0 | } |
195 | 0 | return *pInt256; |
196 | 0 | } |
197 | | |
198 | 0 | Int256 int256Negate(const Int256* pInt256) { |
199 | 0 | return int256Subtract(&int256Zero, pInt256); |
200 | 0 | } |
201 | 0 | Int256 int256Add(const Int256* pLeft, const Int256* pRight) { |
202 | 0 | intx::uint256 result = *(intx::uint256*)pLeft + *(intx::uint256*)pRight; |
203 | 0 | return *(Int256*)&result; |
204 | 0 | } |
205 | 0 | Int256 int256Subtract(const Int256* pLeft, const Int256* pRight) { |
206 | 0 | intx::uint256 result = *(intx::uint256*)pLeft - *(intx::uint256*)pRight; |
207 | 0 | return *(Int256*)&result; |
208 | 0 | } |
209 | 0 | Int256 int256Multiply(const Int256* pLeft, const Int256* pRight) { |
210 | 0 | intx::uint256 result = *(intx::uint256*)pLeft * *(intx::uint256*)pRight; |
211 | 0 | return *(Int256*)&result; |
212 | 0 | } |
213 | 0 | Int256 int256Divide(const Int256* pLeft, const Int256* pRight) { |
214 | 0 | Int256 l = *pLeft, r = *pRight; |
215 | 0 | bool leftNegative = int256Lt(pLeft, &int256Zero), rightNegative = int256Lt(pRight, &int256Zero); |
216 | 0 | if (leftNegative) { |
217 | 0 | l = int256Abs(pLeft); |
218 | 0 | } |
219 | 0 | if (rightNegative) { |
220 | 0 | r = int256Abs(pRight); |
221 | 0 | } |
222 | 0 | intx::uint256 result = *(intx::uint256*)&l / *(intx::uint256*)&r; |
223 | 0 | Int256 res = *(Int256*)&result; |
224 | 0 | if (leftNegative != rightNegative) |
225 | 0 | res = int256Negate(&res); |
226 | 0 | return res; |
227 | 0 | } |
228 | | |
229 | 0 | Int256 int256Mod(const Int256* pLeft, const Int256* pRight) { |
230 | 0 | Int256 left = *pLeft, right = *pRight; |
231 | 0 | bool leftNegative = int256Lt(pLeft, &int256Zero); |
232 | 0 | if (leftNegative) { |
233 | 0 | left = int256Abs(&left); |
234 | 0 | } |
235 | 0 | bool rightNegate = int256Lt(pRight, &int256Zero); |
236 | 0 | if (rightNegate) right = int256Abs(pRight); |
237 | 0 | intx::uint256 result = *(intx::uint256*)&left % *(intx::uint256*)&right; |
238 | 0 | Int256 res = *(Int256*)&result; |
239 | 0 | if (leftNegative) res = int256Negate(&res); |
240 | 0 | return res; |
241 | 0 | } |
242 | 0 | bool int256Lt(const Int256* pLeft, const Int256* pRight) { |
243 | 0 | Int128 hiLeft = int256Hi(pLeft), hiRight = int256Hi(pRight); |
244 | 0 | UInt128 lowLeft = int256Lo(pLeft), lowRight = int256Lo(pRight); |
245 | 0 | return int128Lt(&hiLeft, &hiRight) || (int128Eq(&hiLeft, &hiRight) && uInt128Lt(&lowLeft, &lowRight)); |
246 | 0 | } |
247 | 0 | bool int256Gt(const Int256* pLeft, const Int256* pRight) { |
248 | 0 | return int256Lt(pRight, pLeft); |
249 | 0 | } |
250 | 0 | bool int256Eq(const Int256* pLeft, const Int256* pRight) { |
251 | 0 | Int128 hiLeft = int256Hi(pLeft), hiRight = int256Hi(pRight); |
252 | 0 | UInt128 lowLeft = int256Lo(pLeft), lowRight = int256Lo(pRight); |
253 | 0 | return int128Eq(&hiLeft, &hiRight) && uInt128Eq(&lowLeft, &lowRight); |
254 | 0 | } |
255 | 0 | Int256 int256RightShift(const Int256* pLeft, int32_t shift) { |
256 | 0 | intx::uint256 result = *(intx::uint256*)pLeft >> shift; |
257 | 0 | return *(Int256*)&result; |
258 | 0 | } |
259 | | |
260 | | const Int256 int256One = {uInt128One, int128Zero}; |
261 | | const Int256 int256Zero = {uInt128Zero, int128Zero}; |
262 | | const Int256 int256Two = {uInt128Two, int128Zero}; |