summaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/mkql_saturated_math.h
blob: 74c926a5880ea47f4515c1d28341a1f20efd1704 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once

#include <yql/essentials/core/sql_types/window_direction.h>
#include <yql/essentials/public/decimal/yql_decimal.h>

#include <util/system/compiler.h>
#include <util/system/types.h>
#include <util/system/yassert.h>

#include <cmath>
#include <limits>
#include <type_traits>

namespace NKikimr::NMiniKQL {

using NYql::NWindow::EDirection;

// clang-format off
template <class T>
using TNextWiderSigned =
    std::conditional_t<sizeof(T) == sizeof(i8), i16,
    std::conditional_t<sizeof(T) == sizeof(i16), i32,
    std::conditional_t<sizeof(T) == sizeof(i32), i64,
                       NYql::NDecimal::TInt128>>>;
// clang-format on

enum class EInfBoundary {
    Left,
    Right,
};

// InfBoundary == Right -> [boundary, +inf)
// InfBoundary == Left  -> (-inf,  boundary]
template <class T, class U>
Y_FORCE_INLINE constexpr bool IsBelongToInterval(EInfBoundary InfBoundary, EDirection dir, T from, U delta, T x) {
    Y_DEBUG_ABORT_UNLESS(delta >= 0);
    if constexpr (std::is_floating_point_v<T>) {
        Y_DEBUG_ABORT_UNLESS(!std::isnan(from));
        Y_DEBUG_ABORT_UNLESS(!std::isnan(x));
    }
    if constexpr (std::is_floating_point_v<U>) {
        Y_DEBUG_ABORT_UNLESS(!std::isnan(delta));
    }
    if constexpr (std::is_floating_point_v<T> || std::is_floating_point_v<U>) {
        const auto b = (dir == EDirection::Following) ? (from + delta) : (from - delta);
        Y_DEBUG_ABORT_UNLESS(!std::isnan(b));
        if (InfBoundary == EInfBoundary::Right) {
            return x >= b;
        } else {
            return x <= b;
        }
    } else {
        static_assert(std::is_integral_v<T>, "T must be integral or floating");
        static_assert(!std::is_same_v<T, bool>, "bool is not supported");
        static_assert(std::is_integral_v<U>, "U must be integral or floating");
        static_assert(!std::is_same_v<U, bool>, "U is not supported");
        static_assert(sizeof(T) == sizeof(i8) || sizeof(T) == sizeof(i16) ||
                          sizeof(T) == sizeof(i32) || sizeof(T) == sizeof(i64),
                      "Only 8/16/32/64-bit integers are supported");
        static_assert(sizeof(U) == sizeof(i8) || sizeof(U) == sizeof(i16) ||
                          sizeof(U) == sizeof(i32) || sizeof(U) == sizeof(i64),
                      "Only 8/16/32/64-bit integers are supported");
        using TW = TNextWiderSigned<T>;
        using UW = TNextWiderSigned<U>;
        const auto wf = static_cast<TW>(from);
        const auto wd = static_cast<UW>(delta);
        const auto wx = static_cast<TW>(x);
        const auto b = (dir == EDirection::Following) ? (wf + wd) : (wf - wd);

        if (InfBoundary == EInfBoundary::Right) {
            return wx >= b; // [b, +inf)
        } else {
            return wx <= b; // (-inf, b]
        }
    }
}

Y_FORCE_INLINE constexpr bool IsBelongToInterval(EInfBoundary infBoundary,
                                                 EDirection dir,
                                                 NYql::NDecimal::TInt128 from,
                                                 NYql::NDecimal::TInt128 delta,
                                                 NYql::NDecimal::TInt128 x,
                                                 ui8 precision)
{
    Y_DEBUG_ABORT_UNLESS(NYql::NDecimal::IsNormal(delta));
    Y_DEBUG_ABORT_UNLESS(NYql::NDecimal::IsComparable(x));
    Y_DEBUG_ABORT_UNLESS(NYql::NDecimal::IsComparable(from));

    const auto b = (dir == EDirection::Following)
                       ? NYql::NDecimal::Add(from, delta, precision)
                       : NYql::NDecimal::Sub(from, delta, precision);
    Y_DEBUG_ABORT_UNLESS(NYql::NDecimal::IsComparable(b));

    if (infBoundary == EInfBoundary::Right) {
        return NYql::NDecimal::IsGreaterOrEqual(x, b);
    } else {
        return NYql::NDecimal::IsLessOrEqual(x, b);
    }
}

} // namespace NKikimr::NMiniKQL