-
Notifications
You must be signed in to change notification settings - Fork 367
/
Copy pathutilities.hpp
4544 lines (4500 loc) · 165 KB
/
utilities.hpp
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#define UTILITIES_REGEX
#define UTILITIES_FILE
#define UTILITIES_PNG
#define UTILITIES_CONSOLE_COLOR
#define UTILITIES_CONSOLE_INPUT
#define UTILITIES_CONSOLE_DITHER_LOOKUP
#define CONSOLE_WIDTH 79
//#define UTILITIES_NO_CPP17
#pragma warning(disable:26451)
#pragma warning(disable:6386)
#include <cmath>
#include <vector>
#include <string>
#ifdef UTILITIES_REGEX
#include <regex> // contains <string>, <vector>, <algorithm> and others
#endif // UTILITIES_REGEX
#include <iostream>
#include <chrono>
#include <thread>
#include <functional> // for parallel_for(...)
#undef min
#undef max
using std::string;
using std::vector;
using std::thread;
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef int64_t slong;
typedef uint64_t ulong;
#define pif 3.1415927f
#define pi 3.141592653589793
#define min_char ((char)-128)
#define max_char ((char)127)
#define max_uchar ((uchar)255)
#define min_short ((short)-32768)
#define max_short ((short)32767)
#define max_ushort ((ushort)65535)
#define min_int -2147483648
#define max_int 2147483647
#define max_uint 4294967295u
#define min_slong -9223372036854775808ll
#define max_slong 9223372036854775807ll
#define max_ulong 18446744073709551615ull
#define min_float 1.401298464E-45f
#define max_float 3.402823466E38f
#define epsilon_float 1.192092896E-7f
#define inf_float as_float(0x7F800000)
#define nan_float as_float(0xFFFFFFFF)
#define min_double 4.9406564584124654E-324
#define max_double 1.7976931348623158E308
#define epsilon_double 2.2204460492503131E-16
#define inf_double as_double(0x7FF0000000000000)
#define nan_double as_double(0xFFFFFFFFFFFFFFFF)
inline void parallel_for(const uint N, const uint threads, std::function<void(uint, uint)> lambda) { // usage: parallel_for(N, threads, [&](uint n, uint t) { ... });
vector<thread> thread_array(threads);
for(uint t=0u; t<threads; t++) thread_array[t] = thread([=]() {
for(ulong n=(ulong)N*(ulong)t/(ulong)threads; n<(ulong)N*(ulong)(t+1u)/(ulong)threads; n++) lambda((uint)n, t);
});
for(uint t=0u; t<threads; t++) thread_array[t].join();
}
inline void parallel_for(const uint N, const uint threads, std::function<void(uint)> lambda) { // usage: parallel_for(N, threads, [&](uint n) { ... });
vector<thread> thread_array(threads);
for(uint t=0u; t<threads; t++) thread_array[t] = thread([=]() {
for(ulong n=(ulong)N*(ulong)t/(ulong)threads; n<(ulong)N*(ulong)(t+1u)/(ulong)threads; n++) lambda((uint)n);
});
for(uint t=0u; t<threads; t++) thread_array[t].join();
}
inline void parallel_for(const uint N, std::function<void(uint)> lambda) { // usage: parallel_for(N, [&](uint n) { ... });
parallel_for(N, (uint)thread::hardware_concurrency(), lambda);
}
inline void parallel_for(const ulong N, const uint threads, std::function<void(ulong, uint)> lambda) { // usage: parallel_for(N, threads, [&](ulong n, uint t) { ... });
vector<thread> thread_array(threads);
for(uint t=0u; t<threads; t++) thread_array[t] = thread([=]() {
for(ulong n=N*(ulong)t/(ulong)threads; n<N*(ulong)(t+1u)/(ulong)threads; n++) lambda(n, t);
});
for(uint t=0u; t<threads; t++) thread_array[t].join();
}
inline void parallel_for(const ulong N, const uint threads, std::function<void(ulong)> lambda) { // usage: parallel_for(N, threads, [&](ulong n) { ... });
vector<thread> thread_array(threads);
for(uint t=0u; t<threads; t++) thread_array[t] = thread([=]() {
for(ulong n=N*(ulong)t/(ulong)threads; n<N*(ulong)(t+1u)/(ulong)threads; n++) lambda(n);
});
for(uint t=0u; t<threads; t++) thread_array[t].join();
}
inline void parallel_for(const ulong N, std::function<void(ulong)> lambda) { // usage: parallel_for(N, [&](ulong n) { ... });
parallel_for(N, (uint)thread::hardware_concurrency(), lambda);
}
class Clock {
private:
typedef std::chrono::high_resolution_clock clock;
std::chrono::time_point<clock> t;
public:
inline Clock() { start(); }
inline void start() { t = clock::now(); }
inline double stop() const { return std::chrono::duration_cast<std::chrono::duration<double>>(clock::now()-t).count(); }
};
inline void sleep(const double t) {
if(t>0.0) std::this_thread::sleep_for(std::chrono::milliseconds((int)(1E3*t+0.5)));
}
inline float as_float(const uint x) {
return *(float*)&x;
}
inline uint as_uint(const float x) {
return *(uint*)&x;
}
inline double as_double(const ulong x) {
return *(double*)&x;
}
inline ulong as_ulong(const double x) {
return *(ulong*)&x;
}
inline float half_to_float(const ushort x) { // IEEE-754 16-bit floating-point format (without infinity): 1-5-10, exp-15, +-131008.0, +-6.1035156E-5, +-5.9604645E-8, 3.311 digits
const uint e = (x&0x7C00)>>10; // exponent
const uint m = (x&0x03FF)<<13; // mantissa
const uint v = as_uint((float)m)>>23; // evil log2 bit hack to count leading zeros in denormalized format
return as_float((x&0x8000)<<16 | (e!=0)*((e+112)<<23|m) | ((e==0)&(m!=0))*((v-37)<<23|((m<<(150-v))&0x007FE000))); // sign : normalized : denormalized
}
inline ushort float_to_half(const float x) { // IEEE-754 16-bit floating-point format (without infinity): 1-5-10, exp-15, +-131008.0, +-6.1035156E-5, +-5.9604645E-8, 3.311 digits
const uint b = as_uint(x)+0x00001000; // round-to-nearest-even: add last bit after truncated mantissa
const uint e = (b&0x7F800000)>>23; // exponent
const uint m = b&0x007FFFFF; // mantissa; in line below: 0x007FF000 = 0x00800000-0x00001000 = decimal indicator flag - initial rounding
return (b&0x80000000)>>16 | (e>112)*((((e-112)<<10)&0x7C00)|m>>13) | ((e<113)&(e>101))*((((0x007FF000+m)>>(125-e))+1)>>1) | (e>143)*0x7FFF; // sign : normalized : denormalized : saturate
}
inline float half_to_float_custom(const ushort x) { // custom 16-bit floating-point format, 1-4-11, exp-15, +-1.99951168, +-6.10351562E-5, +-2.98023224E-8, 3.612 digits
const uint e = (x&0x7800)>>11; // exponent
const uint m = (x&0x07FF)<<12; // mantissa
const uint v = as_uint((float)m)>>23; // evil log2 bit hack to count leading zeros in denormalized format
return as_float((x&0x8000)<<16 | (e!=0)*((e+112)<<23|m) | ((e==0)&(m!=0))*((v-37)<<23|((m<<(150-v))&0x007FF000))); // sign : normalized : denormalized
}
inline ushort float_to_half_custom(const float x) { // custom 16-bit floating-point format, 1-4-11, exp-15, +-1.99951168, +-6.10351562E-5, +-2.98023224E-8, 3.612 digits
const uint b = as_uint(x)+0x00000800; // round-to-nearest-even: add last bit after truncated mantissa
const uint e = (b&0x7F800000)>>23; // exponent
const uint m = b&0x007FFFFF; // mantissa; in line below: 0x007FF800 = 0x00800000-0x00000800 = decimal indicator flag - initial rounding
return (b&0x80000000)>>16 | (e>112)*((((e-112)<<11)&0x7800)|m>>12) | ((e<113)&(e>100))*((((0x007FF800+m)>>(124-e))+1)>>1) | (e>127)*0x7FFF; // sign : normalized : denormalized : saturate
}
inline float sq(const float x) {
return x*x;
}
inline float cb(const float x) {
return x*x*x;
}
inline float pow(const float x, const uint n) {
float r = 1.0f;
for(uint i=0u; i<n; i++) {
r *= x;
}
return r;
}
inline float sign(const float x) {
return x>=0.0f ? 1.0f : -1.0f;
}
inline float clamp(const float x, const float a, const float b) {
return fmin(fmax(x, a), b);
}
inline float rsqrt(const float x) {
return 1.0f/sqrt(x);
}
inline float ln(const float x) {
return log(x); // natural logarithm
}
inline int log2_fast(const float x) {
return (as_uint(x)>>23)-127;
}
inline float degrees(const float radians) {
return (180.0f/pif)*radians;
}
inline float radians(const float degrees) {
return (pif/180.0f)*degrees;
}
inline float find_zero(float f(float), float min=-1.0f, float max=1.0f, float offset=0.0f) { // find zero of function f(x) in [min, max] by nested intervals
const uint n = log2_fast((max-min)*1.67772162E7f); // evil log2 hack: log2(x)=(as_uint(x)>>23)-127
float m = 0.5f*(min+max);
const float s = 2.0f*(float)(f(min)>offset)-1.0f;
offset *= s;
for(uint i=0u; i<=n; i++) {
if(s*f(m)>offset) min = m;
else /**********/ max = m;
m = 0.5f*(min+max);
}
return m;
}
inline float integrate(float f(float), const float min, const float max, const uint N=1000000u) { // do numeric integration of function f(x)dx in [min, max] by trapezoidal rule
const float dx = (max-min)/(float)N;
double s = 0.0;
for(uint i=1u; i+1<N; i++) {
s += (double)f(min+(float)i*dx);
}
return (float)(0.5*((double)f(min)+(double)f(max))+s)*dx;
}
inline float derivative(float f(float), const float x) { // compute central first derivative of f(x) at x
const float dx = fmax(fabs(x), 1.0f)*2.0f*epsilon_float;
return (float)((double)f(x+dx)-(double)f(x-dx))*0.5f/dx;
}
inline float second_derivative(float f(float), const float x) { // compute central second derivative of f(x) at x
const float dx = fmax(fabs(x), 1.0f)*2.0f*epsilon_float;
return (float)((double)f(x+dx)-2.0*(double)f(x)+(double)f(x-dx))/sq(dx);
}
inline bool converged(const float x_penultimate, const float x_last, const float x, const float threshold=1E-5f) {
const float slope = fabs(x-x_last);
const float curvature = fabs(x-2.0f*x_last+x_penultimate);
const float t = (x_penultimate+x_last+x)/3.0f*threshold; // small threshold relative to x
return slope<t&&curvature<t; // convergence is reached if both slope and curvature are close to 0
}
inline float fmin(const uint n, const float* const x) {
float r = x[0];
for(uint i=1u; i<n; i++) {
r = fmin(r, x[i]);
}
return r;
}
inline float fmax(const uint n, const float* const x) {
float r = x[0];
for(uint i=1u; i<n; i++) {
r = fmax(r, x[i]);
}
return r;
}
inline float average(const uint n, const float* const x) {
double a = 0.0;
for(uint i=0u; i<n; i++) {
a += (double)x[i];
}
return (float)(a/(double)n);
}
inline float standard_deviation(const uint n, const float* const x) {
const double a = (double)average(n, x);
double s = 0.0;
for(uint i=0u; i<n; i++) {
const double b = (double)x[i]-a;
s += b*b;
}
return (float)sqrt(s/(double)n);
}
inline float random(uint& seed, const float x=1.0f) {
seed = (1103515245u*seed+12345u)%2147483648u; // standard C99 LCG
return x*(float)seed*4.65661287E-10f;
}
inline float random_symmetric(uint& seed, const float x=1.0f) {
return 2.0f*x*(random(seed, 1.0f)-0.5f);
}
inline void lu_solve(float* M, float* x, float* b, const int N) { // solves system of N linear equations M*x=b
for(int i=0; i<N; i++) { // decompose M in M=L*U
for(int j=i+1; j<N; j++) {
M[N*j+i] /= M[N*i+i];
for(int k=i+1; k<N; k++) M[N*j+k] -= M[N*j+i]*M[N*i+k];
}
}
for(int i=0; i<N; i++) { // find solution of L*y=b
x[i] = b[i];
for(int k=0; k<i; k++) x[i] -= M[N*i+k]*x[k];
}
for(int i=N-1; i>=0; i--) { // find solution of U*x=y
for(int k=i+1; k<N; k++) x[i] -= M[N*i+k]*x[k];
x[i] /= M[N*i+i];
}
}
inline double sq(const double x) {
return x*x;
}
inline double cb(const double x) {
return x*x*x;
}
inline double pow(const double x, const uint n) {
double r = 1.0;
for(uint i=0u; i<n; i++) {
r *= x;
}
return r;
}
inline double sign(const double x) {
return x>=0.0 ? 1.0 : -1.0;
}
inline double clamp(const double x, const double a, const double b) {
return fmin(fmax(x, a), b);
}
inline double rsqrt(const double x) {
return 1.0/sqrt(x);
}
inline double ln(const double x) {
return log(x); // natural logarithm
}
inline int log2_fast(const double x) {
return (as_ulong(x)>>52)-1023;
}
inline double degrees(const double radians) {
return (180.0/pi)*radians;
}
inline double radians(const double degrees) {
return (pi/180.0)*degrees;
}
inline double find_zero(double f(double), double min=-1.0, double max=1.0, double offset=0.0) { // find zero of function f(x) in [min, max] by nested intervals
const uint n = log2_fast((max-min)*9.007199254740992E15); // evil log2 hack: log2(x)=(as_ulong(x)>>52)-1023
double m = 0.5*(min+max);
const double s = 2.0*(double)(f(min)>offset)-1.0;
offset *= s;
for(uint i=0u; i<=n; i++) {
if(s*f(m)>offset) min = m;
else /**********/ max = m;
m = 0.5*(min+max);
}
return m;
}
inline double integrate(double f(double), const double min, const double max, const uint N=1000000u) { // do numeric integration of function f(x)dx in [min, max] by trapezoidal rule
const double dx = (max-min)/(double)N;
double s = 0.0;
for(uint i=1u; i+1<N; i++) {
s += f(min+(double)i*dx);
}
return (0.5*(f(min)+f(max))+s)*dx;
}
inline double derivative(double f(double), const double x) { // compute central first derivative of f(x) at x
const double dx = fmax(fabs(x), 1.0)*2.0*epsilon_double;
return (f(x+dx)-f(x-dx))*0.5/dx;
}
inline double second_derivative(double f(double), const double x) { // compute central second derivative of f(x) at x
const double dx = fmax(fabs(x), 1.0)*2.0*epsilon_double;
return (f(x+dx)-2.0*f(x)+f(x-dx))/sq(dx);
}
inline bool converged(const double x_penultimate, const double x_last, const double x, const double threshold=1E-5) {
const double slope = fabs(x-x_last);
const double curvature = fabs(x-2.0*x_last+x_penultimate);
const double t = (x_penultimate+x_last+x)/3.0*threshold; // small threshold relative to x
return slope<t&&curvature<t; // convergence is reached if both slope and curvature are close to 0
}
inline double fmin(const uint n, const double* const x) {
double r = x[0];
for(uint i=1u; i<n; i++) {
r = fmin(r, x[i]);
}
return r;
}
inline double fmax(const uint n, const double* const x) {
double r = x[0];
for(uint i=1u; i<n; i++) {
r = fmax(r, x[i]);
}
return r;
}
inline double average(const uint n, const double* const x) {
double a = 0.0;
for(uint i=0u; i<n; i++) {
a += x[i];
}
return (float)(a/(double)n);
}
inline double standard_deviation(const uint n, const double* const x) {
const double a = average(n, x);
double s = 0.0;
for(uint i=0u; i<n; i++) {
s += sq(x[i]-a);
}
return sqrt(s/(double)n);
}
inline int sq(const int x) {
return x*x;
}
inline int cb(const int x) {
return x*x*x;
}
inline int pow(const int x, const uint n) {
int r = 1;
for(uint i=0u; i<n; i++) {
r *= x;
}
return r;
}
inline int sign(const int x) {
return 1-2*(x>>31&1);
}
inline int min(const int x, const int y) {
return x<y?x:y;
}
inline int max(const int x, const int y) {
return x>y?x:y;
}
inline int clamp(const int x, const int a, const int b) {
return min(max(x, a), b);
}
inline uint sq(const uint x) {
return x*x;
}
inline uint cb(const uint x) {
return x*x*x;
}
inline uint pow(const uint x, const uint n) {
uint r = 1u;
for(uint i=0u; i<n; i++) {
r *= x;
}
return r;
}
inline uint min(const uint x, const uint y) {
return x<y?x:y;
}
inline uint max(const uint x, const uint y) {
return x>y?x:y;
}
inline uint clamp(const uint x, const uint a, const uint b) {
return min(max(x, a), b);
}
inline uint gcd(uint x, uint y) { // greatest common divisor
if(x*y==0u) return 0u;
uint t;
while(y!=0u) {
t = y;
y = x%y;
x = t;
}
return x;
}
inline uint lcm(const uint x, const uint y) { // least common multiple
return x*y==0u ? 0u : x*y/gcd(x, y);
}
inline uint log2_fast(const uint x) {
return (uint)((as_uint((float)x)>>23)-127);
}
inline slong sq(const slong x) {
return x*x;
}
inline slong cb(const slong x) {
return x*x*x;
}
inline slong pow(const slong x, const uint n) {
slong r = 1ll;
for(uint i=0u; i<n; i++) {
r *= x;
}
return r;
}
inline slong sign(const slong x) {
return 1ll-2ll*(x>>63&1ll);
}
inline slong min(const slong x, const slong y) {
return x<y?x:y;
}
inline slong max(const slong x, const slong y) {
return x>y?x:y;
}
inline slong clamp(const slong x, const slong a, const slong b) {
return min(max(x, a), b);
}
inline ulong sq(const ulong x) {
return x*x;
}
inline ulong cb(const ulong x) {
return x*x*x;
}
inline ulong pow(const ulong x, const uint n) {
ulong r = 1ull;
for(uint i=0u; i<n; i++) {
r *= x;
}
return r;
}
inline ulong min(const ulong x, const ulong y) {
return x<y?x:y;
}
inline ulong max(const ulong x, const ulong y) {
return x>y?x:y;
}
inline ulong clamp(const ulong x, const ulong a, const ulong b) {
return min(max(x, a), b);
}
inline ulong gcd(ulong x, ulong y) { // greatest common divisor
if(x*y==0ull) return 0ull;
ulong t;
while(y!=0ull) {
t = y;
y = x%y;
x = t;
}
return x;
}
inline ulong lcm(const ulong x, const ulong y) { // least common multiple
return x*y==0ull ? 0ull : x*y/gcd(x, y);
}
inline uint log2_fast(const ulong x) {
return (uint)((as_ulong((double)x)>>52)-1023);
}
inline int to_int(const float x) {
return (int)(x+0.5f-(float)(x<0.0f));
}
inline int to_int(const double x) {
return (int)(x+0.5-(double)(x<0.0));
}
inline uint to_uint(const float x) {
return (uint)fmax(x+0.5f, 0.5f);
}
inline uint to_uint(const double x) {
return (uint)fmax(x+0.5, 0.5);
}
inline slong to_slong(const float x) {
return (slong)(x+0.5f);
}
inline slong to_slong(const double x) {
return (slong)(x+0.5);
}
inline ulong to_ulong(const float x) {
return (ulong)fmax(x+0.5f, 0.5f);
}
inline ulong to_ulong(const double x) {
return (ulong)fmax(x+0.5, 0.5);
}
inline float reverse_bytes(const float x) { // reverse the order of bytes
float r;
char* cx = (char*)&x;
char* cr = (char*)&r;
cr[0] = cx[3];
cr[1] = cx[2];
cr[2] = cx[1];
cr[3] = cx[0];
return r;
}
inline double reverse_bytes(const double x) { // reverse the order of bytes
double r;
char* cx = (char*)&x;
char* cr = (char*)&r;
cr[0] = cx[7];
cr[1] = cx[6];
cr[2] = cx[5];
cr[3] = cx[4];
cr[4] = cx[3];
cr[5] = cx[2];
cr[6] = cx[1];
cr[7] = cx[0];
return r;
}
inline char reverse_bytes(const char x) { // reverse the order of bytes
return x;
}
inline uchar reverse_bytes(const uchar x) { // reverse the order of bytes
return x;
}
inline short reverse_bytes(const short x) { // reverse the order of bytes
short r;
char* cx = (char*)&x;
char* cr = (char*)&r;
cr[0] = cx[1];
cr[1] = cx[0];
return r;
}
inline ushort reverse_bytes(const ushort x) { // reverse the order of bytes
ushort r;
char* cx = (char*)&x;
char* cr = (char*)&r;
cr[0] = cx[1];
cr[1] = cx[0];
return r;
}
inline int reverse_bytes(const int x) { // reverse the order of bytes
int r;
char* cx = (char*)&x;
char* cr = (char*)&r;
cr[0] = cx[3];
cr[1] = cx[2];
cr[2] = cx[1];
cr[3] = cx[0];
return r;
}
inline uint reverse_bytes(const uint x) { // reverse the order of bytes
uint r;
char* cx = (char*)&x;
char* cr = (char*)&r;
cr[0] = cx[3];
cr[1] = cx[2];
cr[2] = cx[1];
cr[3] = cx[0];
return r;
}
inline slong reverse_bytes(const slong x) { // reverse the order of bytes
slong r;
char* cx = (char*)&x;
char* cr = (char*)&r;
cr[0] = cx[7];
cr[1] = cx[6];
cr[2] = cx[5];
cr[3] = cx[4];
cr[4] = cx[3];
cr[5] = cx[2];
cr[6] = cx[1];
cr[7] = cx[0];
return r;
}
inline ulong reverse_bytes(const ulong x) { // reverse the order of bytes
ulong r;
char* cx = (char*)&x;
char* cr = (char*)&r;
cr[0] = cx[7];
cr[1] = cx[6];
cr[2] = cx[5];
cr[3] = cx[4];
cr[4] = cx[3];
cr[5] = cx[2];
cr[6] = cx[1];
cr[7] = cx[0];
return r;
}
struct int3 { // 3D vector type
int x, y, z;
int3(const int x, const int y, const int z) {
this->x = x;
this->y = y;
this->z = z;
}
int3(const uint x, const uint y, const uint z) {
this->x = (int)x;
this->y = (int)y;
this->z = (int)z;
}
int3(const float x, const float y, const float z) {
this->x = to_int(x);
this->y = to_int(y);
this->z = to_int(z);
}
int3(const double x, const double y, const double z) {
this->x = to_int(x);
this->y = to_int(y);
this->z = to_int(z);
}
int3(const int x) {
this->x = this->y = this->z = x;
}
int3(const uint x) {
this->x = this->y = this->z = (int)x;
}
int3(const float x) {
this->x = this->y = this->z = to_int(x);
}
int3(const double x) {
this->x = this->y = this->z = to_int(x);
}
int3() = default;
int3(const int3& v) = default; // copy constructor (elementwise copy)
int3(int3&& v) = default; // move constructor
~int3() = default;
inline int3& operator=(const int3& v) = default; // copy assignment (elementwise copy)
inline int3& operator=(int3&& v) = default; // move assignment
inline int3& operator+=(const int3& v) { // elementwise addition
this->x += v.x;
this->y += v.y;
this->z += v.z;
return *this;
}
inline int3& operator-=(const int3& v) { // elementwise subtraction
this->x -= v.x;
this->y -= v.y;
this->z -= v.z;
return *this;
}
inline int3& operator+=(const int x) { // elementwise addition
this->x += x;
this->y += x;
this->z += x;
return *this;
}
inline int3& operator-=(const int x) { // elementwise subtraction
this->x -= x;
this->y -= x;
this->z -= x;
return *this;
}
inline int3& operator*=(const int x) { // elementwise multiplication
this->x *= x;
this->y *= x;
this->z *= x;
return *this;
}
inline int3& operator/=(const int x) { // elementwise division
this->x /= x;
this->y /= x;
this->z /= x;
return *this;
}
inline int3& operator+() {
return *this;
}
inline const int3& operator+() const {
return *this;
}
inline int3 operator-() const { // elementwise negation
return int3(-this->x, -this->y, -this->z);
}
inline int3 operator+(const int3& v) const { // elementwise addition
return int3(this->x+v.x, this->y+v.y, this->z+v.z);
}
inline int3 operator-(const int3& v) const { // elementwise subtraction
return int3(this->x-v.x, this->y-v.y, this->z-v.z);
}
inline int operator*(const int3& v) const { // dot product
return this->x*v.x+this->y*v.y+this->z*v.z;
}
inline bool operator==(const int3& v) const { // check for equality
return this->x==v.x&&this->y==v.y&&this->z==v.z;
}
inline bool operator!=(const int3& v) const { // check for inequality
return this->x!=v.x||this->y!=v.y||this->z!=v.z;
}
inline bool operator>(const int3& v) const { // compare length
return sq(this->x)+sq(this->y)+sq(this->z)>sq(v.x)+sq(v.y)+sq(v.z);
}
inline bool operator<(const int3& v) const { // compare length
return sq(this->x)+sq(this->y)+sq(this->z)<sq(v.x)+sq(v.y)+sq(v.z);
}
inline bool operator>=(const int3& v) const { // compare length
return sq(this->x)+sq(this->y)+sq(this->z)>=sq(v.x)+sq(v.y)+sq(v.z);
}
inline bool operator<=(const int3& v) const { // compare length
return sq(this->x)+sq(this->y)+sq(this->z)<=sq(v.x)+sq(v.y)+sq(v.z);
}
};
inline int3 operator+(const int3& v, const int x) { // elementwise addition
return int3(v.x+x, v.y+x, v.z+x);
}
inline int3 operator+(const int x, const int3& v) { // elementwise addition
return v+x;
}
inline int3 operator-(const int3& v, const int x) { // elementwise subtraction
return v+(-x);
}
inline int3 operator-(const int x, const int3& v) { // elementwise subtraction
return int3(x-v.x, x-v.y, x-v.z);
}
inline int3 operator*(const int3& v, const int x) { // elementwise multiplication
return int3(v.x*x, v.y*x, v.z*x);
}
inline int3 operator*(const int x, const int3& v) { // elementwise multiplication
return v*x;
}
inline int3 operator/(const int3& v, const int x) { // elementwise division
return int3(v.x/x, v.y/x, v.z/x);
}
struct uint3 { // 3D vector type
uint x, y, z;
uint3(const uint x, const uint y, const uint z) {
this->x = x;
this->y = y;
this->z = z;
}
uint3(const int x, const int y, const int z) {
this->x = (uint)x;
this->y = (uint)y;
this->z = (uint)z;
}
uint3(const float x, const float y, const float z) {
this->x = to_uint(x);
this->y = to_uint(y);
this->z = to_uint(z);
}
uint3(const double x, const double y, const double z) {
this->x = to_uint(x);
this->y = to_uint(y);
this->z = to_uint(z);
}
uint3(const uint x) {
this->x = this->y = this->z = x;
}
uint3(const int x) {
this->x = this->y = this->z = (uint)x;
}
uint3(const float x) {
this->x = this->y = this->z = to_uint(x);
}
uint3(const double x) {
this->x = this->y = this->z = to_uint(x);
}
uint3() = default;
uint3(const uint3& v) = default; // copy constructor (elementwise copy)
uint3(uint3&& v) = default; // move constructor
~uint3() = default;
inline uint3& operator=(const uint3& v) = default; // copy assignment (elementwise copy)
inline uint3& operator=(uint3&& v) = default; // move assignment
inline uint3& operator+=(const uint3& v) { // elementwise addition
this->x += v.x;
this->y += v.y;
this->z += v.z;
return *this;
}
inline uint3& operator-=(const uint3& v) { // elementwise subtraction
this->x -= v.x;
this->y -= v.y;
this->z -= v.z;
return *this;
}
inline uint3& operator+=(const uint x) { // elementwise addition
this->x += x;
this->y += x;
this->z += x;
return *this;
}
inline uint3& operator-=(const uint x) { // elementwise subtraction
this->x -= x;
this->y -= x;
this->z -= x;
return *this;
}
inline uint3& operator*=(const uint x) { // elementwise multiplication
this->x *= x;
this->y *= x;
this->z *= x;
return *this;
}
inline uint3& operator/=(const uint x) { // elementwise division
this->x /= x;
this->y /= x;
this->z /= x;
return *this;
}
inline uint3& operator+() {
return *this;
}
inline const uint3& operator+() const {
return *this;
}
inline uint3 operator+(const uint3& v) const { // elementwise addition
return uint3(this->x+v.x, this->y+v.y, this->z+v.z);
}
inline uint3 operator-(const uint3& v) const { // elementwise subtraction
return uint3(this->x-v.x, this->y-v.y, this->z-v.z);
}
inline uint operator*(const uint3& v) const { // dot product
return this->x*v.x+this->y*v.y+this->z*v.z;
}
inline bool operator==(const uint3& v) const { // check for equality
return this->x==v.x&&this->y==v.y&&this->z==v.z;
}
inline bool operator!=(const uint3& v) const { // check for inequality
return this->x!=v.x||this->y!=v.y||this->z!=v.z;
}
inline bool operator>(const uint3& v) const { // compare length
return sq(this->x)+sq(this->y)+sq(this->z)>sq(v.x)+sq(v.y)+sq(v.z);
}
inline bool operator<(const uint3& v) const { // compare length
return sq(this->x)+sq(this->y)+sq(this->z)<sq(v.x)+sq(v.y)+sq(v.z);
}
inline bool operator>=(const uint3& v) const { // compare length
return sq(this->x)+sq(this->y)+sq(this->z)>=sq(v.x)+sq(v.y)+sq(v.z);
}
inline bool operator<=(const uint3& v) const { // compare length
return sq(this->x)+sq(this->y)+sq(this->z)<=sq(v.x)+sq(v.y)+sq(v.z);
}
};
inline uint3 operator+(const uint3& v, const uint x) { // elementwise addition
return uint3(v.x+x, v.y+x, v.z+x);
}
inline uint3 operator+(const uint x, const uint3& v) { // elementwise addition
return v+x;
}
inline uint3 operator-(const uint3& v, const uint x) { // elementwise subtraction
return uint3(v.x-x, v.y-x, v.z-x);
}
inline uint3 operator-(const uint x, const uint3& v) { // elementwise subtraction
return uint3(x-v.x, x-v.y, x-v.z);
}
inline uint3 operator*(const uint3& v, const uint x) { // elementwise multiplication
return uint3(v.x*x, v.y*x, v.z*x);
}
inline uint3 operator*(const uint x, const uint3& v) { // elementwise multiplication
return v*x;
}
inline uint3 operator/(const uint3& v, const uint x) { // elementwise division
return uint3(v.x/x, v.y/x, v.z/x);
}
struct float3x3; // forward-declare float3x3
struct float3 { // 3D vector type
float x, y, z;
float3(const float x, const float y, const float z) {
this->x = x;
this->y = y;
this->z = z;
}
float3(const double x, const double y, const double z) {
this->x = (float)x;
this->y = (float)y;
this->z = (float)z;
}
float3(const int x, const int y, const int z) {
this->x = (float)x;
this->y = (float)y;
this->z = (float)z;
}
float3(const uint x, const uint y, const uint z) {
this->x = (float)x;
this->y = (float)y;
this->z = (float)z;
}
float3(const float x) {
this->x = this->y = this->z = x;
}
float3(const double x) {
this->x = this->y = this->z = (float)x;
}
float3(const int x) {
this->x = this->y = this->z = (float)x;
}
float3(const uint x) {
this->x = this->y = this->z = (float)x;
}
float3(const float3x3& m); // forward-declare float3x3 constructor
float3() = default;
float3(const float3& v) = default; // copy constructor (elementwise copy)
float3(float3&& v) = default; // move constructor
~float3() = default;
inline float3& operator=(const float3& v) = default; // copy assignment (elementwise copy)
inline float3& operator=(float3&& v) = default; // move assignment
inline float3& operator=(const float3x3& m); // forward-declare float3x3 copy
inline float3& operator+=(const float3& v) { // elementwise addition
this->x += v.x;
this->y += v.y;
this->z += v.z;
return *this;
}
inline float3& operator-=(const float3& v) { // elementwise subtraction
this->x -= v.x;
this->y -= v.y;
this->z -= v.z;
return *this;
}
inline float3& operator+=(const float x) { // elementwise addition
this->x += x;
this->y += x;
this->z += x;
return *this;
}
inline float3& operator-=(const float x) { // elementwise subtraction
this->x -= x;
this->y -= x;
this->z -= x;
return *this;
}
inline float3& operator*=(const float x) { // elementwise multiplication
this->x *= x;
this->y *= x;
this->z *= x;
return *this;
}
inline float3& operator/=(const float x) { // elementwise division
this->x /= x;
this->y /= x;
this->z /= x;
return *this;
}
inline float3& operator+() {
return *this;
}
inline const float3& operator+() const {
return *this;
}
inline float3 operator-() const { // elementwise negation
return float3(-this->x, -this->y, -this->z);
}
inline float3 operator+(const float3& v) const { // elementwise addition
return float3(this->x+v.x, this->y+v.y, this->z+v.z);
}
inline float3 operator-(const float3& v) const { // elementwise subtraction
return float3(this->x-v.x, this->y-v.y, this->z-v.z);
}
inline float operator*(const float3& v) const { // dot product
return this->x*v.x+this->y*v.y+this->z*v.z;
}
inline bool operator==(const float3& v) const { // check for equality
return this->x==v.x&&this->y==v.y&&this->z==v.z;
}
inline bool operator!=(const float3& v) const { // check for inequality
return this->x!=v.x||this->y!=v.y||this->z!=v.z;
}
inline bool operator>(const float3& v) const { // compare length
return sq(this->x)+sq(this->y)+sq(this->z)>sq(v.x)+sq(v.y)+sq(v.z);
}
inline bool operator<(const float3& v) const { // compare length
return sq(this->x)+sq(this->y)+sq(this->z)<sq(v.x)+sq(v.y)+sq(v.z);
}
inline bool operator>=(const float3& v) const { // compare length
return sq(this->x)+sq(this->y)+sq(this->z)>=sq(v.x)+sq(v.y)+sq(v.z);
}
inline bool operator<=(const float3& v) const { // compare length
return sq(this->x)+sq(this->y)+sq(this->z)<=sq(v.x)+sq(v.y)+sq(v.z);
}
};
inline float3 operator+(const float3& v, const float x) { // elementwise addition
return float3(v.x+x, v.y+x, v.z+x);
}