aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/library/yql/public/decimal/yql_decimal.h
blob: 3c1a57a8480e0aa8d36dff81b8e04d72fc7d5438 (plain) (blame)
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
#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);

}
}