2018-11-26 16:20:40 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionBinaryArithmetic.h>
|
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
#ifdef __SSE2__
|
2018-11-26 16:20:40 +00:00
|
|
|
#define LIBDIVIDE_USE_SSE2 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <libdivide.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_DIVISION;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename A, typename B>
|
|
|
|
struct ModuloImpl
|
|
|
|
{
|
|
|
|
using ResultType = typename NumberTraits::ResultOfModulo<A, B>::Type;
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
|
|
|
throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<A>::Type(a), typename NumberTraits::ToInteger<B>::Type(b));
|
|
|
|
return typename NumberTraits::ToInteger<A>::Type(a) % typename NumberTraits::ToInteger<B>::Type(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
static constexpr bool compilable = false; /// don't know how to throw from LLVM IR
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename A, typename B>
|
|
|
|
struct ModuloByConstantImpl
|
|
|
|
: BinaryOperationImplBase<A, B, ModuloImpl<A, B>>
|
|
|
|
{
|
|
|
|
using ResultType = typename ModuloImpl<A, B>::ResultType;
|
|
|
|
|
|
|
|
static void vector_constant(const PaddedPODArray<A> & a, B b, PaddedPODArray<ResultType> & c)
|
|
|
|
{
|
|
|
|
if (unlikely(b == 0))
|
|
|
|
throw Exception("Division by zero", ErrorCodes::ILLEGAL_DIVISION);
|
|
|
|
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wsign-compare"
|
|
|
|
|
|
|
|
if (unlikely((std::is_signed_v<B> && b == -1) || b == 1))
|
|
|
|
{
|
|
|
|
size_t size = a.size();
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
c[i] = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
|
|
|
libdivide::divider<A> divider(b);
|
|
|
|
|
|
|
|
/// Here we failed to make the SSE variant from libdivide give an advantage.
|
|
|
|
size_t size = a.size();
|
2019-11-13 06:49:22 +00:00
|
|
|
|
|
|
|
/// strict aliasing optimization for char like arrays
|
|
|
|
auto * __restrict src = a.data();
|
|
|
|
auto * __restrict dst = c.data();
|
2019-11-18 12:19:45 +00:00
|
|
|
|
2019-11-13 06:49:22 +00:00
|
|
|
if (b & (b - 1))
|
2019-11-17 20:25:27 +00:00
|
|
|
{
|
2019-11-13 06:49:22 +00:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
dst[i] = src[i] - (src[i] / divider) * b; /// NOTE: perhaps, the division semantics with the remainder of negative numbers is not preserved.
|
2019-11-17 20:25:27 +00:00
|
|
|
}
|
2019-11-13 06:49:22 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// gcc libdivide doesn't work well for pow2 division
|
|
|
|
auto mask = b - 1;
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
dst[i] = src[i] & mask;
|
|
|
|
}
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Specializations are specified for dividing numbers of the type UInt64 and UInt32 by the numbers of the same sign.
|
|
|
|
* Can be expanded to all possible combinations, but more code is needed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <> struct BinaryOperationImpl<UInt64, UInt8, ModuloImpl<UInt64, UInt8>> : ModuloByConstantImpl<UInt64, UInt8> {};
|
|
|
|
template <> struct BinaryOperationImpl<UInt64, UInt16, ModuloImpl<UInt64, UInt16>> : ModuloByConstantImpl<UInt64, UInt16> {};
|
|
|
|
template <> struct BinaryOperationImpl<UInt64, UInt32, ModuloImpl<UInt64, UInt32>> : ModuloByConstantImpl<UInt64, UInt32> {};
|
|
|
|
template <> struct BinaryOperationImpl<UInt64, UInt64, ModuloImpl<UInt64, UInt64>> : ModuloByConstantImpl<UInt64, UInt64> {};
|
|
|
|
|
|
|
|
template <> struct BinaryOperationImpl<UInt32, UInt8, ModuloImpl<UInt32, UInt8>> : ModuloByConstantImpl<UInt32, UInt8> {};
|
|
|
|
template <> struct BinaryOperationImpl<UInt32, UInt16, ModuloImpl<UInt32, UInt16>> : ModuloByConstantImpl<UInt32, UInt16> {};
|
|
|
|
template <> struct BinaryOperationImpl<UInt32, UInt32, ModuloImpl<UInt32, UInt32>> : ModuloByConstantImpl<UInt32, UInt32> {};
|
|
|
|
template <> struct BinaryOperationImpl<UInt32, UInt64, ModuloImpl<UInt32, UInt64>> : ModuloByConstantImpl<UInt32, UInt64> {};
|
|
|
|
|
|
|
|
template <> struct BinaryOperationImpl<Int64, Int8, ModuloImpl<Int64, Int8>> : ModuloByConstantImpl<Int64, Int8> {};
|
|
|
|
template <> struct BinaryOperationImpl<Int64, Int16, ModuloImpl<Int64, Int16>> : ModuloByConstantImpl<Int64, Int16> {};
|
|
|
|
template <> struct BinaryOperationImpl<Int64, Int32, ModuloImpl<Int64, Int32>> : ModuloByConstantImpl<Int64, Int32> {};
|
|
|
|
template <> struct BinaryOperationImpl<Int64, Int64, ModuloImpl<Int64, Int64>> : ModuloByConstantImpl<Int64, Int64> {};
|
|
|
|
|
|
|
|
template <> struct BinaryOperationImpl<Int32, Int8, ModuloImpl<Int32, Int8>> : ModuloByConstantImpl<Int32, Int8> {};
|
|
|
|
template <> struct BinaryOperationImpl<Int32, Int16, ModuloImpl<Int32, Int16>> : ModuloByConstantImpl<Int32, Int16> {};
|
|
|
|
template <> struct BinaryOperationImpl<Int32, Int32, ModuloImpl<Int32, Int32>> : ModuloByConstantImpl<Int32, Int32> {};
|
|
|
|
template <> struct BinaryOperationImpl<Int32, Int64, ModuloImpl<Int32, Int64>> : ModuloByConstantImpl<Int32, Int64> {};
|
|
|
|
|
|
|
|
|
|
|
|
struct NameModulo { static constexpr auto name = "modulo"; };
|
|
|
|
using FunctionModulo = FunctionBinaryArithmetic<ModuloImpl, NameModulo, false>;
|
|
|
|
|
|
|
|
void registerFunctionModulo(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionModulo>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|