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
345
346
|
#pragma once
#include <util/generic/strbuf.h>
#include "yql_wide_int.h"
#include <type_traits>
#include <limits>
namespace NYql {
namespace NDecimal {
#ifdef _win_
#ifndef DONT_USE_NATIVE_INT128
#define DONT_USE_NATIVE_INT128
#endif
#endif
#ifdef DONT_USE_NATIVE_INT128
using TInt128 = TWide<i64>;
using TUint128 = TWide<ui64>;
#else
using TInt128 = signed __int128;
using TUint128 = unsigned __int128;
#endif
template<ui8 Scale> struct TDivider;
#if defined(__clang__) && defined(DONT_USE_NATIVE_INT128)
template<> struct TDivider<0> { static inline constexpr TUint128 Value = 1U; };
template<ui8 Scale> struct TDivider { static inline constexpr TInt128 Value = TDivider<Scale - 1U>::Value * 10U; };
#else
template<> struct TDivider<0> { static constexpr TUint128 Value = 1U; };
template<ui8 Scale> struct TDivider { static constexpr TUint128 Value = TDivider<Scale - 1U>::Value * 10U; };
#endif
constexpr ui8 MaxPrecision = 35;
static_assert(sizeof(TInt128) == 16, "Wrong size of TInt128, expected 16");
inline constexpr TInt128 Inf() {
return TInt128(100000000000000000ULL) * TInt128(1000000000000000000ULL);
}
inline constexpr TInt128 Nan() {
return Inf() + TInt128(1);
}
inline constexpr TInt128 Err() {
return Nan() + TInt128(1);
}
TUint128 GetDivider(ui8 scale);
template<ui8 Precision>
inline constexpr TUint128 GetDivider() {
return TDivider<Precision>::Value;
}
template<ui8 Precision, bool IncLow = false, bool DecHigh = false>
inline constexpr std::pair<TInt128, TInt128> GetBounds() {
return std::make_pair(-GetDivider<Precision>() + (IncLow ? 1 : 0), +GetDivider<Precision>() - (DecHigh ? 1 : 0));
}
bool IsError(TInt128 v);
bool IsNan(TInt128 v);
bool IsInf(TInt128 v);
bool IsNormal(TInt128 v);
bool IsComparable(TInt128 v);
template<ui8 Precision>
inline bool IsNormal(TInt128 v) {
const auto& b = GetBounds<Precision>();
return v > b.first && v < b.second;
}
const char* ToString(TInt128 v, ui8 precision, ui8 scale = 0);
TInt128 FromString(const TStringBuf& str, ui8 precision, ui8 scale = 0);
// Accept string representation with exponent.
TInt128 FromStringEx(const TStringBuf& str, ui8 precision, ui8 scale);
template<typename TMkqlProto>
inline TInt128 FromProto(const TMkqlProto& val) {
ui64 half[2] = {val.GetLow128(), val.GetHi128()};
TInt128 val128;
std::memcpy(&val128, half, sizeof(val128));
return val128;
}
template<typename TValue>
inline constexpr TValue YtDecimalNan() {
return std::numeric_limits<TValue>::max();
}
template<>
inline constexpr TInt128 YtDecimalNan<TInt128>() {
return ~(TInt128(1) << 127);
}
template<typename TValue>
inline constexpr TValue YtDecimalInf() {
return YtDecimalNan<TValue>() - 1;
}
template<typename TValue>
inline TInt128 FromYtDecimal(TValue val) {
static_assert(std::is_same<TInt128, TValue>::value || std::is_signed<TValue>::value, "Expected signed value");
if (YtDecimalNan<TValue>() == val) {
return Nan();
} else if (YtDecimalInf<TValue>() == val) {
return Inf();
} else if (-YtDecimalInf<TValue>() == val) {
return -Inf();
} else {
return TInt128(val);
}
}
template<typename TValue>
inline TValue ToYtDecimal(TInt128 val) {
static_assert(std::is_same<TInt128, TValue>::value || std::is_signed<TValue>::value, "Expected signed value");
if (IsNormal(val)) {
return (TValue)val;
} else if (val == Inf()) {
return YtDecimalInf<TValue>();
} else if (val == -Inf()) {
return -YtDecimalInf<TValue>();
}
return YtDecimalNan<TValue>();
}
inline TInt128 FromHalfs(ui64 lo, i64 hi) {
ui64 half[2] = {lo, static_cast<ui64>(hi)};
TInt128 val128;
std::memcpy(&val128, half, sizeof(val128));
return val128;
}
inline std::pair<ui64, ui64> MakePair(const TInt128 v) {
std::pair<ui64, ui64> r;
std::memcpy(&r, &v, sizeof(v));
return r;
static_assert(sizeof(r) == sizeof(v), "Bad pair size.");
}
bool IsValid(const TStringBuf& str);
// Round to nearest, ties to even.
TInt128 Div(TInt128 a, TInt128 b); // a/b
TInt128 Mul(TInt128 a, TInt128 b); // a*b
TInt128 Mod(TInt128 a, TInt128 b); // a%b
// a*b/c Only for non zero even normal positive divider.
TInt128 MulAndDivNormalDivider(TInt128 a, TInt128 b, TInt128 c);
// a*b/c Only for non zero normal positive multiplier.
TInt128 MulAndDivNormalMultiplier(TInt128 a, TInt128 b, TInt128 c);
struct TDecimal {
TInt128 Value = 0;
TDecimal() = default;
template<typename T>
TDecimal(T t): Value(t) { }
explicit operator TInt128() const {
return Value;
}
TDecimal& operator+=(TDecimal right) {
const auto l = Value;
const auto r = right.Value;
const auto a = l + r;
if (IsNormal(l) && IsNormal(r) && IsNormal(a)) {
Value = a;
} else if (IsNan(l) || IsNan(r) || !a /* inf - inf*/) {
Value = Nan();
} else {
Value = a > 0
? +Inf()
: -Inf();
}
return *this;
}
TDecimal& operator*=(TDecimal right) {
Value = Mul(Value, right.Value);
return *this;
}
TDecimal& operator/=(TDecimal right) {
Value = Div(Value, right.Value);
return *this;
}
friend TDecimal operator+(TDecimal left, TDecimal right) {
left += right;
return left;
}
friend TDecimal operator*(TDecimal left, TDecimal right) {
left *= right;
return left;
}
friend TDecimal operator/(TDecimal left, TDecimal right) {
left /= right;
return left;
}
};
template<typename TRight>
class TDecimalMultiplicator {
protected:
const TInt128 Bound;
public:
TDecimalMultiplicator(
ui8 precision,
ui8 scale = 0 /* unused */)
: Bound(GetDivider(precision))
{
Y_UNUSED(scale);
}
TInt128 Do(TInt128 left, TRight right) const {
TInt128 mul = Mul(left, right);
if (mul > -Bound && mul < +Bound)
return mul;
return IsNan(mul) ? Nan() : (mul > 0 ? +Inf() : -Inf());
}
};
template<>
class TDecimalMultiplicator<TInt128> {
protected:
const TInt128 Bound;
const TInt128 Divider;
public:
TDecimalMultiplicator(
ui8 precision,
ui8 scale)
: Bound(GetDivider(precision))
, Divider(GetDivider(scale))
{ }
TInt128 Do(TInt128 left, TInt128 right) const {
TInt128 mul = Divider > 1 ?
MulAndDivNormalDivider(left, right, Divider):
Mul(left, right);
if (mul > -Bound && mul < +Bound)
return mul;
return IsNan(mul) ? Nan() : (mul > 0 ? +Inf() : -Inf());
}
};
template<typename TRight>
class TDecimalDivisor {
public:
TDecimalDivisor(
ui8 precision = 0 /* unused */,
ui8 scale = 0 /* unused */)
{
Y_UNUSED(precision);
Y_UNUSED(scale);
}
TInt128 Do(TInt128 left, TRight right) const {
return Div(left, right);
}
};
template<>
class TDecimalDivisor<TInt128> {
protected:
const TInt128 Bound;
const TInt128 Divider;
public:
TDecimalDivisor(
ui8 precision,
ui8 scale)
: Bound(GetDivider(precision))
, Divider(GetDivider(scale))
{ }
TInt128 Do(TInt128 left, TInt128 right) const {
TInt128 div = MulAndDivNormalMultiplier(left, Divider, right);
if (div > -Bound && div < +Bound) {
return div;
}
return IsNan(div) ? Nan() : (div > 0 ? +Inf() : -Inf());
}
};
template<typename TRight>
class TDecimalRemainder {
protected:
const TInt128 Bound;
const TInt128 Divider;
public:
TDecimalRemainder(
ui8 precision,
ui8 scale)
: Bound(NYql::NDecimal::GetDivider(precision - scale))
, Divider(NYql::NDecimal::GetDivider(scale))
{ }
TInt128 Do(TInt128 left, TRight right) const {
if constexpr (std::is_signed<TRight>::value) {
if (TInt128(right) >= +Bound || TInt128(right) <= -Bound)
return left;
} else {
if (TInt128(right) >= Bound)
return left;
}
return Mod(left, Mul(Divider, right));
}
};
template<>
class TDecimalRemainder<TInt128> {
public:
TDecimalRemainder(
ui8 precision = 0 /*unused*/,
ui8 scale = 0 /*unused*/)
{
Y_UNUSED(precision);
Y_UNUSED(scale);
}
TInt128 Do(TInt128 left, TInt128 right) const {
return NYql::NDecimal::Mod(left, right);
}
};
}
}
|