forked from OpenBW/openbw
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.h
840 lines (766 loc) · 23.9 KB
/
util.h
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
#ifndef BWGAME_UTIL_H
#define BWGAME_UTIL_H
#include <cstdint>
#include <iterator>
#include <limits>
#include <array>
#include <type_traits>
#include "containers.h"
#include "strf.h"
namespace bwgame {
template<typename utype>
struct xy_t {
utype x {};
utype y {};
xy_t() = default;
xy_t(utype x, utype y) : x(x), y(y) {}
bool operator<(const xy_t& n) const {
if (y == n.y) return x < n.x;
return y < n.y;
}
bool operator>(const xy_t& n) const {
if (y == n.y) return x > n.x;
return y > n.y;
}
bool operator<=(const xy_t& n) const {
if (y == n.y) return x <= n.x;
return y <= n.y;
}
bool operator>=(const xy_t& n) const {
if (y == n.y) return x >= n.x;
return y >= n.y;
}
bool operator==(const xy_t& n) const {
return x == n.x && y == n.y;
}
bool operator!=(const xy_t& n) const {
return x != n.x || y != n.y;
}
xy_t operator-(const xy_t& n) const {
xy_t r(*this);
return r -= n;
}
xy_t& operator-=(const xy_t& n) {
x -= n.x;
y -= n.y;
return *this;
}
xy_t operator+(const xy_t& n) const {
xy_t r(*this);
return r += n;
}
xy_t& operator+=(const xy_t& n) {
x += n.x;
y += n.y;
return *this;
}
xy_t operator -() const {
return xy_t(-x, -y);
}
xy_t operator/(const xy_t& n) const {
xy_t r(*this);
return r /= n;
}
xy_t& operator/=(const xy_t& n) {
x /= n.x;
y /= n.y;
return *this;
}
template<typename T>
xy_t operator/(T&& v) const {
return xy_t(*this) /= v;
}
template<typename T>
xy_t& operator/=(T&& v) {
x /= v;
y /= v;
return *this;
}
xy_t operator*(const xy_t& n) const {
xy_t r(*this);
return r *= n;
}
xy_t& operator*=(const xy_t& n) {
x *= n.x;
y *= n.y;
return *this;
}
template<typename T>
xy_t operator*(T&& v) const {
return xy_t(*this) *= v;
}
template<typename T>
xy_t& operator*=(T&& v) {
x *= v;
y *= v;
return *this;
}
};
template<typename T>
struct rect_t {
T from;
T to;
rect_t() = default;
rect_t(T from, T to) : from(from), to(to) {}
bool operator==(const rect_t& n) const {
return from == n.from && to == n.to;
}
rect_t operator+(const rect_t& n) const {
return { from + n.from, to + n.to };
}
};
template<typename iter_T>
struct iterators_range {
private:
iter_T begin_it;
iter_T end_it;
public:
iterators_range(iter_T begin_it, iter_T end_it) : begin_it(begin_it), end_it(end_it) {}
using iterator = iter_T;
using value_type = typename std::iterator_traits<iterator>::value_type;
using pointer = typename std::iterator_traits<iterator>::pointer;
using reference = typename std::iterator_traits<iterator>::reference;
iterator begin() {
return begin_it;
}
iterator end() {
return end_it;
}
bool empty() const {
return begin_it == end_it;
}
reference front() {
return *begin_it;
}
};
template<typename iter_T>
iterators_range<iter_T> make_iterators_range(iter_T begin, iter_T end) {
return iterators_range<iter_T>(begin, end);
}
template<typename cont_T>
auto make_reverse_range(cont_T&& cont) {
return make_iterators_range(cont.rbegin(), cont.rend());
}
template<typename iterator_T, typename transform_F>
struct transform_iterator {
private:
typedef transform_iterator this_t;
iterator_T ptr;
transform_F f;
public:
using iterator_category = typename std::iterator_traits<iterator_T>::iterator_category;
using reference = typename std::result_of<transform_F(typename std::iterator_traits<iterator_T>::reference)>::type;
using value_type = typename std::remove_cv<typename std::remove_const<reference>::type>::type;
using difference_type = typename std::iterator_traits<iterator_T>::difference_type;
using pointer = typename std::remove_reference<reference>::type*;
template<typename arg_iterator_T, typename arg_transform_F>
transform_iterator(arg_iterator_T&& ptr, arg_transform_F&& f) : ptr(std::forward<arg_iterator_T>(ptr)), f(std::forward<arg_transform_F>(f)) {}
reference operator*() const {
return f(*ptr);
}
reference operator*() {
return f(*ptr);
}
this_t& operator++() {
++ptr;
return *this;
}
this_t operator++(int) {
auto r = *this;
++ptr;
return r;
}
this_t& operator--() {
--ptr;
return *this;
}
this_t operator--(int) {
auto r = *this;
--ptr;
return r;
}
this_t& operator+=(difference_type diff) {
ptr += diff;
return *this;
}
this_t operator+(difference_type diff) const {
auto r = *this;
return r += diff;
}
this_t& operator-=(difference_type diff) {
ptr -= diff;
return *this;
}
this_t operator-(difference_type diff) const {
auto r = *this;
return r -= diff;
}
difference_type operator-(const this_t& other) const {
return ptr - other.ptr;
}
bool operator==(const this_t& rhs) const {
return ptr == rhs.ptr;
}
bool operator!=(const this_t& rhs) const {
return ptr != rhs.ptr;
}
bool operator<(const this_t& rhs) const {
return ptr < rhs.ptr;
}
bool operator<=(const this_t& rhs) const {
return ptr <= rhs.ptr;
}
bool operator>(const this_t& rhs) const {
return ptr > rhs.ptr;
}
bool operator>=(const this_t& rhs) const {
return ptr >= rhs.ptr;
}
};
template<typename iterator_T, typename transform_F>
auto make_transform_iterator(iterator_T&& c, transform_F&& f) {
return transform_iterator<iterator_T, transform_F>(std::forward<iterator_T>(c), std::forward<transform_F>(f));
}
template<typename range_T, typename transform_F>
auto make_transform_range(range_T&& r, transform_F&& f) {
auto begin = make_transform_iterator(r.begin(), std::forward<transform_F>(f));
return make_iterators_range(begin, make_transform_iterator(r.end(), std::forward<transform_F>(f)));
}
template<typename iterator_T, typename predicate_F>
struct filter_iterator {
private:
typedef filter_iterator this_t;
iterator_T ptr;
iterator_T end_ptr;
predicate_F f;
public:
using iterator_category = std::forward_iterator_tag;
using reference = typename std::iterator_traits<iterator_T>::reference;
using value_type = typename std::iterator_traits<iterator_T>::value_type;
using difference_type = typename std::iterator_traits<iterator_T>::difference_type;
using pointer = value_type*;
template<typename arg_iterator_T, typename arg_predicate_F>
filter_iterator(arg_iterator_T&& ptr, arg_iterator_T&& end_ptr, arg_predicate_F&& f) : ptr(std::forward<arg_iterator_T>(ptr)), end_ptr(std::forward<arg_iterator_T>(end_ptr)), f(std::forward<arg_predicate_F>(f)) {
if (ptr != end_ptr && !f(*ptr)) ++*this;
}
reference operator*() const {
return *ptr;
}
this_t& operator++() {
do {
++ptr;
} while (ptr != end_ptr && !f(*ptr));
return *this;
}
this_t operator++(int) {
auto r = *this;
++*this;
return r;
}
bool operator==(const this_t& rhs) const {
return ptr == rhs.ptr;
}
bool operator!=(const this_t& rhs) const {
return ptr != rhs.ptr;
}
bool operator<(const this_t& rhs) const {
return ptr < rhs.ptr;
}
bool operator<=(const this_t& rhs) const {
return ptr <= rhs.ptr;
}
bool operator>(const this_t& rhs) const {
return ptr > rhs.ptr;
}
bool operator>=(const this_t& rhs) const {
return ptr >= rhs.ptr;
}
};
template<typename iterator_T, typename predicate_F>
auto make_filter_iterator(iterator_T&& c, iterator_T&& end, predicate_F&& f) {
return filter_iterator<iterator_T, predicate_F>(std::forward<iterator_T>(c), std::forward<iterator_T>(end), std::forward<predicate_F>(f));
}
template<typename range_T, typename predicate_F>
auto make_filter_range(range_T&& r, predicate_F&& f) {
auto begin = make_filter_iterator(r.begin(), r.end(), std::forward<predicate_F>(f));
return make_iterators_range(begin, make_filter_iterator(r.end(), r.end(), std::forward<predicate_F>(f)));
}
template<typename range_T>
auto ptr(range_T&& r) {
return make_transform_range(r, [](auto& ref) {
return &ref;
});
}
template<typename range_T>
auto reverse(range_T&& r) {
return make_iterators_range(std::make_reverse_iterator(r.end()), std::make_reverse_iterator(r.begin()));
}
template<typename range_T>
auto range_size(range_T&& r) {
auto rv = std::distance(r.begin(), r.end());
return (typename std::make_unsigned<decltype(rv)>::type)rv;
}
struct identity {
template<typename T>
decltype(auto) operator()(T&& v) const {
return std::forward<T>(v);
}
};
template<typename iterator_T, typename score_F>
auto get_best_score(iterator_T begin, iterator_T end, score_F&& score) {
if (begin == end) return end;
auto i = begin;
auto best = i;
auto best_score = score(*i);
++i;
for (; i != end; ++i) {
auto s = score(*i);
if (s < best_score) {
best = i;
best_score = s;
}
}
return best;
}
template<typename cont_T, typename score_F>
auto get_best_score(cont_T&& cont, score_F&& score) {
return get_best_score(cont.begin(), cont.end(), std::forward<score_F>(score));
}
struct nullopt_t {
struct init {
constexpr init() {}
};
constexpr explicit nullopt_t(init) {};
};
static constexpr nullopt_t nullopt{nullopt_t::init{}};
struct in_place_tag {
struct init {};
constexpr explicit in_place_tag(init) {};
};
static inline in_place_tag in_place() {
std::terminate();
}
using in_place_t = in_place_tag(&)();
template<typename T>
struct optional {
private:
typename std::aligned_storage<sizeof(T), alignof(T)>::type buf;
bool has_obj = false;
T* ptr() {
return (T*)&buf;
}
T& obj() {
return *(T*)&buf;
}
const T& obj() const {
return *(T*)&buf;
}
void destroy() {
obj().~value_type();
has_obj = false;
}
public:
using value_type = T;
optional() = default;
optional(nullopt_t) noexcept {}
template<typename NT = T, typename std::enable_if<std::is_copy_constructible<NT>::value>::type* = nullptr>
optional(const optional& n) {
if (n.has_obj) {
has_obj = true;
new (ptr()) value_type(n.obj());
}
}
optional(optional&& n) noexcept(std::is_nothrow_move_constructible<value_type>::value) {
if (n.has_obj) {
new (ptr()) value_type(std::move(n.obj()));
has_obj = true;
}
}
template<typename... args_T>
optional(in_place_t, args_T&&... args) {
has_obj = true;
new (ptr()) value_type(std::forward<args_T>(args)...);
}
~optional() {
if (has_obj) destroy();
}
optional& operator=(nullopt_t) noexcept {
if (has_obj) destroy();
return *this;
}
template<typename NT = T, typename std::enable_if<std::is_copy_assignable<NT>::value>::type* = nullptr>
optional& operator=(const optional& n) noexcept(std::is_nothrow_move_assignable<value_type>::value && std::is_nothrow_move_constructible<value_type>::value) {
if (!n.has_obj) *this = nullopt;
else {
if (has_obj) obj() = n.obj();
else {
has_obj = true;
new (ptr()) value_type(n.obj());
}
}
return *this;
}
optional& operator=(optional&& n) {
if (has_obj) {
if (n.has_obj) obj() = std::move(n.obj());
else destroy();
} else {
if (n.has_obj) {
new (ptr()) value_type(std::move(n.obj()));
has_obj = true;
}
}
return *this;
}
template<typename n_T, typename std::enable_if<std::is_same<std::decay_t<n_T>, value_type>::value>::type* = nullptr>
optional& operator=(n_T&& n) {
if (has_obj) {
obj() = std::forward<n_T>(n);
} else {
new (ptr()) value_type(std::forward<n_T>(n));
has_obj = true;
}
return *this;
}
const value_type* operator->() const {
return ptr();
}
value_type* operator->() {
return ptr();
}
const value_type& operator*() const& {
return obj();
}
value_type& operator*() & {
return obj();
}
const value_type&& operator*() const&& {
return std::move(obj());
}
value_type&& operator*() && {
return std::move(obj());
}
explicit operator bool() const {
return has_obj;
}
bool has_value() const {
return has_obj;
}
void reset() {
if (has_obj) destroy();
}
template<typename... args_T>
void emplace(args_T&&... args) {
if (has_obj) obj().~value_type();
else has_obj = true;
new (ptr()) value_type(std::forward<args_T>(args)...);
}
};
template<size_t bits>
using int_fastn_t = typename std::conditional<bits <= 8, std::int_fast8_t,
typename std::conditional<bits <= 16, int_fast16_t,
typename std::conditional<bits <= 32, int_fast32_t,
typename std::enable_if<bits <= 64, int_fast64_t>::type>::type>::type>::type;
template<size_t bits>
using uint_fastn_t = typename std::conditional<bits <= 8, std::uint_fast8_t,
typename std::conditional<bits <= 16, uint_fast16_t,
typename std::conditional<bits <= 32, uint_fast32_t,
typename std::enable_if<bits <= 64, uint_fast64_t>::type>::type>::type>::type;
template<typename T, std::enable_if<std::is_integral<T>::value && std::numeric_limits<T>::radix == 2>* = nullptr>
using int_bits = std::integral_constant<size_t, std::numeric_limits<T>::digits + std::is_signed<T>::value>;
template<typename T>
using is_native_fast_int = std::integral_constant<bool, std::is_integral<T>::value && std::is_literal_type<T>::value && sizeof(T) <= sizeof(void*)>;
template<size_t t_integer_bits, size_t t_fractional_bits, bool t_is_signed, bool t_exact_integer_bits = false>
struct fixed_point {
static const bool is_signed = t_is_signed;
static const bool exact_integer_bits = t_exact_integer_bits;
static const size_t integer_bits = t_integer_bits;
static const size_t fractional_bits = t_fractional_bits;
static const size_t total_bits = integer_bits + fractional_bits;
using raw_unsigned_type = uint_fastn_t<total_bits>;
using raw_signed_type = int_fastn_t<total_bits>;
using raw_type = typename std::conditional<is_signed, raw_signed_type, raw_unsigned_type>::type;
raw_type raw_value;
using double_size_fixed_point = fixed_point<total_bits * 2 - fractional_bits, fractional_bits, is_signed>;
void wrap() {
if (!exact_integer_bits) return;
raw_value <<= int_bits<raw_type>::value - total_bits;
raw_value >>= int_bits<raw_type>::value - total_bits;
}
static constexpr fixed_point from_raw(raw_type raw_value) {
return fixed_point{exact_integer_bits ? (raw_type)((raw_type)(raw_value << (int_bits<raw_type>::value - total_bits)) >> (int_bits<raw_type>::value - total_bits)) : raw_value};
}
template<typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
static fixed_point integer(T integer_value) {
return from_raw((raw_type)((raw_unsigned_type)integer_value << fractional_bits));
}
static fixed_point zero() {
return integer(0);
}
static fixed_point one() {
return integer(1);
}
raw_type integer_part() const {
return raw_value >> fractional_bits;
}
raw_type fractional_part() const {
return raw_value & (((raw_type)1 << fractional_bits) - 1);
}
template<size_t n_integer_bits, size_t n_fractional_bits, bool n_exact_integer_bits, size_t result_integer_bits = integer_bits, size_t result_fractional_bits = fractional_bits, typename std::enable_if<((result_integer_bits < n_integer_bits || result_fractional_bits < n_fractional_bits) && result_fractional_bits <= n_fractional_bits)>::type* = nullptr>
static auto truncate(const fixed_point<n_integer_bits, n_fractional_bits, is_signed, n_exact_integer_bits>& n) {
using result_type = fixed_point<result_integer_bits, result_fractional_bits, is_signed, exact_integer_bits>;
typename result_type::raw_type raw_value = (typename result_type::raw_type)(n.raw_value >> (n_fractional_bits - result_fractional_bits));
return result_type::from_raw(raw_value);
}
template<size_t n_integer_bits, size_t n_fractional_bits, bool n_exact_integer_bits, size_t result_integer_bits = integer_bits, size_t result_fractional_bits = fractional_bits, typename std::enable_if<((result_integer_bits > n_integer_bits || result_fractional_bits > n_fractional_bits) && result_fractional_bits >= n_fractional_bits)>::type* = nullptr>
static auto extend(const fixed_point<n_integer_bits, n_fractional_bits, is_signed, n_exact_integer_bits>& n) {
using result_type = fixed_point<result_integer_bits, result_fractional_bits, is_signed, exact_integer_bits>;
typename result_type::raw_type raw_value = n.raw_value;
raw_value <<= result_fractional_bits - n_fractional_bits;
return result_type::from_raw(raw_value);
}
fixed_point floor() const {
return integer(integer_part());
}
fixed_point ceil() const {
return (*this + integer(1) - from_raw(1)).floor();
}
fixed_point abs() const {
if (*this >= zero()) return *this;
else return from_raw(-raw_value);
}
auto as_signed() const {
return fixed_point<integer_bits, fractional_bits, true, exact_integer_bits>::from_raw(raw_value);
}
auto as_unsigned() const {
return fixed_point<integer_bits, fractional_bits, false, exact_integer_bits>::from_raw(raw_value);
}
bool operator==(const fixed_point& n) const {
return raw_value == n.raw_value;
}
bool operator!=(const fixed_point& n) const {
return raw_value != n.raw_value;
}
bool operator<(const fixed_point& n) const {
return raw_value < n.raw_value;
}
bool operator<=(const fixed_point& n) const {
return raw_value <= n.raw_value;
}
bool operator>(const fixed_point& n) const {
return raw_value > n.raw_value;
}
bool operator>=(const fixed_point& n) const {
return raw_value >= n.raw_value;
}
fixed_point operator-() const {
static_assert(is_signed, "fixed_point: cannot negate an unsigned number");
return from_raw(-raw_value);
}
fixed_point& operator+=(const fixed_point& n) {
raw_value += n.raw_value;
wrap();
return *this;
}
fixed_point operator+(const fixed_point& n) const {
return from_raw(raw_value + n.raw_value);
}
fixed_point& operator-=(const fixed_point& n) {
raw_value -= n.raw_value;
wrap();
return *this;
}
fixed_point operator-(const fixed_point& n) const {
return from_raw(raw_value - n.raw_value);
}
template<typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
fixed_point& operator/=(T integer_value) {
static_assert(std::is_signed<T>::value == is_signed, "fixed_point: cannot mix signed/unsigned in division");
raw_value /= integer_value;
wrap();
return *this;
}
template<typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
fixed_point operator/(T integer_value) const {
static_assert(std::is_signed<T>::value == is_signed, "fixed_point: cannot mix signed/unsigned in division");
return from_raw(raw_value / integer_value);
}
template<typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
fixed_point& operator*=(T integer_value) {
static_assert(std::is_signed<T>::value == is_signed, "fixed_point: cannot mix signed/unsigned in multiplication");
raw_value *= integer_value;
wrap();
return *this;
}
template<typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
auto operator*(T integer_value) const {
static_assert(std::is_signed<T>::value == is_signed, "fixed_point: cannot mix signed/unsigned in multiplication");
return from_raw(raw_value * integer_value);
}
template<size_t n_integer_bits>
fixed_point& operator*=(const fixed_point<n_integer_bits, fractional_bits, is_signed, exact_integer_bits>& n) {
return *this = *this * n;
}
template<size_t n_integer_bits>
fixed_point& operator/=(const fixed_point<n_integer_bits, fractional_bits, is_signed, exact_integer_bits>& n) {
return *this = *this / n;
}
// rounds towards negative infinity
template<size_t n_integer_bits, size_t n_fractional_bits>
auto operator*(const fixed_point<n_integer_bits, n_fractional_bits, is_signed, exact_integer_bits>& n) {
using result_type = fixed_point<(integer_bits > n_integer_bits ? integer_bits : n_integer_bits), (fractional_bits > n_fractional_bits ? fractional_bits : n_fractional_bits), is_signed, exact_integer_bits>;
using tmp_t = typename fixed_point<result_type::integer_bits, fractional_bits + n_fractional_bits, is_signed>::raw_type;
tmp_t tmp = (tmp_t)raw_value * (tmp_t)n.raw_value >> n_fractional_bits;
return result_type::from_raw((typename result_type::raw_type)tmp);
}
// rounds towards 0
template<size_t n_integer_bits, size_t n_fractional_bits>
auto operator/(const fixed_point<n_integer_bits, n_fractional_bits, is_signed, exact_integer_bits>& n) const {
using result_type = fixed_point<(integer_bits > n_integer_bits ? integer_bits : n_integer_bits), (fractional_bits > n_fractional_bits ? fractional_bits : n_fractional_bits), is_signed, exact_integer_bits>;
using tmp_t = typename fixed_point<result_type::integer_bits, fractional_bits + n_fractional_bits, is_signed>::raw_type;
tmp_t tmp = ((tmp_t)raw_value << n_fractional_bits) / (tmp_t)n.raw_value;
return result_type::from_raw((typename result_type::raw_type)tmp);
}
// returns a * b / c
// rounds towards 0
static fixed_point multiply_divide(fixed_point a, fixed_point b, fixed_point c) {
constexpr raw_type max_value_no_overflow = std::numeric_limits<raw_type>::max() >> (int_bits<raw_type>::value / 2);
using tmp_t = typename fixed_point<integer_bits, fractional_bits + fractional_bits, is_signed>::raw_type;
if (!is_native_fast_int<tmp_t>::value && a.raw_value <= max_value_no_overflow && b.raw_value <= max_value_no_overflow) {
return from_raw(a.raw_value * b.raw_value / c.raw_value);
} else {
return from_raw((tmp_t)a.raw_value * b.raw_value / c.raw_value);
}
}
// returns a / b * c
// rounds towards 0
static fixed_point divide_multiply(fixed_point a, fixed_point b, fixed_point c) {
return from_raw(a.raw_value / b.raw_value * c.raw_value);
}
};
using fp1 = fixed_point<31, 1, true>;
using fp8 = fixed_point<24, 8, true>;
using ufp8 = fixed_point<24, 8, false>;
using fp16 = fixed_point<16, 16, true>;
using ufp16 = fixed_point<16, 16, false>;
using direction_t = fixed_point<0, 8, true, true>;
using xy = xy_t<int>;
using xy_fp8 = xy_t<fp8>;
using rect = rect_t<xy>;
static inline constexpr fp8 operator ""_fp8(unsigned long long int value) {
return fp8::from_raw((fp8::raw_type)value);
}
static inline constexpr direction_t operator ""_dir(unsigned long long int value) {
return direction_t::from_raw((direction_t::raw_type)value);
}
template<typename T, typename index_T, size_t N = (size_t)index_T::None>
struct type_indexed_array {
private:
using arr_T = std::array<T, N>;
arr_T arr;
public:
using value_type = T;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = value_type*;
using const_pointer = const value_type*;
using iterator = typename arr_T::iterator;
using const_iterator = typename arr_T::const_iterator;
using reverse_iterator = typename arr_T::reverse_iterator;
using const_reverse_iterator = typename arr_T::const_reverse_iterator;
reference at(index_T pos) {
return arr.at((size_t)pos);
}
const_reference at(index_T pos) const {
return arr.at((size_t)pos);
}
reference operator[](index_T pos) {
return arr[(size_t)pos];
}
const_reference operator[](index_T pos) const {
return arr[(size_t)pos];
}
reference front() {
return arr.front();
}
const_reference front() const {
return arr.front();
}
reference back() {
return arr.back();
}
const_reference back() const {
return arr.back();
}
pointer data() noexcept {
return arr.data();
}
const_pointer data() const noexcept {
return arr.data();
}
iterator begin() noexcept {
return arr.begin();
}
const_iterator begin() const noexcept {
return arr.begin();
}
const_iterator cbegin() const noexcept {
return arr.cbegin();
}
iterator end() noexcept {
return arr.end();
}
const_iterator end() const noexcept {
return arr.end();
}
const_iterator cend() const noexcept {
return arr.cend();
}
bool empty() const noexcept {
return arr.empty();
}
constexpr size_type size() const noexcept {
return arr.size();
}
constexpr size_type max_size() const noexcept {
return arr.max_size();
}
void fill(const_reference value) {
arr.fill(value);
}
void swap(type_indexed_array& n) {
arr.swap(n.arr);
}
};
template<typename T, typename std::enable_if<std::is_unsigned<T>::value>::type* = nullptr>
T isqrt(T n) {
T r = 0;
T p = (T)1 << (8 * sizeof(T) - 2);
while (p > n) p /= 4u;
while (p) {
if (n >= r + p) {
n -= r + p;
r += 2u * p;
}
r /= 2u;
p /= 4u;
}
return r;
}
template<typename...T>
a_string format(const char*fmt, T&&... args) {
bwgame::a_string str;
bwgame::strf::format(str, fmt, std::forward<T>(args)...);
return str;
}
struct exception : std::runtime_error {
exception(const a_string& str) : std::runtime_error(str.c_str()) {}
};
template<typename...T>
void error(const char* fmt, T&&... args) {
throw exception(format(fmt, std::forward<T>(args)...));
}
}
#endif