ClickHouse
55 строк · 1.8 Кб
1#include <Functions/FunctionFactory.h>
2#include <Functions/FunctionBinaryArithmetic.h>
3
4namespace DB
5{
6namespace ErrorCodes
7{
8extern const int NOT_IMPLEMENTED;
9extern const int LOGICAL_ERROR;
10}
11
12namespace
13{
14
15template <typename A, typename B>
16struct BitRotateRightImpl
17{
18using ResultType = typename NumberTraits::ResultOfBit<A, B>::Type;
19static const constexpr bool allow_fixed_string = false;
20static const constexpr bool allow_string_integer = false;
21
22template <typename Result = ResultType>
23static inline NO_SANITIZE_UNDEFINED Result apply(A a [[maybe_unused]], B b [[maybe_unused]])
24{
25if constexpr (is_big_int_v<A> || is_big_int_v<B>)
26throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Bit rotate is not implemented for big integers");
27else
28return (static_cast<Result>(a) >> static_cast<Result>(b))
29| (static_cast<Result>(a) << ((sizeof(Result) * 8) - static_cast<Result>(b)));
30}
31
32#if USE_EMBEDDED_COMPILER
33static constexpr bool compilable = true;
34
35static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * left, llvm::Value * right, bool)
36{
37if (!left->getType()->isIntegerTy())
38throw Exception(ErrorCodes::LOGICAL_ERROR, "BitRotateRightImpl expected an integral type");
39auto * size = llvm::ConstantInt::get(left->getType(), left->getType()->getPrimitiveSizeInBits());
40return b.CreateOr(b.CreateLShr(left, right), b.CreateShl(left, b.CreateSub(size, right)));
41}
42#endif
43};
44
45struct NameBitRotateRight { static constexpr auto name = "bitRotateRight"; };
46using FunctionBitRotateRight = BinaryArithmeticOverloadResolver<BitRotateRightImpl, NameBitRotateRight, true, false>;
47
48}
49
50REGISTER_FUNCTION(BitRotateRight)
51{
52factory.registerFunction<FunctionBitRotateRight>();
53}
54
55}
56