aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Core/AccurateComparison.h
blob: a201c136e3aa54976006781bf7b25eff7f34753f (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
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
#pragma once

#include <cmath>
#include <limits>
#include <base/DecomposedFloat.h>
#include <Core/Defines.h>
#include <Core/Types.h>
#include <base/extended_types.h>
#include <Common/NaNUtils.h>

/** Preceptually-correct number comparisons.
  * Example: Int8(-1) != UInt8(255)
*/

namespace accurate
{

using namespace DB;


template <typename A, typename B>
bool lessOp(A a, B b)
{
    if constexpr (std::is_same_v<A, B>)
        return a < b;

    /// float vs float
    if constexpr (std::is_floating_point_v<A> && std::is_floating_point_v<B>)
        return a < b;

    /// anything vs NaN
    if (isNaN(a) || isNaN(b))
        return false;

    /// int vs int
    if constexpr (is_integer<A> && is_integer<B>)
    {
        /// same signedness
        if constexpr (is_signed_v<A> == is_signed_v<B>)
            return a < b;

        /// different signedness

        if constexpr (is_signed_v<A> && !is_signed_v<B>)
            return a < 0 || static_cast<make_unsigned_t<A>>(a) < b;

        if constexpr (!is_signed_v<A> && is_signed_v<B>)
            return b >= 0 && a < static_cast<make_unsigned_t<B>>(b);
    }

    /// int vs float
    if constexpr (is_integer<A> && std::is_floating_point_v<B>)
    {
        if constexpr (sizeof(A) <= 4)
            return static_cast<double>(a) < static_cast<double>(b);

        return DecomposedFloat<B>(b).greater(a);
    }

    if constexpr (std::is_floating_point_v<A> && is_integer<B>)
    {
        if constexpr (sizeof(B) <= 4)
            return static_cast<double>(a) < static_cast<double>(b);

        return DecomposedFloat<A>(a).less(b);
    }

    static_assert(is_integer<A> || std::is_floating_point_v<A>);
    static_assert(is_integer<B> || std::is_floating_point_v<B>);
    UNREACHABLE();
}

template <typename A, typename B>
bool greaterOp(A a, B b)
{
    return lessOp(b, a);
}

template <typename A, typename B>
bool greaterOrEqualsOp(A a, B b)
{
    if (isNaN(a) || isNaN(b))
        return false;

    return !lessOp(a, b);
}

template <typename A, typename B>
bool lessOrEqualsOp(A a, B b)
{
    if (isNaN(a) || isNaN(b))
        return false;

    return !lessOp(b, a);
}

template <typename A, typename B>
bool equalsOp(A a, B b)
{
    if constexpr (std::is_same_v<A, B>)
        return a == b;

    /// float vs float
    if constexpr (std::is_floating_point_v<A> && std::is_floating_point_v<B>)
        return a == b;

    /// anything vs NaN
    if (isNaN(a) || isNaN(b))
        return false;

    /// int vs int
    if constexpr (is_integer<A> && is_integer<B>)
    {
        /// same signedness
        if constexpr (is_signed_v<A> == is_signed_v<B>)
            return a == b;

        /// different signedness

        if constexpr (is_signed_v<A> && !is_signed_v<B>)
            return a >= 0 && static_cast<make_unsigned_t<A>>(a) == b;

        if constexpr (!is_signed_v<A> && is_signed_v<B>)
            return b >= 0 && a == static_cast<make_unsigned_t<B>>(b);
    }

    /// int vs float
    if constexpr (is_integer<A> && std::is_floating_point_v<B>)
    {
        if constexpr (sizeof(A) <= 4)
            return static_cast<double>(a) == static_cast<double>(b);

        return DecomposedFloat<B>(b).equals(a);
    }

    if constexpr (std::is_floating_point_v<A> && is_integer<B>)
    {
        if constexpr (sizeof(B) <= 4)
            return static_cast<double>(a) == static_cast<double>(b);

        return DecomposedFloat<A>(a).equals(b);
    }

    /// e.g comparing UUID with integer.
    return false;
}

template <typename A, typename B>
bool notEqualsOp(A a, B b)
{
    return !equalsOp(a, b);
}

/// Converts numeric to an equal numeric of other type.
/// When `strict` is `true` check that result exactly same as input, otherwise just check overflow
template <typename From, typename To, bool strict = true>
inline bool NO_SANITIZE_UNDEFINED convertNumeric(From value, To & result)
{
    /// If the type is actually the same it's not necessary to do any checks.
    if constexpr (std::is_same_v<From, To>)
    {
        result = value;
        return true;
    }

    if constexpr (std::is_floating_point_v<From> && std::is_floating_point_v<To>)
    {
        /// Note that NaNs doesn't compare equal to anything, but they are still in range of any Float type.
        if (isNaN(value))
        {
            result = static_cast<To>(value);
            return true;
        }

        if (value == std::numeric_limits<From>::infinity())
        {
            result = std::numeric_limits<To>::infinity();
            return true;
        }

        if (value == -std::numeric_limits<From>::infinity())
        {
            result = -std::numeric_limits<To>::infinity();
            return true;
        }
    }

    if (greaterOp(value, std::numeric_limits<To>::max())
        || lessOp(value, std::numeric_limits<To>::lowest()))
    {
        return false;
    }

    result = static_cast<To>(value);
    if constexpr (strict)
        return equalsOp(value, result);
    return true;
}

}


namespace DB
{

template <typename A, typename B> struct EqualsOp
{
    /// An operation that gives the same result, if arguments are passed in reverse order.
    using SymmetricOp = EqualsOp<B, A>;

    static UInt8 apply(A a, B b) { return accurate::equalsOp(a, b); }
};

template <typename A, typename B> struct NotEqualsOp
{
    using SymmetricOp = NotEqualsOp<B, A>;
    static UInt8 apply(A a, B b) { return accurate::notEqualsOp(a, b); }
};

template <typename A, typename B> struct GreaterOp;

template <typename A, typename B> struct LessOp
{
    using SymmetricOp = GreaterOp<B, A>;
    static UInt8 apply(A a, B b) { return accurate::lessOp(a, b); }
};

template <typename A, typename B> struct GreaterOp
{
    using SymmetricOp = LessOp<B, A>;
    static UInt8 apply(A a, B b) { return accurate::greaterOp(a, b); }
};

template <typename A, typename B> struct GreaterOrEqualsOp;

template <typename A, typename B> struct LessOrEqualsOp
{
    using SymmetricOp = GreaterOrEqualsOp<B, A>;
    static UInt8 apply(A a, B b) { return accurate::lessOrEqualsOp(a, b); }
};

template <typename A, typename B> struct GreaterOrEqualsOp
{
    using SymmetricOp = LessOrEqualsOp<B, A>;
    static UInt8 apply(A a, B b) { return accurate::greaterOrEqualsOp(a, b); }
};

}