A branch-free fpclassify()
From
Bonita Montero@Bonita.Montero@gmail.com to
comp.lang.c++ on Mon Nov 9 08:41:02 2026
From Newsgroup: comp.lang.c++
template<typename Fp>
constexpr int fp_type( Fp v ) noexcept
{
using namespace std;
static_assert( same_as<Fp, double> || same_as<Fp, float> );
static constexpr bool Dbl = std::same_as<Fp, double>;
using word = std::conditional_t<Dbl, uint64_t, uint32_t>;
static constexpr int
MantBits = Dbl ? 52 : 23,
MaxExp = Dbl ? 0x7FF : 0xFF;
static constexpr word
Sign = ~((word)-1 >> 1),
Exp = (word)MaxExp << MantBits,
MinNormal = (word)1 << MantBits,
Mant = MinNormal - 1;
word
bin = bit_cast<word>( v ),
exp = bin & Exp;
bool nzMant = (bin & Mant) != 0;
int
zroExpType = nzMant ? FP_SUBNORMAL : FP_ZERO,
maxExpType = nzMant ? FP_NAN : FP_INFINITE;
int type = exp != 0 ? maxExpType : zroExpType;
bool normal = bin - MinNormal < Exp - MinNormal;
type = normal ? FP_NORMAL : type;
return type;
}
--- Synchronet 3.22a-Linux NewsLink 1.2