/proc/self/cwd/internal/number.h
Line | Count | Source |
1 | | // Copyright 2022 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef THIRD_PARTY_CEL_CPP_INTERNAL_NUMBER_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_INTERNAL_NUMBER_H_ |
17 | | |
18 | | #include <cstdint> |
19 | | #include <limits> |
20 | | |
21 | | #include "absl/types/variant.h" |
22 | | |
23 | | namespace cel::internal { |
24 | | |
25 | | constexpr int64_t kInt64Max = std::numeric_limits<int64_t>::max(); |
26 | | constexpr int64_t kInt64Min = std::numeric_limits<int64_t>::lowest(); |
27 | | constexpr uint64_t kUint64Max = std::numeric_limits<uint64_t>::max(); |
28 | | constexpr uint64_t kUintToIntMax = static_cast<uint64_t>(kInt64Max); |
29 | | constexpr double kDoubleToIntMax = static_cast<double>(kInt64Max); |
30 | | constexpr double kDoubleToIntMin = static_cast<double>(kInt64Min); |
31 | | constexpr double kDoubleToUintMax = static_cast<double>(kUint64Max); |
32 | | |
33 | | // The highest integer values that are round-trippable after rounding and |
34 | | // casting to double. |
35 | | template <typename T> |
36 | 0 | constexpr int RoundingError() { |
37 | 0 | return 1 << (std::numeric_limits<T>::digits - |
38 | 0 | std::numeric_limits<double>::digits - 1); |
39 | 0 | } Unexecuted instantiation: int cel::internal::RoundingError<long>() Unexecuted instantiation: int cel::internal::RoundingError<unsigned long>() |
40 | | |
41 | | constexpr double kMaxDoubleRepresentableAsInt = |
42 | | static_cast<double>(kInt64Max - RoundingError<int64_t>()); |
43 | | constexpr double kMaxDoubleRepresentableAsUint = |
44 | | static_cast<double>(kUint64Max - RoundingError<uint64_t>()); |
45 | | |
46 | | #define CEL_ABSL_VISIT_CONSTEXPR |
47 | | |
48 | | using NumberVariant = absl::variant<double, uint64_t, int64_t>; |
49 | | |
50 | | enum class ComparisonResult { |
51 | | kLesser, |
52 | | kEqual, |
53 | | kGreater, |
54 | | // Special case for nan. |
55 | | kNanInequal |
56 | | }; |
57 | | |
58 | | // Return the inverse relation (i.e. Invert(cmp(b, a)) is the same as cmp(a, b). |
59 | 65.1k | constexpr ComparisonResult Invert(ComparisonResult result) { |
60 | 65.1k | switch (result) { |
61 | 15.9k | case ComparisonResult::kLesser: |
62 | 15.9k | return ComparisonResult::kGreater; |
63 | 47.2k | case ComparisonResult::kGreater: |
64 | 47.2k | return ComparisonResult::kLesser; |
65 | 273 | case ComparisonResult::kEqual: |
66 | 273 | return ComparisonResult::kEqual; |
67 | 1.66k | case ComparisonResult::kNanInequal: |
68 | 1.66k | return ComparisonResult::kNanInequal; |
69 | 65.1k | } |
70 | 65.1k | } |
71 | | |
72 | | template <typename OutType> |
73 | | struct ConversionVisitor { |
74 | | template <typename InType> |
75 | 24.7k | constexpr OutType operator()(InType v) { |
76 | 24.7k | return static_cast<OutType>(v); |
77 | 24.7k | } long cel::internal::ConversionVisitor<long>::operator()<double>(double) Line | Count | Source | 75 | 5.57k | constexpr OutType operator()(InType v) { | 76 | 5.57k | return static_cast<OutType>(v); | 77 | 5.57k | } |
long cel::internal::ConversionVisitor<long>::operator()<unsigned long>(unsigned long) Line | Count | Source | 75 | 585 | constexpr OutType operator()(InType v) { | 76 | 585 | return static_cast<OutType>(v); | 77 | 585 | } |
long cel::internal::ConversionVisitor<long>::operator()<long>(long) Line | Count | Source | 75 | 7.82k | constexpr OutType operator()(InType v) { | 76 | 7.82k | return static_cast<OutType>(v); | 77 | 7.82k | } |
unsigned long cel::internal::ConversionVisitor<unsigned long>::operator()<double>(double) Line | Count | Source | 75 | 4.97k | constexpr OutType operator()(InType v) { | 76 | 4.97k | return static_cast<OutType>(v); | 77 | 4.97k | } |
unsigned long cel::internal::ConversionVisitor<unsigned long>::operator()<unsigned long>(unsigned long) Line | Count | Source | 75 | 398 | constexpr OutType operator()(InType v) { | 76 | 398 | return static_cast<OutType>(v); | 77 | 398 | } |
unsigned long cel::internal::ConversionVisitor<unsigned long>::operator()<long>(long) Line | Count | Source | 75 | 5.42k | constexpr OutType operator()(InType v) { | 76 | 5.42k | return static_cast<OutType>(v); | 77 | 5.42k | } |
Unexecuted instantiation: double cel::internal::ConversionVisitor<double>::operator()<double>(double) Unexecuted instantiation: double cel::internal::ConversionVisitor<double>::operator()<unsigned long>(unsigned long) Unexecuted instantiation: double cel::internal::ConversionVisitor<double>::operator()<long>(long) |
78 | | }; |
79 | | |
80 | | template <typename T> |
81 | 254k | constexpr ComparisonResult Compare(T a, T b) { |
82 | 254k | return (a > b) ? ComparisonResult::kGreater |
83 | 254k | : (a == b) ? ComparisonResult::kEqual |
84 | 122k | : ComparisonResult::kLesser; |
85 | 254k | } cel::internal::ComparisonResult cel::internal::Compare<double>(double, double) Line | Count | Source | 81 | 227k | constexpr ComparisonResult Compare(T a, T b) { | 82 | 227k | return (a > b) ? ComparisonResult::kGreater | 83 | 227k | : (a == b) ? ComparisonResult::kEqual | 84 | 106k | : ComparisonResult::kLesser; | 85 | 227k | } |
cel::internal::ComparisonResult cel::internal::Compare<unsigned long>(unsigned long, unsigned long) Line | Count | Source | 81 | 27.5k | constexpr ComparisonResult Compare(T a, T b) { | 82 | 27.5k | return (a > b) ? ComparisonResult::kGreater | 83 | 27.5k | : (a == b) ? ComparisonResult::kEqual | 84 | 15.6k | : ComparisonResult::kLesser; | 85 | 27.5k | } |
Unexecuted instantiation: cel::internal::ComparisonResult cel::internal::Compare<long>(long, long) |
86 | | |
87 | 230k | constexpr ComparisonResult DoubleCompare(double a, double b) { |
88 | | // constexpr friendly isnan check. |
89 | 230k | if (!(a == a) || !(b == b)) { |
90 | 2.90k | return ComparisonResult::kNanInequal; |
91 | 2.90k | } |
92 | 227k | return Compare(a, b); |
93 | 230k | } |
94 | | |
95 | | // Implement generic numeric comparison against double value. |
96 | | struct DoubleCompareVisitor { |
97 | 283k | constexpr explicit DoubleCompareVisitor(double v) : v(v) {} |
98 | | |
99 | 0 | constexpr ComparisonResult operator()(double other) const { |
100 | 0 | return DoubleCompare(v, other); |
101 | 0 | } |
102 | | |
103 | 236k | constexpr ComparisonResult operator()(uint64_t other) const { |
104 | 236k | if (v > kDoubleToUintMax) { |
105 | 32.2k | return ComparisonResult::kGreater; |
106 | 203k | } else if (v < 0) { |
107 | 9.14k | return ComparisonResult::kLesser; |
108 | 194k | } else { |
109 | 194k | return DoubleCompare(v, static_cast<double>(other)); |
110 | 194k | } |
111 | 236k | } |
112 | | |
113 | 47.5k | constexpr ComparisonResult operator()(int64_t other) const { |
114 | 47.5k | if (v > kDoubleToIntMax) { |
115 | 9.55k | return ComparisonResult::kGreater; |
116 | 38.0k | } else if (v < kDoubleToIntMin) { |
117 | 2.74k | return ComparisonResult::kLesser; |
118 | 35.2k | } else { |
119 | 35.2k | return DoubleCompare(v, static_cast<double>(other)); |
120 | 35.2k | } |
121 | 47.5k | } |
122 | | double v; |
123 | | }; |
124 | | |
125 | | // Implement generic numeric comparison against uint value. |
126 | | // Delegates to double comparison if either variable is double. |
127 | | struct UintCompareVisitor { |
128 | 76.0k | constexpr explicit UintCompareVisitor(uint64_t v) : v(v) {} |
129 | | |
130 | 43.4k | constexpr ComparisonResult operator()(double other) const { |
131 | 43.4k | return Invert(DoubleCompareVisitor(other)(v)); |
132 | 43.4k | } |
133 | | |
134 | 0 | constexpr ComparisonResult operator()(uint64_t other) const { |
135 | 0 | return Compare(v, other); |
136 | 0 | } |
137 | | |
138 | 32.5k | constexpr ComparisonResult operator()(int64_t other) const { |
139 | 32.5k | if (v > kUintToIntMax || other < 0) { |
140 | 4.98k | return ComparisonResult::kGreater; |
141 | 27.5k | } else { |
142 | 27.5k | return Compare(v, static_cast<uint64_t>(other)); |
143 | 27.5k | } |
144 | 32.5k | } |
145 | | uint64_t v; |
146 | | }; |
147 | | |
148 | | // Implement generic numeric comparison against int value. |
149 | | // Delegates to uint / double if either value is uint / double. |
150 | | struct IntCompareVisitor { |
151 | 21.6k | constexpr explicit IntCompareVisitor(int64_t v) : v(v) {} |
152 | | |
153 | 9.05k | constexpr ComparisonResult operator()(double other) { |
154 | 9.05k | return Invert(DoubleCompareVisitor(other)(v)); |
155 | 9.05k | } |
156 | | |
157 | 12.6k | constexpr ComparisonResult operator()(uint64_t other) { |
158 | 12.6k | return Invert(UintCompareVisitor(other)(v)); |
159 | 12.6k | } |
160 | | |
161 | 0 | constexpr ComparisonResult operator()(int64_t other) { |
162 | 0 | return Compare(v, other); |
163 | 0 | } |
164 | | int64_t v; |
165 | | }; |
166 | | |
167 | | struct CompareVisitor { |
168 | 316k | explicit constexpr CompareVisitor(NumberVariant rhs) : rhs(rhs) {} |
169 | | |
170 | 231k | CEL_ABSL_VISIT_CONSTEXPR ComparisonResult operator()(double v) { |
171 | 231k | return absl::visit(DoubleCompareVisitor(v), rhs); |
172 | 231k | } |
173 | | |
174 | 63.3k | CEL_ABSL_VISIT_CONSTEXPR ComparisonResult operator()(uint64_t v) { |
175 | 63.3k | return absl::visit(UintCompareVisitor(v), rhs); |
176 | 63.3k | } |
177 | | |
178 | 21.6k | CEL_ABSL_VISIT_CONSTEXPR ComparisonResult operator()(int64_t v) { |
179 | 21.6k | return absl::visit(IntCompareVisitor(v), rhs); |
180 | 21.6k | } |
181 | | NumberVariant rhs; |
182 | | }; |
183 | | |
184 | | struct LosslessConvertibleToIntVisitor { |
185 | 18.3k | constexpr bool operator()(double value) const { |
186 | 18.3k | return value >= kDoubleToIntMin && value <= kMaxDoubleRepresentableAsInt && |
187 | 15.8k | value == static_cast<double>(static_cast<int64_t>(value)); |
188 | 18.3k | } |
189 | 626 | constexpr bool operator()(uint64_t value) const { |
190 | 626 | return value <= kUintToIntMax; |
191 | 626 | } |
192 | 7.82k | constexpr bool operator()(int64_t value) const { return true; } |
193 | | }; |
194 | | |
195 | | struct LosslessConvertibleToUintVisitor { |
196 | 18.0k | constexpr bool operator()(double value) const { |
197 | 18.0k | return value >= 0 && value <= kMaxDoubleRepresentableAsUint && |
198 | 10.3k | value == static_cast<double>(static_cast<uint64_t>(value)); |
199 | 18.0k | } |
200 | 398 | constexpr bool operator()(uint64_t value) const { return true; } |
201 | 9.51k | constexpr bool operator()(int64_t value) const { return value >= 0; } |
202 | | }; |
203 | | |
204 | | // Utility class for CEL number operations. |
205 | | // |
206 | | // In CEL expressions, comparisons between different numeric types are treated |
207 | | // as all happening on the same continuous number line. This generally means |
208 | | // that integers and doubles in convertible range are compared after converting |
209 | | // to doubles (tolerating some loss of precision). |
210 | | // |
211 | | // This extends to key lookups -- {1: 'abc'}[1.0f] is expected to work since |
212 | | // 1.0 == 1 in CEL. |
213 | | class Number { |
214 | | public: |
215 | | // Factories to resolve ambiguous overload resolution against literals. |
216 | 209k | static constexpr Number FromInt64(int64_t value) { return Number(value); } |
217 | 109k | static constexpr Number FromUint64(uint64_t value) { return Number(value); } |
218 | 43.0k | static constexpr Number FromDouble(double value) { return Number(value); } |
219 | | |
220 | 309k | constexpr explicit Number(double double_value) : value_(double_value) {} |
221 | 271k | constexpr explicit Number(int64_t int_value) : value_(int_value) {} |
222 | 362k | constexpr explicit Number(uint64_t uint_value) : value_(uint_value) {} |
223 | | |
224 | | // Return a double representation of the value. |
225 | 0 | CEL_ABSL_VISIT_CONSTEXPR double AsDouble() const { |
226 | 0 | return absl::visit(internal::ConversionVisitor<double>(), value_); |
227 | 0 | } |
228 | | |
229 | | // Return signed int64 representation for the value. |
230 | | // Caller must guarantee the underlying value is representatble as an |
231 | | // int. |
232 | 13.9k | CEL_ABSL_VISIT_CONSTEXPR int64_t AsInt() const { |
233 | 13.9k | return absl::visit(internal::ConversionVisitor<int64_t>(), value_); |
234 | 13.9k | } |
235 | | |
236 | | // Return unsigned int64 representation for the value. |
237 | | // Caller must guarantee the underlying value is representable as an |
238 | | // uint. |
239 | 10.7k | CEL_ABSL_VISIT_CONSTEXPR uint64_t AsUint() const { |
240 | 10.7k | return absl::visit(internal::ConversionVisitor<uint64_t>(), value_); |
241 | 10.7k | } |
242 | | |
243 | | // For key lookups, check if the conversion to signed int is lossless. |
244 | 26.7k | CEL_ABSL_VISIT_CONSTEXPR bool LosslessConvertibleToInt() const { |
245 | 26.7k | return absl::visit(internal::LosslessConvertibleToIntVisitor(), value_); |
246 | 26.7k | } |
247 | | |
248 | | // For key lookups, check if the conversion to unsigned int is lossless. |
249 | 27.9k | CEL_ABSL_VISIT_CONSTEXPR bool LosslessConvertibleToUint() const { |
250 | 27.9k | return absl::visit(internal::LosslessConvertibleToUintVisitor(), value_); |
251 | 27.9k | } |
252 | | |
253 | 203k | CEL_ABSL_VISIT_CONSTEXPR bool operator<(Number other) const { |
254 | 203k | return Compare(other) == internal::ComparisonResult::kLesser; |
255 | 203k | } |
256 | | |
257 | 10.8k | CEL_ABSL_VISIT_CONSTEXPR bool operator<=(Number other) const { |
258 | 10.8k | internal::ComparisonResult cmp = Compare(other); |
259 | 10.8k | return cmp != internal::ComparisonResult::kGreater && |
260 | 5.79k | cmp != internal::ComparisonResult::kNanInequal; |
261 | 10.8k | } |
262 | | |
263 | 17.5k | CEL_ABSL_VISIT_CONSTEXPR bool operator>(Number other) const { |
264 | 17.5k | return Compare(other) == internal::ComparisonResult::kGreater; |
265 | 17.5k | } |
266 | | |
267 | 59.5k | CEL_ABSL_VISIT_CONSTEXPR bool operator>=(Number other) const { |
268 | 59.5k | internal::ComparisonResult cmp = Compare(other); |
269 | 59.5k | return cmp != internal::ComparisonResult::kLesser && |
270 | 11.9k | cmp != internal::ComparisonResult::kNanInequal; |
271 | 59.5k | } |
272 | | |
273 | 25.3k | CEL_ABSL_VISIT_CONSTEXPR bool operator==(Number other) const { |
274 | 25.3k | return Compare(other) == internal::ComparisonResult::kEqual; |
275 | 25.3k | } |
276 | | |
277 | 0 | CEL_ABSL_VISIT_CONSTEXPR bool operator!=(Number other) const { |
278 | 0 | return Compare(other) != internal::ComparisonResult::kEqual; |
279 | 0 | } |
280 | | |
281 | | // Visit the underlying number representation, a variant of double, uint64_t, |
282 | | // or int64_t. |
283 | | template <typename T, typename Op> |
284 | | T visit(Op&& op) const { |
285 | | return absl::visit(std::forward<Op>(op), value_); |
286 | | } |
287 | | |
288 | | private: |
289 | | internal::NumberVariant value_; |
290 | | |
291 | | CEL_ABSL_VISIT_CONSTEXPR internal::ComparisonResult Compare( |
292 | 316k | Number other) const { |
293 | 316k | return absl::visit(internal::CompareVisitor(other.value_), value_); |
294 | 316k | } |
295 | | }; |
296 | | |
297 | | } // namespace cel::internal |
298 | | |
299 | | #endif // THIRD_PARTY_CEL_CPP_INTERNAL_NUMBER_H_ |