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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
#pragma once
#include <util/system/types.h>
#include <type_traits>
#include <tuple>
#include <cmath>
namespace NYql {
#ifndef _win_
typedef __int128 i128_t;
typedef unsigned __int128 ui128_t;
#endif
template<typename TOneHalf, typename TSignedPart = std::make_signed_t<TOneHalf>, typename TUnsignedPart = std::make_unsigned_t<TOneHalf>>
class TWide {
private:
using THalf = TOneHalf;
using TPart = TUnsignedPart;
using TIsSigned = std::is_same<THalf, TSignedPart>;
using TIsUnsigned = std::is_same<THalf, TUnsignedPart>;
static_assert(TIsSigned::value || TIsUnsigned::value, "Invalid using of TWide.");
static_assert(sizeof(TSignedPart) == sizeof(TUnsignedPart), "Different sizes of TWide parts.");
TPart Lo;
THalf Hi;
static constexpr auto FullBitSize = sizeof(TPart) << 4U;
static constexpr auto PartBitSize = sizeof(TPart) << 3U;
static constexpr auto QuarterBitSize = sizeof(TPart) << 2U;
static constexpr TPart GetUpperQuarter(TPart h) {
return h >> QuarterBitSize;
}
static constexpr TPart GetLowerQuarter(TPart h) {
const auto mask = TPart(~TPart(0U)) >> QuarterBitSize;
return h & mask;
}
template<typename T>
constexpr std::enable_if_t<sizeof(T) <= sizeof(THalf), T> CastImpl() const {
return static_cast<T>(Lo);
}
template<typename T>
constexpr std::enable_if_t<sizeof(T) == sizeof(THalf) << 1U, T> CastImpl() const {
return *reinterpret_cast<const T*>(this);
}
template<typename T>
constexpr std::enable_if_t<sizeof(THalf) << 1U < sizeof(T), T> CastImpl() const {
return (T(std::make_unsigned_t<T>(Hi) << PartBitSize)) | Lo;
}
constexpr size_t GetBits() const {
size_t out = Hi ? PartBitSize : 0U;
for (auto up = TPart(out ? Hi : Lo); up; up >>= 1U) {
++out;
}
return out;
}
using TUnsigned = TWide<TUnsignedPart, TSignedPart, TUnsignedPart>;
using TSibling = std::conditional_t<std::is_same<THalf, TPart>::value,
TWide<TSignedPart, TSignedPart, TUnsignedPart>, TWide<TUnsignedPart, TSignedPart, TUnsignedPart>>;
friend TSibling;
public:
constexpr TWide() = default;
constexpr TWide(const TWide& rhs) = default;
constexpr TWide(TWide&& rhs) = default;
constexpr TWide& operator=(const TWide& rhs) = default;
constexpr TWide& operator=(TWide&& rhs) = default;
constexpr TWide(const TSibling& rhs)
: Lo(rhs.Lo), Hi(rhs.Hi)
{}
template<typename U, typename L>
constexpr TWide(const U upper_rhs, const L lower_rhs)
: Lo(lower_rhs), Hi(upper_rhs)
{}
template <typename T, std::enable_if_t<std::is_integral<T>::value && sizeof(THalf) < sizeof(T), size_t> Shift = PartBitSize>
constexpr TWide(const T rhs)
: Lo(rhs), Hi(rhs >> Shift)
{}
template <typename T, std::enable_if_t<std::is_integral<T>::value && sizeof(T) <= sizeof(THalf), bool> Signed = std::is_signed<T>::value>
constexpr TWide(const T rhs)
: Lo(rhs), Hi(Signed && rhs < 0 ? ~0 : 0)
{}
template <typename T, typename TArg = std::enable_if_t<std::is_class<T>::value && std::is_same<T, THalf>::value, THalf>>
constexpr explicit TWide(const T& rhs)
: Lo(rhs), Hi(TIsSigned::value && rhs < 0 ? ~0 : 0)
{}
template <typename T, std::enable_if_t<std::is_integral<T>::value && sizeof(THalf) < sizeof(T), size_t> Shift = PartBitSize>
constexpr TWide& operator=(const T rhs) {
Hi = rhs >> Shift;
Lo = rhs;
return *this;
}
template <typename T, std::enable_if_t<std::is_integral<T>::value && sizeof(T) <= sizeof(THalf), bool> Signed = std::is_signed<T>::value>
constexpr TWide& operator=(const T rhs) {
Hi = Signed && rhs < 0 ? ~0 : 0;
Lo = rhs;
return *this;
}
constexpr explicit operator bool() const { return bool(Hi) || bool(Lo); }
constexpr explicit operator i8() const { return CastImpl<i8>(); }
constexpr explicit operator ui8() const { return CastImpl<ui8>(); }
constexpr explicit operator i16() const { return CastImpl<i16>(); }
constexpr explicit operator ui16() const { return CastImpl<ui16>(); }
constexpr explicit operator i32() const { return CastImpl<i32>(); }
constexpr explicit operator ui32() const { return CastImpl<ui32>(); }
constexpr explicit operator i64() const { return CastImpl<i64>(); }
constexpr explicit operator ui64() const { return CastImpl<ui64>(); }
#ifndef _win_
constexpr explicit operator i128_t() const { return CastImpl<i128_t>(); }
constexpr explicit operator ui128_t() const { return CastImpl<ui128_t>(); }
#endif
constexpr explicit operator float() const {
return TIsSigned::value && Hi < 0 ? -float(-*this) : std::fma(float(Hi), std::exp2(float(PartBitSize)), float(Lo));
}
constexpr explicit operator double() const {
return TIsSigned::value && Hi < 0 ? -double(-*this) : std::fma(double(Hi), std::exp2(double(PartBitSize)), double(Lo));
}
constexpr TWide operator~() const {
return TWide(~Hi, ~Lo);
}
constexpr TWide operator+() const {
return TWide(Hi, Lo);
}
constexpr TWide operator-() const {
const auto sign = THalf(1) << PartBitSize - 1U;
if (TIsSigned::value && !Lo && Hi == sign)
return *this;
return ++~*this;
}
constexpr TWide& operator++() {
if (!++Lo) ++Hi;
return *this;
}
constexpr TWide& operator--() {
if (!Lo--) --Hi;
return *this;
}
constexpr TWide operator++(int) {
const TWide r(*this);
++*this;
return r;
}
constexpr TWide operator--(int) {
const TWide r(*this);
--*this;
return r;
}
constexpr TWide& operator&=(const TWide& rhs) {
Hi &= rhs.Hi;
Lo &= rhs.Lo;
return *this;
}
constexpr TWide& operator|=(const TWide& rhs) {
Hi |= rhs.Hi;
Lo |= rhs.Lo;
return *this;
}
constexpr TWide& operator^=(const TWide& rhs) {
Hi ^= rhs.Hi;
Lo ^= rhs.Lo;
return *this;
}
constexpr TWide& operator+=(const TWide& rhs) {
const auto l = Lo;
Lo += rhs.Lo;
Hi += rhs.Hi;
if (l > Lo) ++Hi;
return *this;
}
constexpr TWide& operator-=(const TWide& rhs) {
const auto l = Lo;
Lo -= rhs.Lo;
Hi -= rhs.Hi;
if (l < Lo) --Hi;
return *this;
}
constexpr TWide& operator<<=(const TWide& rhs) {
if (const auto shift = size_t(rhs.Lo) % FullBitSize) {
if (shift < PartBitSize) {
Hi = TPart(Hi) << shift;
Hi |= Lo >> (PartBitSize - shift);
Lo <<= shift;
} else {
Hi = Lo << (shift - PartBitSize);
Lo = 0;
}
}
return *this;
}
constexpr TWide& operator>>=(const TWide& rhs) {
if (const auto shift = size_t(rhs.Lo) % FullBitSize) {
if (shift < PartBitSize) {
Lo >>= shift;
Lo |= TPart(Hi) << (PartBitSize - shift);
Hi >>= shift;
} else {
Lo = Hi >> (shift - PartBitSize);
Hi = TIsSigned::value && Hi < 0 ? ~0 : 0;
}
}
return *this;
}
constexpr TWide& operator*=(const TWide& rhs) { return *this = Mul(*this, rhs); }
constexpr TWide& operator/=(const TWide& rhs) { return *this = DivMod(*this, rhs).first; }
constexpr TWide& operator%=(const TWide& rhs) { return *this = DivMod(*this, rhs).second; }
constexpr TWide operator&(const TWide& rhs) const { return TWide(*this) &= rhs; }
constexpr TWide operator|(const TWide& rhs) const { return TWide(*this) |= rhs; }
constexpr TWide operator^(const TWide& rhs) const { return TWide(*this) ^= rhs; }
constexpr TWide operator+(const TWide& rhs) const { return TWide(*this) += rhs; }
constexpr TWide operator-(const TWide& rhs) const { return TWide(*this) -= rhs; }
constexpr TWide operator*(const TWide& rhs) const { return Mul(*this, rhs); }
constexpr TWide operator/(const TWide& rhs) const { return DivMod(*this, rhs).first; }
constexpr TWide operator%(const TWide& rhs) const { return DivMod(*this, rhs).second; }
constexpr TWide operator<<(const TWide& rhs) const { return TWide(*this) <<= rhs; }
constexpr TWide operator>>(const TWide& rhs) const { return TWide(*this) >>= rhs; }
template <typename T>
constexpr std::enable_if_t<std::is_integral<T>::value, T> operator&(const T rhs) const { return T(*this) & rhs; }
constexpr bool operator==(const TWide& rhs) const { return std::tie(Hi, Lo) == std::tie(rhs.Hi, rhs.Lo); }
constexpr bool operator!=(const TWide& rhs) const { return std::tie(Hi, Lo) != std::tie(rhs.Hi, rhs.Lo); }
constexpr bool operator>=(const TWide& rhs) const { return std::tie(Hi, Lo) >= std::tie(rhs.Hi, rhs.Lo); }
constexpr bool operator<=(const TWide& rhs) const { return std::tie(Hi, Lo) <= std::tie(rhs.Hi, rhs.Lo); }
constexpr bool operator>(const TWide& rhs) const { return std::tie(Hi, Lo) > std::tie(rhs.Hi, rhs.Lo); }
constexpr bool operator<(const TWide& rhs) const { return std::tie(Hi, Lo) < std::tie(rhs.Hi, rhs.Lo); }
private:
static constexpr TWide Mul(const TWide& lhs, const TWide& rhs) {
const TPart lq[] = {GetLowerQuarter(lhs.Lo), GetUpperQuarter(lhs.Lo), GetLowerQuarter(lhs.Hi), GetUpperQuarter(lhs.Hi)};
const TPart rq[] = {GetLowerQuarter(rhs.Lo), GetUpperQuarter(rhs.Lo), GetLowerQuarter(rhs.Hi), GetUpperQuarter(rhs.Hi)};
const TPart prod0[] = {TPart(lq[0] * rq[0])};
const TPart prod1[] = {TPart(lq[0] * rq[1]), TPart(lq[1] * rq[0])};
const TPart prod2[] = {TPart(lq[0] * rq[2]), TPart(lq[1] * rq[1]), TPart(lq[2] * rq[0])};
const TPart prod3[] = {TPart(lq[0] * rq[3]), TPart(lq[1] * rq[2]), TPart(lq[2] * rq[1]), TPart(lq[3] * rq[0])};
const TPart fourthQ = GetLowerQuarter(prod0[0]);
const TPart thirdQ = GetUpperQuarter(prod0[0])
+ GetLowerQuarter(prod1[0]) + GetLowerQuarter(prod1[1]);
const TPart secondQ = GetUpperQuarter(thirdQ)
+ GetUpperQuarter(prod1[0]) + GetUpperQuarter(prod1[1])
+ GetLowerQuarter(prod2[0]) + GetLowerQuarter(prod2[1]) + GetLowerQuarter(prod2[2]);
const TPart firstQ = GetUpperQuarter(secondQ)
+ GetUpperQuarter(prod2[0]) + GetUpperQuarter(prod2[1]) + GetUpperQuarter(prod2[2])
+ GetLowerQuarter(prod3[0]) + GetLowerQuarter(prod3[1]) + GetLowerQuarter(prod3[2]) + GetLowerQuarter(prod3[3]);
return TWide((firstQ << QuarterBitSize) | GetLowerQuarter(secondQ), (thirdQ << QuarterBitSize) | fourthQ);
}
static constexpr std::pair<TWide, TWide> DivMod(const TWide& lhs, const TWide& rhs) {
const bool nl = TIsSigned::value && lhs.Hi < 0;
const bool nr = TIsSigned::value && rhs.Hi < 0;
const TUnsigned l = nl ? -lhs : +lhs, r = nr ? -rhs : +rhs;
TUnsigned div = 0, mod = 0;
for (auto x = l.GetBits(); x;) {
mod <<= 1;
div <<= 1;
if (--x < PartBitSize ? l.Lo & (TPart(1U) << x) : l.Hi & (TPart(1U) << x - PartBitSize)) {
++mod;
}
if (mod >= r) {
mod -= r;
++div;
}
}
if (nl) mod = -mod;
if (nr != nl) div = -div;
return {div, mod};
}
};
template<typename T> struct THalfOf;
template<> struct THalfOf<i16> { typedef i8 Type; };
template<> struct THalfOf<ui16> { typedef ui8 Type; };
template<> struct THalfOf<i32> { typedef i16 Type; };
template<> struct THalfOf<ui32> { typedef ui16 Type; };
template<> struct THalfOf<i64> { typedef i32 Type; };
template<> struct THalfOf<ui64> { typedef ui32 Type; };
#ifndef _win_
template<> struct THalfOf<i128_t> { typedef i64 Type; };
template<> struct THalfOf<ui128_t> { typedef ui64 Type; };
#endif
template<typename T> struct THalfOf {};
template<typename T> struct TPairOf;
template<> struct TPairOf<i8> { typedef i16 Type; };
template<> struct TPairOf<ui8> { typedef ui16 Type; };
template<> struct TPairOf<i16> { typedef i32 Type; };
template<> struct TPairOf<ui16> { typedef ui32 Type; };
template<> struct TPairOf<i32> { typedef i64 Type; };
template<> struct TPairOf<ui32> { typedef ui64 Type; };
#ifndef _win_
template<> struct TPairOf<i64> { typedef i128_t Type; };
template<> struct TPairOf<ui64> { typedef ui128_t Type; };
#endif
template<typename T> struct TPairOf {};
}
|