-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtdsc_and_cpuid_some_examples
167 lines (133 loc) · 3.6 KB
/
rtdsc_and_cpuid_some_examples
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// https://learn.microsoft.com/pl-pl/cpp/intrinsics/cpuid-cpuidex?view=msvc-170
// https://www.intel.com/content/dam/develop/external/us/en/documents/architecture-instruction-set-extensions-programming-reference.pdf
#include <Windows.h>
#include <stdio.h>
#include <intrin.h>
#define PP(x1, x2) \
printf("%d %d \n", (x1), (x2));
// #define __X86__MACHINE
int main()
{
// only debug mode
// set project to x86 and define __X86__MACHINE macro to run this asm code
// MS Studio 2022 - Project Properties > Configuration Manager > set to x86 platform
// retrivered:
// EAX, EBX, ECD, EDX - Processor Brand String
// for 0x80000002 code my CPU Intel i3 2220 the brand id -> Intel(R)
#ifdef __X86__MACHINE
__asm {
mov eax, 0x80000002
cpuid
int 3
}
#endif
int buf[4];
// tabela z kodami - drugi parametr funkcji __cpuid jest manualu architecture-instruction-set-extensions-program...
__cpuid(buf, 0x80000002);
for (int i = 0; i < 4; i++) {
printf("%d %x \n", i, buf[i]);
}
/*for (int i = 0; i < 4; i++) {
int c1 = buf[i] & 0xff000000;
printf("%c %x %d \n", c1, buf[i] & 0xff000000, (c1 >> 24));
}*/
// _bittest - https://learn.microsoft.com/en-us/cpp/intrinsics/bittest-bittest64?view=msvc-170
long num = 0x0f00;
for (int i = 0; i < 31; i++) {
printf("[ bit test ] %d \n", _bittest(&num, i));
}
// how to convert 0b00000111000 to digital representation
// like python bin(0x0f00) > '0b111100000000'
// and backward '0b111100000000' > 0x0f00 (3840)
// but this example is a bug
const char number[] = "0b0011001011";
char reversed_number[32];
// reversed
int j = 0;
for (int i = sizeof(number); i > 0; i--) {
printf("%c ", number[i]);
reversed_number[j++] = number[i];
}
reversed_number[j] = '\0';
int __digit_normal = 0;
for (int i = 0; i < sizeof(reversed_number); i++) {
if (reversed_number[i] == 'b') break;
if (reversed_number[i] & 1) {
__digit_normal += (1 << i);
printf("index %d \n", i);
}
printf("%c ", reversed_number[i]);
}
printf("\n");
printf("%d \n", __digit_normal);
printf("\n");
// no reversed
int digit = 0;
int len = 2;
for (int i = 0; i < sizeof(number); i++) {
if (i >= 2 && number[1] == 'b') {
if (number[i] == '\0') {
break;
}
if (number[i] & 1) {
digit += (1 << i);
printf(" selected index : %d \n", i);
}
//digit = digit << number[i];
printf(" [ >> ] %c %d\n", number[i], len++);
}
}
printf("%zu %d\n", sizeof(number), digit);
char buff[4];
// 0 20202020
// 1 49202020
// 2 6c65746e
// 3 20295228
int step = 0;
int base = 0x6c65746e;
int k = 0x000000ff;
int idx = 0;
for (int i = 0; i < 4; i++) {
int mask = (k << step);
buff[idx] = ((base & mask) >> step) & 0xff;
int c = ((base & mask) >> step) & 0xff;
printf("%d %c\n", c, c);
idx++;
printf("%x \n", ((base & mask) >> step) & 0xff);
step += 8;
}
printf("==> %d \n", buff);
// reverse
for (int i = 3; i >= 0; i--) {
printf("%c", buff[i]);
}
printf("\n ------------------ \n");
for (int i = 0; i <= 3; i++) {
printf("%c", buff[i]);
}
/*if (p == NULL)
return 1;*/
//printf("%d %x \n", p, GetLastError());
/*
unsigned __int64 start = __rdtsc();
unsigned __int64 end;
HANDLE p = OpenThread(THREAD_QUERY_INFORMATION | THREAD_SUSPEND_RESUME, FALSE, GetCurrentThreadId());
int k = 0;
for (int i = 0; i < 1000000; i++) {
k++;
}
end = __rdtsc();
printf("%I64d %I64d %I64d \n", start, end, (end-start));
SuspendThread(p);
start = __rdtsc();
k = 0;
for (int i = 0; i < 1000000; i++) {
k++;
}
end = __rdtsc();
printf("%I64d %I64d %I64d \n", start, end, (end - start));
SuspendThread(p);
//while (1);
*/
return 0;
}