2015-01-10 02:30:03 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-07-24 12:58:01 +00:00
|
|
|
#include <tuple>
|
2019-05-28 21:17:48 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
2017-06-15 09:12:32 +00:00
|
|
|
#include <city.h>
|
|
|
|
|
2017-11-09 23:37:57 +00:00
|
|
|
#include <Core/Types.h>
|
2014-04-28 01:48:24 +00:00
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
#ifdef __SSE4_2__
|
2017-05-09 19:07:35 +00:00
|
|
|
#include <nmmintrin.h>
|
|
|
|
#endif
|
|
|
|
|
2014-04-28 01:48:24 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-06-15 09:12:32 +00:00
|
|
|
/// For aggregation by SipHash, UUID type or concatenation of several fields.
|
2014-04-28 01:48:24 +00:00
|
|
|
struct UInt128
|
|
|
|
{
|
2017-07-04 16:10:36 +00:00
|
|
|
/// Suppress gcc7 warnings: 'prev_key.DB::UInt128::low' may be used uninitialized in this function
|
2017-03-31 05:09:11 +00:00
|
|
|
#if !__clang__
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
|
|
|
#endif
|
|
|
|
|
2017-07-16 03:05:40 +00:00
|
|
|
/// This naming assumes little endian.
|
2017-07-04 16:10:36 +00:00
|
|
|
UInt64 low;
|
|
|
|
UInt64 high;
|
2014-04-28 01:48:24 +00:00
|
|
|
|
2017-06-15 09:12:32 +00:00
|
|
|
UInt128() = default;
|
2019-08-03 11:02:40 +00:00
|
|
|
explicit UInt128(const UInt64 low_, const UInt64 high_) : low(low_), high(high_) {}
|
2018-08-15 15:29:57 +00:00
|
|
|
explicit UInt128(const UInt64 rhs) : low(rhs), high() {}
|
|
|
|
|
2017-07-16 03:05:40 +00:00
|
|
|
auto tuple() const { return std::tie(high, low); }
|
2017-07-05 10:03:24 +00:00
|
|
|
|
2019-05-28 21:17:48 +00:00
|
|
|
String toHexString() const
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
os << std::setw(16) << std::setfill('0') << std::hex << high << low;
|
|
|
|
return String(os.str());
|
|
|
|
}
|
|
|
|
|
2017-07-16 03:05:40 +00:00
|
|
|
bool inline operator== (const UInt128 rhs) const { return tuple() == rhs.tuple(); }
|
|
|
|
bool inline operator!= (const UInt128 rhs) const { return tuple() != rhs.tuple(); }
|
|
|
|
bool inline operator< (const UInt128 rhs) const { return tuple() < rhs.tuple(); }
|
|
|
|
bool inline operator<= (const UInt128 rhs) const { return tuple() <= rhs.tuple(); }
|
|
|
|
bool inline operator> (const UInt128 rhs) const { return tuple() > rhs.tuple(); }
|
|
|
|
bool inline operator>= (const UInt128 rhs) const { return tuple() >= rhs.tuple(); }
|
2017-06-15 09:12:32 +00:00
|
|
|
|
2017-07-16 03:05:40 +00:00
|
|
|
template <typename T> bool inline operator== (const T rhs) const { return *this == UInt128(rhs); }
|
|
|
|
template <typename T> bool inline operator!= (const T rhs) const { return *this != UInt128(rhs); }
|
|
|
|
template <typename T> bool inline operator>= (const T rhs) const { return *this >= UInt128(rhs); }
|
|
|
|
template <typename T> bool inline operator> (const T rhs) const { return *this > UInt128(rhs); }
|
|
|
|
template <typename T> bool inline operator<= (const T rhs) const { return *this <= UInt128(rhs); }
|
|
|
|
template <typename T> bool inline operator< (const T rhs) const { return *this < UInt128(rhs); }
|
2017-06-30 20:30:13 +00:00
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <typename T> explicit operator T() const { return static_cast<T>(low); }
|
2014-04-28 01:48:24 +00:00
|
|
|
|
2017-03-31 05:09:11 +00:00
|
|
|
#if !__clang__
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2017-07-24 07:48:26 +00:00
|
|
|
UInt128 & operator= (const UInt64 rhs) { low = rhs; high = 0; return *this; }
|
2014-04-28 01:48:24 +00:00
|
|
|
};
|
|
|
|
|
2017-07-16 03:05:40 +00:00
|
|
|
template <typename T> bool inline operator== (T a, const UInt128 b) { return UInt128(a) == b; }
|
|
|
|
template <typename T> bool inline operator!= (T a, const UInt128 b) { return UInt128(a) != b; }
|
|
|
|
template <typename T> bool inline operator>= (T a, const UInt128 b) { return UInt128(a) >= b; }
|
|
|
|
template <typename T> bool inline operator> (T a, const UInt128 b) { return UInt128(a) > b; }
|
|
|
|
template <typename T> bool inline operator<= (T a, const UInt128 b) { return UInt128(a) <= b; }
|
|
|
|
template <typename T> bool inline operator< (T a, const UInt128 b) { return UInt128(a) < b; }
|
2017-06-15 09:12:32 +00:00
|
|
|
|
2019-05-30 08:23:20 +00:00
|
|
|
template <> inline constexpr bool IsNumber<UInt128> = true;
|
2017-07-16 03:05:40 +00:00
|
|
|
template <> struct TypeName<UInt128> { static const char * get() { return "UInt128"; } };
|
2018-08-20 15:17:55 +00:00
|
|
|
template <> struct TypeId<UInt128> { static constexpr const TypeIndex value = TypeIndex::UInt128; };
|
2017-07-05 12:40:41 +00:00
|
|
|
|
2014-04-28 01:48:24 +00:00
|
|
|
struct UInt128Hash
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t operator()(UInt128 x) const
|
|
|
|
{
|
2017-07-04 16:10:36 +00:00
|
|
|
return CityHash_v1_0_2::Hash128to64({x.low, x.high});
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2014-04-28 01:48:24 +00:00
|
|
|
};
|
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
#ifdef __SSE4_2__
|
2016-01-13 20:21:56 +00:00
|
|
|
|
2014-12-26 03:41:31 +00:00
|
|
|
struct UInt128HashCRC32
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t operator()(UInt128 x) const
|
|
|
|
{
|
|
|
|
UInt64 crc = -1ULL;
|
2017-07-04 16:10:36 +00:00
|
|
|
crc = _mm_crc32_u64(crc, x.low);
|
|
|
|
crc = _mm_crc32_u64(crc, x.high);
|
2017-04-01 07:20:54 +00:00
|
|
|
return crc;
|
|
|
|
}
|
2014-12-26 03:41:31 +00:00
|
|
|
};
|
|
|
|
|
2016-01-13 20:21:56 +00:00
|
|
|
#else
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// On other platforms we do not use CRC32. NOTE This can be confusing.
|
2016-01-13 20:21:56 +00:00
|
|
|
struct UInt128HashCRC32 : public UInt128Hash {};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-04-28 01:48:24 +00:00
|
|
|
struct UInt128TrivialHash
|
|
|
|
{
|
2017-07-04 16:10:36 +00:00
|
|
|
size_t operator()(UInt128 x) const { return x.low; }
|
2014-04-28 01:48:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/** Used for aggregation, for putting a large number of constant-length keys in a hash table.
|
2015-02-22 05:53:16 +00:00
|
|
|
*/
|
|
|
|
struct UInt256
|
|
|
|
{
|
2017-03-31 05:09:11 +00:00
|
|
|
|
|
|
|
/// Suppress gcc7 warnings: 'prev_key.DB::UInt256::a' may be used uninitialized in this function
|
|
|
|
#if !__clang__
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
|
|
|
#endif
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt64 a;
|
|
|
|
UInt64 b;
|
|
|
|
UInt64 c;
|
|
|
|
UInt64 d;
|
2015-02-22 05:53:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool operator== (const UInt256 rhs) const
|
|
|
|
{
|
|
|
|
return a == rhs.a && b == rhs.b && c == rhs.c && d == rhs.d;
|
2015-02-22 15:30:31 +00:00
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/* So it's no better.
|
2017-04-01 07:20:54 +00:00
|
|
|
return 0xFFFF == _mm_movemask_epi8(_mm_and_si128(
|
|
|
|
_mm_cmpeq_epi8(
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(&a)),
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(&rhs.a))),
|
|
|
|
_mm_cmpeq_epi8(
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(&c)),
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(&rhs.c)))));*/
|
|
|
|
}
|
2015-02-22 05:53:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool operator!= (const UInt256 rhs) const { return !operator==(rhs); }
|
2015-02-22 05:53:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool operator== (const UInt64 rhs) const { return a == rhs && b == 0 && c == 0 && d == 0; }
|
|
|
|
bool operator!= (const UInt64 rhs) const { return !operator==(rhs); }
|
2015-02-22 05:53:16 +00:00
|
|
|
|
2017-03-31 05:09:11 +00:00
|
|
|
#if !__clang__
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt256 & operator= (const UInt64 rhs) { a = rhs; b = 0; c = 0; d = 0; return *this; }
|
2015-02-22 05:53:16 +00:00
|
|
|
};
|
|
|
|
|
2016-09-23 05:49:55 +00:00
|
|
|
struct UInt256Hash
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t operator()(UInt256 x) const
|
|
|
|
{
|
|
|
|
/// NOTE suboptimal
|
2017-06-21 08:35:38 +00:00
|
|
|
return CityHash_v1_0_2::Hash128to64({CityHash_v1_0_2::Hash128to64({x.a, x.b}), CityHash_v1_0_2::Hash128to64({x.c, x.d})});
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-09-23 05:49:55 +00:00
|
|
|
};
|
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
#ifdef __SSE4_2__
|
2016-01-13 20:21:56 +00:00
|
|
|
|
2015-02-22 05:53:16 +00:00
|
|
|
struct UInt256HashCRC32
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t operator()(UInt256 x) const
|
|
|
|
{
|
|
|
|
UInt64 crc = -1ULL;
|
2017-05-09 19:07:35 +00:00
|
|
|
crc = _mm_crc32_u64(crc, x.a);
|
|
|
|
crc = _mm_crc32_u64(crc, x.b);
|
|
|
|
crc = _mm_crc32_u64(crc, x.c);
|
|
|
|
crc = _mm_crc32_u64(crc, x.d);
|
2017-04-01 07:20:54 +00:00
|
|
|
return crc;
|
|
|
|
}
|
2015-02-22 05:53:16 +00:00
|
|
|
};
|
|
|
|
|
2016-01-13 20:21:56 +00:00
|
|
|
#else
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// We do not need to use CRC32 on other platforms. NOTE This can be confusing.
|
2017-12-20 05:34:41 +00:00
|
|
|
struct UInt256HashCRC32 : public UInt256Hash {};
|
2016-01-13 20:21:56 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-11-02 05:55:06 +00:00
|
|
|
}
|
2017-03-31 05:09:11 +00:00
|
|
|
|
2017-06-15 09:12:32 +00:00
|
|
|
template <> struct is_signed<DB::UInt128>
|
|
|
|
{
|
|
|
|
static constexpr bool value = false;
|
|
|
|
};
|
2017-03-31 05:09:11 +00:00
|
|
|
|
2017-06-15 09:12:32 +00:00
|
|
|
template <> struct is_unsigned<DB::UInt128>
|
|
|
|
{
|
|
|
|
static constexpr bool value = true;
|
|
|
|
};
|
2017-07-05 12:40:41 +00:00
|
|
|
|
|
|
|
template <> struct is_integral<DB::UInt128>
|
|
|
|
{
|
|
|
|
static constexpr bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Operator +, -, /, *, % aren't implemented so it's not an arithmetic type
|
|
|
|
template <> struct is_arithmetic<DB::UInt128>
|
|
|
|
{
|
|
|
|
static constexpr bool value = false;
|
|
|
|
};
|
2019-11-02 05:55:06 +00:00
|
|
|
|
|
|
|
/// Overload hash for type casting
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
template <> struct hash<DB::UInt128>
|
|
|
|
{
|
|
|
|
size_t operator()(const DB::UInt128 & u) const
|
|
|
|
{
|
|
|
|
return CityHash_v1_0_2::Hash128to64({u.low, u.high});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-28 01:48:24 +00:00
|
|
|
}
|