1 | #include <Functions/FunctionFactory.h> |
2 | #include <Functions/FunctionBinaryArithmetic.h> |
3 | |
4 | namespace DB |
5 | { |
6 | |
7 | template <typename A, typename B> |
8 | struct BitRotateRightImpl |
9 | { |
10 | using ResultType = typename NumberTraits::ResultOfBit<A, B>::Type; |
11 | |
12 | template <typename Result = ResultType> |
13 | static inline NO_SANITIZE_UNDEFINED Result apply(A a, B b) |
14 | { |
15 | return (static_cast<Result>(a) >> static_cast<Result>(b)) |
16 | | (static_cast<Result>(a) << ((sizeof(Result) * 8) - static_cast<Result>(b))); |
17 | } |
18 | |
19 | #if USE_EMBEDDED_COMPILER |
20 | static constexpr bool compilable = true; |
21 | |
22 | static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * left, llvm::Value * right, bool) |
23 | { |
24 | if (!left->getType()->isIntegerTy()) |
25 | throw Exception("BitRotateRightImpl expected an integral type" , ErrorCodes::LOGICAL_ERROR); |
26 | auto * size = llvm::ConstantInt::get(left->getType(), left->getType()->getPrimitiveSizeInBits()); |
27 | return b.CreateOr(b.CreateLShr(left, right), b.CreateShl(left, b.CreateSub(size, right))); |
28 | } |
29 | #endif |
30 | }; |
31 | |
32 | struct NameBitRotateRight { static constexpr auto name = "bitRotateRight" ; }; |
33 | using FunctionBitRotateRight = FunctionBinaryArithmetic<BitRotateRightImpl, NameBitRotateRight>; |
34 | |
35 | void registerFunctionBitRotateRight(FunctionFactory & factory) |
36 | { |
37 | factory.registerFunction<FunctionBitRotateRight>(); |
38 | } |
39 | |
40 | } |
41 | |