Skip to content

Commit

Permalink
Modified all intrinsics to generics
Browse files Browse the repository at this point in the history
removed dependence on bmi2 intrinsics and x86intrin.h
  • Loading branch information
willmcgowan committed Jan 29, 2024
1 parent 5484fa9 commit 3184a10
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 83 deletions.
4 changes: 2 additions & 2 deletions open_spiel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ if(${BUILD_TYPE} STREQUAL "Testing")
# A build used for running tests: keep all runtime checks (assert,
# SPIEL_CHECK_*, SPIEL_DCHECK_*), but turn on some speed optimizations,
# otherwise tests run for too long.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -march=x86-64-v3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
endif()

if(${BUILD_TYPE} STREQUAL "Release")
# Optimized release build: turn off debug runtime checks (assert,
# SPIEL_DCHECK_*) and turn on highest speed optimizations.
# The difference in perfomance can be up to 10x higher.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG -O3 -march=x86-64-v3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG -O3 ")
endif()

if(APPLE)
Expand Down
52 changes: 26 additions & 26 deletions open_spiel/games/german_whist_foregame/german_whist_endgame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Node {
Node(uint32_t cards, std::array<uint32_t, kNumSuits> suit_masks, char trump,bool player) {
cards_ = cards;
suit_masks_ = suit_masks;
total_tricks_ = __builtin_popcount(cards);
total_tricks_ = popcnt_u32(cards);
trump_ = trump;
moves_ = 0;
player_ = player;
Expand Down Expand Up @@ -83,15 +83,15 @@ class Node {
void RemoveCard(ActionStruct action) {
//Removes card from cards_//
uint32_t mask_b = ~0;
mask_b =_bzhi_u32(mask_b, action.index);
mask_b =bzhi_u32(mask_b, action.index);
uint32_t mask_a = ~mask_b;
mask_a = _blsr_u32(mask_a);
mask_a = blsr_u32(mask_a);
uint32_t copy_a = cards_ & mask_a;
uint32_t copy_b = cards_ & mask_b;
copy_a = copy_a >> 1;
cards_ = copy_a | copy_b;
//decrements appropriate suits//
suit_masks_[action.suit] = _blsr_u32(suit_masks_[action.suit])>>1;
suit_masks_[action.suit] = blsr_u32(suit_masks_[action.suit])>>1;
char suit = action.suit;
suit++;
while (suit < kNumSuits) {
Expand All @@ -102,7 +102,7 @@ class Node {
void InsertCard(ActionStruct action) {
//inserts card into cards_//
uint32_t mask_b = ~0;
mask_b = _bzhi_u32(mask_b, action.index);
mask_b = bzhi_u32(mask_b, action.index);
uint32_t mask_a = ~mask_b;
uint32_t copy_b = cards_ & mask_b;
uint32_t copy_a = cards_ & mask_a;
Expand All @@ -128,17 +128,17 @@ class Node {
//this implies player 1 achieves the minimax value of the original game ie the value is remaining tricks - value of the original game for this transformed game//
//also does not take advantage of single suit isomorphism. Namely all single suit games with the same card distribution are isomorphic. Currently this considers all trump, all no trump games as distinct//
uint64_t suit_sig = 0;
char trump_length = __builtin_popcount(suit_masks_[trump_]);
char trump_length = popcnt_u32(suit_masks_[trump_]);
if (trump_length > kNumRanks) {
throw;
}
std::vector<Triple> non_trump_lengths;
for (char i = 0; i < kNumSuits; ++i) {
if (i != trump_) {
char length = __builtin_popcount(suit_masks_[i]);
char length = popcnt_u32(suit_masks_[i]);
uint32_t sig = suit_masks_[i]&cards_;
if (suit_masks_[i] != 0) {
sig = (sig >> (_tzcnt_u32(suit_masks_[i])));
sig = (sig >> (tzcnt_u32(suit_masks_[i])));
}
if (length > kNumRanks) {
throw 1;
Expand All @@ -157,19 +157,19 @@ class Node {
std::array<uint32_t, kNumSuits> suit_cards;
suit_cards[0] = cards_ & suit_masks_[trump_];
if (suit_masks_[trump_] != 0) {
suit_cards[0] = suit_cards[0] >> _tzcnt_u32(suit_masks_[trump_]);
suit_cards[0] = suit_cards[0] >> tzcnt_u32(suit_masks_[trump_]);
}
uint32_t sum = __builtin_popcount(suit_masks_[trump_]);
uint32_t sum = popcnt_u32(suit_masks_[trump_]);
uint32_t cards = 0|suit_cards[0];
for (size_t i = 0; i < non_trump_lengths.size(); ++i) {
suit_cards[i] = cards_ & suit_masks_[non_trump_lengths[i].index];
uint32_t val = 0;
if (suit_masks_[non_trump_lengths[i].index] != 0) {
val = _tzcnt_u32(suit_masks_[non_trump_lengths[i].index]);
val = tzcnt_u32(suit_masks_[non_trump_lengths[i].index]);
}
suit_cards[i]= suit_cards[i] >>val;
suit_cards[i] = suit_cards[i] << sum;
sum += __builtin_popcount(suit_masks_[non_trump_lengths[i].index]);
sum += popcnt_u32(suit_masks_[non_trump_lengths[i].index]);
cards = cards | suit_cards[i];
}
//cards = cards | (player_ << 31);
Expand All @@ -186,7 +186,7 @@ class Node {
#endif
}
uint64_t AltKey() {
uint32_t mask = _bzhi_u32(~0, 2 * RemainingTricks());
uint32_t mask = bzhi_u32(~0, 2 * RemainingTricks());
return key_ ^ (uint64_t)mask;
}
//Move Ordering Heuristics//
Expand All @@ -200,7 +200,7 @@ class Node {
uint32_t suit_cards = copy_cards & suit_masks_[suit];
uint32_t mask = suit_cards & ~(suit_cards >> 1);
//represents out of the stategically inequivalent cards in a suit that a player holds, what rank is it, rank 0 is highest rank etc//
int suit_rank = __builtin_popcount(_bzhi_u32(mask, action.index));
int suit_rank = popcnt_u32(bzhi_u32(mask, action.index));
ApplyAction(action);
std::vector<ActionStruct> moves = LegalActions();
UndoAction(action);
Expand Down Expand Up @@ -230,7 +230,7 @@ class Node {
uint32_t suit_cards = copy_cards & suit_masks_[suit];
uint32_t mask = suit_cards & ~(suit_cards >> 1);
//represents out of the stategically inequivalent cards in a suit that a player holds, what rank is it, rank 0 is highest rank etc//
int suit_rank = __builtin_popcount(_bzhi_u32(mask, action.index));
int suit_rank = popcnt_u32(bzhi_u32(mask, action.index));
if (!Trick(lead, action)) {
return -kNumRanks - suit_rank;
}
Expand Down Expand Up @@ -274,14 +274,14 @@ class Node {
}
if ((lead || (follow && (correct_suit || void_in_suit)))) {
while (suit_mask != 0) {
uint32_t best = _tzcnt_u32(suit_mask);
uint32_t best = tzcnt_u32(suit_mask);
if (moves_ % 2 == 0) {
temp.push_back({ ActionStruct(best, i, player_),LeadOrdering(ActionStruct(best, i, player_)) });
}
else {
temp.push_back({ ActionStruct(best, i, player_),FollowOrdering(ActionStruct(best, i, player_)) });
}
suit_mask = _blsr_u32(suit_mask);
suit_mask = blsr_u32(suit_mask);
}
}
}
Expand Down Expand Up @@ -428,9 +428,9 @@ char IncrementalAlphaBetaMemoryIso(Node* node, char alpha, char beta,int depth,
if (node->Moves() % 2 == 0&& depth==0) {
node->UpdateNodeKey();
key = (player) ? node->AltKey() : node->GetNodeKey();
uint32_t cards = key & _bzhi_u64(~0, 32);
uint32_t cards = key & bzhi_u64(~0, 32);
uint32_t colex = HalfColexer(cards, &bin_coeffs);
uint32_t suits = (key & (~0 ^ _bzhi_u64(~0, 32))) >> 32;
uint32_t suits = (key & (~0 ^ bzhi_u64(~0, 32))) >> 32;
uint32_t suit_rank = SuitRanks->at(suits);
char value = (player) ? node->RemainingTricks() - TTable->Get(colex,suit_rank) :TTable->Get(colex,suit_rank);
return value+node->Score();
Expand Down Expand Up @@ -519,16 +519,16 @@ std::vector<Node> GWhistGenerator(int num,unsigned int seed){
int cum_sum =0;
for (int j = 0; j < kNumSuits; ++j) {
if (j == 0) {
suits[j] = _bzhi_u32(~0, suit_lengths[j]);
suits[j] = bzhi_u32(~0, suit_lengths[j]);
}
else {
suits[j] = (_bzhi_u32(~0, suit_lengths[j]+cum_sum)) ^ _bzhi_u32(~0,cum_sum);
suits[j] = (bzhi_u32(~0, suit_lengths[j]+cum_sum)) ^ bzhi_u32(~0,cum_sum);
}
cum_sum+= suit_lengths[j];
}
out.push_back(Node(cards, suits, 0,false));
#ifdef DEBUG
std::cout << __builtin_popcount(cards) << " " << __builtin_popcount(suits[0]) + __builtin_popcount(suits[1]) + __builtin_popcount(suits[2]) + __builtin_popcount(suits[3]) << std::endl;
std::cout << popcnt_u32(cards) << " " << popcnt_u32(suits[0]) + popcnt_u32(suits[1]) + popcnt_u32(suits[2]) + popcnt_u32(suits[3]) << std::endl;
std::cout << cards << " " << suits[0] << " " << suits[1] << " " << suits[2] << " " << suits[3] << std::endl;
#endif

Expand Down Expand Up @@ -561,12 +561,12 @@ void ThreadSolver(int size_endgames, vectorNa* outTTable, vectorNa* TTable, std:
}
for (int i = 0; i < suit_splits.size(); ++i) {
std::array<uint32_t, kNumSuits> suit_arr;
suit_arr[0] = _bzhi_u32(~0, suit_splits[i] & 0b1111);
int sum = suit_splits[i] & 0b1111;
suit_arr[0] = bzhi_u32(~0, suit_splits[i] & 0b1111);
uint32_t sum = suit_splits[i] & 0b1111;
for (int j = 1; j < kNumSuits; ++j) {
uint32_t mask = _bzhi_u32(~0, sum);
uint32_t mask = bzhi_u32(~0, sum);
sum += (suit_splits[i] & (0b1111 << (4 * j))) >> 4 * j;
suit_arr[j] = _bzhi_u32(~0, sum);
suit_arr[j] = bzhi_u32(~0, sum);
suit_arr[j] = suit_arr[j] ^ mask;
}
Node node(cards, suit_arr, 0, false);
Expand Down
Loading

0 comments on commit 3184a10

Please sign in to comment.