comparison test/test_arith.h @ 2:f12b5dd4e18c

*: many new operations and a real test suite
author Paper <paper@tflc.us>
date Tue, 22 Oct 2024 22:39:05 -0400
parents
children 3c5545b1568f
comparison
equal deleted inserted replaced
1:1d9d2308c1d2 2:f12b5dd4e18c
1 #define CREATE_TEST(sign, psign, bits, size, op, equiv) \
2 static int test_arith_v##sign##int##bits##x##size##_##op(v##sign##int##bits##x##size a, v##sign##int##bits##x##size b) \
3 { \
4 sign##int##bits##_t orig_a[size], orig_b[size], orig_c[size]; \
5 \
6 v##sign##int##bits##x##size c = v##sign##int##bits##x##size##_##op(a, b); \
7 \
8 v##sign##int##bits##x##size##_store(a, orig_a); \
9 v##sign##int##bits##x##size##_store(b, orig_b); \
10 v##sign##int##bits##x##size##_store(c, orig_c); \
11 \
12 for (int i = 0; i < size; i++) { \
13 if ((sign##int##bits##_t)(equiv) != orig_c[i]) { \
14 fprintf(stderr, "v" #sign "int" #bits "x" #size "_" #op " test FAILED at index %d: (" #equiv ") [%" PRI ## psign ## bits "] does not equal result [%" PRI ## psign ## bits "]!\n", i, equiv, orig_c[i]); \
15 print_v##sign##int##bits##x##size(stderr,a); \
16 print_v##sign##int##bits##x##size(stderr,b); \
17 print_v##sign##int##bits##x##size(stderr,c); \
18 fprintf(stderr, "\n"); \
19 return 1; \
20 } \
21 } \
22 \
23 return 0; \
24 }
25
26 #define CREATE_TEST_SHIFT(sign, psign, bits, size, op, equiv) \
27 static int test_arith_v##sign##int##bits##x##size##_##op(v##sign##int##bits##x##size a, vuint##bits##x##size b) \
28 { \
29 sign##int##bits##_t orig_a[size], orig_c[size]; \
30 uint##bits##_t orig_b[size]; \
31 \
32 v##sign##int##bits##x##size c = v##sign##int##bits##x##size##_##op(a, b); \
33 \
34 v##sign##int##bits##x##size##_store(a, orig_a); \
35 vuint##bits##x##size##_store(b, orig_b); \
36 v##sign##int##bits##x##size##_store(c, orig_c); \
37 \
38 for (int i = 0; i < size; i++) { \
39 if ((sign##int##bits##_t)(equiv) != orig_c[i]) { \
40 fprintf(stderr, "v" #sign "int" #bits "x" #size "_" #op " test FAILED at index %d: (" #equiv ") [%" PRI ## psign ## bits "] does not equal result [%" PRI ## psign ## bits "]!\n", i, equiv, orig_c[i]); \
41 print_v##sign##int##bits##x##size(stderr,a); \
42 print_vuint##bits##x##size(stderr,b); \
43 print_v##sign##int##bits##x##size(stderr,c); \
44 fprintf(stderr, "\n"); \
45 return 1; \
46 } \
47 } \
48 \
49 return 0; \
50 }
51
52 #define CREATE_TESTS(sign, psign, bits, size) \
53 CREATE_TEST(sign, psign, bits, size, add, orig_a[i] + orig_b[i]) \
54 CREATE_TEST(sign, psign, bits, size, sub, orig_a[i] - orig_b[i]) \
55 CREATE_TEST(sign, psign, bits, size, mul, orig_a[i] * orig_b[i]) \
56 CREATE_TEST(sign, psign, bits, size, div, (orig_b[i]) ? (orig_a[i] / orig_b[i]) : 0) \
57 CREATE_TEST(sign, psign, bits, size, and, orig_a[i] & orig_b[i]) \
58 CREATE_TEST(sign, psign, bits, size, or, orig_a[i] | orig_b[i]) \
59 CREATE_TEST(sign, psign, bits, size, xor, orig_a[i] ^ orig_b[i]) \
60 CREATE_TEST(sign, psign, bits, size, avg, (orig_a[i] * orig_b[i]) / 2) \
61 CREATE_TEST_SHIFT(sign, psign, bits, size, rshift, vec_##sign##rshift(orig_a[i], orig_b[i])) \
62 CREATE_TEST_SHIFT(sign, psign, bits, size, lshift, vec_##sign##lshift(orig_a[i], orig_b[i])) \
63 CREATE_TEST_SHIFT(sign, psign, bits, size, lrshift, vec_##sign##lrshift(orig_a[i], orig_b[i]))
64
65 #define CREATE_TESTS_2(bits, size) \
66 CREATE_TESTS(, d, bits, size) \
67 CREATE_TESTS(u, u, bits, size)
68
69 CREATE_TESTS_2(8, 16)
70 CREATE_TESTS_2(16, 8)
71 CREATE_TESTS_2(32, 4)
72 CREATE_TESTS_2(64, 2)
73
74 #undef CREATE_TESTS_2
75 #undef CREATE_TESTS
76 #undef CREATE_TEST
77
78 static int test_arith(void)
79 {
80 int ret = 0;
81
82 #define RUN_TESTS(sign, bits, size) \
83 for (size_t i = 0U; i < ARRAY_SIZE(testval##sign##bits); i++) { \
84 const v##sign##int##bits##x##size a = vtest##sign##bits##x##size(i); \
85 for (size_t j = 0U; j < ARRAY_SIZE(testval##sign##bits); j++) { \
86 const v##sign##int##bits##x##size b = vtest##sign##bits##x##size(j); \
87 ret |= test_arith_v##sign##int##bits##x##size##_add(a, b); \
88 ret |= test_arith_v##sign##int##bits##x##size##_sub(a, b); \
89 ret |= test_arith_v##sign##int##bits##x##size##_mul(a, b); \
90 ret |= test_arith_v##sign##int##bits##x##size##_div(a, b); \
91 ret |= test_arith_v##sign##int##bits##x##size##_and(a, b); \
92 ret |= test_arith_v##sign##int##bits##x##size##_or(a, b); \
93 ret |= test_arith_v##sign##int##bits##x##size##_xor(a, b); \
94 } \
95 } \
96 \
97 for (size_t i = 0U; i < ARRAY_SIZE(testval##sign##bits); i++) { \
98 const v##sign##int##bits##x##size a = vtest##sign##bits##x##size(i); \
99 for (size_t j = 0U; j < ARRAY_SIZE(testvalu##bits); j++) { \
100 const vuint##bits##x##size b = vtestu##bits##x##size(j); \
101 ret |= test_arith_v##sign##int##bits##x##size##_rshift(a, b); \
102 ret |= test_arith_v##sign##int##bits##x##size##_lshift(a, b); \
103 ret |= test_arith_v##sign##int##bits##x##size##_lrshift(a, b); \
104 } \
105 }
106
107 #define RUN_TESTS_2(bits, size) \
108 RUN_TESTS(, bits, size) \
109 RUN_TESTS(u, bits, size)
110
111 RUN_TESTS_2(8, 16)
112 RUN_TESTS_2(16, 8)
113 RUN_TESTS_2(32, 4)
114 RUN_TESTS_2(64, 2)
115
116 #undef RUN_TESTS_2
117 #undef RUN_TESTS
118
119 return ret;
120 }