1 | /****************** |
---|
2 | * Python template to auto gen llc test file. |
---|
3 | * sudo easy_install Jinja2 to install the Jinja2 template system |
---|
4 | * {{HeaderComment}} |
---|
5 | */ |
---|
6 | |
---|
7 | #include "utility.h" |
---|
8 | #include <ctime> |
---|
9 | #include <iostream> |
---|
10 | #include <cstdlib> |
---|
11 | using namespace std; |
---|
12 | {% set Names = FunctionNamesI1 + FunctionNamesI2 + FunctionNamesI4 + FunctionNamesI8 %} |
---|
13 | {% set INS_EXT_VEC_ELT_FWs = [2, 4] %} |
---|
14 | |
---|
15 | extern "C" { |
---|
16 | {% for name in Names %} |
---|
17 | SIMD_type {{ name.c }}(SIMD_type a, SIMD_type b); |
---|
18 | {% endfor %} |
---|
19 | |
---|
20 | {% for fw in INS_EXT_VEC_ELT_FWs %} |
---|
21 | SIMD_type insertelement_{{ fw }}(SIMD_type a, short elt, int idx); |
---|
22 | short extractelement_{{ fw }}(SIMD_type a, int idx); |
---|
23 | {% endfor %} |
---|
24 | |
---|
25 | //long add/shift |
---|
26 | SIMD_type add_128(SIMD_type a, SIMD_type b); |
---|
27 | SIMD_type sll_128(SIMD_type a, SIMD_type b); |
---|
28 | |
---|
29 | SIMD_type add_with_carry_ir(SIMD_type a, SIMD_type b, SIMD_type carry_in, SIMD_type *carry_out, SIMD_type *sum); |
---|
30 | |
---|
31 | {% for fw in [128]%} |
---|
32 | void uadd_with_overflow_i{{ fw }}(bitblock{{fw}}_t a, bitblock{{fw}}_t b, bitblock{{fw}}_t *sum, short *obit); |
---|
33 | {% endfor %} |
---|
34 | } |
---|
35 | |
---|
36 | void fill_random(SIMD_type &a, SIMD_type &b) |
---|
37 | { |
---|
38 | a = mvmd<8>::fill((int)rand() % (1 << 8)); |
---|
39 | b = mvmd<8>::fill((int)rand() % (1 << 8)); |
---|
40 | } |
---|
41 | |
---|
42 | int test_long_stream_addition(const SIMD_type &a, const SIMD_type &b) |
---|
43 | { |
---|
44 | SIMD_type c, d, e, f; |
---|
45 | short x, y; |
---|
46 | |
---|
47 | //test uadd.with.overflow.i128 |
---|
48 | c = mvmd<32>::fill(0); |
---|
49 | SIMD_type sum0, obit0, sum1, sum2, obit2; |
---|
50 | short obit1; |
---|
51 | adc(a, b, c, obit0, sum0); |
---|
52 | uadd_with_overflow_i128(a, b, &sum1, &obit1); |
---|
53 | x = 0; |
---|
54 | if (Store2String(sum0,1) != Store2String(sum1,1)) { |
---|
55 | cout << "uadd_with_overflow_i128, sum error" << endl; |
---|
56 | x = 1; |
---|
57 | } |
---|
58 | if (Store2String(mvmd<128>::fill(obit1),1) != Store2String(obit0,1)) { |
---|
59 | cout << "uadd_with_overflow_i128 failed, obit error" << endl; |
---|
60 | x = 1; |
---|
61 | } |
---|
62 | if (x) { |
---|
63 | cout << "a = " << Store2String(a, 1) << endl; |
---|
64 | cout << "b = " << Store2String(b, 1) << endl; |
---|
65 | cout << "sum0 = " << Store2String(sum0, 1) << endl; |
---|
66 | cout << "sum1 = " << Store2String(sum1, 1) << endl; |
---|
67 | cout << "obit0 = " << Store2String(obit0, 1) << endl; |
---|
68 | cout << "obit1 = " << obit1 << endl; |
---|
69 | return 1; |
---|
70 | } |
---|
71 | |
---|
72 | //test add_with_carry in pure IR |
---|
73 | y = rand() % 2; |
---|
74 | c = mvmd<128>::fill(y); |
---|
75 | adc(a, b, c, obit0, sum0); |
---|
76 | add_with_carry_ir(a, b, c, &obit2, &sum2); |
---|
77 | if (Store2String(sum0,1) != Store2String(sum2,1) || |
---|
78 | Store2String(obit0,1) != Store2String(obit2,1)) { |
---|
79 | cout << "add_with_carry_ir failed." << endl; |
---|
80 | cout << "a = " << Store2String(a, 1) << endl; |
---|
81 | cout << "b = " << Store2String(b, 1) << endl; |
---|
82 | cout << "sum0 = " << Store2String(sum0, 1) << endl; |
---|
83 | cout << "sum2 = " << Store2String(sum2, 1) << endl; |
---|
84 | cout << "obit0 = " << Store2String(obit0, 1) << endl; |
---|
85 | cout << "obit2 = " << Store2String(obit2, 1) << endl; |
---|
86 | return 1; |
---|
87 | } |
---|
88 | |
---|
89 | return 0; |
---|
90 | } |
---|
91 | |
---|
92 | int main() { |
---|
93 | SIMD_type a, b, c, d, e, f; |
---|
94 | short x, y; |
---|
95 | srand(time(0)); |
---|
96 | |
---|
97 | for (unsigned i = 0; i < 500; ++i) { |
---|
98 | fill_random(a, b); |
---|
99 | |
---|
100 | //All the binary ops |
---|
101 | {% for name in Names %} |
---|
102 | {% if name.flag == "" %} |
---|
103 | c = {{ name.c }}(a, b); |
---|
104 | d = {{ name.cxx }}(a, b); |
---|
105 | if (Store2String(c,1) != Store2String(d,1)) { |
---|
106 | cout << "{{ name.c }} faild." << endl; |
---|
107 | cout << "A = " << Store2String(a, 1) << endl; |
---|
108 | cout << "B = " << Store2String(b, 1) << endl; |
---|
109 | cout << "c = " << Store2String(c, 1) << endl; |
---|
110 | cout << "cxx=" << Store2String(d, 1) << endl; |
---|
111 | |
---|
112 | return 1; |
---|
113 | } |
---|
114 | |
---|
115 | {% endif %} |
---|
116 | {% endfor %} |
---|
117 | |
---|
118 | //Shifting tests need special treatment |
---|
119 | {% for name in Names %} |
---|
120 | {% if name.flag == "shifting" %} |
---|
121 | {% for x in range(0, name.fw) %} |
---|
122 | c = {{ name.cxx }}<{{ x }}>(a); |
---|
123 | f = mvmd<{{ name.fw }}>::fill({{ x }}); |
---|
124 | d = {{ name.c }}(a, f); |
---|
125 | if (Store2String(c,1) != Store2String(d,1)) { |
---|
126 | cout << "{{ name.c }} faild." << endl; |
---|
127 | return 1; |
---|
128 | } |
---|
129 | |
---|
130 | {% endfor %} |
---|
131 | {% endif %} |
---|
132 | {% endfor %} |
---|
133 | |
---|
134 | //insert_vector_elt and extract_ |
---|
135 | {% for fw in INS_EXT_VEC_ELT_FWs %} |
---|
136 | {% for idx in range(0, 128 // fw) %} |
---|
137 | x = extractelement_{{ fw }}(a, {{ idx }}); |
---|
138 | y = mvmd<{{ fw }}>::extract<{{ idx }}>(a); |
---|
139 | if (x != y) { |
---|
140 | cout << "extractelement_{{fw}} failed." << endl; |
---|
141 | return 1; |
---|
142 | } |
---|
143 | |
---|
144 | x = rand() % 100; |
---|
145 | c = insertelement_{{ fw }}(a, x, {{ idx }}); |
---|
146 | d = mvmd<{{ fw }}>::insert<{{ idx }}>(a, x); |
---|
147 | if (Store2String(c,1) != Store2String(d,1)) { |
---|
148 | cout << "insertelement_{{fw}} failed." << endl; |
---|
149 | return 1; |
---|
150 | } |
---|
151 | {% endfor %} |
---|
152 | {% endfor %} |
---|
153 | |
---|
154 | //try i128 add and shift |
---|
155 | c = simd<128>::add(a, b); |
---|
156 | d = add_128(a, b); |
---|
157 | if (Store2String(c,1) != Store2String(d,1)) { |
---|
158 | cout << "add_128 failed." << endl; |
---|
159 | return 1; |
---|
160 | } |
---|
161 | |
---|
162 | {% set imm = range(0,128) | random %} |
---|
163 | c = simd<128>::slli<{{ imm }}>(a); |
---|
164 | f = mvmd<128>::fill({{ imm }}); |
---|
165 | d = sll_128(a, f); |
---|
166 | if (Store2String(c,1) != Store2String(d,1)) { |
---|
167 | cout << "sll_128 failed." << endl; |
---|
168 | return 1; |
---|
169 | } |
---|
170 | |
---|
171 | if (test_long_stream_addition(a, b)) |
---|
172 | return 1; |
---|
173 | } |
---|
174 | |
---|
175 | return 0; |
---|
176 | } |
---|
177 | |
---|