Skip to content

<random>: generate_canonical() could avoid calling log2() at runtime #1964

@StephanTLavavej

Description

@StephanTLavavej

Gautham Beeraka reported a performance issue in generate_canonical(), where it calls log2():

STL/stl/inc/random

Lines 248 to 259 in 4c862ee

template <class _Real, size_t _Bits, class _Gen>
_NODISCARD _Real generate_canonical(_Gen& _Gx) { // build a floating-point value from random sequence
_RNG_REQUIRE_REALTYPE(generate_canonical, _Real);
const size_t _Digits = static_cast<size_t>(numeric_limits<_Real>::digits);
const size_t _Minbits = _Digits < _Bits ? _Digits : _Bits;
const _Real _Gxmin = static_cast<_Real>((_Gx.min)());
const _Real _Gxmax = static_cast<_Real>((_Gx.max)());
const _Real _Rx = (_Gxmax - _Gxmin) + _Real{1};
const int _Ceil = static_cast<int>(_STD ceil(static_cast<_Real>(_Minbits) / _STD log2(_Rx)));

For engines like mt19937 and mt19937_64, this is a really expensive way of computing 32.0 and 64.0.

Solving this in a completely general manner might be somewhat difficult, but perhaps we could simply special-case the Standard engines that are known to have a power-of-2 range.

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixedSomething works now, yay!performanceMust go faster

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions