2018-11-26 16:20:40 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionBinaryArithmetic.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-08-19 11:52:17 +00:00
|
|
|
extern const int NOT_IMPLEMENTED;
|
2020-02-25 18:10:48 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
2018-11-26 16:20:40 +00:00
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
template <typename A, typename B>
|
|
|
|
struct BitRotateRightImpl
|
|
|
|
{
|
|
|
|
using ResultType = typename NumberTraits::ResultOfBit<A, B>::Type;
|
2020-02-14 07:11:37 +00:00
|
|
|
static const constexpr bool allow_fixed_string = false;
|
2021-08-30 06:37:23 +00:00
|
|
|
static const constexpr bool allow_string_integer = false;
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
2020-08-19 11:52:17 +00:00
|
|
|
static inline NO_SANITIZE_UNDEFINED Result apply(A a [[maybe_unused]], B b [[maybe_unused]])
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
2020-09-04 13:33:02 +00:00
|
|
|
if constexpr (is_big_int_v<A> || is_big_int_v<B>)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Bit rotate is not implemented for big integers");
|
2020-08-19 11:52:17 +00:00
|
|
|
else
|
|
|
|
return (static_cast<Result>(a) >> static_cast<Result>(b))
|
|
|
|
| (static_cast<Result>(a) << ((sizeof(Result) * 8) - static_cast<Result>(b)));
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
static constexpr bool compilable = true;
|
|
|
|
|
|
|
|
static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * left, llvm::Value * right, bool)
|
|
|
|
{
|
|
|
|
if (!left->getType()->isIntegerTy())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "BitRotateRightImpl expected an integral type");
|
2018-11-26 16:20:40 +00:00
|
|
|
auto * size = llvm::ConstantInt::get(left->getType(), left->getType()->getPrimitiveSizeInBits());
|
|
|
|
return b.CreateOr(b.CreateLShr(left, right), b.CreateShl(left, b.CreateSub(size, right)));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NameBitRotateRight { static constexpr auto name = "bitRotateRight"; };
|
2021-01-30 04:13:49 +00:00
|
|
|
using FunctionBitRotateRight = BinaryArithmeticOverloadResolver<BitRotateRightImpl, NameBitRotateRight, true, false>;
|
2018-11-26 16:20:40 +00:00
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(BitRotateRight)
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionBitRotateRight>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|