Debian Codegraph @

tests/test_aligned_alloc.c

raw
1 /*
2 * Test that SIMDMalloc returns properly aligned memory and SIMDFree
3 * releases it without error.
4 *
5 * Compile:
6 * gcc -march=native tests/test_aligned_alloc.c -I src -I include -o test_aligned_alloc
7 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdint.h>
11 #include <assert.h>
12 #include "simd_instruction.h"
13
14 int main(void) {
15 int alignments[] = {16, 32, 64};
16 int sizes[] = {64, 1024, 65536};
17
18 for (int a = 0; a < 3; a++) {
19 for (int s = 0; s < 3; s++) {
20 void *ptr = SIMDMalloc(sizes[s], alignments[a]);
21 assert(ptr != NULL);
22 assert(((uintptr_t)ptr % alignments[a]) == 0);
23
24 /* Write to verify the memory is usable */
25 ((char *)ptr)[0] = 'A';
26 ((char *)ptr)[sizes[s] - 1] = 'Z';
27
28 SIMDFree(ptr);
29 }
30 }
31
32 printf("PASS: SIMDMalloc/SIMDFree alignment verified\n");
33 return 0;
34 }