-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHWBITPERM_Demo.cpp
194 lines (175 loc) · 7.39 KB
/
HWBITPERM_Demo.cpp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "stdafx.h"
#include "HWBITPERM_Demo.h"
extern CPU_Props cpu_props;
using namespace std;
bitperm_methods hw_reference[] = {
{"PEXT32_HW zmm, zmm, zmm ", "SKX ", 32, BEXT32_HW_Lat, BEXT32_HW_Tp, BEXT32_HW, FEAT_AVX512BW, BEXT, 0},
{"PDEP32_HW zmm, zmm, zmm ", "SKX ", 32, BDEP32_HW_Lat, BDEP32_HW_Tp, BDEP32_HW, FEAT_AVX512BW, BDEP, 1},
{"BGRP32_HW zmm, zmm, zmm ", "SKX ", 32, BGRP32_HW_Lat, BGRP32_HW_Tp, BGRP32_HW, FEAT_AVX512BW, BGRP, 2},
{"PEXT64_HW zmm, zmm, zmm ", "SKX ", 64, BEXT64_HW_Lat, BEXT64_HW_Tp, BEXT64_HW, FEAT_AVX512BW, BEXT, 3},
{"PDEP64_HW zmm, zmm, zmm ", "SKX ", 64, BDEP64_HW_Lat, BDEP64_HW_Tp, BDEP64_HW, FEAT_AVX512BW, BDEP, 4},
{"BGRP64_HW zmm, zmm, zmm ", "SKX ", 64, BGRP64_HW_Lat, BGRP64_HW_Tp, BGRP64_HW, FEAT_AVX512BW, BGRP, 5},
};
void HWBITPERM_Compare(__m512i p, __m512i mask, __m512i ref, int bitness, BITPERM type) {
for (int b = 0; b < (sizeof(hw_reference) / sizeof(bitperm_methods)); b++) {
if (cpu_props.IsFeat(hw_reference[b].feats) && (bitness == hw_reference[b].bitness) && (type == hw_reference[b].type)) {
__m512i res = (hw_reference[b].func)(p, mask);
__mmask64 test = _mm512_cmpeq_epi8_mask(res, ref);
if (test != ~0ULL)
if (bitness == 32) {
printRes32(hw_reference[b].name, p);
printRes32(hw_reference[b].name, mask);
printRes32(hw_reference[b].name, ref);
printRes32(hw_reference[b].name, res);
} else {
printRes(hw_reference[b].name, p);
printRes(hw_reference[b].name, mask);
printRes(hw_reference[b].name, ref);
printRes(hw_reference[b].name, res);
}
assert(test == ~0ULL);
}
}
}
unsigned int _pgrp_u32(unsigned int p, unsigned int m) {
unsigned int zeros = _pext_u32(p, ~m) << (_mm_popcnt_u32(m));
unsigned int ones = _pext_u32(p, m);
return zeros | ones;
}
unsigned __int64 _pgrp_u64(unsigned __int64 p, unsigned __int64 m) {
unsigned __int64 zeros = _pext_u64(p, ~m) << (_mm_popcnt_u64(m));
unsigned __int64 ones = _pext_u64(p, m);
return zeros | ones;
}
void HWBITPERM_Check64(void) {
__m512i p, m, ref_ext, ref_dep, ref_grp;
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
int b = 8 * j + i;
p.m512i_u64[i] = ~0ULL;
m.m512i_u64[i] = (((1ULL) << b) + ((1ULL << 63) >> b)) | (1ULL << 32) | (1ULL << 32) | (1ULL << 16) | (1ULL << 48);
ref_ext.m512i_i64[i] = _pext_u64(p.m512i_u64[i], m.m512i_u64[i]);
ref_dep.m512i_i64[i] = _pdep_u64(p.m512i_u64[i], m.m512i_u64[i]);
ref_grp.m512i_i64[i] = _pgrp_u64(p.m512i_u64[i], m.m512i_u64[i]);
}
HWBITPERM_Compare(p, m, ref_ext, 64, BEXT);
HWBITPERM_Compare(p, m, ref_dep, 64, BDEP);
HWBITPERM_Compare(p, m, ref_grp, 64, BGRP);
}
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
int b = 8 * j + i;
p.m512i_u64[i] = (((1ULL) << b) + ((1ULL << 63) >> b)) | (1ULL << 32) | (1ULL << 32) | (1ULL << 16) | (1ULL << 48);
m.m512i_u64[i] = ~0ULL;
ref_ext.m512i_i64[i] = _pext_u64(p.m512i_u64[i], m.m512i_u64[i]);
ref_dep.m512i_i64[i] = _pdep_u64(p.m512i_u64[i], m.m512i_u64[i]);
ref_grp.m512i_i64[i] = _pgrp_u64(p.m512i_u64[i], m.m512i_u64[i]);
}
HWBITPERM_Compare(p, m, ref_ext, 64, BEXT);
HWBITPERM_Compare(p, m, ref_dep, 64, BDEP);
HWBITPERM_Compare(p, m, ref_grp, 64, BGRP);
}
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
int b = 8 * j + i;
p.m512i_u64[i] = ~0ULL;
m.m512i_u64[i] = _bzhi_u64(~0, b + 1);
ref_ext.m512i_i64[i] = _pext_u64(p.m512i_u64[i], m.m512i_u64[i]);
ref_dep.m512i_i64[i] = _pdep_u64(p.m512i_u64[i], m.m512i_u64[i]);
ref_grp.m512i_i64[i] = _pgrp_u64(p.m512i_u64[i], m.m512i_u64[i]);
}
HWBITPERM_Compare(p, m, ref_ext, 64, BEXT);
HWBITPERM_Compare(p, m, ref_dep, 64, BDEP);
HWBITPERM_Compare(p, m, ref_grp, 64, BGRP);
}
for (int j = 0; j < 1000; j++) {
for (int i = 0; i < 8; i++) {
while (!_rdrand64_step(&p.m512i_u64[i]));
while (!_rdrand64_step(&m.m512i_u64[i]));
ref_ext.m512i_i64[i] = _pext_u64(p.m512i_u64[i], m.m512i_u64[i]);
ref_dep.m512i_i64[i] = _pdep_u64(p.m512i_u64[i], m.m512i_u64[i]);
ref_grp.m512i_i64[i] = _pgrp_u64(p.m512i_u64[i], m.m512i_u64[i]);
}
HWBITPERM_Compare(p, m, ref_ext, 64, BEXT);
HWBITPERM_Compare(p, m, ref_dep, 64, BDEP);
HWBITPERM_Compare(p, m, ref_grp, 64, BGRP);
}
}
void HWBITPERM_Check32(void) {
__m512i p, m, ref_ext, ref_dep, ref_grp;
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 16; i++) {
int b = 16 * j + i;
p.m512i_u32[i] = ~0UL;
m.m512i_u32[i] = (((1UL) << b) + ((1UL << 31) >> b)) | (1UL << 16);
ref_ext.m512i_u32[i] = _pext_u32(p.m512i_u32[i], m.m512i_u32[i]);
ref_dep.m512i_u32[i] = _pdep_u32(p.m512i_u32[i], m.m512i_u32[i]);
ref_grp.m512i_u32[i] = _pgrp_u32(p.m512i_u32[i], m.m512i_u32[i]);
}
HWBITPERM_Compare(p, m, ref_ext, 32, BEXT);
HWBITPERM_Compare(p, m, ref_dep, 32, BDEP);;
HWBITPERM_Compare(p, m, ref_grp, 32, BGRP);;
}
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 16; i++) {
int b = 16 * j + i;
p.m512i_u32[i] = (((1UL) << b) + ((1UL << 31) >> b)) | (1UL << 16);
m.m512i_u32[i] = ~0UL;
ref_ext.m512i_u32[i] = _pext_u32(p.m512i_u32[i], m.m512i_u32[i]);
ref_dep.m512i_u32[i] = _pdep_u32(p.m512i_u32[i], m.m512i_u32[i]);
ref_grp.m512i_u32[i] = _pgrp_u32(p.m512i_u32[i], m.m512i_u32[i]);
}
HWBITPERM_Compare(p, m, ref_ext, 32, BEXT);
HWBITPERM_Compare(p, m, ref_dep, 32, BDEP);;
HWBITPERM_Compare(p, m, ref_grp, 32, BGRP);;
}
for (int j = 0; j < 2; j++) {
for (int i = 0; i < 16; i++) {
int b = 16 * j + i;
p.m512i_u32[i] = ~0UL;
m.m512i_u32[i] = _bzhi_u32(~0, b + 1);
ref_ext.m512i_u32[i] = _pext_u32(p.m512i_u32[i], m.m512i_u32[i]);
ref_dep.m512i_u32[i] = _pdep_u32(p.m512i_u32[i], m.m512i_u32[i]);
ref_grp.m512i_u32[i] = _pgrp_u32(p.m512i_u32[i], m.m512i_u32[i]);
}
HWBITPERM_Compare(p, m, ref_ext, 32, BEXT);
HWBITPERM_Compare(p, m, ref_dep, 32, BDEP);
HWBITPERM_Compare(p, m, ref_grp, 32, BGRP);
}
for (int j = 0; j < 1000; j++) {
for (int i = 0; i < 16; i++) {
while (!_rdrand32_step(&p.m512i_u32[i]));
while (!_rdrand32_step(&m.m512i_u32[i]));
ref_ext.m512i_u32[i] = _pext_u32(p.m512i_u32[i], m.m512i_u32[i]);
ref_dep.m512i_u32[i] = _pdep_u32(p.m512i_u32[i], m.m512i_u32[i]);
ref_grp.m512i_u32[i] = _pgrp_u32(p.m512i_u32[i], m.m512i_u32[i]);
}
HWBITPERM_Compare(p, m, ref_ext, 32, BEXT);
HWBITPERM_Compare(p, m, ref_dep, 32, BDEP);
HWBITPERM_Compare(p, m, ref_grp, 32, BGRP);
}
}
void HWBITPERM_Time(int method) {
unsigned __int64 minlat = ULONG_MAX;
unsigned __int64 mintp = ULONG_MAX;
for (int retry = 0; retry < DEPEXT219_RETRIES; retry++) {
minlat = min(minlat, (hw_reference[method].lat)());
}
for (int retry = 0; retry < DEPEXT219_RETRIES; retry++) {
mintp = min(mintp, (hw_reference[method].tp)());
}
cout << hw_reference[method].isaName << hw_reference[method].name << ": " << (double)minlat / (double)DEPEXT219_REPEATS << " | ";
cout << (double)mintp / (double)DEPEXT219_REPEATS << " L|T";
cout << endl;
}
void HWBITPERM_Test() {
SetThread(2);
HWBITPERM_Check64();
HWBITPERM_Check32();
cout << setw(5) << fixed << setprecision(2);
cout << endl<< "HW/Scalar TSC CLKs:----------------------" << endl;
for (int b = 0; b < (sizeof(hw_reference) / sizeof(bitperm_methods)); b++) {
if ((cpu_props.IsFeat(hw_reference[b].feats)) && (hw_reference[b].lat != NULL) && (hw_reference[b].tp != NULL))
HWBITPERM_Time(b);
}
}