aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/GCDLCMImpl.h
blob: d6f20149c12dc9064eca30643b4713f73833aff7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once

#include <DataTypes/NumberTraits.h>
#include <Common/Exception.h>
#include <base/extended_types.h>
#include <limits>
#include <type_traits>

#include "clickhouse_config.h"


namespace DB
{

namespace ErrorCodes
{
    extern const int DECIMAL_OVERFLOW;
}


template <typename A, typename B, typename Impl, typename Name>
struct GCDLCMImpl
{
    using ResultType = typename NumberTraits::ResultOfAdditionMultiplication<A, B>::Type;
    static const constexpr bool allow_fixed_string = false;
    static const constexpr bool allow_string_integer = false;

    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));
        throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<B>::Type(b), typename NumberTraits::ToInteger<A>::Type(a));

        using Int = typename NumberTraits::ToInteger<Result>::Type;

        if constexpr (is_signed_v<Result>)
        {
            /// gcd() internally uses std::abs()
            Int a_s = static_cast<Int>(a);
            Int b_s = static_cast<Int>(b);
            Int min = std::numeric_limits<Int>::lowest();
            Int max = std::numeric_limits<Int>::max();
            if (unlikely((a_s == min || a_s == max) || (b_s == min || b_s == max)))
                throw Exception(ErrorCodes::DECIMAL_OVERFLOW,
                                "Intermediate result overflow (signed a = {}, signed b = {}, min = {}, max = {})",
                                a_s, b_s, min, max);
        }

        return Impl::applyImpl(a, b);
    }

#if USE_EMBEDDED_COMPILER
    static constexpr bool compilable = false; /// exceptions (and a non-trivial algorithm)
#endif
};

}