aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/invoke_builtins/mkql_builtins_shiftleft.cpp
blob: 7096fd4a7410c1386018dc50a0b115703f6b7013 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "mkql_builtins_impl.h"  // Y_IGNORE

namespace NKikimr {
namespace NMiniKQL {

namespace {

template<typename TInput, typename TOutput>
struct TShiftLeft : public TShiftArithmeticBinary<TInput, TOutput, TShiftLeft<TInput, TOutput>> {
    static TOutput Do(TInput arg, ui8 bits)
    {
        return (bits < sizeof(arg) * CHAR_BIT) ? (arg << bits) : 0;
    }

#ifndef MKQL_DISABLE_CODEGEN
    static Value* Gen(Value* arg, Value* bits, const TCodegenContext&, BasicBlock*& block)
    {
        const auto zero = ConstantInt::get(arg->getType(), 0);
        const auto maxb = ConstantInt::get(bits->getType(), sizeof(TInput) * CHAR_BIT);
        const auto check = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT, bits, maxb, "check", block);
        const auto zext = arg->getType() == bits->getType() ? bits : new ZExtInst(bits, arg->getType(), "zext", block);
        const auto shl = BinaryOperator::CreateShl(arg, zext, "shl", block);
        const auto res = SelectInst::Create(check, shl, zero, "res", block);
        return res;
    }
#endif
};

}

void RegisterShiftLeft(IBuiltinFunctionRegistry& registry) {
    RegisterUnsignedShiftFunctionOpt<TShiftLeft, TBinaryShiftArgsOpt>(registry, "ShiftLeft");
}

} // namespace NMiniKQL
} // namespace NKikimr