summaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/mkql_safe_arithmetic_ops.h
blob: d8ae77b5f1ff670dcb6fc496d73c1262902022a3 (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
102
103
104
105
106
107
#pragma once

#include <yql/essentials/minikql/mkql_numeric_cast.h>
#include <yql/essentials/public/decimal/yql_decimal.h>

#include <type_traits>
#include <concepts>

namespace NKikimr {
namespace NMiniKQL {

// Safe arithmetic operations that avoid undefined behavior from signed integer overflow
// by performing operations in unsigned arithmetic and casting back to signed.

// SafeAdd: Addition without signed overflow UB
template <typename T>
[[nodiscard]] constexpr T SafeAdd(T u, T v)
    requires std::is_integral_v<T> || std::is_same_v<T, NYql::NDecimal::TInt128>
{
    using TUnsigned = TMakeUnsigned_t<T>;
    return static_cast<T>(static_cast<TUnsigned>(u) +
                          static_cast<TUnsigned>(v));
}

template <typename T>
[[nodiscard]] constexpr T SafeAdd(T u, T v)
    requires std::is_floating_point_v<T>
{
    return u + v;
}

// SafeSub: Subtraction without signed overflow UB
template <typename T>
[[nodiscard]] constexpr T SafeSub(T u, T v)
    requires std::is_integral_v<T> || std::is_same_v<T, NYql::NDecimal::TInt128>
{
    using TUnsigned = TMakeUnsigned_t<T>;
    return static_cast<T>(static_cast<TUnsigned>(u) -
                          static_cast<TUnsigned>(v));
}

template <typename T>
[[nodiscard]] constexpr T SafeSub(T u, T v)
    requires std::is_floating_point_v<T>
{
    return u - v;
}

// SafeMul: Multiplication without signed overflow UB
// Special handling for 16-bit types to avoid implicit promotion overflow
template <typename T>
[[nodiscard]] constexpr T SafeMul(T u, T v)
    requires std::is_same_v<T, i16> || std::is_same_v<T, ui16>
{
    return static_cast<ui32>(u) * static_cast<ui32>(v);
}

template <typename T>
[[nodiscard]] constexpr T SafeMul(T u, T v)
    requires(std::is_integral_v<T> || std::is_same_v<T, NYql::NDecimal::TInt128>) &&
            (!std::is_same_v<T, i16>) && (!std::is_same_v<T, ui16>)
{
    using TUnsigned = TMakeUnsigned_t<T>;
    return static_cast<T>(static_cast<TUnsigned>(u) *
                          static_cast<TUnsigned>(v));
}

template <typename T>
[[nodiscard]] constexpr T SafeMul(T u, T v)
    requires std::is_floating_point_v<T>
{
    return u * v;
}

// SafeInc: Increment without signed overflow UB
template <typename T>
[[nodiscard]] constexpr T SafeInc(T u)
{
    return SafeAdd(u, T{1});
}

// SafeDec: Decrement without signed overflow UB
template <typename T>
[[nodiscard]] constexpr T SafeDec(T u)
{
    return SafeSub(u, T{1});
}

// SafeNeg: Negation without signed overflow UB (e.g., -INT_MIN)
// Uses two's complement: -x = ~x + 1
template <typename T>
[[nodiscard]] constexpr T SafeNeg(T u)
    requires std::is_integral_v<T> || std::is_same_v<T, NYql::NDecimal::TInt128>
{
    using TUnsigned = TMakeUnsigned_t<T>;
    return static_cast<T>(~static_cast<TUnsigned>(u) + TUnsigned{1});
}

template <typename T>
[[nodiscard]] constexpr T SafeNeg(T u)
    requires std::is_floating_point_v<T>
{
    return -u;
}

} // namespace NMiniKQL
} // namespace NKikimr