ClickHouse

Форк
0
/
bitRotateRight.cpp 
55 строк · 1.8 Кб
1
#include <Functions/FunctionFactory.h>
2
#include <Functions/FunctionBinaryArithmetic.h>
3

4
namespace DB
5
{
6
namespace ErrorCodes
7
{
8
    extern const int NOT_IMPLEMENTED;
9
    extern const int LOGICAL_ERROR;
10
}
11

12
namespace
13
{
14

15
template <typename A, typename B>
16
struct BitRotateRightImpl
17
{
18
    using ResultType = typename NumberTraits::ResultOfBit<A, B>::Type;
19
    static const constexpr bool allow_fixed_string = false;
20
    static const constexpr bool allow_string_integer = false;
21

22
    template <typename Result = ResultType>
23
    static inline NO_SANITIZE_UNDEFINED Result apply(A a [[maybe_unused]], B b [[maybe_unused]])
24
    {
25
        if constexpr (is_big_int_v<A> || is_big_int_v<B>)
26
            throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Bit rotate is not implemented for big integers");
27
        else
28
            return (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
33
    static constexpr bool compilable = true;
34

35
    static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * left, llvm::Value * right, bool)
36
    {
37
        if (!left->getType()->isIntegerTy())
38
            throw Exception(ErrorCodes::LOGICAL_ERROR, "BitRotateRightImpl expected an integral type");
39
        auto * size = llvm::ConstantInt::get(left->getType(), left->getType()->getPrimitiveSizeInBits());
40
        return b.CreateOr(b.CreateLShr(left, right), b.CreateShl(left, b.CreateSub(size, right)));
41
    }
42
#endif
43
};
44

45
struct NameBitRotateRight { static constexpr auto name = "bitRotateRight"; };
46
using FunctionBitRotateRight = BinaryArithmeticOverloadResolver<BitRotateRightImpl, NameBitRotateRight, true, false>;
47

48
}
49

50
REGISTER_FUNCTION(BitRotateRight)
51
{
52
    factory.registerFunction<FunctionBitRotateRight>();
53
}
54

55
}
56

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.