Pop-Up Thingie
Sidebar
Too Lazy BBS
Home
Forum
Files
Chat
Bulletins
Top doors
MOTD
BBS Stats
Radio
AvatarChat
Dark
Register
Log in
Username
Password
Sidebar
Forum
USENET
cplusplus
Fastest version of nextafter
From
Bonita Montero
@Bonita.Montero@gmail.com to
comp.lang.c++
on Thu Aug 13 15:44:57 2026
From Newsgroup: comp.lang.c++
Similar to nextafter, but much faster:
template<typename Fp>
template<bool Up>
constexpr Fp fp_tools<Fp>::next( Fp v ) noexcept
{
using namespace std;
constexpr Fp Min = numeric_limits<Fp>::denorm_min();
sword sBin = bit_cast<word>( v );
word uBin = sBin & ~Sign;
if( !uBin ) [[unlikely]]
return Up ? Min : -Min;
sword mxDiff = uBin - Exp;
if( mxDiff > 0 ) [[unlikely]]
return v;
bool toInf = Up ? sBin >= 0 : sBin < 0;
if( !mxDiff && toInf ) [[unlikely]]
return v;
sBin += toInf ? 1 : -1;
word exp = sBin & Exp;
if( exp == Exp || !exp ) [[unlikely]]
{
feraiseexcept( FE_INEXACT | (exp == Exp ? FE_OVERFLOW : FE_UNDERFLOW) );
errno = ERANGE;
return v;
}
return bit_cast<Fp>( sBin );
}
--- Synchronet 3.22a-Linux NewsLink 1.2
Who's Online
System Info
Sysop:
Amessyroom
Location:
Fayetteville, NC
Users:
74
Nodes:
6 (
0
/
6
)
Uptime:
51:09:37
Calls:
1,100
Files:
1,339
Messages:
276,012