forked from kittybupu/openVCB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenVCB_Helpers.hh
More file actions
448 lines (362 loc) · 13.3 KB
/
Copy pathopenVCB_Helpers.hh
File metadata and controls
448 lines (362 loc) · 13.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#pragma once
#ifndef G1Gjjm08JMgUZALRqEUy6y5CngZGIr6L5gKrj321mPkr5Jds
#define G1Gjjm08JMgUZALRqEUy6y5CngZGIr6L5gKrj321mPkr5Jds
#include "openVCB.h"
#if defined _MSC_VER
# if !(defined __GNUC__ || defined __clang__ || defined __INTEL_COMPILER || defined __INTEL_LLVM_COMPILER)
# pragma warning(disable: 5030) // Unrecognized attribute
#endif
#endif
namespace openVCB {
/****************************************************************************************/
/*
* Hideous boilerplate garbage to make using `enum class` objects less infuriatingly
* tedious and "cast-tastic". This nonsense takes up less space on the page with the
* long lines than if properly formatted, which is a win in my book.
*/
template <typename T> concept Integral = std::is_integral<T>::value;
ND OVCB_CONSTEXPR Logic operator>>(Logic val, uint n) { return static_cast<Logic>(static_cast<uint>(val) >> n); }
ND OVCB_CONSTEXPR Logic operator<<(Logic val, uint n) { return static_cast<Logic>(static_cast<uint>(val) << n); }
ND OVCB_CONSTEXPR Logic operator& (Logic val1, uint val2) { return static_cast<Logic>(static_cast<uint>(val1) & val2); }
ND OVCB_CONSTEXPR Logic operator| (Logic val1, uint val2) { return static_cast<Logic>(static_cast<uint>(val1) | val2); }
ND OVCB_CONSTEXPR Logic operator& (Logic val1, Logic val2) { return static_cast<Logic>(static_cast<uint>(val1) & static_cast<uint>(val2)); }
ND OVCB_CONSTEXPR Logic operator| (Logic val1, Logic val2) { return static_cast<Logic>(static_cast<uint>(val1) | static_cast<uint>(val2)); }
ND OVCB_CONSTEXPR Logic operator~ (Logic val) { return static_cast<Logic>(~static_cast<uint>(val)); }
template <Integral T>
ND OVCB_CONSTEXPR bool operator==(Logic op1, T op2)
{
return op1 == static_cast<Logic>(op2);
}
ND OVCB_CONSTEXPR Ink operator>>(Ink val, uint n) { return static_cast<Ink>(static_cast<uint>(val) >> n); }
ND OVCB_CONSTEXPR Ink operator<<(Ink val, uint n) { return static_cast<Ink>(static_cast<uint>(val) << n); }
ND OVCB_CONSTEXPR Ink operator& (Ink val1, uint val2) { return static_cast<Ink>(static_cast<uint>(val1) & val2); }
ND OVCB_CONSTEXPR Ink operator| (Ink val1, uint val2) { return static_cast<Ink>(static_cast<uint>(val1) | val2); }
ND OVCB_CONSTEXPR Ink operator& (Ink val1, Ink val2) { return static_cast<Ink>(static_cast<uint>(val1) & static_cast<uint>(val2)); }
ND OVCB_CONSTEXPR Ink operator| (Ink val1, Ink val2) { return static_cast<Ink>(static_cast<uint>(val1) | static_cast<uint>(val2)); }
ND OVCB_CONSTEXPR int operator+ (Ink val1, Ink val2) { return static_cast<int>(val1) + static_cast<int>(val2); }
ND OVCB_CONSTEXPR int operator- (Ink val1, Ink val2) { return static_cast<int>(val1) - static_cast<int>(val2); }
ND OVCB_CONSTEXPR int operator+ (Ink val1, int val2) { return static_cast<int>(val1) + val2; }
ND OVCB_CONSTEXPR int operator- (Ink val1, int val2) { return static_cast<int>(val1) - val2; }
ND OVCB_CONSTEXPR Ink operator~ (Ink val) { return static_cast<Ink>(~static_cast<uint>(val)); }
template <Integral T>
ND OVCB_CONSTEXPR bool operator==(Ink op1, T op2)
{
return op1 == static_cast<Ink>(op2);
}
ND OVCB_CONSTEXPR Ink operator|(Ink val1, Logic val2) { return static_cast<Ink> (static_cast<uint>(val1) | static_cast<uint>(val2)); }
ND OVCB_CONSTEXPR Ink operator&(Ink val1, Logic val2) { return static_cast<Ink> (static_cast<uint>(val1) & static_cast<uint>(val2)); }
ND OVCB_CONSTEXPR Logic operator|(Logic val1, Ink val2) { return static_cast<Logic>(static_cast<uint>(val1) | static_cast<uint>(val2)); }
ND OVCB_CONSTEXPR Logic operator&(Logic val1, Ink val2) { return static_cast<Logic>(static_cast<uint>(val1) & static_cast<uint>(val2)); }
constexpr bool operator==(InkPixel const &first, InkPixel const &second) noexcept
{
return first.ink == second.ink && first.meta == second.meta;
}
/*--------------------------------------------------------------------------------------*/
/* Helper routines for Logic objects. */
/**
* \brief Sets the ink (logic) type to be as specified by state.
* \param logic The Ink (logic) value to modify.
* \param state Should be 0 to turn off, 1 to turn on.
* \return The modified value.
*/
ND OVCB_CONSTEXPR Logic SetOn(Logic logic, unsigned state)
{
return (logic & ~Logic::_logicOn) | (state << 7);
}
// Sets the ink type to be on
ND OVCB_CONSTEXPR Logic SetOn(Logic logic)
{
return logic | Logic::_logicOn;
}
// Sets the ink type to be off
ND OVCB_CONSTEXPR Logic SetOff(Logic logic)
{
return logic & ~Logic::_logicOn;
}
// Gets the ink active state
ND OVCB_CONSTEXPR bool IsOn(Logic logic)
{
return static_cast<bool>(logic & Logic::_logicOn);
}
/*--------------------------------------------------------------------------------------*/
/* Helper routines for Ink objects. */
/**
* \brief Sets the ink type to be as specified by state.
* \param ink The Ink value to modify.
* \param state Should be 0 to turn off, 1 to turn on.
* \return The modified value.
*/
ND OVCB_CONSTEXPR Ink SetOn(Ink ink, unsigned state)
{
return (ink & ~Ink::_inkOn) | (state << 7);
}
// Sets the ink type to be on.
ND OVCB_CONSTEXPR Ink SetOn(Ink ink)
{
return ink | Ink::_inkOn;
}
// Sets the ink type to be off
ND OVCB_CONSTEXPR Ink SetOff(Ink ink)
{
return ink & ~Ink::_inkOn;
}
// Gets the ink active state
ND OVCB_CONSTEXPR bool IsOn(Ink ink)
{
return static_cast<bool>(ink & Ink::_inkOn);
}
// Gets the logic type of said ink
// Define the logics of each ink here.
inline Logic
inkLogicType(Ink ink)
{
ink = SetOff(ink);
switch (ink) { // NOLINT(clang-diagnostic-switch-enum)
case Ink::Latch: return Logic::Latch;
case Ink::Clock: return Logic::Clock;
case Ink::Random: return Logic::Random;
case Ink::Timer: return Logic::Timer;
case Ink::Breakpoint: return Logic::Breakpoint;
case Ink::Xor:
return Logic::Xor;
case Ink::Xnor:
return Logic::Xnor;
case Ink::Not:
case Ink::Nor:
case Ink::And:
return Logic::Zero;
case Ink::Nand:
case Ink::Or:
case Ink::Buffer:
case Ink::Trace:
default:
return Logic::NonZero;
}
}
// Gets the string name of the ink
extern char const *getInkString(Ink ink);
/*--------------------------------------------------------------------------------------*/
/**
* \brief Stores the vmem array. This union makes the byte-addressed variant of openVCB
* simpler to implement and should have no runtime impact at all.
* @note This level of obsessive nonsense is excessive even for me.
*/
union VMemWrapper
{
uint32_t *i;
uint8_t *b;
#ifdef OVCB_BYTE_ORIENTED_VMEM
# define DEF_POINTER b
using value_type = uint8_t;
#else
# define DEF_POINTER i
using value_type = uint32_t;
#endif
//---------------------------------------------------------------------------------
VMemWrapper() noexcept = default;
explicit VMemWrapper(uint32_t *ptr) noexcept
: i(ptr)
{}
explicit VMemWrapper(uint8_t *ptr) noexcept
: b(ptr)
{}
// ReSharper disable once CppNonExplicitConvertingConstructor
VMemWrapper(std::nullptr_t) noexcept
: DEF_POINTER(nullptr)
{}
//---------------------------------------------------------------------------------
// Automatically use the default member when indexing.
ND auto const &operator[](size_t idx) const noexcept { return DEF_POINTER[idx]; }
ND auto &operator[](size_t idx) noexcept { return DEF_POINTER[idx]; }
ND auto &def() noexcept { return DEF_POINTER; }
ND auto const &def() const noexcept { return DEF_POINTER; }
// Assign pointers to the default union member.
VMemWrapper &operator=(value_type *ptr) noexcept
{
DEF_POINTER = ptr;
return *this;
}
// Allow assigning nullptr directly.
VMemWrapper &operator=(std::nullptr_t) noexcept
{
DEF_POINTER = nullptr;
return *this;
}
// Allow comparing with nullptr directly.
ND bool constexpr operator==(std::nullptr_t) const noexcept
{
return DEF_POINTER == nullptr;
}
// Allow checking whether the pointer null by placing it in a boolean
// context just as if this were a bare pointer.
ND explicit constexpr operator bool() const noexcept
{
return DEF_POINTER != nullptr;
}
ND uint32_t *word_at_byte(size_t offset) const noexcept
{
return reinterpret_cast<uint32_t *>(b + offset);
}
};
#undef DEF_POINTER
/*--------------------------------------------------------------------------------------*/
class StringArray final
{
char **array_;
uint32_t capacity_;
uint32_t qty_ = 0;
static constexpr size_t default_capacity = 32;
public:
explicit StringArray(uint32_t capacity = default_capacity)
: array_(new char *[capacity]),
capacity_(capacity)
{}
~StringArray()
{
if (array_) {
for (unsigned i = 0; i < qty_; ++i) {
delete[] array_[i];
array_[i] = nullptr;
}
delete[] array_;
capacity_ = qty_ = 0;
array_ = nullptr;
}
}
StringArray(StringArray const &) = delete;
StringArray(StringArray &&) noexcept = delete;
StringArray &operator=(StringArray const &) = delete;
StringArray &operator=(StringArray &&) noexcept = delete;
ND char *push_blank(size_t len)
{
if (qty_ + 1 == capacity_) {
capacity_ += default_capacity;
auto **tmp = new char *[capacity_];
memmove(tmp, array_, qty_ * sizeof(char *));
delete[] array_;
array_ = tmp;
}
auto *str = new char[len + 1];
array_[qty_++] = str;
return str;
}
void push(char const *orig, size_t len)
{
char *str = push_blank(len);
memcpy(str, orig, len + 1);
}
void push(char const *orig) { push(orig, strlen(orig)); }
void push(std::string const &orig) { push(orig.data(), orig.size()); }
void push(std::string_view const &orig) { push(orig.data(), orig.size()); }
template <size_t N>
void push(char const (&str)[N])
{
push(str, N);
}
ND bool empty() const { return qty_ == 0; }
ND uint32_t size() const { return qty_; }
ND uint32_t capacity() const { return capacity_; }
ND char const *const *const &data() const &
{
return array_;
}
ND char **&data() &
{
return array_;
}
};
/*--------------------------------------------------------------------------------------*/
class RandomBitProvider
{
using random_type = std::minstd_rand;
public:
RandomBitProvider()
: RandomBitProvider(rnd_device_())
{}
explicit RandomBitProvider(uint32_t seed)
: random_engine_(seed),
current_(random_engine_())
{}
ND unsigned operator()()
{
if (avail_ > 0) {
--avail_;
} else {
avail_ = num_bits - 1U;
current_ = random_engine_();
}
unsigned ret = current_ & 1U;
current_ >>= 1;
return ret;
}
private:
static constexpr int num_bits = sizeof(uint32_t) * CHAR_BIT;
inline static std::random_device rnd_device_{};
random_type random_engine_;
uint32_t current_;
int avail_ = num_bits;
};
/*--------------------------------------------------------------------------------------*/
class ClockCounter final
{
public:
ClockCounter()
: periods_({1, 1})
{}
explicit ClockCounter(uint16_t period)
: periods_({period, period})
{}
explicit ClockCounter(uint16_t low, uint16_t high)
: periods_({low, high})
{}
bool tick()
{
if (++counter_ >= periods_[state_]) {
state_ = state_ ? 0 : 1;
counter_ = 0;
return true;
}
return false;
}
ND bool is_zero() const { return counter_ == 0; }
ND int counter() const { return counter_; }
void set_period(uint16_t period)
{
set_period(period, period);
}
void set_period(uint16_t low, uint16_t high)
{
periods_[0] = low;
periods_[1] = high;
}
std::vector<int32_t> GIDs;
private:
std::array<uint16_t, 2> periods_;
uint16_t counter_ = 0;
uint8_t state_ = 0;
};
class TimerCounter final
{
public:
explicit TimerCounter(uint32_t period = 1)
: period_(period)
{}
bool tick()
{
if (++counter_ >= period_) [[unlikely]] {
counter_ = 0;
return true;
}
return false;
}
ND bool is_zero() const { return counter_ == 0; }
ND uint32_t counter() const { return counter_; }
void set_period(uint32_t val) { period_ = val; }
std::vector<int32_t> GIDs;
private:
uint32_t period_;
uint32_t counter_ = 0;
};
/****************************************************************************************/
} // namespace openVCB
#endif