aboutsummaryrefslogtreecommitdiffhomepage
path: root/sample/ccmp.cpp
blob: ff75796baaf1fea9e75bae05f81ed83ed35a2225 (plain)
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
/*
	An example of ccmp
	> g++ ccmp.cpp -I ../xbyak
	> sde -future -- ./a.out
*/
#include <stdio.h>
#include <xbyak/xbyak.h>
#include <xbyak/xbyak_util.h>

using namespace Xbyak;

struct Code1 : Xbyak::CodeGenerator {
	Code1()
	{
		Xbyak::util::StackFrame sf(this, 2);
		const auto& p1 = sf.p[0];
		const auto& p2 = sf.p[1];
		int dfv = 0;
		cmp(p1, 3);
		ctesta(p2, 1, dfv); // eflags = (p1 > 3) ? ((p2 & 1) == 0) : dfv;
		setz(al|T_zu);
	}
};

struct Code2 : Xbyak::CodeGenerator {
	Code2()
	{
		Xbyak::util::StackFrame sf(this, 3);
		const auto& p1 = sf.p[0];
		const auto& p2 = sf.p[1];
		const auto& p3 = sf.p[2];
		int dfv = 0;
		cmp(p1, 1);
		ccmpe(p2, 2, dfv); // eflags = p1==1 ? p2==2 : dfv;
		ccmpe(p3, 3, dfv); // eflags = (p1==1 && p2==2) ? p3==3 : dfv;
		setz(al|T_zu); // p1==1 && p2==2 && p3==3
	}
};


int main()
	try
{
	{
		puts("(p1 > 3) && ((p2 & 1) == 0)");
		Code1 c;
		auto f = c.getCode<int (*)(int, int)>();
		for (int p1 = 2; p1 < 5; p1++) {
			for (int p2 = 0; p2 < 3; p2++) {
				printf("p1=%d p2=%d ret=%d (%d)\n", p1, p2, f(p1, p2), p1 > 3 && ((p2&1) == 0));
			}
		}
	}
	{
		puts("p1 == 1 && p2 == 2 && p3 == 3");
		Code2 c;
		auto f = c.getCode<int (*)(int, int, int)>();
		for (int p1 = 0; p1 < 3; p1++) {
			for (int p2 = 1; p2 < 4; p2++) {
				for (int p3 = 2; p3 < 5; p3++) {
					printf("p1=%d p2=%d p3=%d ret=%d (%d)\n", p1, p2, p3, f(p1, p2, p3), p1==1 && p2==2 && p3==3);
				}
			}
		}
	}
} catch (std::exception& e) {
	printf("ERR %s\n", e.what());
}