-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory management.h
796 lines (688 loc) · 33.4 KB
/
memory management.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
/************************************************************
* File: memory management.h Created: 2008/12/08 *
* Last modified: 2025/02/19 *
* *
* Notes: 2024/05/02: Added support for data tracking. *
* *
* Copyright (c) David William Bull *
************************************************************/
#pragma once
#pragma intrinsic(_InterlockedExchange64)
#include <windows.h>
#include <corecrt_malloc.h>
#include "typedefs.h"
#include "common functions.h"
#ifdef DATA_TRACKING
#include "data tracking.h"
extern SYSTEM_DATA sysData;
#endif
#define _MEMORY_MANAGER_
#define malloc1(byteCount) malloc(byteCount, 1u)
#define malloc2(byteCount) malloc(byteCount, 2u)
#define malloc4(byteCount) malloc(byteCount, 4u)
#define malloc8(byteCount) malloc(byteCount, 8u)
#define malloc16(byteCount) malloc(byteCount, 16u)
#define malloc32(byteCount) malloc(byteCount, 32u)
#define malloc64(byteCount) malloc(byteCount, 64u)
// Declare 1-dimensional array at 16-byte boundary
#define declare1d16(dataType, variableName, dim) \
dataType *const variableName = (dataType *)malloc(RoundUpToNearest16(sizeof(dataType) * (dim)), 16u)
// Declare 1-dimensional array at 32-byte boundary
#define declare1d32(dataType, variableName, dim) \
dataType *const variableName = (dataType *)malloc(RoundUpToNearest32(sizeof(dataType) * (dim)), 32u)
// Declare 1-dimensional array at 64-byte boundary
#define declare1d64(dataType, variableName, dim) \
dataType *const variableName = (dataType *)malloc(RoundUpToNearest64(sizeof(dataType) * (dim)), 64u)
// Declare 2-dimensional array at 16-byte boundary
#define declare2d16(dataType, variableName, dim1, dim2) \
dataType (*const variableName)[dim2] = (dataType (*)[dim2])malloc(RoundUpToNearest16(sizeof(dataType) * ((dim1) * (dim2))), 16u)
// Declare 2-dimensional array at 32-byte boundary
#define declare2d32(dataType, variableName, dim1, dim2) \
dataType (*const variableName)[dim2] = (dataType (*)[dim2])malloc(RoundUpToNearest32(sizeof(dataType) * ((dim1) * (dim2))), 32u)
// Declare 2-dimensional array at 64-byte boundary
#define declare2d64(dataType, variableName, dim1, dim2) \
dataType (*const variableName)[dim2] = (dataType (*)[dim2])malloc(RoundUpToNearest64(sizeof(dataType) * ((dim1) * (dim2))), 64u)
// Declare 1-dimensional array at 16-byte boundary, then zero contents
#define declare1d16z(dataType, variableName, dim) \
dataType *const variableName = (dataType *)salloc(RoundUpToNearest16(sizeof(dataType) * (dim)), 16u, null128)
// Declare 1-dimensional array at 32-byte boundary, then zero contents
#define declare1d32z(dataType, variableName, dim) \
dataType *const variableName = (dataType *)salloc(RoundUpToNearest32(sizeof(dataType) * (dim)), 32u, null128)
// Declare 1-dimensional array at 64-byte boundary, then zero contents
#define declare1d64z(dataType, variableName, dim) \
dataType *const variableName = (dataType *)salloc(RoundUpToNearest64(sizeof(dataType) * (dim)), 64u, null128)
// Declare 2-dimensional array at 16-byte boundary, then zero contents
#define declare2d16z(dataType, variableName, dim1, dim2) \
dataType (*const variableName)[dim2] = (dataType (*)[dim2])salloc(RoundUpToNearest16(sizeof(dataType) * ((dim1) * (dim2))), 16u, null128)
// Declare 2-dimensional array at 32-byte boundary, then zero contents
#define declare2d32z(dataType, variableName, dim1, dim2) \
dataType (*const variableName)[dim2] = (dataType (*)[dim2])salloc(RoundUpToNearest32(sizeof(dataType) * ((dim1) * (dim2))), 32u, null128)
// Declare 2-dimensional array at 64-byte boundary, then zero contents
#define declare2d64z(dataType, variableName, dim1, dim2) \
dataType (*const variableName)[dim2] = (dataType (*)[dim2])salloc(RoundUpToNearest64(sizeof(dataType) * ((dim1) * (dim2))), 64u, null128)
// Declare 1-dimensional array at 16-byte boundary, then set the entire array to a repeating pattern of 8~512 bits
#define declare1d16s(dataType, variableName, dim, bitPattern) \
dataType *const variableName = (dataType *)salloc(RoundUpToNearest16(sizeof(dataType) * (dim)), 16u, bitPattern)
// Declare 1-dimensional array at 32-byte boundary, then set the entire array to a repeating pattern of 8~512 bits
#define declare1d32s(dataType, variableName, dim, bitPattern) \
dataType *const variableName = (dataType *)salloc(RoundUpToNearest32(sizeof(dataType) * (dim)), 32u, bitPattern)
// Declare 1-dimensional array at 64-byte boundary, then set the entire array to a repeating pattern of 8~512 bits
#define declare1d64s(dataType, variableName, dim, bitPattern) \
dataType *const variableName = (dataType *)salloc(RoundUpToNearest64(sizeof(dataType) * (dim)), 64u, bitPattern)
// Allocates RAM at 16-byte boundary, then sets the entire array to a repeating pattern of 8~512 bits
#define salloc16(byteCount, bitPattern) salloc(byteCount, 16u, bitPattern)
// Allocates RAM at 16-byte boundary, then sets the entire array to a repeating pattern of 8~512 bits
#define salloc1d16(dataType, dim, bitPattern) \
(dataType *)salloc(RoundUpToNearest16(sizeof(dataType) * (dim)), 16u, bitPattern)
// Allocates RAM at 16-byte boundary, then sets the entire array to a repeating pattern of 8~512 bits
#define salloc2d16(dataType, dim1, dim2, bitPattern) \
(dataType (*)[dim2])salloc(RoundUpToNearest16(sizeof(dataType) * ((dim1) * (dim2))), 16u, bitPattern)
// Allocates RAM at 32-byte boundary, then sets the entire array to a repeating pattern of 8~512 bits
#define salloc32(byteCount, bitPattern) salloc(byteCount, 32u, bitPattern)
// Allocates RAM at 32-byte boundary, then sets the entire array to a repeating pattern of 8~512 bits
#define salloc1d32(dataType, dim, bitPattern) \
(dataType *)salloc(RoundUpToNearest32(sizeof(dataType) * (dim)), 32u, bitPattern)
// Allocates RAM at 32-byte boundary, then sets the entire array to a repeating pattern of 8~512 bits
#define salloc2d32(dataType, dim1, dim2, bitPattern) \
(dataType (*)[dim2])salloc(RoundUpToNearest32(sizeof(dataType) * ((dim1) * (dim2))), 32u, bitPattern)
// Allocates RAM at 64-byte boundary, then sets the entire array to a repeating pattern of 8~512 bits
#define salloc64(byteCount, bitPattern) salloc(byteCount, 64u, bitPattern)
// Allocates RAM at 64-byte boundary, then sets the entire array to a repeating pattern of 8~512 bits
#define salloc1d64(dataType, dim, bitPattern) \
(dataType *)salloc(RoundUpToNearest64(sizeof(dataType) * (dim)), 64u, bitPattern)
// Allocates RAM at 64-byte boundary, then sets the entire array to a repeating pattern of 8~512 bits
#define salloc2d64(dataType, dim1, dim2, bitPattern) \
(dataType (*)[dim2])salloc(RoundUpToNearest64(sizeof(dataType) * ((dim1) * (dim2))), 64u, bitPattern)
// Allocates RAM at 16-byte boundary, then sets the entire array to zero
#define zalloc16(byteCount) salloc(byteCount, 16u, null128)
// Allocates RAM at 16-byte boundary, then sets the entire array to zero
#define zalloc1d16(dataType, dim) \
(dataType *)salloc(RoundUpToNearest16(sizeof(dataType) * (dim)), 16u, null128)
// Allocates RAM at 16-byte boundary, then sets the entire array to zero
#define zalloc2d16(dataType, dim1, dim2) \
(dataType (*)[dim2])salloc(RoundUpToNearest16(sizeof(dataType) * ((dim1) * (dim2))), 16u, null128)
#ifdef __AVX__
// Allocates RAM at 32-byte boundary, then sets the entire array to zero
#define zalloc32(byteCount) salloc(byteCount, 32u, null256)
// Allocates RAM at 32-byte boundary, then sets the entire array to zero
#define zalloc1d32(dataType, dim) \
(dataType *)salloc(RoundUpToNearest32(sizeof(dataType) * (dim)), 32u, null256)
// Allocates RAM at 32-byte boundary, then sets the entire array to zero
#define zalloc2d32(dataType, dim1, dim2) \
(dataType (*)[dim2])salloc(RoundUpToNearest32(sizeof(dataType) * ((dim1) * (dim2))), 32u, null256)
#else
// Allocates RAM at 32-byte boundary, then sets the entire array to zero
#define zalloc32(byteCount) salloc(byteCount, 32u, null128)
// Allocates RAM at 32-byte boundary, then sets the entire array to zero
#define zalloc1d32(dataType, dim) \
(dataType *)salloc(RoundUpToNearest32(sizeof(dataType) * (dim)), 32u, null128)
// Allocates RAM at 32-byte boundary, then sets the entire array to zero
#define zalloc2d32(dataType, dim1, dim2) \
(dataType (*)[dim2])salloc(RoundUpToNearest32(sizeof(dataType) * ((dim1) * (dim2))), 32u, null128)
#endif
#ifdef __AVX512__
// Allocates RAM at 64-byte boundary, then sets the entire array to zero
#define zalloc64(byteCount) salloc(byteCount, 64u, null512)
// Allocates RAM at 64-byte boundary, then sets the entire array to zero
#define zalloc1d64(dataType, dim) \
(dataType *)salloc(RoundUpToNearest64(sizeof(dataType) * (dim)), 64u, null512)
// Allocates RAM at 64-byte boundary, then sets the entire array to zero
#define zalloc2d64(dataType, dim1, dim2) \
(dataType (*)[dim2])salloc(RoundUpToNearest64(sizeof(dataType) * ((dim1) * (dim2))), 64u, null512)
#else
#ifdef __AVX__
// Allocates RAM at 64-byte boundary, then sets the entire array to zero
#define zalloc64(byteCount) salloc(byteCount, 64u, null256)
// Allocates RAM at 64-byte boundary, then sets the entire array to zero
#define zalloc1d64(dataType, dim) \
(dataType *)salloc(RoundUpToNearest64(sizeof(dataType) * (dim)), 64u, null256)
// Allocates RAM at 64-byte boundary, then sets the entire array to zero
#define zalloc2d64(dataType, dim1, dim2) \
(dataType (*)[dim2])salloc(RoundUpToNearest64(sizeof(dataType) * ((dim1) * (dim2))), 64u, null256)
#else
// Allocates RAM at 64-byte boundary, then sets the entire array to zero
#define zalloc64(byteCount) salloc(byteCount, 64, null128)
// Allocates RAM at 64-byte boundary, then sets the entire array to zero
#define zalloc1d64(dataType, dim) \
(dataType *)salloc(RoundUpToNearest64(sizeof(dataType) * (dim)), 64u, null128)
// Allocates RAM at 64-byte boundary, then sets the entire array to zero
#define zalloc2d64(dataType, dim1, dim2) \
(dataType (*)[dim2])salloc(RoundUpToNearest64(sizeof(dataType) * ((dim1) * (dim2))), 64u, null128)
#endif
#endif
// Allocate RAM at aligned boundary
inline ptrc malloc(csize_t byteCount, csize_t alignment) {
ptrc pointer = _aligned_malloc(byteCount, alignment);
#ifdef DATA_TRACKING
if(pointer) {
sysData.mem.byteCount[sysData.mem.allocations] = byteCount;
sysData.mem.location[sysData.mem.allocations++] = pointer;
sysData.mem.allocated += byteCount;
}
#endif
return pointer;
}
// Allocates RAM at aligned boundary, then sets the entire array to a repeating 8-bit pattern
inline ptrc salloc(csize_t numBytes, csize_t alignment, cui8 bitPattern) {
ptrc pointer = _aligned_malloc(numBytes, alignment);
if(pointer) {
#ifdef DATA_TRACKING
sysData.mem.byteCount[sysData.mem.allocations] = numBytes;
sysData.mem.location[sysData.mem.allocations++] = pointer;
sysData.mem.allocated += numBytes;
#endif
cui64 bitPat8 = ui64(bitPattern);
cui64 bitPat16 = bitPat8 | (bitPat8 << 8u);
cui64 bitPat32 = bitPat16 | (bitPat16 << 16u);
cui64 bitPat64 = bitPat32 | (bitPat32 << 32u);
cui64 limit = numBytes >> 3;
ui64 os;
for(os = 0; os < limit; os++) ((ui64ptr)pointer)[os] = bitPat64;
for(os <<= 3; os < numBytes; os++) ((ui8ptr)pointer)[os] = ((ui8 (&)[8])bitPat64)[os & 0x07];
}
return pointer;
}
// Allocates RAM at aligned boundary, then sets the entire array to a repeating 16-bit pattern
inline ptrc salloc(csize_t numBytes, csize_t alignment, cui16 bitPattern) {
ptrc pointer = _aligned_malloc(numBytes, alignment);
if(pointer) {
#ifdef DATA_TRACKING
sysData.mem.byteCount[sysData.mem.allocations] = numBytes;
sysData.mem.location[sysData.mem.allocations++] = pointer;
sysData.mem.allocated += numBytes;
#endif
cui64 bitPat16 = ui64(bitPattern);
cui64 bitPat32 = bitPat16 | (bitPat16 << 16u);
cui64 bitPat64 = bitPat32 | (bitPat32 << 32u);
cui64 limit = numBytes >> 3;
ui64 os;
for(os = 0; os < limit; os++) ((ui64ptr)pointer)[os] = bitPat64;
for(os <<= 3; os < numBytes; os++) ((ui8ptr)pointer)[os] = ((ui8 (&)[8])bitPat64)[os & 0x07];
}
return pointer;
}
// Allocates RAM at aligned boundary, then sets the entire array to a repeating 32-bit pattern
inline ptrc salloc(csize_t numBytes, csize_t alignment, cui32 bitPattern) {
ptrc pointer = _aligned_malloc(numBytes, alignment);
if(pointer) {
#ifdef DATA_TRACKING
sysData.mem.byteCount[sysData.mem.allocations] = numBytes;
sysData.mem.location[sysData.mem.allocations++] = pointer;
sysData.mem.allocated += numBytes;
#endif
cui64 bitPat32 = ui64(bitPattern);
cui64 bitPat64 = bitPat32 | (bitPat32 << 32u);
cui64 limit = numBytes >> 3;
ui64 os;
for(os = 0; os < limit; os++) ((ui64ptr)pointer)[os] = bitPat64;
for(os <<= 3; os < numBytes; os++) ((ui8ptr)pointer)[os] = ((ui8 (&)[8])bitPat64)[os & 0x07];
}
return pointer;
}
// Allocates RAM at aligned boundary, then sets the entire array to a repeating 64-bit pattern
inline ptrc salloc(csize_t numBytes, csize_t alignment, cui64 bitPattern) {
ptrc pointer = _aligned_malloc(numBytes, alignment);
if(pointer) {
#ifdef DATA_TRACKING
sysData.mem.byteCount[sysData.mem.allocations] = numBytes;
sysData.mem.location[sysData.mem.allocations++] = pointer;
sysData.mem.allocated += numBytes;
#endif
cui64 limit = numBytes >> 3;
ui64 os;
for(os = 0; os < limit; os++) ((ui64ptr)pointer)[os] = bitPattern;
for(os <<= 3; os < numBytes; os++) ((ui8ptr)pointer)[os] = ((ui8 (&)[8])bitPattern)[os & 0x07];
}
return pointer;
}
// Allocates RAM at aligned (to multiple of 16 byte) boundary, then sets the entire array to a repeating 128-bit pattern
inline ptrc salloc(csize_t numBytes, csize_t alignment, cui128 bitPattern) {
ptrc pointer = _aligned_malloc(numBytes, alignment);
if(pointer) {
#ifdef DATA_TRACKING
sysData.mem.byteCount[sysData.mem.allocations] = numBytes;
sysData.mem.location[sysData.mem.allocations++] = pointer;
sysData.mem.allocated += numBytes;
#endif
cui64 limit = numBytes >> 4;
ui64 os;
for(os = 0; os < limit; os++) ((ui128ptr)pointer)[os] = bitPattern;
for(os <<= 4; os < numBytes; os++) ((ui8ptr)pointer)[os] = ((ui8 (&)[16])bitPattern)[os & 0x0F];
}
return pointer;
}
// Allocates RAM at aligned (to multiple of 32 byte) boundary, then sets the entire array to a repeating 256-bit pattern
inline ptrc salloc(csize_t numBytes, csize_t alignment, cui256 bitPattern) {
ptrc pointer = _aligned_malloc(numBytes, alignment);
if(pointer) {
#ifdef DATA_TRACKING
sysData.mem.byteCount[sysData.mem.allocations] = numBytes;
sysData.mem.location[sysData.mem.allocations++] = pointer;
sysData.mem.allocated += numBytes;
#endif
cui64 limit = numBytes >> 5;
ui64 os;
for(os = 0; os < limit; os++) ((ui256ptr)pointer)[os] = bitPattern;
for(os <<= 5; os < numBytes; os++) ((ui8ptr)pointer)[os] = ((ui8 (&)[32])bitPattern)[os & 0x01F];
}
return pointer;
}
// Allocates RAM at aligned (to multiple of 64 byte) boundary, then sets the entire array to a repeating 512-bit pattern
inline ptrc salloc(csize_t numBytes, csize_t alignment, cui512 bitPattern) {
ptrc pointer = _aligned_malloc(numBytes, alignment);
if(pointer) {
#ifdef DATA_TRACKING
sysData.mem.byteCount[sysData.mem.allocations] = numBytes;
sysData.mem.location[sysData.mem.allocations++] = pointer;
sysData.mem.allocated += numBytes;
#endif
cui64 limit = numBytes >> 6;
ui64 os;
for(os = 0; os < limit; os++) ((ui512ptr)pointer)[os] = bitPattern;
for(os <<= 3; os < (numBytes >> 3); os++) ((ui32ptr)pointer)[os] = ((ui8 (&)[16])bitPattern)[os & 0x0F];
for(os <<= 6; os < numBytes; os++) ((ui8ptr)pointer)[os] = ((ui8 (&)[64])bitPattern)[os & 0x03F];
}
return pointer;
}
// Frees a pointer and returns true if successful.
#define mfree1(pointer) mdealloc(pointer)
// Frees a variable number of pointers. Each bit represents each pointer (in argument order), and will be true if the relevant pointer is freed.
#define mfree(pointer, ...) mdealloc_(pointer, __VA_ARGS__, -1ll)
// Frees a pointer and returns true if successful.
inline cui64 mdealloc(ptrc pointer) {
if(pointer) {
#ifdef DATA_TRACKING
cui32 allocations = sysData.mem.allocations;
vptrptrc location = sysData.mem.location;
ui32 index = 0;
// Find entry
while(pointer != location[index] && allocations >= index) index++;
// Is the pointer invalid?
if(index >= allocations) return false;
--sysData.mem.allocations;
sysData.mem.allocated -= sysData.mem.byteCount[index];
sysData.mem.location[index] = 0;
sysData.mem.byteCount[index] = 0;
location[index] = 0;
#endif
_aligned_free(pointer);
return true;
}
return false;
}
// Frees a variable number of pointers. Each bit represents each pointer (in argument order), and will be true if the relevant pointer is freed.
inline cui64 mdealloc_(ptr pointer, ...) {
va_list val; va_start(val, pointer);
ui64 retVal = 0, ptrBit = 0x01u;
for(; (ui64 &)pointer != -1; pointer = va_arg(val, ptrc)) {
if(pointer) {
#ifdef DATA_TRACKING
cui32 allocations = sysData.mem.allocations;
vptrptrc location = sysData.mem.location;
ui32 index = 0;
// Find entry
while(pointer != location[index] && allocations >= index) index++;
// Is the pointer invalid?
if(index >= allocations) continue;
--sysData.mem.allocations;
sysData.mem.allocated -= sysData.mem.byteCount[index];
sysData.mem.location[index] = 0;
sysData.mem.byteCount[index] = 0;
location[index] = 0;
#endif
_aligned_free(pointer);
retVal |= ptrBit;
ptrBit <<= 1;
}
}
va_end(val);
return retVal;
}
// Set a region of memory to zero
inline void mzero(ptrc addr, cui64 numBytes) {
ui64 i;
#ifdef __AVX512__
if(numBytes & 0x02Fu) {
#endif
#ifdef __AVX__
if(numBytes & 0x01Fu) {
#endif
if(numBytes & 0x0F) {
cui64 count = numBytes >> 3;
for(i = 0; i < count; ++i) ((ui64ptr)addr)[i] = 0ull;
for(i <<= 3; i < numBytes; ++i) ((ui8ptr)addr)[i] = 0u;
return;
}
cui64 count = numBytes >> 4;
for(i = 0; i < count; ++i) ((ui128ptr)addr)[i] = null128;
for(i <<= 2; i < (numBytes >> 2); ++i) ((ui32ptr)addr)[i] = 0u;
for(i <<= 2; i < numBytes; ++i) ((ui8ptr)addr)[i] = 0u;
return;
#ifdef __AVX__
}
cui64 count = numBytes >> 5;
for(i = 0; i < count; ++i) ((ui256ptr)addr)[i] = null256;
for(i <<= 3; i < (numBytes >> 2); ++i) ((ui32ptr)addr)[i] = 0u;
for(i <<= 2; i < numBytes; ++i) ((ui8ptr)addr)[i] = 0u;
return;
#endif
#ifdef __AVX512__
}
cui64 count = numBytes >> 6;
for(i = 0; i < count; ++i) ((ui512ptr)addr)[i] = null512;
for(i <<= 3; i < (numBytes >> 3); ++i) ((ui64ptr)addr)[i] = 0u;
for(i <<= 3; i < numBytes; ++i) ((ui8ptr)addr)[i] = 0u;
#endif
}
// Set a region of memory to zero
inline void mzero(vptrc addr, cui64 numBytes) {
ui64 i;
#ifdef __AVX512__
if(numBytes & 0x02F) {
#endif
#ifdef __AVX__
if(numBytes & 0x01F) {
#endif
if(numBytes & 0x0F) {
cui64 count = numBytes >> 3;
for(i = 0; i < count; ++i) ((ui64ptr)addr)[i] = 0ull;
for(i <<= 3; i < numBytes; ++i) ((ui8ptr)addr)[i] = 0u;
return;
}
cui64 count = numBytes >> 4;
for(i = 0; i < count; ++i) ((ui128ptr)addr)[i] = null128;
for(i <<= 2; i < (numBytes >> 2); ++i) ((ui32ptr)addr)[i] = 0u;
for(i <<= 2; i < numBytes; ++i) ((ui8ptr)addr)[i] = 0u;
return;
#ifdef __AVX__
}
cui64 count = numBytes >> 5;
for(i = 0; i < count; ++i) ((ui256ptr)addr)[i] = null256;
for(i <<= 3; i < (numBytes >> 2); ++i) ((ui32ptr)addr)[i] = 0u;
for(i <<= 2; i < numBytes; ++i) ((ui8ptr)addr)[i] = 0u;
return;
#endif
#ifdef __AVX512__
}
cui64 count = numBytes >> 6;
for(i = 0; i < count; ++i) ((ui512ptr)addr)[i] = null512;
for(i <<= 3; i < (numBytes >> 3); ++i) ((ui64ptr)addr)[i] = 0u;
for(i <<= 3; i < numBytes; ++i) ((ui8ptr)addr)[i] = 0u;
#endif
}
// Set a region of memory to zero
inline void mzero(ui128ptrc addr, cui64 numBytes) {
ui64 i;
cui64 count = numBytes >> 4;
for(i = 0; i < count; ++i) ((ui128ptr)addr)[i] = null128;
for(i <<= 2; i < (numBytes >> 2); ++i) ((ui32ptr)addr)[i] = 0u;
for(i <<= 2; i < numBytes; ++i) ((ui8ptr)addr)[i] = 0u;
return;
}
// Set a region of memory to zero
inline void mzero(ui256ptrc addr, cui64 numBytes) {
ui64 i;
#ifdef __AVX__
cui64 count = numBytes >> 5;
for(i = 0; i < count; ++i) ((ui256ptr)addr)[i] = null256;
for(i <<= 3; i < (numBytes >> 2); ++i) ((ui32ptr)addr)[i] = 0u;
for(i <<= 2; i < numBytes; ++i) ((ui8ptr)addr)[i] = 0u;
#else
cui64 count = numBytes >> 4;
for(i = 0; i < count; ++i) ((ui128ptr)addr)[i] = null128;
for(i <<= 2; i < (numBytes >> 2); ++i) ((ui32ptr)addr)[i] = 0u;
for(i <<= 2; i < numBytes; ++i) ((ui8ptr)addr)[i] = 0u;
#endif
return;
}
// Set a region of memory to zero
inline void mzero(ui512ptrc addr, cui64 numBytes) {
ui64 i;
#ifdef __AVX512__
cui64 count = numBytes >> 6;
for(i = 0; i < count; ++i) ((ui512ptr)addr)[i] = null512;
for(i <<= 3; i < (numBytes >> 3); ++i) ((ui64ptr)addr)[i] = 0u;
for(i <<= 3; i < numBytes; ++i) ((ui8ptr)addr)[i] = 0u;
#else
#ifdef __AVX__
cui64 count = numBytes >> 5;
for(i = 0; i < count; ++i) ((ui256ptr)addr)[i] = null256;
for(i <<= 3; i < (numBytes >> 2); ++i) ((ui32ptr)addr)[i] = 0u;
for(i <<= 2; i < numBytes; ++i) ((ui8ptr)addr)[i] = 0u;
#else
cui64 count = numBytes >> 4;
for(i = 0; i < count; ++i) ((ui128ptr)addr)[i] = null128;
for(i <<= 2; i < (numBytes >> 2); ++i) ((ui32ptr)addr)[i] = 0u;
for(i <<= 2; i < numBytes; ++i) ((ui8ptr)addr)[i] = 0u;
#endif
#endif
return;
}
// Set a region of memory to a repeating pattern
#define setmem(addr, numBytes, bitPattern) mset(addr, numBytes, bitPattern)
// Set a region of 16-byte-aligned memory to a repeating 128-bit pattern
inline void mset(ptrc addr, cui64 numBytes, cui128 bitPattern) {
cui64 limit = numBytes >> 4;
ui64 os;
for(os = 0; os < limit; os++) ((ui128ptr)addr)[os] = bitPattern;
for(os <<= 4; os < numBytes; os++) ((ui8ptr)addr)[os] = bitPattern.m128i_u8[os & 0x0F];
}
// Set a region of 32-byte-aligned memory to a repeating 256-bit pattern
inline void mset(ptrc addr, cui64 numBytes, cui256 bitPattern) {
cui64 limit = numBytes >> 5;
ui64 os;
for(os = 0; os < limit; os++) ((ui256ptr)addr)[os] = bitPattern;
for(os <<= 5; os < numBytes; os++) ((ui8ptr)addr)[os] = bitPattern.m256i_u8[os & 0x01F];
}
// Set a region of 64-byte-aligned memory to a repeating 512-bit pattern
inline void mset(ptrc addr, cui64 numBytes, cui512 bitPattern) {
cui64 limit = numBytes >> 6;
ui64 os;
for(os = 0; os < limit; os++) ((ui512ptr)addr)[os] = bitPattern;
for(os <<= 3; os < (numBytes >> 3); os++) ((ui64ptr)addr)[os] = bitPattern.m512i_u64[os & 0x0F];
for(os <<= 3; os < numBytes; os++) ((ui8ptr)addr)[os] = bitPattern.m512i_u8[os & 0x03F];
}
#ifdef __AVX512__
#define _mm_PB64_mm_ cui512 { .m512i_u64 = { bitPattern64, bitPattern64, bitPattern64, bitPattern64, \
bitPattern64, bitPattern64, bitPattern64, bitPattern64 } }
#else
#ifdef __AVX__
#define _mm_PB64_mm_ cui256 { .m256i_u64 = { bitPattern64, bitPattern64, bitPattern64, bitPattern64 } }
#else
#define _mm_PB64_mm_ cui128 { .m128i_u64 = { bitPattern64, bitPattern64 } }
#endif
#endif
// Set a region of 64-byte-aligned memory to a repeating 8-bit pattern
inline void mset(ptrc addr, cui64 numBytes, cui8 bitPattern8) {
cui64 bitPattern = ui64(bitPattern8);
cui64 bitPattern16 = bitPattern | (bitPattern << 8u);
cui64 bitPattern32 = bitPattern16 | (bitPattern16 << 16u);
cui64 bitPattern64 = bitPattern32 | (bitPattern32 << 32u);
mset(addr, numBytes, _mm_PB64_mm_);
}
// Set a region of 64-byte-aligned memory to a repeating 16-bit pattern
inline void mset(ptrc addr, cui64 numBytes, cui16 bitPattern16) {
cui64 bitPattern = ui64(bitPattern16);
cui64 bitPattern32 = bitPattern | (bitPattern << 16u);
cui64 bitPattern64 = bitPattern32 | (bitPattern32 << 32u);
mset(addr, numBytes, _mm_PB64_mm_);
}
// Set a region of 64-byte-aligned memory to a repeating 32-bit pattern
inline void mset(ptrc addr, cui64 numBytes, cui32 bitPattern32) {
cui64 bitPattern = ui64(bitPattern32);
cui64 bitPattern64 = bitPattern | (bitPattern << 32u);
mset(addr, numBytes, _mm_PB64_mm_);
}
// Set a region of 64-byte-aligned memory to a repeating 64-bit pattern
inline void mset(ptrc addr, cui64 numBytes, cui64 bitPattern64) {
mset(addr, numBytes, _mm_PB64_mm_);
}
#undef _mm_PB64_mm_
// Copy byteCount bytes of unaligned data
inline void Copy(cptrc source, ptrc dest, cui64 byteCount) {
cui64 k = byteCount >> 2;
ui64 i = 0;
#ifdef __AVX512__
cui64 j = byteCount >> 6;
for(; i < j; i++) ((ui512ptr)dest)[i] = _mm512_loadu_epi64(&((ui512ptr)source)[i]);
i <<= 4;
#else
#ifdef __AVX__
cui64 j = byteCount >> 5;
for(; i < j; i++) ((ui256ptr)dest)[i] = _mm256_lddqu_si256(&((ui256ptr)source)[i]);
i <<= 3;
#else
cui64 j = byteCount >> 4;
for(; i < j; i++) ((ui128ptr)dest)[i] = _mm_lddqu_si128(&((ui128ptr)source)[i]);
i <<= 2;
#endif
#endif
for(; i < k; i++) ((ui32ptr)dest)[i] = ((ui32ptr)source)[i];
for(i = k << 2; i < byteCount; i++) ((ui8ptr)dest)[i] = ((ui8ptr)source)[i];
}
// Copy byteCount (rounded-down to the nearest 8) bytes of data
inline void Copy8(cptrc source, ptrc dest, cui64 byteCount) {
cui64 j = byteCount >> 3;
for(ui64 i = 0; i < j; i++) ((ui64ptr)dest)[i] = ((ui64ptr)source)[i];
}
// Copy byteCount (rounded-down to the nearest 8) bytes of data
inline void Copy8(vptrc source, vptrc dest, cui64 byteCount) {
cui64 j = byteCount >> 3;
for(ui64 i = 0; i < j; i++) ((vui64ptr)dest)[i] = ((vui64ptr)source)[i];
}
// Copy byteCount (rounded-down to the nearest 16) bytes of 128-bit-aligned data via SIMD instruction
inline void Copy16(cptrc source, ptrc dest, cui64 byteCount) {
cui64 j = byteCount >> 4;
for(ui64 i = 0; i < j; i++) ((ui128ptr)dest)[i] = _mm_load_si128(&((ui128ptr)source)[i]);
}
// Copy byteCount (rounded-down to the nearest 16) bytes of 128-bit-aligned data via SIMD instruction
inline void Copy16(vptrc source, vptrc dest, cui64 byteCount) {
cui64 j = byteCount >> 4;
for(ui64 i = 0; i < j; i++) ((ui128ptr)dest)[i] = _mm_load_si128(&((ui128ptr)source)[i]);
}
// Copy byteCount (rounded-down to the nearest 32) bytes of 256-bit-aligned data via SIMD instruction
inline void Copy32(cptrc source, ptrc dest, cui64 byteCount) {
#ifdef __AVX__
cui64 j = byteCount >> 5;
for(ui64 i = 0; i < j; i++) ((ui256ptr)dest)[i] = _mm256_load_si256(&((ui256ptr)source)[i]);
#else
cui64 j = byteCount >> 4;
for(ui64 i = 0; i < j; i++) ((ui128ptr)dest)[i] = _mm_load_si128(&((ui128ptr)source)[i]);
#endif
}
// Copy byteCount (rounded-down to the nearest 32) bytes of 256-bit-aligned data via SIMD instruction
inline void Copy32(vptrc source, vptrc dest, cui64 byteCount) {
#ifdef __AVX__
cui64 j = byteCount >> 5;
for(ui64 i = 0; i < j; i++) ((ui256ptr)dest)[i] = _mm256_load_si256(&((ui256ptr)source)[i]);
#else
cui64 j = byteCount >> 4;
for(ui64 i = 0; i < j; i++) ((ui128ptr)dest)[i] = _mm_load_si128(&((ui128ptr)source)[i]);
#endif
}
// Copy byteCount (rounded-down to the nearest 64) bytes of 512-bit-aligned data via SIMD instruction
inline void Copy64(cptrc source, ptrc dest, cui64 byteCount) {
#ifdef __AVX512__
cui64 j = byteCount >> 6;
for(ui64 i = 0; i < j; i++) ((ui512ptr)dest)[i] = _mm512_load_epi32(&((ui512ptr)source)[i]);
#else
#ifdef __AVX__
cui64 j = byteCount >> 5;
for(ui64 i = 0; i < j; i++) ((ui256ptr)dest)[i] = _mm256_load_si256(&((ui256ptr)source)[i]);
#else
cui64 j = byteCount >> 4;
for(ui64 i = 0; i < j; i++) ((ui128ptr)dest)[i] = _mm_load_si128(&((ui128ptr)source)[i]);
#endif
#endif
}
// Copy byteCount (rounded-down to the nearest 64) bytes of 512-bit-aligned data via SIMD instruction
inline void Copy64(vptrc source, vptrc dest, cui64 byteCount) {
#ifdef __AVX512__
cui64 j = ui64((byteCount + 63) >> 6);
for(ui64 i = 0; i < j; i++) ((ui512ptr)dest)[i] = _mm512_load_epi32(&((ui512ptr)source)[i]);
#else
#ifdef __AVX__
cui64 j = ui64((byteCount + 31) >> 5);
for(ui64 i = 0; i < j; i++) ((ui256ptr)dest)[i] = _mm256_load_si256(&((ui256ptr)source)[i]);
#else
cui64 j = ui64((byteCount + 15) >> 4);
for(ui64 i = 0; i < j; i++) ((ui128ptr)dest)[i] = _mm_load_si128(&((ui128ptr)source)[i]);
#endif
#endif
}
// Non-temporally copy byteCount (rounded-down to the nearest 16) bytes of 128-bit-aligned data via SIMD instruction
inline void Stream16(cptrc source, ptrc dest, cui64 byteCount) {
#ifdef __AVX__
cui64 j = byteCount >> 4;
for(ui64 i = 0; i < j; i++) ((ui128ptr)dest)[i] = _mm_stream_load_si128(&((ui128ptr)source)[i]);
#else
cui64 j = byteCount >> 4;
for(ui64 i = 0; i < j; i++) _mm_stream_si128(&((ui128ptr)dest)[i], ((ui128ptr)source)[i]);
#endif
}
// Non-temporally copy byteCount (rounded-down to the nearest 32) bytes of 256-bit-aligned data via SIMD instruction
inline void Stream32(cptrc source, ptrc dest, cui64 byteCount) {
#ifdef __AVX__
cui64 j = byteCount >> 5;
for(ui64 i = 0; i < j; i++) ((ui256ptr)dest)[i] = _mm256_stream_load_si256(&((ui256ptr)source)[i]);
#else
cui64 j = byteCount >> 4;
for(ui64 i = 0; i < j; i++) ((ui128ptr)dest)[i] = _mm_stream_load_si128(&((ui128ptr)source)[i]);
#endif
}
// Non-temporally copy byteCount (rounded-down to the nearest 64) bytes of 512-bit-aligned data via SIMD instruction
inline void Stream64(cptrc source, ptrc dest, cui64 byteCount) {
#ifdef __AVX512__
cui64 j = ui64((byteCount + 63) >> 6);
for(ui64 i = 0; i < j; i++) ((ui512ptr)dest)[i] = _mm512_stream_load_epi32(&((ui512ptr)source)[i]);
#else
#ifdef __AVX__
cui64 j = ui64((byteCount + 31) >> 5);
for(ui64 i = 0; i < j; i++)
((ui256ptr)dest)[i] = _mm256_stream_load_si256(&((ui256ptr)source)[i]);
#else
cui64 j = ui64((byteCount + 15) >> 4);
for(ui64 i = 0; i < j; i++) ((ui128ptr)dest)[i] = _mm_stream_load_si128(&((ui128ptr)source)[i]);
#endif
#endif
}
// (Non-temporally) Copy byteCount (rounded-up to the nearest 16/32/64) bytes of 128/256/512-bit-aligned data via SIMD instruction.
// If either source or dest is unaligned, standard copy is used.
inline void Stream(cptrc source, ptrc dest, cui64 byteCount) {
if(((ui64 &)source & 0x0Fu) || ((ui64 &)dest & 0x0Fu)) Copy(source, dest, byteCount);
else if(((ui64 &)source & 0x010u) || ((ui64 &)dest & 0x010u)) Stream16(source, dest, RoundUpToNearest16(byteCount));
else if(((ui64 &)source & 0x020u) || ((ui64 &)dest & 0x020u)) Stream32(source, dest, RoundUpToNearest32(byteCount));
else Stream64(source, dest, RoundUpToNearest64(byteCount));
}
// Interlock copy byteCount (rounded-down to the nearest 8) bytes of data
inline void LockedCopy(ptrc source, vptrc dest, csi32 byteCount) {
csi32 j = (byteCount + 7) >> 3;
for(si32 i = 0; i < j; i++) _InterlockedExchange64(&((vsi64ptr)dest)[i], ((si64ptr)source)[i]);
}
// Interlock copy byteCount (rounded-down to the nearest 8) bytes of data
inline void LockedCopy(vptrc source, vptrc dest, csi32 byteCount) {
csi32 j = (byteCount + 7) >> 3;
for(si32 i = 0; i < j; i++) _InterlockedExchange64(&((vsi64ptr)dest)[i], ((si64ptr)source)[i]);
}
// Interlock swap byteCount (rounded-down to the nearest 8) bytes of data
inline void LockedSwap(ptrc source1, vptrc source2, csi32 byteCount) {
csi32 j = (byteCount + 7) >> 3;
for(si32 i = 0; i < j; i++) ((vsi64ptr)source1)[i] = _InterlockedExchange64(&((vsi64ptr)source2)[i], ((si64ptr)source1)[i]);
}
// Interlock swap byteCount (rounded-down to the nearest 8) bytes of data
inline void LockedSwap(vptrc source1, vptrc source2, csi32 byteCount) {
csi32 j = (byteCount + 7) >> 3;
for(si32 i = 0; i < j; i++) ((vsi64ptr)source1)[i] = _InterlockedExchange64(&((vsi64ptr)source2)[i], ((si64ptr)source1)[i]);
}
// Interlock move byteCount (rounded-down to the nearest 8) bytes of data and zeroes source
inline void LockedMoveAndClear(vptrc source, ptrc dest, csi32 byteCount) {
csi32 j = (byteCount + 7) >> 3;
for(si32 i = 0; i < j; i++) ((vsi64ptr)dest)[i] = _InterlockedExchange64(&((vsi64ptr)source)[i], 0);
}
// Interlock move byteCount (rounded-down to the nearest 8) bytes of data and zeroes source
inline void LockedMoveAndClear(vptrc source, vptrc dest, csi32 byteCount) {
csi32 j = (byteCount + 7) >> 3;
for(si32 i = 0; i < j; i++)
((vsi64ptr)dest)[i] = _InterlockedExchange64(&((vsi64ptr)source)[i], 0);
}
#undef bitPatternx8