Skip to content

<numeric>: _Countr_zero used by std::gcd has unnecessary zero checks #2292

@AlexGuteniev

Description

@AlexGuteniev

Originally reported as DevCom-1560717

_Countr_zero in this loop has unnecessary checks for zero input in the compiled code:

STL/stl/inc/numeric

Lines 567 to 577 in d8f03cf

_Mx_magnitude >>= _Mx_trailing_zeroes;
do {
_Nx_magnitude >>= static_cast<unsigned long>(_Countr_zero(_Nx_magnitude));
if (_Mx_magnitude > _Nx_magnitude) {
_Common_unsigned _Temp = _Mx_magnitude;
_Mx_magnitude = _Nx_magnitude;
_Nx_magnitude = _Temp;
}
_Nx_magnitude -= _Mx_magnitude;
} while (_Nx_magnitude != 0U);

See the demo with __assume workaround: https://godbolt.org/z/Ts7ExjbYb

As the reported noted, compiler codegen could be improved.

But from the library, it can also be fixed by making specialized _Countr_zero that does not check for zero input, and calling the specialized version from std::gcd.

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