Nix
2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Toggle main menu visibility
Loading...
Searching...
No Matches
checked-arithmetic.hh
Go to the documentation of this file.
1
#pragma once
7
8
#include <compare>
9
#include <concepts>
// IWYU pragma: keep
10
#include <exception>
11
#include <ostream>
12
#include <limits>
13
#include <optional>
14
#include <type_traits>
15
16
namespace
nix::checked {
17
18
class
DivideByZero : std::exception
19
{
20
public
:
21
DivideByZero() =
default
;
22
DivideByZero(DivideByZero &&) =
default
;
23
DivideByZero(
const
DivideByZero &) =
default
;
24
DivideByZero & operator=(DivideByZero &&) =
default
;
25
DivideByZero & operator=(
const
DivideByZero &) =
default
;
26
virtual
~DivideByZero();
27
};
28
33
template
<std::
int
egral T>
34
struct
Checked
35
{
36
using
Inner = T;
37
38
// TODO: this must be a "trivial default constructor", which means it
39
// cannot set the value to NOT DO UB on uninit.
40
T value;
41
42
Checked() =
default
;
43
44
explicit
Checked(
const
T value)
45
: value{value}
46
{
47
}
48
49
Checked(
const
Checked<T> & other) =
default
;
50
Checked(Checked<T> && other) =
default
;
51
Checked<T> & operator=(
const
Checked<T> & other) =
default
;
52
53
std::strong_ordering operator<=>(
const
Checked<T> & other)
const
=
default
;
54
55
std::strong_ordering operator<=>(
const
T & other)
const
56
{
57
return
value <=> other;
58
}
59
60
explicit
operator
T()
const
61
{
62
return
value;
63
}
64
65
enum class
OverflowKind {
66
NoOverflow,
67
Overflow,
68
DivByZero,
69
};
70
71
class
Result
72
{
73
T value;
74
OverflowKind overflowed_;
75
76
public
:
77
Result(T value,
bool
overflowed)
78
: value{value}
79
, overflowed_{overflowed ? OverflowKind::Overflow : OverflowKind::NoOverflow}
80
{
81
}
82
83
Result(T value, OverflowKind overflowed)
84
: value{value}
85
, overflowed_{overflowed}
86
{
87
}
88
89
bool
operator==(Result other)
const
90
{
91
return
value == other.value && overflowed_ == other.overflowed_;
92
}
93
94
std::optional<T> valueChecked()
const
95
{
96
if
(overflowed_ != OverflowKind::NoOverflow) {
97
return
std::nullopt;
98
}
else
{
99
return
value;
100
}
101
}
102
108
T
valueWrapping
()
const
109
{
110
if
(overflowed_ == OverflowKind::DivByZero) {
111
throw
DivideByZero
{};
112
}
113
return
value;
114
}
115
116
bool
overflowed()
const
117
{
118
return
overflowed_ == OverflowKind::Overflow;
119
}
120
121
bool
divideByZero()
const
122
{
123
return
overflowed_ == OverflowKind::DivByZero;
124
}
125
};
126
127
Result operator+(
const
Checked<T> other)
const
128
{
129
return
(*
this
) + other.value;
130
}
131
132
Result
operator+(
const
T other)
const
133
{
134
T result;
135
bool
overflowed = __builtin_add_overflow(value, other, &result);
136
return
Result
{result, overflowed};
137
}
138
139
Result
operator-(
const
Checked<T> other)
const
140
{
141
return
(*
this
) - other.value;
142
}
143
144
Result
operator-(
const
T other)
const
145
{
146
T result;
147
bool
overflowed = __builtin_sub_overflow(value, other, &result);
148
return
Result
{result, overflowed};
149
}
150
151
Result
operator*(
const
Checked<T> other)
const
152
{
153
return
(*
this
) * other.value;
154
}
155
156
Result
operator*(
const
T other)
const
157
{
158
T result;
159
bool
overflowed = __builtin_mul_overflow(value, other, &result);
160
return
Result
{result, overflowed};
161
}
162
163
Result
operator/(
const
Checked<T> other)
const
164
{
165
return
(*
this
) / other.value;
166
}
167
174
Result
operator/
(
const
T other)
const
175
{
176
constexpr
const
T minV = std::numeric_limits<T>::min();
177
178
// It's only possible to overflow with signed division since doing so
179
// requires crossing the two's complement limits by MIN / -1 (since
180
// two's complement has one more in range in the negative direction
181
// than in the positive one).
182
if
(std::is_signed<T>() && (value == minV && other == -1)) {
183
return
Result{minV,
true
};
184
}
else
if
(other == 0) {
185
return
Result{0, OverflowKind::DivByZero};
186
}
else
{
187
T result = value / other;
188
return
Result{result,
false
};
189
}
190
}
191
};
192
193
template
<std::
int
egral T>
194
std::ostream & operator<<(std::ostream & ios, Checked<T> v)
195
{
196
ios << v.value;
197
return
ios;
198
}
199
200
}
// namespace nix::checked
nix::checked::Checked::Result
Definition
checked-arithmetic.hh:72
nix::checked::Checked::Result::valueWrapping
T valueWrapping() const
Definition
checked-arithmetic.hh:108
nix::checked::DivideByZero
Definition
checked-arithmetic.hh:19
nix::checked::Checked::operator/
Result operator/(const T other) const
Definition
checked-arithmetic.hh:174
libutil
include
nix
util
checked-arithmetic.hh
Generated by
1.18.0