Blender V5.3
constexpr.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include <bit>
12#include <cassert>
13#include <cstdint>
14#include <vector>
15
16namespace bsl {
17
26 enum CompType {
27 BOOL = 0x00,
28 UINT = 0x10,
29 INT = 0x20,
30 FLOAT = 0x30,
31 };
32
52
53 bool is_array() const
54 {
55 return values.size() > comp_len();
56 }
57
58 bool is_scalar() const
59 {
60 return comp_len() == 1;
61 }
62
63 int comp_len() const
64 {
65 return type_ & 0x0F;
66 }
67
69 {
70 return CompType(type_ & 0xF0);
71 }
72
73 static uint32_t cast_value(CompType from, CompType to, uint32_t value)
74 {
75 if (from == to) {
76 return value;
77 }
78 if (to == BOOL) {
79 return (from == FLOAT) ? uint32_t(std::bit_cast<float>(value) != 0.0f) :
80 uint32_t(value != 0);
81 }
82 if (to == FLOAT) {
83 return std::bit_cast<uint32_t>((from == INT) ? float(std::bit_cast<int32_t>(value)) :
84 float(value));
85 }
86 if (from == FLOAT) {
87 const float f = std::bit_cast<float>(value);
88 return (to == UINT) ? uint32_t(int64_t(f)) : std::bit_cast<uint32_t>(int32_t(f));
89 }
90 return value;
91 }
92
93 void convert_or_broadcast(Type dst, std::vector<uint32_t> &r) const
94 {
95 int dst_len = dst & 0x0F;
96 CompType dst_comp = CompType(dst & 0xF0);
97 r.resize(dst_len);
98
99 if (is_array()) {
100 /* Invalid cast. */
101 return;
102 }
103
104 if (is_scalar()) {
105 /* Broadcast. */
106 r = std::vector<uint32_t>(dst_len, cast_value(comp_type(), dst_comp, values[0]));
107 return;
108 }
109
110 if (comp_len() == dst_len) {
111 /* Convert. */
112 for (int i = 0; i < dst_len; ++i) {
113 r[i] = cast_value(comp_type(), dst_comp, values[i]);
114 }
115 return;
116 }
117 /* Invalid cast. */
118 }
119
120 /* Returning bytes of the target type. */
121 uint32_t convert_scalar(CompType dst_comp) const
122 {
123 if (is_array()) {
124 /* Invalid cast. */
125 return 0;
126 }
127
128 if (is_scalar()) {
129 /* Convert. */
130 return cast_value(comp_type(), dst_comp, values[0]);
131 }
132 /* Invalid cast. Trying to cast a non scalar into a scalar. */
133 assert(0);
134 return 0;
135 }
136
137 std::vector<uint32_t> values = {0};
138
139 explicit ConstexprValue(int32_t s) : type_(INT1), values({uint32_t(s)}) {}
140 explicit ConstexprValue(uint32_t s) : type_(UINT1), values({s}) {}
141 explicit ConstexprValue(bool s) : type_(BOOL1), values({s}) {}
142 explicit ConstexprValue(float s) : type_(FLOAT1), values({std::bit_cast<uint32_t>(s)}) {}
143
144 explicit ConstexprValue(Type t, const ConstexprValue &x) : type_(t)
145 {
146 x.convert_or_broadcast(t, values);
147 }
148
150 : type_(t), values({x.convert_scalar(comp_type()), y.convert_scalar(comp_type())})
151 {
152 }
153
155 const ConstexprValue &x,
156 const ConstexprValue &y,
157 const ConstexprValue &z)
158 : type_(t),
159 values({x.convert_scalar(comp_type()),
160 y.convert_scalar(comp_type()),
161 z.convert_scalar(comp_type())})
162 {
163 }
164
166 const ConstexprValue &x,
167 const ConstexprValue &y,
168 const ConstexprValue &z,
169 const ConstexprValue &w)
170 : type_(t),
171 values({x.convert_scalar(comp_type()),
172 y.convert_scalar(comp_type()),
173 z.convert_scalar(comp_type()),
174 w.convert_scalar(comp_type())})
175 {
176 }
177
178 /* --- Unary Arithmetic & Bitwise Operators --- */
179
181 {
182 return *this;
183 }
184
186 {
187 return constexpr_op([](auto &&l, auto && /*r*/) { return -l; }, *this, *this);
188 }
189
191 {
192 return constexpr_op([](auto &&l, auto && /*r*/) { return ~int(l); }, *this, *this);
193 }
194
196 {
197 return constexpr_op([](auto &&l, auto && /*r*/) { return int(!int(l)); }, *this, *this);
198 }
199
200 // /* --- Pre/Post Increment & Decrement --- */
201
203 {
204 for (auto &v : values) {
205 ++v;
206 }
207 return *this;
208 }
210 {
211 for (auto &v : values) {
212 --v;
213 }
214 return *this;
215 }
216
218 {
219 ConstexprValue tmp = *this;
220 ++(*this);
221 return tmp;
222 }
224 {
225 ConstexprValue tmp = *this;
226 --(*this);
227 return tmp;
228 }
229
230 /* --- Compound Assignment Operators --- */
231
232#define COMPOUND_ASSIGN(op, ...) \
233 ConstexprValue operator op(const ConstexprValue &rhs) \
234 { \
235 *this = constexpr_op([](auto &&l, auto &&r) { return __VA_ARGS__; }, *this, rhs); \
236 return *this; \
237 }
238
239 COMPOUND_ASSIGN(+=, l + r)
240 COMPOUND_ASSIGN(-=, l - r)
241 COMPOUND_ASSIGN(*=, l *r)
242 COMPOUND_ASSIGN(/=, l / r)
243 /* Cast operands for int operations. Undefined operation are filtered upfront. */
250
251#undef COMPOUND_ASSIGN
252
253 const uint32_t &comp_raw(int i) const
254 {
255 return values[std::min(i, int(values.size() - 1))];
256 }
257 uint32_t &comp_raw(int i)
258 {
259 return values[std::min(i, int(values.size() - 1))];
260 }
261
262 template<typename T> T comp_as(int i) const
263 {
264 return std::bit_cast<T>(comp_raw(i));
265 }
266
267 template<typename T> void set_as(int i, T v)
268 {
269 comp_raw(i) = std::bit_cast<uint32_t>(v);
270 }
271
272 template<typename T> T comp_cast(int i) const
273 {
274 switch (comp_type()) {
275 case BOOL:
276 return static_cast<T>(bool(std::bit_cast<int32_t>(comp_raw(i))));
277 case UINT:
278 return static_cast<T>(std::bit_cast<uint32_t>(comp_raw(i)));
279 case INT:
280 return static_cast<T>(std::bit_cast<int32_t>(comp_raw(i)));
281 case FLOAT:
282 return static_cast<T>(std::bit_cast<float>(comp_raw(i)));
283 }
284 return 0;
285 }
286
287 template<typename Func>
288 static ConstexprValue constexpr_op(const Func &fn,
289 const ConstexprValue &lhs,
290 const ConstexprValue &rhs,
291 const bool is_bool_result = false,
292 const bool use_left_type = false)
293 {
294 const int dst_len = std::max(lhs.comp_len(), rhs.comp_len());
295 const int promoted_comp = std::max(lhs.comp_type(), rhs.comp_type());
296 const int dst_comp = is_bool_result ? BOOL : (use_left_type ? lhs.comp_type() : promoted_comp);
297 ConstexprValue r{0};
298 /* Type promotion. */
299 r.type_ = ConstexprValue::Type(dst_comp | dst_len);
300 r.values.resize(r.comp_len());
301
302 if (lhs.is_array() && rhs.is_array()) {
303 /* Error. */
304 return r;
305 }
306 if (lhs.comp_len() == rhs.comp_len() || lhs.is_scalar() || rhs.is_scalar()) {
307#define LOOP(A) \
308 for (int i = 0; i < dst_len; ++i) { \
309 A; \
310 }
311
312 switch (lhs.comp_type()) {
313 case BOOL:
314 switch (rhs.comp_type()) {
315 case BOOL:
316 LOOP(r.set_as(i, fn(lhs.comp_as<int32_t>(i), rhs.comp_as<int32_t>(i))));
317 break;
318 case UINT:
319 LOOP(r.set_as(i, fn(lhs.comp_as<int32_t>(i), rhs.comp_as<uint32_t>(i))));
320 break;
321 case INT:
322 LOOP(r.set_as(i, fn(lhs.comp_as<int32_t>(i), rhs.comp_as<int32_t>(i))));
323 break;
324 case FLOAT:
325 LOOP(r.set_as(i, fn(lhs.comp_as<int32_t>(i), rhs.comp_as<float>(i))));
326 break;
327 }
328 break;
329 case UINT:
330 switch (rhs.comp_type()) {
331 case BOOL:
332 LOOP(r.set_as(i, fn(lhs.comp_as<uint32_t>(i), rhs.comp_as<int32_t>(i))));
333 break;
334 case UINT:
335 LOOP(r.set_as(i, fn(lhs.comp_as<uint32_t>(i), rhs.comp_as<uint32_t>(i))));
336 break;
337 case INT:
338 LOOP(r.set_as(i, fn(lhs.comp_as<uint32_t>(i), rhs.comp_as<int32_t>(i))));
339 break;
340 case FLOAT:
341 LOOP(r.set_as(i, fn(lhs.comp_as<uint32_t>(i), rhs.comp_as<float>(i))));
342 break;
343 }
344 break;
345 case INT:
346 switch (rhs.comp_type()) {
347 case BOOL:
348 LOOP(r.set_as(i, fn(lhs.comp_as<int32_t>(i), rhs.comp_as<int32_t>(i))));
349 break;
350 case UINT:
351 LOOP(r.set_as(i, fn(lhs.comp_as<int32_t>(i), rhs.comp_as<uint32_t>(i))));
352 break;
353 case INT:
354 LOOP(r.set_as(i, fn(lhs.comp_as<int32_t>(i), rhs.comp_as<int32_t>(i))));
355 break;
356 case FLOAT:
357 LOOP(r.set_as(i, fn(lhs.comp_as<int32_t>(i), rhs.comp_as<float>(i))));
358 break;
359 }
360 break;
361 case FLOAT:
362 switch (rhs.comp_type()) {
363 case BOOL:
364 LOOP(r.set_as(i, fn(lhs.comp_as<float>(i), rhs.comp_as<int32_t>(i))));
365 break;
366 case UINT:
367 LOOP(r.set_as(i, fn(lhs.comp_as<float>(i), rhs.comp_as<uint32_t>(i))));
368 break;
369 case INT:
370 LOOP(r.set_as(i, fn(lhs.comp_as<float>(i), rhs.comp_as<int32_t>(i))));
371 break;
372 case FLOAT:
373 LOOP(r.set_as(i, fn(lhs.comp_as<float>(i), rhs.comp_as<float>(i))));
374 break;
375 }
376 break;
377 }
378
379#undef LOOP
380
381 return r;
382 }
383 /* Error */
384 return r;
385 }
386
387 template<typename T> static constexpr auto cast_to_int_if_not_uint(T val)
388 {
389 if constexpr (std::is_same_v<T, unsigned int>) {
390 return val;
391 }
392 else {
393 return static_cast<int>(val);
394 }
395 }
396};
397
398template<typename Func> auto apply_integral(Func func, const auto &lhs, const auto &rhs)
399{
400 using T = std::decay_t<decltype(lhs)>;
401 using U = std::decay_t<decltype(rhs)>;
402
403 if constexpr (std::is_integral_v<T> && std::is_integral_v<U>) {
404 return func(lhs, rhs);
405 }
406 else {
407 /* Invalid operands: operation require integral types. */
408 assert(0);
409 return 0;
410 }
411}
412
414{
415 return ConstexprValue::constexpr_op([](auto &&l, auto &&r) { return l + r; }, lhs, rhs);
416}
417
419{
420 return ConstexprValue::constexpr_op([](auto &&l, auto &&r) { return l - r; }, lhs, rhs);
421}
422
424{
425 return ConstexprValue::constexpr_op([](auto &&l, auto &&r) { return l * r; }, lhs, rhs);
426}
427
429{
430 return ConstexprValue::constexpr_op([](auto &&l, auto &&r) { return l / r; }, lhs, rhs);
431}
432
433/* Cast result to int to allow bit-casting the result. */
434
436{
438 [](auto &&l, auto &&r) { return int(l && r); }, lhs, rhs, true);
439}
440
442{
444 [](auto &&l, auto &&r) { return int(l || r); }, lhs, rhs, true);
445}
446
448{
450 [](auto &&l, auto &&r) { return int(l == r); }, lhs, rhs, true);
451}
452
454{
456 [](auto &&l, auto &&r) { return int(l != r); }, lhs, rhs, true);
457}
458
460{
462 [](auto &&l, auto &&r) { return int(l < r); }, lhs, rhs, true);
463}
464
466{
468 [](auto &&l, auto &&r) { return int(l <= r); }, lhs, rhs, true);
469}
470
472{
474 [](auto &&l, auto &&r) { return int(l > r); }, lhs, rhs, true);
475}
476
478{
480 [](auto &&l, auto &&r) { return int(l >= r); }, lhs, rhs, true);
481}
482
484{
486 [](auto &&l, auto &&r) {
487 return apply_integral([](auto &&l, auto &&r) { return l % r; }, l, r);
488 },
489 lhs,
490 rhs);
491}
492
494{
496 [](auto &&l, auto &&r) {
497 return apply_integral([](auto &&l, auto &&r) { return l & r; }, l, r);
498 },
499 lhs,
500 rhs);
501}
502
504{
506 [](auto &&l, auto &&r) {
507 return apply_integral([](auto &&l, auto &&r) { return l | r; }, l, r);
508 },
509 lhs,
510 rhs);
511}
512
514{
516 [](auto &&l, auto &&r) {
517 return apply_integral([](auto &&l, auto &&r) { return l ^ r; }, l, r);
518 },
519 lhs,
520 rhs);
521}
522
524{
526 [](auto &&l, auto &&r) {
527 return apply_integral([](auto &&l, auto &&r) { return l << r; }, l, r);
528 },
529 lhs,
530 rhs,
531 false,
532 true);
533}
534
536{
538 [](auto &&l, auto &&r) {
539 return apply_integral([](auto &&l, auto &&r) { return l >> r; }, l, r);
540 },
541 lhs,
542 rhs,
543 false,
544 true);
545}
546
547inline bool contains_zero(const ConstexprValue &v)
548{
549 for (auto val : v.values) {
550 if (val == 0) {
551 return true;
552 }
553 }
554 return false;
555}
556
557inline bool anyLessThan(const ConstexprValue &lhs, int rhs)
558{
559 for (int i = 0; i < int(lhs.values.size()); i++) {
560 if (lhs.comp_cast<int64_t>(i) < rhs) {
561 return true;
562 }
563 }
564 return false;
565}
566
567inline bool anyGreaterThanEqual(const ConstexprValue &lhs, int rhs)
568{
569 for (int i = 0; i < int(lhs.values.size()); i++) {
570 if (lhs.comp_cast<int64_t>(i) >= rhs) {
571 return true;
572 }
573 }
574 return false;
575}
576
577} // namespace bsl
#define U
long long int int64_t
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define COMPOUND_ASSIGN(op,...)
Definition constexpr.hh:232
#define LOOP(A)
#define T
Definition codegen.cc:18
bool anyLessThan(const ConstexprValue &lhs, int rhs)
Definition constexpr.hh:557
bool anyGreaterThanEqual(const ConstexprValue &lhs, int rhs)
Definition constexpr.hh:567
ConstexprValue operator-(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:418
ConstexprValue operator>(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:471
ConstexprValue operator^(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:513
ConstexprValue operator||(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:441
ConstexprValue operator>>(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:535
ConstexprValue operator*(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:423
ConstexprValue operator&(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:493
ConstexprValue operator|(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:503
ConstexprValue operator==(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:447
ConstexprValue operator&&(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:435
bool contains_zero(const ConstexprValue &v)
Definition constexpr.hh:547
ConstexprValue operator>=(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:477
ConstexprValue operator/(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:428
ConstexprValue operator<(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:459
ConstexprValue operator!=(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:453
ConstexprValue operator<=(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:465
ConstexprValue operator+(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:413
ConstexprValue operator<<(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:523
auto apply_integral(Func func, const auto &lhs, const auto &rhs)
Definition constexpr.hh:398
ConstexprValue operator%(const ConstexprValue &lhs, const ConstexprValue &rhs)
Definition constexpr.hh:483
ConstexprValue & operator++()
Definition constexpr.hh:202
ConstexprValue operator~() const
Definition constexpr.hh:190
ConstexprValue(Type t, const ConstexprValue &x, const ConstexprValue &y, const ConstexprValue &z)
Definition constexpr.hh:154
ConstexprValue operator-() const
Definition constexpr.hh:185
ConstexprValue(float s)
Definition constexpr.hh:142
ConstexprValue(Type t, const ConstexprValue &x, const ConstexprValue &y)
Definition constexpr.hh:149
ConstexprValue(int32_t s)
Definition constexpr.hh:139
int comp_len() const
Definition constexpr.hh:63
void convert_or_broadcast(Type dst, std::vector< uint32_t > &r) const
Definition constexpr.hh:93
ConstexprValue operator--(int)
Definition constexpr.hh:223
bool is_scalar() const
Definition constexpr.hh:58
ConstexprValue(Type t, const ConstexprValue &x, const ConstexprValue &y, const ConstexprValue &z, const ConstexprValue &w)
Definition constexpr.hh:165
CompType comp_type() const
Definition constexpr.hh:68
cast_to_int_if_not_uint(l) &cast_to_int_if_not_uint(r)) COMPOUND_ASSIGN(|
ConstexprValue(uint32_t s)
Definition constexpr.hh:140
ConstexprValue operator!() const
Definition constexpr.hh:195
static uint32_t cast_value(CompType from, CompType to, uint32_t value)
Definition constexpr.hh:73
COMPOUND_ASSIGN(%=, cast_to_int_if_not_uint(l) % cast_to_int_if_not_uint(r)) COMPOUND_ASSIGN(&
static constexpr auto cast_to_int_if_not_uint(T val)
Definition constexpr.hh:387
static ConstexprValue constexpr_op(const Func &fn, const ConstexprValue &lhs, const ConstexprValue &rhs, const bool is_bool_result=false, const bool use_left_type=false)
Definition constexpr.hh:288
uint32_t convert_scalar(CompType dst_comp) const
Definition constexpr.hh:121
ConstexprValue operator+() const
Definition constexpr.hh:180
enum bsl::ConstexprValue::Type type_
void set_as(int i, T v)
Definition constexpr.hh:267
ConstexprValue(Type t, const ConstexprValue &x)
Definition constexpr.hh:144
ConstexprValue & operator--()
Definition constexpr.hh:209
uint32_t & comp_raw(int i)
Definition constexpr.hh:257
bool is_array() const
Definition constexpr.hh:53
T comp_cast(int i) const
Definition constexpr.hh:272
T comp_as(int i) const
Definition constexpr.hh:262
std::vector< uint32_t > values
Definition constexpr.hh:137
ConstexprValue operator++(int)
Definition constexpr.hh:217