• Fastest version of nextafter

    From Bonita Montero@3:633/10 to All on Thu Aug 13 15:44:57 2026
    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 );
    }

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)