2018-11-26 16:20:40 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionBinaryArithmetic.h>
|
|
|
|
|
2020-04-11 11:14:01 +00:00
|
|
|
#if defined(__SSE2__)
|
|
|
|
# define LIBDIVIDE_SSE2 1
|
2018-11-26 16:20:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <libdivide.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-03-03 02:33:13 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_DIVISION;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2020-02-25 09:45:23 +00:00
|
|
|
/// Optimizations for integer modulo by a constant.
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
template <typename A, typename B>
|
|
|
|
struct ModuloByConstantImpl
|
2020-09-15 10:31:15 +00:00
|
|
|
: BinaryOperation<A, B, ModuloImpl<A, B>>
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
2021-02-28 08:28:30 +00:00
|
|
|
using Op = ModuloImpl<A, B>;
|
|
|
|
using ResultType = typename Op::ResultType;
|
2020-02-14 07:11:37 +00:00
|
|
|
static const constexpr bool allow_fixed_string = false;
|
2018-11-26 16:20:40 +00:00
|
|
|
|
2021-02-28 08:28:30 +00:00
|
|
|
template <OpCase op_case>
|
|
|
|
static void NO_INLINE process(const A * __restrict a, const B * __restrict b, ResultType * __restrict c, size_t size)
|
|
|
|
{
|
|
|
|
if constexpr (op_case == OpCase::Vector)
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
c[i] = Op::template apply<ResultType>(a[i], b[i]);
|
|
|
|
else if constexpr (op_case == OpCase::LeftConstant)
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
c[i] = Op::template apply<ResultType>(*a, b[i]);
|
|
|
|
else
|
|
|
|
vectorConstant(a, *b, c, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ResultType process(A a, B b) { return Op::template apply<ResultType>(a, b); }
|
|
|
|
|
2021-02-28 07:25:56 +00:00
|
|
|
static void NO_INLINE NO_SANITIZE_UNDEFINED vectorConstant(const A * __restrict src, B b, ResultType * __restrict dst, size_t size)
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wsign-compare"
|
|
|
|
|
2020-07-05 00:28:08 +00:00
|
|
|
/// Modulo with too small divisor.
|
2018-11-26 16:20:40 +00:00
|
|
|
if (unlikely((std::is_signed_v<B> && b == -1) || b == 1))
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2020-02-23 23:14:27 +00:00
|
|
|
dst[i] = 0;
|
2018-11-26 16:20:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-05 00:28:08 +00:00
|
|
|
/// Modulo with too large divisor.
|
|
|
|
if (unlikely(b > std::numeric_limits<A>::max()
|
|
|
|
|| (std::is_signed_v<A> && std::is_signed_v<B> && b < std::numeric_limits<A>::lowest())))
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
dst[i] = src[i];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
2020-07-05 00:28:08 +00:00
|
|
|
if (unlikely(static_cast<A>(b) == 0))
|
|
|
|
throw Exception("Division by zero", ErrorCodes::ILLEGAL_DIVISION);
|
|
|
|
|
2021-03-14 14:31:07 +00:00
|
|
|
/// Division by min negative value.
|
|
|
|
if (std::is_signed_v<B> && b == std::numeric_limits<B>::lowest())
|
|
|
|
throw Exception("Division by the most negative number", ErrorCodes::ILLEGAL_DIVISION);
|
|
|
|
|
|
|
|
/// Modulo of division by negative number is the same as the positive number.
|
|
|
|
if (b < 0)
|
|
|
|
b = -b;
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
/// Here we failed to make the SSE variant from libdivide give an advantage.
|
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
|
|
|
{
|
2021-04-15 13:59:00 +00:00
|
|
|
libdivide::divider<A> divider(b);
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-15 18:45:19 +00:00
|
|
|
template <typename A, typename B>
|
|
|
|
struct ModuloLegacyByConstantImpl : ModuloByConstantImpl<A, B>
|
|
|
|
{
|
|
|
|
using Op = ModuloLegacyImpl<A, B>;
|
|
|
|
};
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-12-25 16:32:59 +00:00
|
|
|
namespace impl_
|
|
|
|
{
|
2018-11-26 16:20:40 +00:00
|
|
|
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> {};
|
2020-12-25 16:32:59 +00:00
|
|
|
}
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
struct NameModulo { static constexpr auto name = "modulo"; };
|
2020-09-05 14:12:47 +00:00
|
|
|
using FunctionModulo = BinaryArithmeticOverloadResolver<ModuloImpl, NameModulo, false>;
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
void registerFunctionModulo(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionModulo>();
|
2020-07-20 01:03:46 +00:00
|
|
|
factory.registerAlias("mod", "modulo", FunctionFactory::CaseInsensitive);
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 18:45:19 +00:00
|
|
|
struct NameModuloLegacy { static constexpr auto name = "moduloLegacy"; };
|
2021-05-15 18:32:20 +00:00
|
|
|
using FunctionModuloLegacy = BinaryArithmeticOverloadResolver<ModuloLegacyImpl, NameModuloLegacy, false>;
|
2021-05-15 18:45:19 +00:00
|
|
|
|
|
|
|
void registerFunctionModuloLegacy(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionModuloLegacy>();
|
|
|
|
}
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|