forked from emadrid-at-ccm/singulargy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmarks.cpp
326 lines (290 loc) · 9.83 KB
/
benchmarks.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "detail/ep/mechanisms.h"
#include "ep/nextSubset.h"
#include "ep/Compare.h"
#include "ep/timing/benchmark.h"
#include <iostream>
struct ProgressiveGenerator {
uint64_t m_state = 1;
constexpr ProgressiveGenerator() = default;
constexpr ProgressiveGenerator(int v): m_state(v) {}
constexpr uint64_t next() {
auto rv = m_state;
m_state = ep::nextSubset(rv);
return rv;
}
constexpr uint64_t operator()() { return next(); }
};
int isFOAK(uint64_t cards) {
ep::SWARRank ranks(cards);
ep::RankCounts counts(ranks);
auto foaks = ep::core::greaterEqualSWAR<4>(counts.counts());
return foaks;
}
ep::SWARRank straights(ep::RankCounts rc) {
auto present = ep::core::greaterEqualSWAR<1>(rc.counts());
auto acep =
present.at(ep::abbreviations::rA) ?
uint64_t(0xF) << ep::abbreviations::rA :
uint64_t(0);
auto ranks = present.value();
auto sk = ranks << 4;
auto rak = ranks & sk;
auto rt = acep | (ranks << 16);
auto rqj = rak << 8;
auto rakqj = rak & rqj;
return ep::SWARRank(rakqj & rt);
}
uint64_t flush(uint64_t ranks) {
auto ranksMask = ep::core::makeBitmask<4, uint64_t>(1);
for(auto n = 4; n--; ) {
auto masked = ranks & ranksMask;
auto count = __builtin_popcountll(masked);
if(5 <= count) { return masked; }
ranksMask <<= 1;
}
return 0;
}
/// \return true if the cards have a three of a kind and a pair
int isTOAKplusPAIRv1(uint64_t cards) {
ep::SWARRank ranks(cards);
ep::RankCounts counts(ranks);
auto toaks = ep::core::greaterEqualSWAR<3>(counts.counts());
if(!toaks) { return false; }
auto toakIndex = toaks.top();
auto cleared = counts.clearAt(toakIndex);
auto pairs = ep::core::greaterEqualSWAR<2>(cleared.counts());
return pairs;
}
int colorBlindRank(uint64_t cards) {
ep::SWARRank ranks(cards);
ep::RankCounts counts(ranks);
auto toaks = ep::core::greaterEqualSWAR<3>(counts.counts());
if(toaks) {
auto foaks = ep::core::greaterEqualSWAR<4>(counts.counts());
if(foaks) {
return ep::FOUR_OF_A_KIND;
}
auto bestToak = counts.best();
auto cleared = counts.clearAt(bestToak);
auto pairs = cleared.greaterEqual<2>();
if(pairs) { return ep::FULL_HOUSE; }
auto s = straights(counts);
if(s) { return ep::STRAIGHT; }
return ep::THREE_OF_A_KIND;
}
auto s = straights(counts);
if(s) { return ep::STRAIGHT; }
auto pairs = counts.greaterEqual<2>();
if(pairs) {
auto bestPair = pairs.top();
auto cleared = counts.clearAt(bestPair);
auto secondPair = cleared.greaterEqual<2>();
if(secondPair) { return ep::TWO_PAIRS; }
return ep::PAIR;
}
return ep::HIGH_CARDS;
}
inline int encode(int hand, int h, int l) {
return (hand << 28) | (h << 14) | l;
}
//#include <x86intrin.h>
int rankFlushG(uint64_t cards) {
auto s = straights(ep::RankCounts(ep::SWARRank(cards)));
if(s) {
return encode(ep::STRAIGHT_FLUSH, 0, 1 << s.top());
}
for(auto count = __builtin_popcountll(cards); __builtin_expect(5 < count--, 0); ) {
cards &= cards - 1;
}
auto high = ep::toRanks(cards);
return encode(ep::FLUSH, 0, high);
}
int rankFlushCheat(uint64_t cards) {
if(straights(ep::RankCounts(ep::SWARRank(cards)))) { return ep::STRAIGHT_FLUSH; }
return ep::FLUSH;
}
int whole(uint64_t cards) {
ep::SWARRank ranks(cards);
ep::RankCounts counts(ranks);
auto toaks = ep::core::greaterEqualSWAR<3>(counts.counts());
if(toaks) {
auto foaks = ep::core::greaterEqualSWAR<4>(counts.counts());
if(foaks) {
auto high = foaks.top();
auto cleared = ranks.clear(high);
auto kicker = cleared.top();
return encode(ep::FOUR_OF_A_KIND, high, 1 << kicker);
}
auto bestToak = counts.best();
auto cleared = counts.clearAt(bestToak);
auto pairs = cleared.greaterEqual<2>();
if(pairs) {
return encode(ep::FULL_HOUSE, 1 << bestToak, 1 << pairs.top());
}
auto flushCards = flush(cards);
if(flushCards) { return rankFlushG(flushCards); }
auto s = straights(counts);
if(s) { return encode(ep::STRAIGHT, 0, 1 << s.top()); }
auto best = cleared.best();
auto low = 1 << best;
auto recleared = cleared.clearAt(best);
auto worst = recleared.best();
low |= 1 << worst;
return encode(ep::THREE_OF_A_KIND, 0, low);
}
auto flushCards = flush(cards);
if(flushCards) { return rankFlushG(flushCards); }
auto s = straights(counts);
if(s) { return encode(ep::STRAIGHT, 0, 1 << s.top()); }
auto pairs = counts.greaterEqual<2>();
if(pairs) {
auto bestPair = pairs.best();
auto cleared = counts.clearAt(bestPair);
auto secondPairs = cleared.greaterEqual<2>();
if(secondPairs) {
auto worsePair = secondPairs.best();
auto recleared = cleared.clearAt(worsePair);
auto high = (1 << bestPair) | (1 << worsePair);
auto tricleared = recleared.clearAt(worsePair);
return encode(ep::TWO_PAIRS, high, 1 << tricleared.best());
}
auto top = cleared.best();
auto recleared = cleared.clearAt(top);
auto middle = recleared.best();
auto tricleared = recleared.clearAt(middle);
auto bottom = tricleared.best();
return encode(ep::PAIR, 1 << bestPair, (1 << top) | (1 << middle) | (1 << bottom));
}
cards &= cards - 1;
cards &= cards - 1;
return encode(ep::HIGH_CARDS, 0, ep::toRanks(cards));
}
int wholeCheat(uint64_t cards) {
ep::SWARRank ranks(cards);
ep::RankCounts counts(ranks);
auto toaks = counts.greaterEqual<3>();
if(toaks) {
auto foaks = counts.greaterEqual<4>();
if(foaks) {
return ep::FOUR_OF_A_KIND;
}
auto bestToak = counts.best();
auto cleared = counts.clearAt(bestToak);
auto pairs = cleared.greaterEqual<2>();
if(pairs) { return ep::FULL_HOUSE; }
auto flushCards = flush(cards);
if(flushCards) { return rankFlushCheat(flushCards); }
auto s = straights(counts);
if(s) { return ep::STRAIGHT; }
return ep::THREE_OF_A_KIND;
}
auto flushCards = flush(cards);
if(flushCards) { return rankFlushCheat(flushCards); }
auto s = straights(counts);
if(s) { return ep::STRAIGHT; }
auto pairs = counts.greaterEqual<2>();
if(pairs) {
auto bestPair = pairs.best();
auto cleared = counts.clearAt(bestPair);
auto secondPair = cleared.greaterEqual<2>();
if(secondPair) { return ep::TWO_PAIRS; }
return ep::PAIR;
}
return ep::HIGH_CARDS;
}
uint64_t sideEffect = 0;
template<typename Callable>
long driver(ProgressiveGenerator gen, int count, Callable fun) {
auto nested = [&]() {
for(auto c = count; c--; ) {
auto cards = gen.next();
sideEffect ^= cards;
fun(cards);
}
};
return ep::timing::benchmark(nested);
};
int (*operation)(uint64_t) = isTOAKplusPAIRv1;
#include <iostream>
#include <iomanip>
int
#ifdef BENCHMARKS
main
#else
benchmarks
#endif
(int argc, const char *argv[]) {
std::cout << std::setprecision(4);
ProgressiveGenerator gen(0x7F);
auto count = 133784560;
auto toakPlusPairs = 0;
auto xorCheck = -1;
auto empty = [](uint64_t) {};
auto v1fun = [&](uint64_t cards) {
auto result = operation(cards);
if(ep::THREE_OF_A_KIND == result) { ++toakPlusPairs; }
xorCheck ^= result;
/*auto st = straights(ep::SWARRank(cards));
if(0x11111 == (0x11111 & cards)) {
std::cout << std::endl << ep::SWARRank(cards) << ' ' << st << std::endl;
}
if(st) {
static auto strs = 10;
if(0 < strs--) {
std::cout << std::endl << ep::SWARRank(cards);
}
}*/
};
auto indicators = [=](long time, long base) {
auto divisor = (time == base ? base : time - base);
std::cout << time << ' ' << 1.0*divisor/base << ' ' << count/divisor <<
' ' << 1000.0*divisor/count;
};
auto base = driver(gen, count, empty);
indicators(base, base);
std::cout.flush();
if(1 == argc) {
auto v1 = driver(gen, count, v1fun);
std::cout << " |1 ";
indicators(v1, base);
std::cout.flush();
operation = [](uint64_t c) { return colorBlindRank(c); };
auto v2 = driver(gen, count, v1fun);
std::cout << " |cb ";
indicators(v2, base);
std::cout.flush();
operation = [](uint64_t c) -> int { return flush(c); };
auto v3 = driver(gen, count, v1fun);
std::cout << " |f ";
indicators(v3, base);
std::cout.flush();
operation = [](uint64_t c) -> int { return whole(c); };
auto v4 = driver(gen, count, v1fun);
std::cout << " |w ";
indicators(v4, base);
std::cout.flush();
}
operation = [](uint64_t c) -> int {
//ep::SWARRank ranks(c); ep::SWARSuit ss = ep::convert(ranks);
//ep::CSet cs = { ss, ranks };
return ep::handRank(c).code;
};
auto v5 = driver(gen, count, v1fun);
std::cout << " |h ";
indicators(v5, base);
std::cout.flush();
if(1 == argc) {
operation = [](uint64_t c) -> int {
/*ep::SWARRank ranks(c); ep::SWARSuit ss = ep::convert(ranks);
ep::CSet cs = { ss, ranks };
return ep::naive::handRank(cs);*/
return wholeCheat(c);
};
auto v6 = driver(gen, count, v1fun);
std::cout << " |c ";
indicators(v6, base);
}
std::cout << ' ' << toakPlusPairs << ' ' << std::hex << sideEffect << ' ' <<
xorCheck << std::endl;
return 0;
}