2019-02-05 14:50:25 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionUnaryArithmetic.h>
|
|
|
|
#include <DataTypes/NumberTraits.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2019-08-15 18:46:16 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2019-08-15 18:46:16 +00:00
|
|
|
extern const int BAD_CAST;
|
|
|
|
}
|
2019-02-05 14:50:25 +00:00
|
|
|
|
2020-04-01 23:51:21 +00:00
|
|
|
/// Working with UInt8: last bit = can be true, previous = can be false (Like dbms/Storages/MergeTree/BoolMask.h).
|
2019-08-04 13:03:38 +00:00
|
|
|
/// This function provides "NOT" operation for BoolMasks by swapping last two bits ("can be true" <-> "can be false").
|
|
|
|
template <typename A>
|
|
|
|
struct BitSwapLastTwoImpl
|
2019-02-05 14:50:25 +00:00
|
|
|
{
|
2019-08-04 13:03:38 +00:00
|
|
|
using ResultType = UInt8;
|
2020-02-14 08:17:32 +00:00
|
|
|
static constexpr const bool allow_fixed_string = false;
|
2019-08-04 13:03:38 +00:00
|
|
|
|
|
|
|
static inline ResultType NO_SANITIZE_UNDEFINED apply(A a)
|
|
|
|
{
|
2019-08-15 18:46:16 +00:00
|
|
|
if constexpr (!std::is_same_v<A, ResultType>)
|
2019-08-15 18:48:48 +00:00
|
|
|
throw DB::Exception("It's a bug! Only UInt8 type is supported by __bitSwapLastTwo.", ErrorCodes::BAD_CAST);
|
2019-08-04 13:03:38 +00:00
|
|
|
return static_cast<ResultType>(
|
|
|
|
((static_cast<ResultType>(a) & 1) << 1) | ((static_cast<ResultType>(a) >> 1) & 1));
|
|
|
|
}
|
2019-02-05 14:50:25 +00:00
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
static constexpr bool compilable = true;
|
|
|
|
|
|
|
|
static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * arg, bool)
|
|
|
|
{
|
|
|
|
if (!arg->getType()->isIntegerTy())
|
|
|
|
throw Exception("__bitSwapLastTwo expected an integral type", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
return b.CreateOr(
|
|
|
|
b.CreateShl(b.CreateAnd(arg, 1), 1),
|
|
|
|
b.CreateAnd(b.CreateLShr(arg, 1), 1)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#endif
|
2019-08-04 13:03:38 +00:00
|
|
|
};
|
2019-02-05 14:50:25 +00:00
|
|
|
|
2019-08-04 13:03:38 +00:00
|
|
|
struct NameBitSwapLastTwo { static constexpr auto name = "__bitSwapLastTwo"; };
|
|
|
|
using FunctionBitSwapLastTwo = FunctionUnaryArithmetic<BitSwapLastTwoImpl, NameBitSwapLastTwo, true>;
|
2019-02-05 14:50:25 +00:00
|
|
|
|
2019-08-04 13:03:38 +00:00
|
|
|
template <> struct FunctionUnaryArithmeticMonotonicity<NameBitSwapLastTwo>
|
2019-02-05 14:50:25 +00:00
|
|
|
{
|
2019-08-04 13:03:38 +00:00
|
|
|
static bool has() { return false; }
|
|
|
|
static IFunction::Monotonicity get(const Field &, const Field &)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void registerFunctionBitSwapLastTwo(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionBitSwapLastTwo>();
|
2019-02-05 14:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|