Added more bitwise functions [#CLICKHOUSE-2884].

This commit is contained in:
Alexey Milovidov 2017-03-20 22:16:02 +03:00
parent a2d4f9907b
commit 910c542994
4 changed files with 40 additions and 3 deletions

View File

@ -20,9 +20,10 @@ namespace ErrorCodes
}
/** Арифметические функции: +, -, *, /, %,
* intDiv (целочисленное деление), унарный минус.
* Битовые функции: |, &, ^, ~.
/** Arithmetic operations: +, -, *, /, %,
* intDiv (integer division), unary minus.
* Bitwise operations: |, &, ^, ~.
* Etc.
*/
template<typename A, typename B, typename Op, typename ResultType_ = typename Op::ResultType>
@ -272,6 +273,32 @@ struct BitShiftRightImpl
}
};
template<typename A, typename B>
struct BitRotateLeftImpl
{
using ResultType = typename NumberTraits::ResultOfBit<A, B>::Type;
template <typename Result = ResultType>
static inline Result apply(A a, B b)
{
return (static_cast<Result>(a) << static_cast<Result>(b))
| (static_cast<Result>(a) >> ((sizeof(Result) * 8) - static_cast<Result>(b)));
}
};
template<typename A, typename B>
struct BitRotateRightImpl
{
using ResultType = typename NumberTraits::ResultOfBit<A, B>::Type;
template <typename Result = ResultType>
static inline Result apply(A a, B b)
{
return (static_cast<Result>(a) >> static_cast<Result>(b))
| (static_cast<Result>(a) << ((sizeof(Result) * 8) - static_cast<Result>(b)));
}
};
template<typename A, typename B>
struct LeastBaseImpl
@ -885,6 +912,8 @@ struct NameBitXor { static constexpr auto name = "bitXor"; };
struct NameBitNot { static constexpr auto name = "bitNot"; };
struct NameBitShiftLeft { static constexpr auto name = "bitShiftLeft"; };
struct NameBitShiftRight { static constexpr auto name = "bitShiftRight"; };
struct NameBitRotateLeft { static constexpr auto name = "bitRotateLeft"; };
struct NameBitRotateRight { static constexpr auto name = "bitRotateRight"; };
struct NameLeast { static constexpr auto name = "least"; };
struct NameGreatest { static constexpr auto name = "greatest"; };
@ -903,6 +932,8 @@ using FunctionBitXor = FunctionBinaryArithmetic<BitXorImpl, NameBitXor>;
using FunctionBitNot = FunctionUnaryArithmetic<BitNotImpl, NameBitNot, true>;
using FunctionBitShiftLeft = FunctionBinaryArithmetic<BitShiftLeftImpl, NameBitShiftLeft>;
using FunctionBitShiftRight = FunctionBinaryArithmetic<BitShiftRightImpl, NameBitShiftRight>;
using FunctionBitRotateLeft = FunctionBinaryArithmetic<BitRotateLeftImpl, NameBitRotateLeft>;
using FunctionBitRotateRight = FunctionBinaryArithmetic<BitRotateRightImpl, NameBitRotateRight>;
using FunctionLeast = FunctionBinaryArithmetic<LeastImpl, NameLeast>;
using FunctionGreatest = FunctionBinaryArithmetic<GreatestImpl, NameGreatest>;

View File

@ -21,6 +21,8 @@ void registerFunctionsArithmetic(FunctionFactory & factory)
factory.registerFunction<FunctionBitNot>();
factory.registerFunction<FunctionBitShiftLeft>();
factory.registerFunction<FunctionBitShiftRight>();
factory.registerFunction<FunctionBitRotateLeft>();
factory.registerFunction<FunctionBitRotateRight>();
factory.registerFunction<FunctionLeast>();
factory.registerFunction<FunctionGreatest>();
}

View File

@ -0,0 +1,2 @@
03
C000000000000000

View File

@ -0,0 +1,2 @@
SELECT hex(bitRotateLeft(0x8000000000000001, 1));
SELECT hex(bitRotateRight(0x8000000000000001, 1));