2018-11-26 16:20:40 +00:00
|
|
|
#include <type_traits>
|
2021-06-15 19:55:21 +00:00
|
|
|
#include <common/bit_cast.h>
|
2018-11-26 16:20:40 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionUnaryArithmetic.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-09-04 13:33:02 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
template <typename T>
|
2020-08-19 11:52:17 +00:00
|
|
|
inline std::enable_if_t<std::is_integral_v<T> && (sizeof(T) <= sizeof(UInt32)), T>
|
2018-11-26 16:20:40 +00:00
|
|
|
roundDownToPowerOfTwo(T x)
|
|
|
|
{
|
|
|
|
return x <= 0 ? 0 : (T(1) << (31 - __builtin_clz(x)));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-08-19 11:52:17 +00:00
|
|
|
inline std::enable_if_t<std::is_integral_v<T> && (sizeof(T) == sizeof(UInt64)), T>
|
2018-11-26 16:20:40 +00:00
|
|
|
roundDownToPowerOfTwo(T x)
|
|
|
|
{
|
|
|
|
return x <= 0 ? 0 : (T(1) << (63 - __builtin_clzll(x)));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline std::enable_if_t<std::is_same_v<T, Float32>, T>
|
|
|
|
roundDownToPowerOfTwo(T x)
|
|
|
|
{
|
2021-06-15 19:55:21 +00:00
|
|
|
return bit_cast<T>(bit_cast<UInt32>(x) & ~((1ULL << 23) - 1));
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline std::enable_if_t<std::is_same_v<T, Float64>, T>
|
|
|
|
roundDownToPowerOfTwo(T x)
|
|
|
|
{
|
2021-06-15 19:55:21 +00:00
|
|
|
return bit_cast<T>(bit_cast<UInt64>(x) & ~((1ULL << 52) - 1));
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 11:52:17 +00:00
|
|
|
template <typename T>
|
|
|
|
inline std::enable_if_t<is_big_int_v<T>, T>
|
2020-09-04 13:33:02 +00:00
|
|
|
roundDownToPowerOfTwo(T)
|
2020-08-19 11:52:17 +00:00
|
|
|
{
|
2020-09-04 13:33:02 +00:00
|
|
|
throw Exception("roundToExp2() for big integers is not implemented", ErrorCodes::NOT_IMPLEMENTED);
|
2020-08-19 11:52:17 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
/** For integer data types:
|
|
|
|
* - if number is greater than zero, round it down to nearest power of two (example: roundToExp2(100) = 64, roundToExp2(64) = 64);
|
|
|
|
* - otherwise, return 0.
|
|
|
|
*
|
|
|
|
* For floating point data types: zero out mantissa, but leave exponent.
|
|
|
|
* - if number is greater than zero, round it down to nearest power of two (example: roundToExp2(3) = 2);
|
|
|
|
* - negative powers are also used (example: roundToExp2(0.7) = 0.5);
|
|
|
|
* - if number is zero, return zero;
|
|
|
|
* - if number is less than zero, the result is symmetrical: roundToExp2(x) = -roundToExp2(-x). (example: roundToExp2(-0.3) = -0.25);
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct RoundToExp2Impl
|
|
|
|
{
|
|
|
|
using ResultType = T;
|
2020-02-14 08:17:32 +00:00
|
|
|
static constexpr const bool allow_fixed_string = false;
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
static inline T apply(T x)
|
|
|
|
{
|
|
|
|
return roundDownToPowerOfTwo<T>(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
static constexpr bool compilable = false;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NameRoundToExp2 { static constexpr auto name = "roundToExp2"; };
|
|
|
|
using FunctionRoundToExp2 = FunctionUnaryArithmetic<RoundToExp2Impl, NameRoundToExp2, false>;
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
template <> struct FunctionUnaryArithmeticMonotonicity<NameRoundToExp2> : PositiveMonotonicity {};
|
|
|
|
|
|
|
|
void registerFunctionRoundToExp2(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionRoundToExp2>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|