2011-08-09 15:57:33 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-01-13 18:15:12 +00:00
|
|
|
#include <type_traits>
|
2011-08-09 15:57:33 +00:00
|
|
|
|
2018-10-12 11:23:05 +00:00
|
|
|
#include <Core/Types.h>
|
|
|
|
|
2017-12-07 03:35:19 +00:00
|
|
|
|
2011-08-09 15:57:33 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-12-07 03:35:19 +00:00
|
|
|
/** Allows get the result type of the functions +, -, *, /, %, intDiv (integer division).
|
2017-05-28 14:32:59 +00:00
|
|
|
* The rules are different from those used in C++.
|
2011-08-09 15:57:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace NumberTraits
|
|
|
|
{
|
2011-08-21 03:41:37 +00:00
|
|
|
|
2016-05-03 23:19:14 +00:00
|
|
|
struct Error {};
|
|
|
|
|
2017-12-07 03:35:19 +00:00
|
|
|
constexpr size_t max(size_t x, size_t y)
|
2016-08-04 15:06:27 +00:00
|
|
|
{
|
2017-12-07 03:35:19 +00:00
|
|
|
return x > y ? x : y;
|
|
|
|
}
|
2016-08-04 15:06:27 +00:00
|
|
|
|
2017-12-07 03:35:19 +00:00
|
|
|
constexpr size_t min(size_t x, size_t y)
|
2016-08-04 15:06:27 +00:00
|
|
|
{
|
2017-12-07 03:35:19 +00:00
|
|
|
return x < y ? x : y;
|
|
|
|
}
|
2016-08-04 15:06:27 +00:00
|
|
|
|
2020-09-04 13:33:02 +00:00
|
|
|
/// @note There's no auto scale to larger big integer, only for integral ones.
|
2020-10-27 11:04:03 +00:00
|
|
|
/// It's cause of (U)Int64 backward compatibility and very big performance penalties.
|
2017-12-07 03:35:19 +00:00
|
|
|
constexpr size_t nextSize(size_t size)
|
2016-08-04 15:06:27 +00:00
|
|
|
{
|
2020-09-04 13:33:02 +00:00
|
|
|
if (size < 8)
|
|
|
|
return size * 2;
|
|
|
|
return size;
|
2020-08-19 11:52:17 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 03:35:19 +00:00
|
|
|
template <bool is_signed, bool is_floating, size_t size>
|
|
|
|
struct Construct
|
2016-08-04 15:06:27 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using Type = Error;
|
2016-08-04 15:06:27 +00:00
|
|
|
};
|
|
|
|
|
2017-12-07 03:35:19 +00:00
|
|
|
template <> struct Construct<false, false, 1> { using Type = UInt8; };
|
|
|
|
template <> struct Construct<false, false, 2> { using Type = UInt16; };
|
|
|
|
template <> struct Construct<false, false, 4> { using Type = UInt32; };
|
|
|
|
template <> struct Construct<false, false, 8> { using Type = UInt64; };
|
2021-05-03 16:12:28 +00:00
|
|
|
template <> struct Construct<false, false, 16> { using Type = UInt128; };
|
2020-09-01 09:54:50 +00:00
|
|
|
template <> struct Construct<false, false, 32> { using Type = UInt256; };
|
2018-11-26 00:56:50 +00:00
|
|
|
template <> struct Construct<false, true, 1> { using Type = Float32; };
|
|
|
|
template <> struct Construct<false, true, 2> { using Type = Float32; };
|
|
|
|
template <> struct Construct<false, true, 4> { using Type = Float32; };
|
|
|
|
template <> struct Construct<false, true, 8> { using Type = Float64; };
|
|
|
|
template <> struct Construct<true, false, 1> { using Type = Int8; };
|
|
|
|
template <> struct Construct<true, false, 2> { using Type = Int16; };
|
|
|
|
template <> struct Construct<true, false, 4> { using Type = Int32; };
|
|
|
|
template <> struct Construct<true, false, 8> { using Type = Int64; };
|
2020-08-19 11:52:17 +00:00
|
|
|
template <> struct Construct<true, false, 16> { using Type = Int128; };
|
2020-09-04 13:33:02 +00:00
|
|
|
template <> struct Construct<true, false, 32> { using Type = Int256; };
|
2018-11-26 00:56:50 +00:00
|
|
|
template <> struct Construct<true, true, 1> { using Type = Float32; };
|
|
|
|
template <> struct Construct<true, true, 2> { using Type = Float32; };
|
|
|
|
template <> struct Construct<true, true, 4> { using Type = Float32; };
|
|
|
|
template <> struct Construct<true, true, 8> { using Type = Float64; };
|
2016-05-03 23:19:14 +00:00
|
|
|
|
2016-08-04 15:06:27 +00:00
|
|
|
|
2017-05-28 14:32:59 +00:00
|
|
|
/** The result of addition or multiplication is calculated according to the following rules:
|
|
|
|
* - if one of the arguments is floating-point, the result is a floating point, otherwise - the whole;
|
|
|
|
* - if one of the arguments is signed, the result is signed, otherwise it is unsigned;
|
|
|
|
* - the result contains more bits (not only meaningful) than the maximum in the arguments
|
|
|
|
* (for example, UInt8 + Int32 = Int64).
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2016-05-03 23:19:14 +00:00
|
|
|
template <typename A, typename B> struct ResultOfAdditionMultiplication
|
|
|
|
{
|
2017-06-10 09:04:31 +00:00
|
|
|
using Type = typename Construct<
|
2019-11-02 05:55:06 +00:00
|
|
|
is_signed_v<A> || is_signed_v<B>,
|
2017-12-07 03:35:19 +00:00
|
|
|
std::is_floating_point_v<A> || std::is_floating_point_v<B>,
|
2020-09-04 13:33:02 +00:00
|
|
|
nextSize(max(sizeof(A), sizeof(B)))>::Type;
|
2016-05-03 23:19:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename A, typename B> struct ResultOfSubtraction
|
|
|
|
{
|
2017-06-10 09:04:31 +00:00
|
|
|
using Type = typename Construct<
|
2017-12-07 03:35:19 +00:00
|
|
|
true,
|
|
|
|
std::is_floating_point_v<A> || std::is_floating_point_v<B>,
|
2020-09-04 13:33:02 +00:00
|
|
|
nextSize(max(sizeof(A), sizeof(B)))>::Type;
|
2016-05-03 23:19:14 +00:00
|
|
|
};
|
|
|
|
|
2017-05-28 14:32:59 +00:00
|
|
|
/** When dividing, you always get a floating-point number.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2016-05-03 23:19:14 +00:00
|
|
|
template <typename A, typename B> struct ResultOfFloatingPointDivision
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using Type = Float64;
|
2016-05-03 23:19:14 +00:00
|
|
|
};
|
|
|
|
|
2017-05-28 14:32:59 +00:00
|
|
|
/** For integer division, we get a number with the same number of bits as in divisible.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2016-05-03 23:19:14 +00:00
|
|
|
template <typename A, typename B> struct ResultOfIntegerDivision
|
|
|
|
{
|
2017-06-10 09:04:31 +00:00
|
|
|
using Type = typename Construct<
|
2019-11-02 05:55:06 +00:00
|
|
|
is_signed_v<A> || is_signed_v<B>,
|
2017-12-07 03:35:19 +00:00
|
|
|
false,
|
|
|
|
sizeof(A)>::Type;
|
2016-05-03 23:19:14 +00:00
|
|
|
};
|
|
|
|
|
2021-02-04 11:56:04 +00:00
|
|
|
/** Division with remainder you get a number with the same number of bits as in divisor,
|
|
|
|
* or larger in case of signed type.
|
|
|
|
*/
|
2016-05-03 23:19:14 +00:00
|
|
|
template <typename A, typename B> struct ResultOfModulo
|
|
|
|
{
|
2021-02-04 11:56:04 +00:00
|
|
|
static constexpr bool result_is_signed = is_signed_v<A>;
|
2021-02-04 16:36:31 +00:00
|
|
|
/// If modulo of division can yield negative number, we need larger type to accommodate it.
|
2021-02-04 11:56:04 +00:00
|
|
|
/// Example: toInt32(-199) % toUInt8(200) will return -199 that does not fit in Int8, only in Int16.
|
|
|
|
static constexpr size_t size_of_result = result_is_signed ? nextSize(sizeof(B)) : sizeof(B);
|
|
|
|
using Type0 = typename Construct<result_is_signed, false, size_of_result>::Type;
|
2020-08-19 11:52:17 +00:00
|
|
|
using Type = std::conditional_t<std::is_floating_point_v<A> || std::is_floating_point_v<B>, Float64, Type0>;
|
2016-05-03 23:19:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename A> struct ResultOfNegate
|
|
|
|
{
|
2017-06-10 09:04:31 +00:00
|
|
|
using Type = typename Construct<
|
2017-12-07 03:35:19 +00:00
|
|
|
true,
|
|
|
|
std::is_floating_point_v<A>,
|
2020-09-04 13:33:02 +00:00
|
|
|
is_signed_v<A> ? sizeof(A) : nextSize(sizeof(A))>::Type;
|
2016-05-03 23:19:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename A> struct ResultOfAbs
|
|
|
|
{
|
2017-06-10 09:04:31 +00:00
|
|
|
using Type = typename Construct<
|
2017-12-07 03:35:19 +00:00
|
|
|
false,
|
|
|
|
std::is_floating_point_v<A>,
|
|
|
|
sizeof(A)>::Type;
|
2016-05-03 23:19:14 +00:00
|
|
|
};
|
|
|
|
|
2017-05-28 14:32:59 +00:00
|
|
|
/** For bitwise operations, an integer is obtained with number of bits is equal to the maximum of the arguments.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2016-05-03 23:19:14 +00:00
|
|
|
template <typename A, typename B> struct ResultOfBit
|
|
|
|
{
|
2017-06-10 09:04:31 +00:00
|
|
|
using Type = typename Construct<
|
2019-11-02 05:55:06 +00:00
|
|
|
is_signed_v<A> || is_signed_v<B>,
|
2017-12-07 03:35:19 +00:00
|
|
|
false,
|
|
|
|
std::is_floating_point_v<A> || std::is_floating_point_v<B> ? 8 : max(sizeof(A), sizeof(B))>::Type;
|
2016-05-03 23:19:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename A> struct ResultOfBitNot
|
|
|
|
{
|
2017-06-10 09:04:31 +00:00
|
|
|
using Type = typename Construct<
|
2019-11-02 05:55:06 +00:00
|
|
|
is_signed_v<A>,
|
2017-12-07 03:35:19 +00:00
|
|
|
false,
|
|
|
|
sizeof(A)>::Type;
|
2016-05-03 23:19:14 +00:00
|
|
|
};
|
|
|
|
|
2017-01-13 18:15:12 +00:00
|
|
|
|
2017-05-28 14:32:59 +00:00
|
|
|
/** Type casting for `if` function:
|
2017-12-07 03:35:19 +00:00
|
|
|
* UInt<x>, UInt<y> -> UInt<max(x,y)>
|
|
|
|
* Int<x>, Int<y> -> Int<max(x,y)>
|
|
|
|
* Float<x>, Float<y> -> Float<max(x, y)>
|
|
|
|
* UInt<x>, Int<y> -> Int<max(x*2, y)>
|
|
|
|
* Float<x>, [U]Int<y> -> Float<max(x, y*2)>
|
2018-10-10 18:37:54 +00:00
|
|
|
* Decimal<x>, Decimal<y> -> Decimal<max(x,y)>
|
2018-10-12 11:23:05 +00:00
|
|
|
* UUID, UUID -> UUID
|
2017-12-07 03:35:19 +00:00
|
|
|
* UInt64 , Int<x> -> Error
|
|
|
|
* Float<x>, [U]Int64 -> Error
|
|
|
|
*/
|
2016-05-03 23:19:14 +00:00
|
|
|
template <typename A, typename B>
|
|
|
|
struct ResultOfIf
|
|
|
|
{
|
2017-12-07 03:35:19 +00:00
|
|
|
static constexpr bool has_float = std::is_floating_point_v<A> || std::is_floating_point_v<B>;
|
2020-08-19 11:52:17 +00:00
|
|
|
static constexpr bool has_integer = is_integer_v<A> || is_integer_v<B>;
|
2019-11-02 05:55:06 +00:00
|
|
|
static constexpr bool has_signed = is_signed_v<A> || is_signed_v<B>;
|
|
|
|
static constexpr bool has_unsigned = !is_signed_v<A> || !is_signed_v<B>;
|
2020-08-19 11:52:17 +00:00
|
|
|
static constexpr bool has_big_int = is_big_int_v<A> || is_big_int_v<B>;
|
2017-12-07 03:35:19 +00:00
|
|
|
|
2019-11-02 05:55:06 +00:00
|
|
|
static constexpr size_t max_size_of_unsigned_integer = max(is_signed_v<A> ? 0 : sizeof(A), is_signed_v<B> ? 0 : sizeof(B));
|
|
|
|
static constexpr size_t max_size_of_signed_integer = max(is_signed_v<A> ? sizeof(A) : 0, is_signed_v<B> ? sizeof(B) : 0);
|
2020-08-19 11:52:17 +00:00
|
|
|
static constexpr size_t max_size_of_integer = max(is_integer_v<A> ? sizeof(A) : 0, is_integer_v<B> ? sizeof(B) : 0);
|
2017-12-07 03:35:19 +00:00
|
|
|
static constexpr size_t max_size_of_float = max(std::is_floating_point_v<A> ? sizeof(A) : 0, std::is_floating_point_v<B> ? sizeof(B) : 0);
|
|
|
|
|
2018-10-10 18:37:54 +00:00
|
|
|
using ConstructedType = typename Construct<has_signed, has_float,
|
2017-12-07 03:35:19 +00:00
|
|
|
((has_float && has_integer && max_size_of_integer >= max_size_of_float)
|
|
|
|
|| (has_signed && has_unsigned && max_size_of_unsigned_integer >= max_size_of_signed_integer))
|
|
|
|
? max(sizeof(A), sizeof(B)) * 2
|
|
|
|
: max(sizeof(A), sizeof(B))>::Type;
|
2018-10-10 18:37:54 +00:00
|
|
|
|
2021-05-06 15:45:58 +00:00
|
|
|
using Type =
|
|
|
|
std::conditional_t<std::is_same_v<A, B>, A,
|
|
|
|
std::conditional_t<IsDecimalNumber<A> && IsDecimalNumber<B>,
|
|
|
|
std::conditional_t<(sizeof(A) > sizeof(B)), A, B>,
|
|
|
|
std::conditional_t<!IsDecimalNumber<A> && !IsDecimalNumber<B>,
|
|
|
|
ConstructedType, Error>>>;
|
2016-05-03 23:19:14 +00:00
|
|
|
};
|
|
|
|
|
2017-05-28 14:32:59 +00:00
|
|
|
/** Before applying operator `%` and bitwise operations, operands are casted to whole numbers. */
|
2016-05-03 23:19:14 +00:00
|
|
|
template <typename A> struct ToInteger
|
|
|
|
{
|
2017-06-10 09:04:31 +00:00
|
|
|
using Type = typename Construct<
|
2019-11-02 05:55:06 +00:00
|
|
|
is_signed_v<A>,
|
2017-12-07 03:35:19 +00:00
|
|
|
false,
|
|
|
|
std::is_floating_point_v<A> ? 8 : sizeof(A)>::Type;
|
2016-05-03 23:19:14 +00:00
|
|
|
};
|
|
|
|
|
2017-01-13 18:15:12 +00:00
|
|
|
|
|
|
|
// CLICKHOUSE-29. The same depth, different signs
|
2019-01-22 19:56:53 +00:00
|
|
|
// NOTE: This case is applied for 64-bit integers only (for backward compatibility), but could be used for any-bit integers
|
2017-01-13 18:15:12 +00:00
|
|
|
template <typename A, typename B>
|
2017-12-25 04:01:46 +00:00
|
|
|
constexpr bool LeastGreatestSpecialCase =
|
2020-08-19 11:52:17 +00:00
|
|
|
std::is_integral_v<A> && std::is_integral_v<B>
|
2017-06-10 09:04:31 +00:00
|
|
|
&& (8 == sizeof(A) && sizeof(A) == sizeof(B))
|
2019-11-02 05:55:06 +00:00
|
|
|
&& (is_signed_v<A> ^ is_signed_v<B>);
|
2017-01-13 18:15:12 +00:00
|
|
|
|
|
|
|
template <typename A, typename B>
|
2017-12-25 04:01:46 +00:00
|
|
|
using ResultOfLeast = std::conditional_t<LeastGreatestSpecialCase<A, B>,
|
2017-12-07 03:35:19 +00:00
|
|
|
typename Construct<true, false, sizeof(A)>::Type,
|
2017-06-10 09:04:31 +00:00
|
|
|
typename ResultOfIf<A, B>::Type>;
|
2017-01-13 18:15:12 +00:00
|
|
|
|
|
|
|
template <typename A, typename B>
|
2017-12-25 04:01:46 +00:00
|
|
|
using ResultOfGreatest = std::conditional_t<LeastGreatestSpecialCase<A, B>,
|
2017-12-07 03:35:19 +00:00
|
|
|
typename Construct<false, false, sizeof(A)>::Type,
|
2017-06-10 09:04:31 +00:00
|
|
|
typename ResultOfIf<A, B>::Type>;
|
2017-01-13 18:15:12 +00:00
|
|
|
|
2011-08-09 15:57:33 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 11:52:17 +00:00
|
|
|
template <typename T>
|
|
|
|
static inline auto littleBits(const T & x)
|
|
|
|
{
|
2021-01-26 19:04:03 +00:00
|
|
|
return static_cast<UInt8>(x);
|
2020-08-19 11:52:17 +00:00
|
|
|
}
|
|
|
|
|
2011-08-09 15:57:33 +00:00
|
|
|
}
|