Newer
Older
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#ifndef ASIC_SIMULATION_CORE_OPERATIONS_H
#define ASIC_SIMULATION_CORE_OPERATIONS_H
#define NOMINMAX
#include "../debug.h"
#include "../number.h"
#include "operation.h"
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <stdexcept>
#include <utility>
namespace asic {
class constant_operation final : public abstract_operation {
public:
constant_operation(result_key key, number value)
: abstract_operation(std::move(key))
, m_value(value) {}
[[nodiscard]] std::size_t output_count() const noexcept final {
return 1;
}
private:
[[nodiscard]] number evaluate_output_impl(std::size_t, evaluation_context const&) const final {
ASIC_DEBUG_MSG("Evaluating constant.");
return m_value;
}
number m_value;
};
class addition_operation final : public binary_operation {
public:
explicit addition_operation(result_key key)
: binary_operation(std::move(key)) {}
[[nodiscard]] std::size_t output_count() const noexcept final {
return 1;
}
private:
[[nodiscard]] number evaluate_output_impl(std::size_t, evaluation_context const& context) const final {
ASIC_DEBUG_MSG("Evaluating addition.");
return this->evaluate_lhs(context) + this->evaluate_rhs(context);
}
};
class subtraction_operation final : public binary_operation {
public:
explicit subtraction_operation(result_key key)
: binary_operation(std::move(key)) {}
[[nodiscard]] std::size_t output_count() const noexcept final {
return 1;
}
private:
[[nodiscard]] number evaluate_output_impl(std::size_t, evaluation_context const& context) const final {
ASIC_DEBUG_MSG("Evaluating subtraction.");
return this->evaluate_lhs(context) - this->evaluate_rhs(context);
}
};
class multiplication_operation final : public binary_operation {
public:
explicit multiplication_operation(result_key key)
: binary_operation(std::move(key)) {}
[[nodiscard]] std::size_t output_count() const noexcept final {
return 1;
}
private:
[[nodiscard]] number evaluate_output_impl(std::size_t, evaluation_context const& context) const final {
ASIC_DEBUG_MSG("Evaluating multiplication.");
return this->evaluate_lhs(context) * this->evaluate_rhs(context);
}
};
class division_operation final : public binary_operation {
public:
explicit division_operation(result_key key)
: binary_operation(std::move(key)) {}
[[nodiscard]] std::size_t output_count() const noexcept final {
return 1;
}
private:
[[nodiscard]] number evaluate_output_impl(std::size_t, evaluation_context const& context) const final {
ASIC_DEBUG_MSG("Evaluating division.");
return this->evaluate_lhs(context) / this->evaluate_rhs(context);
}
};
class min_operation final : public binary_operation {
public:
explicit min_operation(result_key key)
: binary_operation(std::move(key)) {}
[[nodiscard]] std::size_t output_count() const noexcept final {
return 1;
}
private:
[[nodiscard]] number evaluate_output_impl(std::size_t, evaluation_context const& context) const final {
ASIC_DEBUG_MSG("Evaluating min.");
auto const lhs = this->evaluate_lhs(context);
if (lhs.imag() != 0) {
throw std::runtime_error{"Min does not support complex numbers."};
}
auto const rhs = this->evaluate_rhs(context);
if (rhs.imag() != 0) {
throw std::runtime_error{"Min does not support complex numbers."};
}
return std::min(lhs.real(), rhs.real());
}
};
class max_operation final : public binary_operation {
public:
explicit max_operation(result_key key)
: binary_operation(std::move(key)) {}
[[nodiscard]] std::size_t output_count() const noexcept final {
return 1;
}
private:
[[nodiscard]] number evaluate_output_impl(std::size_t, evaluation_context const& context) const final {
ASIC_DEBUG_MSG("Evaluating max.");
auto const lhs = this->evaluate_lhs(context);
if (lhs.imag() != 0) {
throw std::runtime_error{"Max does not support complex numbers."};
}
auto const rhs = this->evaluate_rhs(context);
if (rhs.imag() != 0) {
throw std::runtime_error{"Max does not support complex numbers."};
}
return std::max(lhs.real(), rhs.real());
}
};
class square_root_operation final : public unary_operation {
public:
explicit square_root_operation(result_key key)
: unary_operation(std::move(key)) {}
[[nodiscard]] std::size_t output_count() const noexcept final {
return 1;
}
private:
[[nodiscard]] number evaluate_output_impl(std::size_t, evaluation_context const& context) const final {
ASIC_DEBUG_MSG("Evaluating sqrt.");
return std::sqrt(this->evaluate_input(context));
}
};
class complex_conjugate_operation final : public unary_operation {
public:
explicit complex_conjugate_operation(result_key key)
: unary_operation(std::move(key)) {}
[[nodiscard]] std::size_t output_count() const noexcept final {
return 1;
}
private:
[[nodiscard]] number evaluate_output_impl(std::size_t, evaluation_context const& context) const final {
ASIC_DEBUG_MSG("Evaluating conj.");
return std::conj(this->evaluate_input(context));
}
};
class absolute_operation final : public unary_operation {
public:
explicit absolute_operation(result_key key)
: unary_operation(std::move(key)) {}
[[nodiscard]] std::size_t output_count() const noexcept final {
return 1;
}
private:
[[nodiscard]] number evaluate_output_impl(std::size_t, evaluation_context const& context) const final {
ASIC_DEBUG_MSG("Evaluating abs.");
return std::abs(this->evaluate_input(context));
}
};
class constant_multiplication_operation final : public unary_operation {
public:
constant_multiplication_operation(result_key key, number value)
: unary_operation(std::move(key))
, m_value(value) {}
[[nodiscard]] std::size_t output_count() const noexcept final {
return 1;
}
private:
[[nodiscard]] number evaluate_output_impl(std::size_t, evaluation_context const& context) const final {
ASIC_DEBUG_MSG("Evaluating cmul.");
return this->evaluate_input(context) * m_value;
}
number m_value;
};
class butterfly_operation final : public binary_operation {
public:
explicit butterfly_operation(result_key key)
: binary_operation(std::move(key)) {}
[[nodiscard]] std::size_t output_count() const noexcept final {
return 2;
}
private:
[[nodiscard]] number evaluate_output_impl(std::size_t index, evaluation_context const& context) const final {
ASIC_DEBUG_MSG("Evaluating bfly.");
if (index == 0) {
return this->evaluate_lhs(context) + this->evaluate_rhs(context);
}
return this->evaluate_lhs(context) - this->evaluate_rhs(context);
}
};
} // namespace asic
#endif // ASIC_SIMULATION_CORE_OPERATIONS_H