Skip to content

Commit

Permalink
avoid hexfloats which not support in C++14
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGraey committed Aug 3, 2020
1 parent 8b04e1e commit 4938680
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/support/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ template<typename T> bool IsPowerOf2(T v) {
template<typename T> bool IsPowerOf2Float(T v) {
static_assert(std::is_floating_point<T>::value, "unexpected type");

const double MIN_POT = 0x1.0p-1022; // 0x001 << 52
const double MAX_POT = 0x1.0p+1022; // 0x7FD << 52
const double MIN_POT = 0x001ULL << 52; // 0x1.0p-1022
const double MAX_POT = 0x7FDULL << 52; // 0x1.0p+1022
// TODO: use different implementations for 32-bit and 64-bit floats
double x = v; // promote 32-bit floats to 64-bit floats
uint64_t y = reinterpret_cast<uint64_t&>(x) & (0x7FFULL << 52);
double z = reinterpret_cast<double&>(y);
if (z < MIN_POT || z > MAX_POT) {
if (z < reinterpret_cast<const double&>(MIN_POT) ||
z > reinterpret_cast<const double&>(MAX_POT)) {
return false;
}
return x == z;
Expand Down

0 comments on commit 4938680

Please sign in to comment.