2017-01-13 19:45:18 +00:00
|
|
|
#pragma once
|
2017-02-02 22:08:19 +00:00
|
|
|
|
2018-09-03 13:34:16 +00:00
|
|
|
#include <cmath>
|
2017-02-02 22:08:19 +00:00
|
|
|
#include <limits>
|
2019-03-13 13:58:04 +00:00
|
|
|
#include "Defines.h"
|
|
|
|
#include "Types.h"
|
2020-09-22 11:35:46 +00:00
|
|
|
#include <common/extended_types.h>
|
2018-09-03 15:55:16 +00:00
|
|
|
#include <Common/NaNUtils.h>
|
2017-07-24 12:58:01 +00:00
|
|
|
#include <Common/UInt128.h>
|
2016-11-21 13:21:35 +00:00
|
|
|
|
2017-01-11 13:38:36 +00:00
|
|
|
/** Preceptually-correct number comparisons.
|
|
|
|
* Example: Int8(-1) != UInt8(255)
|
|
|
|
*/
|
|
|
|
|
2016-11-21 13:21:35 +00:00
|
|
|
namespace accurate
|
|
|
|
{
|
|
|
|
|
2017-02-07 15:38:57 +00:00
|
|
|
using DB::UInt64;
|
|
|
|
|
2016-11-21 13:21:35 +00:00
|
|
|
/** Cases:
|
2017-04-01 07:20:54 +00:00
|
|
|
1) Safe conversion (in case of default C++ operators)
|
|
|
|
a) int vs any int
|
|
|
|
b) uint vs any uint
|
|
|
|
c) float vs any float
|
|
|
|
2) int vs uint
|
|
|
|
a) sizeof(int) <= sizeof(uint). Accurate comparison with MAX_INT tresholds
|
|
|
|
b) sizeof(int) > sizeof(uint). Casting to int
|
|
|
|
3) integral_type vs floating_type
|
|
|
|
a) sizeof(integral_type) <= 4. Comparison via casting arguments to Float64
|
|
|
|
b) sizeof(integral_type) == 8. Accurate comparison. Consider 3 sets of intervals:
|
|
|
|
1) interval between adjacent floats less or equal 1
|
|
|
|
2) interval between adjacent floats greater then 2
|
|
|
|
3) float is outside [MIN_INT64; MAX_INT64]
|
2016-11-21 13:21:35 +00:00
|
|
|
*/
|
|
|
|
|
2017-01-11 13:38:36 +00:00
|
|
|
// Case 1. Is pair of floats or pair of ints or pair of uints
|
|
|
|
template <typename A, typename B>
|
2017-12-25 04:01:46 +00:00
|
|
|
constexpr bool is_safe_conversion = (std::is_floating_point_v<A> && std::is_floating_point_v<B>)
|
2020-08-19 11:52:17 +00:00
|
|
|
|| (is_integer_v<A> && is_integer_v<B> && !(is_signed_v<A> ^ is_signed_v<B>));
|
2017-01-11 13:38:36 +00:00
|
|
|
template <typename A, typename B>
|
2017-12-25 04:01:46 +00:00
|
|
|
using bool_if_safe_conversion = std::enable_if_t<is_safe_conversion<A, B>, bool>;
|
2017-01-11 13:38:36 +00:00
|
|
|
template <typename A, typename B>
|
2017-12-25 04:01:46 +00:00
|
|
|
using bool_if_not_safe_conversion = std::enable_if_t<!is_safe_conversion<A, B>, bool>;
|
2017-01-11 13:38:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// Case 2. Are params IntXX and UIntYY ?
|
2016-11-21 13:21:35 +00:00
|
|
|
template <typename TInt, typename TUInt>
|
2019-11-02 05:55:06 +00:00
|
|
|
constexpr bool is_any_int_vs_uint
|
2020-08-19 11:52:17 +00:00
|
|
|
= is_integer_v<TInt> && is_integer_v<TUInt> && is_signed_v<TInt> && is_unsigned_v<TUInt>;
|
2016-11-21 13:21:35 +00:00
|
|
|
|
2017-01-11 13:38:36 +00:00
|
|
|
// Case 2a. Are params IntXX and UIntYY and sizeof(IntXX) >= sizeof(UIntYY) (in such case will use accurate compare)
|
2016-11-21 13:21:35 +00:00
|
|
|
template <typename TInt, typename TUInt>
|
2017-12-25 04:01:46 +00:00
|
|
|
constexpr bool is_le_int_vs_uint = is_any_int_vs_uint<TInt, TUInt> && (sizeof(TInt) <= sizeof(TUInt));
|
2016-11-21 13:21:35 +00:00
|
|
|
|
2020-08-19 11:52:17 +00:00
|
|
|
static_assert(is_le_int_vs_uint<Int128, DB::UInt128>);
|
2020-09-01 09:54:50 +00:00
|
|
|
static_assert(is_le_int_vs_uint<Int128, DB::UInt256>);
|
2020-08-19 11:52:17 +00:00
|
|
|
|
2016-11-21 13:21:35 +00:00
|
|
|
template <typename TInt, typename TUInt>
|
2017-12-25 04:01:46 +00:00
|
|
|
using bool_if_le_int_vs_uint_t = std::enable_if_t<is_le_int_vs_uint<TInt, TUInt>, bool>;
|
2016-11-21 13:21:35 +00:00
|
|
|
|
|
|
|
template <typename TInt, typename TUInt>
|
2017-01-13 19:45:18 +00:00
|
|
|
inline bool_if_le_int_vs_uint_t<TInt, TUInt> greaterOpTmpl(TInt a, TUInt b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return static_cast<TUInt>(a) > b && a >= 0 && b <= static_cast<TUInt>(std::numeric_limits<TInt>::max());
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TUInt, typename TInt>
|
2017-01-13 19:45:18 +00:00
|
|
|
inline bool_if_le_int_vs_uint_t<TInt, TUInt> greaterOpTmpl(TUInt a, TInt b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return a > static_cast<TUInt>(b) || b < 0 || a > static_cast<TUInt>(std::numeric_limits<TInt>::max());
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TInt, typename TUInt>
|
2017-01-13 19:45:18 +00:00
|
|
|
inline bool_if_le_int_vs_uint_t<TInt, TUInt> equalsOpTmpl(TInt a, TUInt b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return static_cast<TUInt>(a) == b && a >= 0 && b <= static_cast<TUInt>(std::numeric_limits<TInt>::max());
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TUInt, typename TInt>
|
2017-01-13 19:45:18 +00:00
|
|
|
inline bool_if_le_int_vs_uint_t<TInt, TUInt> equalsOpTmpl(TUInt a, TInt b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return a == static_cast<TUInt>(b) && b >= 0 && a <= static_cast<TUInt>(std::numeric_limits<TInt>::max());
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-11 13:38:36 +00:00
|
|
|
// Case 2b. Are params IntXX and UIntYY and sizeof(IntXX) > sizeof(UIntYY) (in such case will cast UIntYY to IntXX and compare)
|
2016-11-21 13:21:35 +00:00
|
|
|
template <typename TInt, typename TUInt>
|
2017-12-25 04:01:46 +00:00
|
|
|
constexpr bool is_gt_int_vs_uint = is_any_int_vs_uint<TInt, TUInt> && (sizeof(TInt) > sizeof(TUInt));
|
2016-11-21 13:21:35 +00:00
|
|
|
|
|
|
|
template <typename TInt, typename TUInt>
|
2017-12-25 04:01:46 +00:00
|
|
|
using bool_if_gt_int_vs_uint = std::enable_if_t<is_gt_int_vs_uint<TInt, TUInt>, bool>;
|
2016-11-21 13:21:35 +00:00
|
|
|
|
|
|
|
template <typename TInt, typename TUInt>
|
2017-01-13 19:45:18 +00:00
|
|
|
inline bool_if_gt_int_vs_uint<TInt, TUInt> greaterOpTmpl(TInt a, TUInt b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2021-01-26 19:04:03 +00:00
|
|
|
return static_cast<TInt>(a) > static_cast<TInt>(b);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TInt, typename TUInt>
|
2017-01-13 19:45:18 +00:00
|
|
|
inline bool_if_gt_int_vs_uint<TInt, TUInt> greaterOpTmpl(TUInt a, TInt b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2020-09-01 09:54:50 +00:00
|
|
|
using CastA = std::conditional_t<is_big_int_v<TInt> && std::is_same_v<TUInt, DB::UInt128>, DB::UInt256, TInt>;
|
|
|
|
|
2021-01-26 19:04:03 +00:00
|
|
|
return static_cast<CastA>(a) > b;
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TInt, typename TUInt>
|
2017-01-13 19:45:18 +00:00
|
|
|
inline bool_if_gt_int_vs_uint<TInt, TUInt> equalsOpTmpl(TInt a, TUInt b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2021-01-26 19:04:03 +00:00
|
|
|
return static_cast<TInt>(a) == static_cast<TInt>(b);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TInt, typename TUInt>
|
2017-01-13 19:45:18 +00:00
|
|
|
inline bool_if_gt_int_vs_uint<TInt, TUInt> equalsOpTmpl(TUInt a, TInt b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2021-01-26 19:04:03 +00:00
|
|
|
return static_cast<TInt>(a) == static_cast<TInt>(b);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-11 13:38:36 +00:00
|
|
|
// Case 3a. Comparison via conversion to double.
|
2016-11-21 13:21:35 +00:00
|
|
|
template <typename TAInt, typename TAFloat>
|
2019-11-02 05:55:06 +00:00
|
|
|
using bool_if_double_can_be_used
|
2020-08-19 11:52:17 +00:00
|
|
|
= std::enable_if_t<is_integer_v<TAInt> && (sizeof(TAInt) <= 4) && std::is_floating_point_v<TAFloat>, bool>;
|
2016-11-21 13:21:35 +00:00
|
|
|
|
|
|
|
template <typename TAInt, typename TAFloat>
|
2017-01-13 19:45:18 +00:00
|
|
|
inline bool_if_double_can_be_used<TAInt, TAFloat> greaterOpTmpl(TAInt a, TAFloat b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return static_cast<double>(a) > static_cast<double>(b);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TAInt, typename TAFloat>
|
2017-01-13 19:45:18 +00:00
|
|
|
inline bool_if_double_can_be_used<TAInt, TAFloat> greaterOpTmpl(TAFloat a, TAInt b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return static_cast<double>(a) > static_cast<double>(b);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TAInt, typename TAFloat>
|
2017-01-13 19:45:18 +00:00
|
|
|
inline bool_if_double_can_be_used<TAInt, TAFloat> equalsOpTmpl(TAInt a, TAFloat b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return static_cast<double>(a) == static_cast<double>(b);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TAInt, typename TAFloat>
|
2017-01-13 19:45:18 +00:00
|
|
|
inline bool_if_double_can_be_used<TAInt, TAFloat> equalsOpTmpl(TAFloat a, TAInt b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return static_cast<double>(a) == static_cast<double>(b);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 11:52:17 +00:00
|
|
|
// Big integers vs Float (not equal in any case for now, until big floats are introduced?)
|
|
|
|
template <typename TABigInt, typename TAFloat>
|
|
|
|
constexpr bool if_big_int_vs_float = (is_big_int_v<TABigInt> && std::is_floating_point_v<TAFloat>);
|
|
|
|
|
|
|
|
template <typename TABigInt, typename TAFloat>
|
|
|
|
using bool_if_big_int_vs_float = std::enable_if_t<if_big_int_vs_float<TABigInt, TAFloat>, bool>;
|
|
|
|
|
|
|
|
template <typename TABigInt, typename TAFloat>
|
|
|
|
inline bool_if_big_int_vs_float<TABigInt, TAFloat> greaterOpTmpl(TAFloat, TABigInt)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TABigInt, typename TAFloat>
|
|
|
|
inline bool_if_big_int_vs_float<TABigInt, TAFloat> greaterOpTmpl(TABigInt, TAFloat)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TABigInt, typename TAFloat>
|
|
|
|
inline bool_if_big_int_vs_float<TABigInt, TAFloat> equalsOpTmpl(TAFloat, TABigInt)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TABigInt, typename TAFloat>
|
|
|
|
inline bool_if_big_int_vs_float<TABigInt, TAFloat> equalsOpTmpl(TABigInt, TAFloat)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-04 10:09:39 +00:00
|
|
|
/* Final implementations */
|
2017-01-11 13:38:36 +00:00
|
|
|
|
|
|
|
|
2016-11-21 13:21:35 +00:00
|
|
|
template <typename A, typename B>
|
2017-05-23 14:38:14 +00:00
|
|
|
inline bool_if_not_safe_conversion<A, B> greaterOp(A a, B b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return greaterOpTmpl(a, b);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 13:38:36 +00:00
|
|
|
template <typename A, typename B>
|
2017-05-23 14:38:14 +00:00
|
|
|
inline bool_if_safe_conversion<A, B> greaterOp(A a, B b)
|
2017-01-11 13:38:36 +00:00
|
|
|
{
|
2020-09-01 09:54:50 +00:00
|
|
|
using CastA1 = std::conditional_t<is_big_int_v<B> && std::is_same_v<A, UInt8>, uint8_t, A>;
|
|
|
|
using CastB1 = std::conditional_t<is_big_int_v<A> && std::is_same_v<B, UInt8>, uint8_t, B>;
|
|
|
|
|
|
|
|
using CastA = std::conditional_t<is_big_int_v<B> && std::is_same_v<A, DB::UInt128>, B, CastA1>;
|
|
|
|
using CastB = std::conditional_t<is_big_int_v<A> && std::is_same_v<B, DB::UInt128>, A, CastB1>;
|
|
|
|
|
|
|
|
if constexpr (is_big_int_v<A> || is_big_int_v<B>)
|
2021-01-26 19:04:03 +00:00
|
|
|
return static_cast<CastA>(a) > static_cast<CastB>(b);
|
2020-08-19 11:52:17 +00:00
|
|
|
else
|
|
|
|
return a > b;
|
2017-01-11 13:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Case 3b. 64-bit integers vs floats comparison.
|
2017-01-11 15:31:54 +00:00
|
|
|
// See hint at https://github.com/JuliaLang/julia/issues/257 (but it doesn't work properly for -2**63)
|
2017-01-11 13:38:36 +00:00
|
|
|
|
2016-11-21 13:21:35 +00:00
|
|
|
constexpr DB::Int64 MAX_INT64_WITH_EXACT_FLOAT64_REPR = 9007199254740992LL; // 2^53
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2016-11-21 13:21:35 +00:00
|
|
|
inline bool greaterOp<DB::Float64, DB::Int64>(DB::Float64 f, DB::Int64 i)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (-MAX_INT64_WITH_EXACT_FLOAT64_REPR <= i && i <= MAX_INT64_WITH_EXACT_FLOAT64_REPR)
|
|
|
|
return f > static_cast<DB::Float64>(i);
|
2016-11-21 13:21:35 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return (f >= static_cast<DB::Float64>(std::numeric_limits<DB::Int64>::max())) // rhs is 2**63 (not 2^63 - 1)
|
|
|
|
|| (f > static_cast<DB::Float64>(std::numeric_limits<DB::Int64>::min()) && static_cast<DB::Int64>(f) > i);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2016-11-21 13:21:35 +00:00
|
|
|
inline bool greaterOp<DB::Int64, DB::Float64>(DB::Int64 i, DB::Float64 f)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (-MAX_INT64_WITH_EXACT_FLOAT64_REPR <= i && i <= MAX_INT64_WITH_EXACT_FLOAT64_REPR)
|
|
|
|
return f < static_cast<DB::Float64>(i);
|
2016-11-21 13:21:35 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return (f < static_cast<DB::Float64>(std::numeric_limits<DB::Int64>::min()))
|
|
|
|
|| (f < static_cast<DB::Float64>(std::numeric_limits<DB::Int64>::max()) && i > static_cast<DB::Int64>(f));
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2016-11-21 13:21:35 +00:00
|
|
|
inline bool greaterOp<DB::Float64, DB::UInt64>(DB::Float64 f, DB::UInt64 u)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (u <= static_cast<DB::UInt64>(MAX_INT64_WITH_EXACT_FLOAT64_REPR))
|
|
|
|
return f > static_cast<DB::Float64>(u);
|
2016-11-21 13:21:35 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return (f >= static_cast<DB::Float64>(std::numeric_limits<DB::UInt64>::max()))
|
|
|
|
|| (f >= 0 && static_cast<DB::UInt64>(f) > u);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2016-11-21 13:21:35 +00:00
|
|
|
inline bool greaterOp<DB::UInt64, DB::Float64>(DB::UInt64 u, DB::Float64 f)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (u <= static_cast<DB::UInt64>(MAX_INT64_WITH_EXACT_FLOAT64_REPR))
|
|
|
|
return static_cast<DB::Float64>(u) > f;
|
2016-11-21 13:21:35 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return (f < 0)
|
|
|
|
|| (f < static_cast<DB::Float64>(std::numeric_limits<DB::UInt64>::max()) && u > static_cast<UInt64>(f));
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 13:38:36 +00:00
|
|
|
// Case 3b for float32
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2016-11-21 13:21:35 +00:00
|
|
|
inline bool greaterOp<DB::Float32, DB::Int64>(DB::Float32 f, DB::Int64 i)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return greaterOp(static_cast<DB::Float64>(f), i);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2016-11-21 13:21:35 +00:00
|
|
|
inline bool greaterOp<DB::Int64, DB::Float32>(DB::Int64 i, DB::Float32 f)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return greaterOp(i, static_cast<DB::Float64>(f));
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2016-11-21 13:21:35 +00:00
|
|
|
inline bool greaterOp<DB::Float32, DB::UInt64>(DB::Float32 f, DB::UInt64 u)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return greaterOp(static_cast<DB::Float64>(f), u);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2016-11-21 13:21:35 +00:00
|
|
|
inline bool greaterOp<DB::UInt64, DB::Float32>(DB::UInt64 u, DB::Float32 f)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return greaterOp(u, static_cast<DB::Float64>(f));
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2017-06-15 09:12:32 +00:00
|
|
|
inline bool greaterOp<DB::Float64, DB::UInt128>(DB::Float64 f, DB::UInt128 u)
|
|
|
|
{
|
2021-04-04 22:38:47 +00:00
|
|
|
/// TODO: This is wrong.
|
2017-07-05 12:40:41 +00:00
|
|
|
return u.low == 0 && greaterOp(f, u.high);
|
2017-06-15 09:12:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2017-06-15 09:12:32 +00:00
|
|
|
inline bool greaterOp<DB::UInt128, DB::Float64>(DB::UInt128 u, DB::Float64 f)
|
|
|
|
{
|
2021-04-04 22:38:47 +00:00
|
|
|
/// TODO: This is wrong.
|
2017-07-05 12:40:41 +00:00
|
|
|
return u.low != 0 || greaterOp(u.high, f);
|
2017-06-15 09:12:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2017-06-15 09:12:32 +00:00
|
|
|
inline bool greaterOp<DB::Float32, DB::UInt128>(DB::Float32 f, DB::UInt128 u)
|
|
|
|
{
|
|
|
|
return greaterOp(static_cast<DB::Float64>(f), u);
|
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2017-06-15 09:12:32 +00:00
|
|
|
inline bool greaterOp<DB::UInt128, DB::Float32>(DB::UInt128 u, DB::Float32 f)
|
|
|
|
{
|
|
|
|
return greaterOp(u, static_cast<DB::Float64>(f));
|
|
|
|
}
|
2016-11-21 13:21:35 +00:00
|
|
|
|
|
|
|
template <typename A, typename B>
|
2017-05-23 14:38:14 +00:00
|
|
|
inline bool_if_not_safe_conversion<A, B> equalsOp(A a, B b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return equalsOpTmpl(a, b);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 13:38:36 +00:00
|
|
|
template <typename A, typename B>
|
2017-05-23 14:38:14 +00:00
|
|
|
inline bool_if_safe_conversion<A, B> equalsOp(A a, B b)
|
2017-01-11 13:38:36 +00:00
|
|
|
{
|
2020-09-01 09:54:50 +00:00
|
|
|
using LargestType = std::conditional_t<(sizeof(A) > sizeof(B)) || ((sizeof(A) == sizeof(B)) && !std::is_same_v<A, DB::UInt128>), A, B>;
|
2020-08-19 11:52:17 +00:00
|
|
|
|
2021-01-26 19:04:03 +00:00
|
|
|
return static_cast<LargestType>(a) == static_cast<LargestType>(b);
|
2017-01-11 13:38:36 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2018-12-27 01:20:06 +00:00
|
|
|
inline bool NO_SANITIZE_UNDEFINED equalsOp<DB::Float64, DB::UInt64>(DB::Float64 f, DB::UInt64 u)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return static_cast<DB::UInt64>(f) == u && f == static_cast<DB::Float64>(u);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2018-12-27 01:20:06 +00:00
|
|
|
inline bool NO_SANITIZE_UNDEFINED equalsOp<DB::UInt64, DB::Float64>(DB::UInt64 u, DB::Float64 f)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return u == static_cast<DB::UInt64>(f) && static_cast<DB::Float64>(u) == f;
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2018-12-27 01:20:06 +00:00
|
|
|
inline bool NO_SANITIZE_UNDEFINED equalsOp<DB::Float64, DB::Int64>(DB::Float64 f, DB::Int64 u)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return static_cast<DB::Int64>(f) == u && f == static_cast<DB::Float64>(u);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2018-12-27 01:20:06 +00:00
|
|
|
inline bool NO_SANITIZE_UNDEFINED equalsOp<DB::Int64, DB::Float64>(DB::Int64 u, DB::Float64 f)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return u == static_cast<DB::Int64>(f) && static_cast<DB::Float64>(u) == f;
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2018-12-27 01:20:06 +00:00
|
|
|
inline bool NO_SANITIZE_UNDEFINED equalsOp<DB::Float32, DB::UInt64>(DB::Float32 f, DB::UInt64 u)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return static_cast<DB::UInt64>(f) == u && f == static_cast<DB::Float32>(u);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2018-12-27 01:20:06 +00:00
|
|
|
inline bool NO_SANITIZE_UNDEFINED equalsOp<DB::UInt64, DB::Float32>(DB::UInt64 u, DB::Float32 f)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return u == static_cast<DB::UInt64>(f) && static_cast<DB::Float32>(u) == f;
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2018-12-27 01:20:06 +00:00
|
|
|
inline bool NO_SANITIZE_UNDEFINED equalsOp<DB::Float32, DB::Int64>(DB::Float32 f, DB::Int64 u)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return static_cast<DB::Int64>(f) == u && f == static_cast<DB::Float32>(u);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2018-12-27 01:20:06 +00:00
|
|
|
inline bool NO_SANITIZE_UNDEFINED equalsOp<DB::Int64, DB::Float32>(DB::Int64 u, DB::Float32 f)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return u == static_cast<DB::Int64>(f) && static_cast<DB::Float32>(u) == f;
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2018-12-27 01:20:06 +00:00
|
|
|
inline bool NO_SANITIZE_UNDEFINED equalsOp<DB::UInt128, DB::Float64>(DB::UInt128 u, DB::Float64 f)
|
2017-06-15 09:12:32 +00:00
|
|
|
{
|
2021-04-04 22:38:47 +00:00
|
|
|
/// TODO: This is wrong.
|
2017-07-05 12:40:41 +00:00
|
|
|
return u.low == 0 && equalsOp(static_cast<UInt64>(u.high), f);
|
2017-06-15 09:12:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2017-06-15 09:12:32 +00:00
|
|
|
inline bool equalsOp<DB::UInt128, DB::Float32>(DB::UInt128 u, DB::Float32 f)
|
|
|
|
{
|
|
|
|
return equalsOp(u, static_cast<DB::Float64>(f));
|
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2017-06-15 09:12:32 +00:00
|
|
|
inline bool equalsOp<DB::Float64, DB::UInt128>(DB::Float64 f, DB::UInt128 u)
|
|
|
|
{
|
2017-07-05 12:40:41 +00:00
|
|
|
return equalsOp(u, f);
|
2017-06-15 09:12:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <>
|
2017-06-15 09:12:32 +00:00
|
|
|
inline bool equalsOp<DB::Float32, DB::UInt128>(DB::Float32 f, DB::UInt128 u)
|
|
|
|
{
|
|
|
|
return equalsOp(static_cast<DB::Float64>(f), u);
|
|
|
|
}
|
|
|
|
|
2018-12-27 01:20:06 +00:00
|
|
|
inline bool NO_SANITIZE_UNDEFINED greaterOp(DB::Int128 i, DB::Float64 f)
|
2018-08-15 16:43:40 +00:00
|
|
|
{
|
2020-09-22 11:35:46 +00:00
|
|
|
static constexpr Int128 min_int128 = minInt128();
|
|
|
|
static constexpr Int128 max_int128 = maxInt128();
|
2018-08-15 16:43:40 +00:00
|
|
|
|
|
|
|
if (-MAX_INT64_WITH_EXACT_FLOAT64_REPR <= i && i <= MAX_INT64_WITH_EXACT_FLOAT64_REPR)
|
|
|
|
return static_cast<DB::Float64>(i) > f;
|
|
|
|
|
|
|
|
return (f < static_cast<DB::Float64>(min_int128))
|
|
|
|
|| (f < static_cast<DB::Float64>(max_int128) && i > static_cast<DB::Int128>(f));
|
|
|
|
}
|
|
|
|
|
2018-12-27 01:20:06 +00:00
|
|
|
inline bool NO_SANITIZE_UNDEFINED greaterOp(DB::Float64 f, DB::Int128 i)
|
2018-08-15 16:43:40 +00:00
|
|
|
{
|
2020-09-22 11:35:46 +00:00
|
|
|
static constexpr Int128 min_int128 = minInt128();
|
|
|
|
static constexpr Int128 max_int128 = maxInt128();
|
2018-08-15 16:43:40 +00:00
|
|
|
|
|
|
|
if (-MAX_INT64_WITH_EXACT_FLOAT64_REPR <= i && i <= MAX_INT64_WITH_EXACT_FLOAT64_REPR)
|
|
|
|
return f > static_cast<DB::Float64>(i);
|
|
|
|
|
|
|
|
return (f >= static_cast<DB::Float64>(max_int128))
|
|
|
|
|| (f > static_cast<DB::Float64>(min_int128) && static_cast<DB::Int128>(f) > i);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool greaterOp(DB::Int128 i, DB::Float32 f) { return greaterOp(i, static_cast<DB::Float64>(f)); }
|
|
|
|
inline bool greaterOp(DB::Float32 f, DB::Int128 i) { return greaterOp(static_cast<DB::Float64>(f), i); }
|
2018-07-25 19:38:21 +00:00
|
|
|
|
2018-12-27 01:20:06 +00:00
|
|
|
inline bool NO_SANITIZE_UNDEFINED equalsOp(DB::Int128 i, DB::Float64 f) { return i == static_cast<DB::Int128>(f) && static_cast<DB::Float64>(i) == f; }
|
|
|
|
inline bool NO_SANITIZE_UNDEFINED equalsOp(DB::Int128 i, DB::Float32 f) { return i == static_cast<DB::Int128>(f) && static_cast<DB::Float32>(i) == f; }
|
2018-07-25 19:38:21 +00:00
|
|
|
inline bool equalsOp(DB::Float64 f, DB::Int128 i) { return equalsOp(i, f); }
|
|
|
|
inline bool equalsOp(DB::Float32 f, DB::Int128 i) { return equalsOp(i, f); }
|
2016-11-21 13:21:35 +00:00
|
|
|
|
|
|
|
template <typename A, typename B>
|
2017-05-23 14:38:14 +00:00
|
|
|
inline bool_if_not_safe_conversion<A, B> notEqualsOp(A a, B b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return !equalsOp(a, b);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 13:38:36 +00:00
|
|
|
template <typename A, typename B>
|
2017-05-23 14:38:14 +00:00
|
|
|
inline bool_if_safe_conversion<A, B> notEqualsOp(A a, B b)
|
2017-01-11 13:38:36 +00:00
|
|
|
{
|
2020-09-01 09:54:50 +00:00
|
|
|
using CastA1 = std::conditional_t<is_big_int_v<B> && std::is_same_v<A, UInt8>, uint8_t, A>;
|
|
|
|
using CastB1 = std::conditional_t<is_big_int_v<A> && std::is_same_v<B, UInt8>, uint8_t, B>;
|
|
|
|
|
|
|
|
using CastA = std::conditional_t<is_big_int_v<B> && std::is_same_v<A, DB::UInt128>, B, CastA1>;
|
|
|
|
using CastB = std::conditional_t<is_big_int_v<A> && std::is_same_v<B, DB::UInt128>, A, CastB1>;
|
|
|
|
|
|
|
|
if constexpr (is_big_int_v<A> || is_big_int_v<B>)
|
2021-01-26 19:04:03 +00:00
|
|
|
return static_cast<CastA>(a) != static_cast<CastB>(b);
|
2020-08-19 11:52:17 +00:00
|
|
|
else
|
|
|
|
return a != b;
|
2017-01-11 13:38:36 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 13:21:35 +00:00
|
|
|
|
|
|
|
template <typename A, typename B>
|
2017-05-23 14:38:14 +00:00
|
|
|
inline bool_if_not_safe_conversion<A, B> lessOp(A a, B b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return greaterOp(b, a);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 13:38:36 +00:00
|
|
|
template <typename A, typename B>
|
2017-05-23 14:38:14 +00:00
|
|
|
inline bool_if_safe_conversion<A, B> lessOp(A a, B b)
|
2017-01-11 13:38:36 +00:00
|
|
|
{
|
2020-09-01 09:54:50 +00:00
|
|
|
using CastA1 = std::conditional_t<is_big_int_v<B> && std::is_same_v<A, UInt8>, uint8_t, A>;
|
|
|
|
using CastB1 = std::conditional_t<is_big_int_v<A> && std::is_same_v<B, UInt8>, uint8_t, B>;
|
|
|
|
|
|
|
|
using CastA = std::conditional_t<is_big_int_v<B> && std::is_same_v<A, DB::UInt128>, B, CastA1>;
|
|
|
|
using CastB = std::conditional_t<is_big_int_v<A> && std::is_same_v<B, DB::UInt128>, A, CastB1>;
|
|
|
|
|
|
|
|
if constexpr (is_big_int_v<A> || is_big_int_v<B>)
|
2021-01-26 19:04:03 +00:00
|
|
|
return static_cast<CastA>(a) < static_cast<CastB>(b);
|
2020-08-19 11:52:17 +00:00
|
|
|
else
|
|
|
|
return a < b;
|
2017-01-11 13:38:36 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 13:21:35 +00:00
|
|
|
|
|
|
|
template <typename A, typename B>
|
2017-05-23 14:38:14 +00:00
|
|
|
inline bool_if_not_safe_conversion<A, B> lessOrEqualsOp(A a, B b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2018-09-03 15:55:16 +00:00
|
|
|
if (isNaN(a) || isNaN(b))
|
|
|
|
return false;
|
2017-04-01 07:20:54 +00:00
|
|
|
return !greaterOp(a, b);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 13:38:36 +00:00
|
|
|
template <typename A, typename B>
|
2017-05-23 14:38:14 +00:00
|
|
|
inline bool_if_safe_conversion<A, B> lessOrEqualsOp(A a, B b)
|
2017-01-11 13:38:36 +00:00
|
|
|
{
|
2020-09-01 09:54:50 +00:00
|
|
|
using CastA1 = std::conditional_t<is_big_int_v<B> && std::is_same_v<A, UInt8>, uint8_t, A>;
|
|
|
|
using CastB1 = std::conditional_t<is_big_int_v<A> && std::is_same_v<B, UInt8>, uint8_t, B>;
|
|
|
|
|
|
|
|
using CastA = std::conditional_t<is_big_int_v<B> && std::is_same_v<A, DB::UInt128>, B, CastA1>;
|
|
|
|
using CastB = std::conditional_t<is_big_int_v<A> && std::is_same_v<B, DB::UInt128>, A, CastB1>;
|
|
|
|
|
|
|
|
if constexpr (is_big_int_v<A> || is_big_int_v<B>)
|
2021-01-26 19:04:03 +00:00
|
|
|
return static_cast<CastA>(a) <= static_cast<CastB>(b);
|
2020-08-19 11:52:17 +00:00
|
|
|
else
|
|
|
|
return a <= b;
|
2017-01-11 13:38:36 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 13:21:35 +00:00
|
|
|
|
|
|
|
template <typename A, typename B>
|
2017-05-23 14:38:14 +00:00
|
|
|
inline bool_if_not_safe_conversion<A, B> greaterOrEqualsOp(A a, B b)
|
2016-11-21 13:21:35 +00:00
|
|
|
{
|
2018-09-03 15:55:16 +00:00
|
|
|
if (isNaN(a) || isNaN(b))
|
|
|
|
return false;
|
2017-04-01 07:20:54 +00:00
|
|
|
return !greaterOp(b, a);
|
2016-11-21 13:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 13:38:36 +00:00
|
|
|
template <typename A, typename B>
|
2017-05-23 14:38:14 +00:00
|
|
|
inline bool_if_safe_conversion<A, B> greaterOrEqualsOp(A a, B b)
|
2017-01-11 13:38:36 +00:00
|
|
|
{
|
2020-09-01 09:54:50 +00:00
|
|
|
using CastA1 = std::conditional_t<is_big_int_v<B> && std::is_same_v<A, UInt8>, uint8_t, A>;
|
|
|
|
using CastB1 = std::conditional_t<is_big_int_v<A> && std::is_same_v<B, UInt8>, uint8_t, B>;
|
|
|
|
|
|
|
|
using CastA = std::conditional_t<is_big_int_v<B> && std::is_same_v<A, DB::UInt128>, B, CastA1>;
|
|
|
|
using CastB = std::conditional_t<is_big_int_v<A> && std::is_same_v<B, DB::UInt128>, A, CastB1>;
|
|
|
|
|
|
|
|
if constexpr (is_big_int_v<A> || is_big_int_v<B>)
|
2021-01-26 19:04:03 +00:00
|
|
|
return static_cast<CastA>(a) >= static_cast<CastB>(b);
|
2020-08-19 11:52:17 +00:00
|
|
|
else
|
|
|
|
return a >= b;
|
2017-01-11 13:38:36 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 16:10:40 +00:00
|
|
|
/// Converts numeric to an equal numeric of other type.
|
|
|
|
template <typename From, typename To>
|
|
|
|
inline bool NO_SANITIZE_UNDEFINED convertNumeric(From value, To & result)
|
|
|
|
{
|
2019-05-13 23:44:55 +00:00
|
|
|
/// 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;
|
|
|
|
}
|
|
|
|
|
2020-12-05 14:55:37 +00:00
|
|
|
if constexpr (std::is_floating_point_v<From> && std::is_floating_point_v<To>)
|
2020-12-05 13:18:56 +00:00
|
|
|
{
|
2020-12-02 10:09:13 +00:00
|
|
|
/// Note that NaNs doesn't compare equal to anything, but they are still in range of any Float type.
|
|
|
|
if (isNaN(value))
|
|
|
|
{
|
|
|
|
result = value;
|
|
|
|
return true;
|
|
|
|
}
|
2020-12-05 13:18:56 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-12-02 10:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (accurate::greaterOp(value, std::numeric_limits<To>::max())
|
2020-12-05 13:18:56 +00:00
|
|
|
|| accurate::greaterOp(std::numeric_limits<To>::lowest(), value))
|
2019-04-17 16:10:40 +00:00
|
|
|
{
|
2020-12-02 10:09:13 +00:00
|
|
|
return false;
|
2019-04-17 16:10:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 13:46:14 +00:00
|
|
|
result = static_cast<To>(value);
|
|
|
|
return equalsOp(value, result);
|
2019-04-17 16:10:40 +00:00
|
|
|
}
|
|
|
|
|
2018-09-02 01:12:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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); }
|
|
|
|
};
|
2016-11-21 13:21:35 +00:00
|
|
|
|
|
|
|
}
|