| 1 | #include <Functions/FunctionFactory.h> |
|---|---|
| 2 | #include <Functions/FunctionBinaryArithmetic.h> |
| 3 | #include <numeric> |
| 4 | |
| 5 | |
| 6 | namespace DB |
| 7 | { |
| 8 | |
| 9 | template <typename A, typename B> |
| 10 | struct GCDImpl |
| 11 | { |
| 12 | using ResultType = typename NumberTraits::ResultOfAdditionMultiplication<A, B>::Type; |
| 13 | |
| 14 | template <typename Result = ResultType> |
| 15 | static inline Result apply(A a, B b) |
| 16 | { |
| 17 | throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<A>::Type(a), typename NumberTraits::ToInteger<B>::Type(b)); |
| 18 | throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<B>::Type(b), typename NumberTraits::ToInteger<A>::Type(a)); |
| 19 | return std::gcd( |
| 20 | typename NumberTraits::ToInteger<Result>::Type(a), |
| 21 | typename NumberTraits::ToInteger<Result>::Type(b)); |
| 22 | } |
| 23 | |
| 24 | #if USE_EMBEDDED_COMPILER |
| 25 | static constexpr bool compilable = false; /// exceptions (and a non-trivial algorithm) |
| 26 | #endif |
| 27 | }; |
| 28 | |
| 29 | struct NameGCD { static constexpr auto name = "gcd"; }; |
| 30 | using FunctionGCD = FunctionBinaryArithmetic<GCDImpl, NameGCD, false>; |
| 31 | |
| 32 | void registerFunctionGCD(FunctionFactory & factory) |
| 33 | { |
| 34 | factory.registerFunction<FunctionGCD>(); |
| 35 | } |
| 36 | |
| 37 | } |
| 38 |