forked from daphne-project/daphne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryOpCode.h
More file actions
215 lines (199 loc) · 11.3 KB
/
Copy pathBinaryOpCode.h
File metadata and controls
215 lines (199 loc) · 11.3 KB
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
/*
* Copyright 2021 The DAPHNE Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <runtime/local/datastructures/FixedSizeStringValueType.h>
#include <cstdint>
#include <string>
#include <string_view>
// ****************************************************************************
// Enum for binary op codes and their names
// ****************************************************************************
enum class BinaryOpCode {
// Arithmetic.
ADD, // addition
SUB, // subtraction
MUL, // multiplication
DIV, // division
POW, // to the power of
MOD, // modulus
LOG, // logarithm
// Comparisons.
EQ, // equal
NEQ, // not equal
LT, // less than
LE, // less equal
GT, // greater than
GE, // greater equal
// Min/max.
MIN,
MAX,
// Logical.
AND,
OR,
// Bitwise.
BITWISE_AND,
// Strings.
CONCAT
};
/**
* @brief Array of the "names" of the `BinaryOpCode`s.
*
* Must contain the same elements as `BinaryOpCode` in the same order,
* such that we can obtain the name corresponding to a `BinaryOpCode` `opCode`
* by `binary_op_codes[static_cast<int>(opCode)]`.
*/
static std::string_view binary_op_codes[] = {
// Arithmetic.
"ADD", "SUB", "MUL", "DIV", "POW", "MOD", "LOG",
// Comparisons.
"EQ", "NEQ", "LT", "LE", "GT", "GE",
// Min/max.
"MIN", "MAX",
// Logical.
"AND", "OR",
// Bitwise.
"BITWISE_AND",
// Strings.
"CONCAT"};
// ****************************************************************************
// Specification which binary ops should be supported on which value types
// ****************************************************************************
/**
* @brief Template constant specifying if the given binary operation
* should be supported on arguments of the given value types.
*
* @tparam VTRes The result value type.
* @tparam VTLhs The left-hand-side argument value type.
* @tparam VTRhs The right-hand-side argument value type.
* @tparam op The binary operation.
*/
template <BinaryOpCode op, typename VTRes, typename VTLhs, typename VTRhs>
static constexpr bool supportsBinaryOp = false;
// Macros for concisely specifying which binary operations should be
// supported on which value types.
// Generates code specifying that the binary operation `Op` should be supported
// on the value type `VT` (for the result and the two arguments, for
// simplicity).
#define SUPPORT(Op, VT) template <> constexpr bool supportsBinaryOp<BinaryOpCode::Op, VT, VT, VT> = true;
// Generates code specifying that the binary operation `Op` should be supported on
// the value types `VTLhs` and `VTRhs` with result `VTRes`.
#define SUPPORT_RLR(Op, VTRes, VTLhs, VTRhs) \
template <> constexpr bool supportsBinaryOp<BinaryOpCode::Op, VTRes, VTLhs, VTRhs> = true;
// Generates code specifying that all binary operations of a certain category
// should be supported on the given value type `VT` (for the result and the two
// arguments, for simplicity).
#define SUPPORT_ARITHMETIC(VT) \
/* Arithmetic. */ \
SUPPORT(ADD, VT) \
SUPPORT(SUB, VT) \
SUPPORT(MUL, VT) \
SUPPORT(DIV, VT) \
SUPPORT(POW, VT) \
SUPPORT(MOD, VT) \
SUPPORT(LOG, VT)
#define SUPPORT_EQUALITY(VT) \
/* Comparisons. */ \
SUPPORT(EQ, VT) \
SUPPORT(NEQ, VT)
#define SUPPORT_COMPARISONS(VT) \
/* Comparisons. */ \
SUPPORT(LT, VT) \
SUPPORT(LE, VT) \
SUPPORT(GT, VT) \
SUPPORT(GE, VT) \
/* Min/max. */ \
SUPPORT(MIN, VT) \
SUPPORT(MAX, VT)
#define SUPPORT_LOGICAL(VT) \
/* Logical. */ \
SUPPORT(AND, VT) \
SUPPORT(OR, VT)
#define SUPPORT_BITWISE(VT) \
/* Bitwise. */ \
SUPPORT(BITWISE_AND, VT)
// Generates code specifying that all binary operations of a certain category should be
// supported on the given argument value types `VTLhs` and `VTRhs` (for the left and right-hand-side
// arguments, respectively) and the given result value type `VTRes`.
#define SUPPORT_COMPARISONS_RLR(VTRes, VTLhs, VTRhs) \
/* string Comparisons operations. */ \
SUPPORT_RLR(LT, VTRes, VTLhs, VTRhs) \
SUPPORT_RLR(GT, VTRes, VTLhs, VTRhs)
#define SUPPORT_COMPARISONS_EQUAL_RLR(VTRes, VTLhs, VTRhs) \
/* string Comparisons operations. */ \
SUPPORT_RLR(LE, VTRes, VTLhs, VTRhs) \
SUPPORT_RLR(GE, VTRes, VTLhs, VTRhs)
#define SUPPORT_EQUALITY_RLR(VTRes, VTLhs, VTRhs) \
/* string Comparisons operations. */ \
SUPPORT_RLR(EQ, VTRes, VTLhs, VTRhs) \
SUPPORT_RLR(NEQ, VTRes, VTLhs, VTRhs)
#define SUPPORT_STRING_RLR(VTRes, VTLhs, VTRhs) \
/* string concatenation operations. */ \
/* Since the result may not fit in FixedStr16,*/ \
/* it always return std::string*/ \
SUPPORT_RLR(CONCAT, VTRes, VTLhs, VTRhs)
// Generates code specifying that all binary operations typically supported on a
// certain category of value types should be supported on the given value type
// `VT` (for the result and the two arguments, for simplicity).
#define SUPPORT_NUMERIC_FP(VT) \
SUPPORT_ARITHMETIC(VT) \
SUPPORT_EQUALITY(VT) \
SUPPORT_COMPARISONS(VT) \
SUPPORT_LOGICAL(VT)
#define SUPPORT_NUMERIC_INT(VT) \
SUPPORT_ARITHMETIC(VT) \
SUPPORT_EQUALITY(VT) \
SUPPORT_COMPARISONS(VT) \
SUPPORT_LOGICAL(VT) \
SUPPORT_BITWISE(VT)
// Concise specification of which binary operations should be supported on
// which value types.
SUPPORT_EQUALITY(bool)
SUPPORT_NUMERIC_FP(double)
SUPPORT_NUMERIC_FP(float)
SUPPORT_NUMERIC_INT(int64_t)
SUPPORT_NUMERIC_INT(int32_t)
SUPPORT_NUMERIC_INT(int8_t)
SUPPORT_NUMERIC_INT(uint64_t)
SUPPORT_NUMERIC_INT(uint32_t)
SUPPORT_NUMERIC_INT(uint8_t)
// Strings binary operations.
SUPPORT_EQUALITY_RLR(int64_t, std::string, std::string)
SUPPORT_EQUALITY_RLR(int64_t, FixedStr16, FixedStr16)
SUPPORT_EQUALITY_RLR(int64_t, const char *, const char *)
SUPPORT_EQUALITY_RLR(int64_t, std::string, const char *)
SUPPORT_COMPARISONS_RLR(int64_t, std::string, std::string)
SUPPORT_COMPARISONS_RLR(int64_t, FixedStr16, FixedStr16)
SUPPORT_COMPARISONS_RLR(int64_t, std::string, const char *)
SUPPORT_COMPARISONS_EQUAL_RLR(int64_t, std::string, std::string)
SUPPORT_COMPARISONS_EQUAL_RLR(int64_t, std::string, const char *)
SUPPORT_STRING_RLR(std::string, std::string, std::string)
SUPPORT_STRING_RLR(std::string, FixedStr16, FixedStr16)
SUPPORT_STRING_RLR(const char *, const char *, const char *)
SUPPORT_STRING_RLR(std::string, std::string, const char *)
// Undefine helper macros.
#undef SUPPORT
#undef SUPPORT_RLR
#undef SUPPORT_ARITHMETIC
#undef SUPPORT_EQUALITY
#undef SUPPORT_COMPARISONS
#undef SUPPORT_LOGICAL
#undef SUPPORT_BITWISE
#undef SUPPORT_NUMERIC_FP
#undef SUPPORT_NUMERIC_INT
#undef SUPPORT_EQUALITY_RLR
#undef SUPPORT_COMPARISONS_RLR
#undef SUPPORT_COMPARISONS_EQUAL_RLR
#undef SUPPORT_STRING_RLR