Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sf_test.cpp
Go to the documentation of this file.
1#define XBYAK_NO_OP_NAMES
2#include <xbyak/xbyak_util.h>
3
4#ifdef XBYAK32
5 #error "this sample is for only 64-bit mode"
6#endif
7
8using namespace Xbyak::util;
9
10struct Code : public Xbyak::CodeGenerator {
11 void gen1()
12 {
13 StackFrame sf(this, 1);
14 mov(rax, sf.p[0]);
15 }
16 void gen2()
17 {
18 StackFrame sf(this, 2);
19 lea(rax, ptr [sf.p[0] + sf.p[1]]);
20 }
21 void gen3()
22 {
23 StackFrame sf(this, 3);
24 mov(rax, sf.p[0]);
25 add(rax, sf.p[1]);
26 add(rax, sf.p[2]);
27 }
28 void gen4()
29 {
30 StackFrame sf(this, 4);
31 mov(rax, sf.p[0]);
32 add(rax, sf.p[1]);
33 add(rax, sf.p[2]);
34 add(rax, sf.p[3]);
35 }
36
37 void gen5()
38 {
39 StackFrame sf(this, 4, UseRCX);
40 xor_(rcx, rcx);
41 mov(rax, sf.p[0]);
42 add(rax, sf.p[1]);
43 add(rax, sf.p[2]);
44 add(rax, sf.p[3]);
45 }
46
47 void gen6()
48 {
49 StackFrame sf(this, 4, UseRCX | UseRDX);
50 xor_(rcx, rcx);
51 xor_(rdx, rdx);
52 mov(rax, sf.p[0]);
53 add(rax, sf.p[1]);
54 add(rax, sf.p[2]);
55 add(rax, sf.p[3]);
56 }
57
58 void gen7()
59 {
60 StackFrame sf(this, 3, UseRCX | UseRDX);
61 xor_(rcx, rcx);
62 xor_(rdx, rdx);
63 mov(rax, sf.p[0]);
64 add(rax, sf.p[1]);
65 add(rax, sf.p[2]);
66 }
67
68 void gen8()
69 {
70 StackFrame sf(this, 3, 3 | UseRCX | UseRDX);
71 xor_(rcx, rcx);
72 xor_(rdx, rdx);
73 mov(sf.t[0], 1);
74 mov(sf.t[1], 2);
75 mov(sf.t[2], 3);
76 mov(rax, sf.p[0]);
77 add(rax, sf.p[1]);
78 add(rax, sf.p[2]);
79 }
80
81 void gen9()
82 {
83 StackFrame sf(this, 3, 3 | UseRCX | UseRDX, 32);
84 xor_(rcx, rcx);
85 xor_(rdx, rdx);
86 mov(sf.t[0], 1);
87 mov(sf.t[1], 2);
88 mov(sf.t[2], 3);
89 mov(rax, sf.p[0]);
90 add(rax, sf.p[1]);
91 add(rax, sf.p[2]);
92 mov(ptr [rsp + 8 * 0], rax);
93 mov(ptr [rsp + 8 * 1], rax);
94 mov(ptr [rsp + 8 * 2], rax);
95 mov(ptr [rsp + 8 * 3], rax);
96 }
97
98 void gen10()
99 {
100 StackFrame sf(this, 4, 8 | UseRCX | UseRDX, 32);
101 xor_(rcx, rcx);
102 xor_(rdx, rdx);
103 for (int i = 0; i < 8; i++) {
104 mov(sf.t[i], i);
105 }
106 mov(rax, sf.p[0]);
107 add(rax, sf.p[1]);
108 add(rax, sf.p[2]);
109 add(rax, sf.p[3]);
110 mov(ptr [rsp + 8 * 0], rax);
111 mov(ptr [rsp + 8 * 1], rax);
112 mov(ptr [rsp + 8 * 2], rax);
113 mov(ptr [rsp + 8 * 3], rax);
114 }
115
116 void gen11()
117 {
118 StackFrame sf(this, 0, UseRCX);
119 xor_(rcx, rcx);
120 mov(rax, 3);
121 }
122
123 void gen12()
124 {
125 StackFrame sf(this, 4, UseRDX);
126 xor_(rdx, rdx);
127 mov(rax, sf.p[0]);
128 add(rax, sf.p[1]);
129 add(rax, sf.p[2]);
130 add(rax, sf.p[3]);
131 }
132};
133
136 : Xbyak::CodeGenerator(4096 * 32)
137 {
138 }
139 void gen(int pNum, int tNum, int stackSizeByte)
140 {
141 StackFrame sf(this, pNum, tNum, stackSizeByte);
142 if (tNum & UseRCX) xor_(rcx, rcx);
143 if (tNum & UseRDX) xor_(rdx, rdx);
144 for (int i = 0, n = tNum & ~(UseRCX | UseRDX); i < n; i++) {
145 mov(sf.t[i], 5);
146 }
147 for (int i = 0; i < stackSizeByte; i++) {
148 mov(byte [rsp + i], 0);
149 }
150 mov(rax, 1);
151 for (int i = 0; i < pNum; i++) {
152 add(rax, sf.p[i]);
153 }
154 }
155};
156
157static int errNum = 0;
158void check(int x, int y)
159{
160 if (x != y) {
161 printf("err x=%d, y=%d\n", x, y);
162 errNum++;
163 }
164}
165
166void verify(const Xbyak::uint8 *f, int pNum)
167{
168 switch (pNum) {
169 case 0:
170 check(1, Xbyak::CastTo<int (*)()>(f)());
171 return;
172 case 1:
173 check(11, Xbyak::CastTo<int (*)(int)>(f)(10));
174 return;
175 case 2:
176 check(111, Xbyak::CastTo<int (*)(int, int)>(f)(10, 100));
177 return;
178 case 3:
179 check(1111, Xbyak::CastTo<int (*)(int, int, int)>(f)(10, 100, 1000));
180 return;
181 case 4:
182 check(11111, Xbyak::CastTo<int (*)(int, int, int, int)>(f)(10, 100, 1000, 10000));
183 return;
184 default:
185 printf("ERR pNum=%d\n", pNum);
186 exit(1);
187 }
188}
189
191{
192 Code2 code;
193 for (int stackSize = 0; stackSize < 32; stackSize += 7) {
194 for (int pNum = 0; pNum < 4; pNum++) {
195 for (int mode = 0; mode < 4; mode++) {
196 int maxNum = 0;
197 int opt = 0;
198 if (mode == 0) {
199 maxNum = 10;
200 } else if (mode == 1) {
201 maxNum = 9;
202 opt = UseRCX;
203 } else if (mode == 2) {
204 maxNum = 9;
205 opt = UseRDX;
206 } else {
207 maxNum = 8;
208 opt = UseRCX | UseRDX;
209 }
210 for (int tNum = 0; tNum < maxNum; tNum++) {
211// printf("pNum=%d, tNum=%d, stackSize=%d\n", pNum, tNum | opt, stackSize);
212 const Xbyak::uint8 *f = code.getCurr();
213 code.gen(pNum, tNum | opt, stackSize);
214 verify(f, pNum);
215 }
216 }
217 }
218 }
219}
220
222{
223 Code code;
224 int (*f1)(int) = code.getCurr<int (*)(int)>();
225 code.gen1();
226 check(5, f1(5));
227
228 int (*f2)(int, int) = code.getCurr<int (*)(int, int)>();
229 code.gen2();
230 check(9, f2(3, 6));
231
232 int (*f3)(int, int, int) = code.getCurr<int (*)(int, int, int)>();
233 code.gen3();
234 check(14, f3(1, 4, 9));
235
236 int (*f4)(int, int, int, int) = code.getCurr<int (*)(int, int, int, int)>();
237 code.gen4();
238 check(30, f4(1, 4, 9, 16));
239
240 int (*f5)(int, int, int, int) = code.getCurr<int (*)(int, int, int, int)>();
241 code.gen5();
242 check(23, f5(2, 5, 7, 9));
243
244 int (*f6)(int, int, int, int) = code.getCurr<int (*)(int, int, int, int)>();
245 code.gen6();
246 check(18, f6(3, 4, 5, 6));
247
248 int (*f7)(int, int, int) = code.getCurr<int (*)(int, int, int)>();
249 code.gen7();
250 check(12, f7(3, 4, 5));
251
252 int (*f8)(int, int, int) = code.getCurr<int (*)(int, int, int)>();
253 code.gen8();
254 check(23, f8(5, 8, 10));
255
256 int (*f9)(int, int, int) = code.getCurr<int (*)(int, int, int)>();
257 code.gen9();
258 check(60, f9(10, 20, 30));
259
260 int (*f10)(int, int, int, int) = code.getCurr<int (*)(int, int, int, int)>();
261 code.gen10();
262 check(100, f10(10, 20, 30, 40));
263
264 int (*f11)() = code.getCurr<int (*)()>();
265 code.gen11();
266 check(3, f11());
267
268 int (*f12)(int, int, int, int) = code.getCurr<int (*)(int, int, int, int)>();
269 code.gen12();
270 check(24, f12(3, 5, 7, 9));
271}
272
273void put(const Xbyak::util::Pack& p)
274{
275 for (size_t i = 0, n = p.size(); i < n; i++) {
276 printf("%s ", p[i].toString());
277 }
278 printf("\n");
279}
280
281void verifyPack(const Xbyak::util::Pack& p, const int *tbl, size_t tblNum)
282{
283 for (size_t i = 0; i < tblNum; i++) {
284 check(p[i].getIdx(), tbl[i]);
285 }
286}
287
289{
290 const int N = 10;
291 Xbyak::Reg64 regTbl[N];
292 for (int i = 0; i < N; i++) {
293 regTbl[i] = Xbyak::Reg64(i);
294 }
295 Xbyak::util::Pack p(regTbl, N);
296 const struct {
297 int pos;
298 int num;
299 int tbl[10];
300 } tbl[] = {
301 { 0, 10, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } },
302 { 1, 9, { 1, 2, 3, 4, 5, 6, 7, 8, 9 } },
303 { 2, 8, { 2, 3, 4, 5, 6, 7, 8, 9 } },
304 { 3, 7, { 3, 4, 5, 6, 7, 8, 9 } },
305 { 4, 6, { 4, 5, 6, 7, 8, 9 } },
306 { 5, 5, { 5, 6, 7, 8, 9 } },
307 { 6, 4, { 6, 7, 8, 9 } },
308 { 7, 3, { 7, 8, 9 } },
309 { 8, 2, { 8, 9 } },
310 { 9, 1, { 9 } },
311 { 3, 5, { 3, 4, 5, 6, 7 } },
312 };
313 for (size_t i = 0; i < sizeof(tbl) / sizeof(*tbl); i++) {
314 const int pos = tbl[i].pos;
315 const int num = tbl[i].num;
316 verifyPack(p.sub(pos, num), tbl[i].tbl, num);
317 if (pos + num == N) {
318 verifyPack(p.sub(pos), tbl[i].tbl, num);
319 }
320 }
321}
322
323int main()
324 try
325{
326 testAll();
327
328 testPartial();
329 testPack();
330 printf("errNum=%d\n", errNum);
331} catch (std::exception& e) {
332 printf("err %s\n", e.what());
333 return 1;
334} catch (...) {
335 puts("ERR");
336 return 1;
337}
338
const mie::Vuint & p
Definition bn.cpp:27
const uint8 * getCurr() const
Definition xbyak.h:908
void xor_(const Operand &op, uint32 imm)
Definition xbyak.h:1279
void add(const Operand &op, uint32 imm)
Definition xbyak.h:6
void mov(const Operand &reg1, const Operand &reg2)
Definition xbyak.h:2210
void lea(const Reg &reg, const Address &addr)
Definition xbyak.h:432
const AddressFrame ptr
Definition xbyak.h:2090
int errNum
Definition cvt_test.cpp:47
const struct Ptn tbl[]
void put()
Definition gen_code.cpp:234
LOGGING_API void printf(Category category, const char *format,...)
Definition Logging.cpp:30
Definition xbyak.h:104
const To CastTo(From p)
Definition xbyak.h:279
const int N
Definition quantize.cpp:54
void testPartial()
Definition sf_test.cpp:221
void verify(const Xbyak::uint8 *f, int pNum)
Definition sf_test.cpp:166
void testAll()
Definition sf_test.cpp:190
void verifyPack(const Xbyak::util::Pack &p, const int *tbl, size_t tblNum)
Definition sf_test.cpp:281
void testPack()
Definition sf_test.cpp:288
int main()
Definition sf_test.cpp:323
void gen(int pNum, int tNum, int stackSizeByte)
Definition sf_test.cpp:139
Code2()
Definition sf_test.cpp:135
Definition bench.cpp:18
void gen4()
Definition sf_test.cpp:28
void gen6()
Definition sf_test.cpp:47
void gen8()
Definition sf_test.cpp:68
void gen10()
Definition sf_test.cpp:98
void gen9()
Definition sf_test.cpp:81
void gen11()
Definition sf_test.cpp:116
void gen7()
Definition sf_test.cpp:58
void gen3()
Definition sf_test.cpp:21
void gen12()
Definition sf_test.cpp:123
void gen1()
Definition sf_test.cpp:11
void gen2()
Definition sf_test.cpp:16
void gen5()
Definition sf_test.cpp:37
size_t size() const
Definition zm.h:519
static void sub(VuintT &out, const VuintT &x, const VuintT &y)
Definition zm.h:597