1 | #include <Functions/FunctionFactory.h> |
2 | #include <Functions/FunctionBinaryArithmetic.h> |
3 | |
4 | namespace DB |
5 | { |
6 | |
7 | template <typename A, typename B> |
8 | struct BitRotateLeftImpl |
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("BitRotateLeftImpl expected an integral type" , ErrorCodes::LOGICAL_ERROR); |
26 | auto * size = llvm::ConstantInt::get(left->getType(), left->getType()->getPrimitiveSizeInBits()); |
27 | /// XXX how is this supposed to behave in signed mode? |
28 | return b.CreateOr(b.CreateShl(left, right), b.CreateLShr(left, b.CreateSub(size, right))); |
29 | } |
30 | #endif |
31 | }; |
32 | |
33 | struct NameBitRotateLeft { static constexpr auto name = "bitRotateLeft" ; }; |
34 | using FunctionBitRotateLeft = FunctionBinaryArithmetic<BitRotateLeftImpl, NameBitRotateLeft>; |
35 | |
36 | void registerFunctionBitRotateLeft(FunctionFactory & factory) |
37 | { |
38 | factory.registerFunction<FunctionBitRotateLeft>(); |
39 | } |
40 | |
41 | } |
42 | |