diff --git a/stl/inc/limits b/stl/inc/limits index f93a63ac90f..bfe6ee02843 100644 --- a/stl/inc/limits +++ b/stl/inc/limits @@ -1058,9 +1058,14 @@ _NODISCARD int _Checked_x86_x64_countr_zero(const _Ty _Val) noexcept { constexpr _Ty _Max = (numeric_limits<_Ty>::max)(); #ifndef __AVX2__ - const bool _Definitely_have_tzcnt = __isa_available >= __ISA_AVAILABLE_AVX2; - if (!_Definitely_have_tzcnt && _Val == 0) { - return _Digits; + // Because the widening done below will always give a non-0 value, checking for tzcnt + // is not required for 8-bit and 16-bit since the only difference in behavior between + // bsf and tzcnt is when the value is 0. + if constexpr (_Digits > 16) { + const bool _Definitely_have_tzcnt = __isa_available >= __ISA_AVAILABLE_AVX2; + if (!_Definitely_have_tzcnt && _Val == 0) { + return _Digits; + } } #endif // __AVX2__