#include "mkql_builtins_impl.h" // Y_IGNORE // Y_IGNORE #include #include namespace NKikimr { namespace NMiniKQL { namespace { template struct TCountBits : public TSimpleArithmeticUnary> { static TOutput Do(TInput val) { if constexpr (std::is_signed_v) { return std::popcount(static_cast>(val)); } else { return std::popcount(val); } } #ifndef MKQL_DISABLE_CODEGEN static Value* Gen(Value* arg, const TCodegenContext& ctx, BasicBlock*& block) { auto& context = ctx.Codegen.GetContext(); auto& module = ctx.Codegen.GetModule(); const auto fnType = FunctionType::get(arg->getType(), {arg->getType()}, false); const auto& name = GetFuncNameForType("llvm.ctpop"); const auto func = module.getOrInsertFunction(name, fnType).getCallee(); const auto result = CallInst::Create(fnType, func, {arg}, "popcount", block); return StaticCast(result, context, block); } #endif }; } void RegisterCountBits(IBuiltinFunctionRegistry& registry) { RegisterUnaryIntegralFunctionOpt(registry, "CountBits"); } } // namespace NMiniKQL } // namespace NKikimr