Skip to content

Commit

Permalink
Fix undefined behavior
Browse files Browse the repository at this point in the history
no functional change
  • Loading branch information
xu-shawn committed Jan 20, 2025
1 parent 8e3e22b commit 703a9b3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
11 changes: 6 additions & 5 deletions src/bitboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <cstdint>
#include <cstdlib>
#include <string>
Expand Down Expand Up @@ -268,11 +269,11 @@ inline int popcount(Bitboard b) {

#ifndef USE_POPCNT

union {
Bitboard bb;
uint16_t u[4];
} v = {b};
return PopCnt16[v.u[0]] + PopCnt16[v.u[1]] + PopCnt16[v.u[2]] + PopCnt16[v.u[3]];
std::uint16_t indices[4];
std::memcpy(indices, &b, sizeof(b));
return PopCnt16[indices[0]] + PopCnt16[indices[1]] + PopCnt16[indices[2]]
+ PopCnt16[indices[3]];


#elif defined(_MSC_VER)

Expand Down
7 changes: 2 additions & 5 deletions src/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,8 @@ void sync_cout_start();
void sync_cout_end();

// True if and only if the binary is compiled on a little-endian machine
static inline const union {
uint32_t i;
char c[4];
} Le = {0x01020304};
static inline const bool IsLittleEndian = (Le.c[0] == 4);
static inline const std::uint16_t Le = 1;
static inline const bool IsLittleEndian = *reinterpret_cast<const char*>(&Le) == 1;


template<typename T, std::size_t MaxSize>
Expand Down

0 comments on commit 703a9b3

Please sign in to comment.