Skip to content

Commit da28073

Browse files
pdaniell-nvdabrain34
authored andcommitted
decode: Follow up fix for count_trailing_zeros() for 32-bit MSVC
In the original fix (#130) I got the direction of the bitscan wrong. I thought it was from MSB to LSB, but _BitScanForward (and count_trailing_zeros) scans from LSB to MSB.
1 parent 9299d12 commit da28073

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

vk_video_decoder/libs/NvVideoParser/include/cpudetect.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef CPUDETECT_H
22
#define CPUDETECT_H
33

4+
#include <assert.h>
5+
46
enum SIMD_ISA
57
{
68
NOSIMD = 0,
@@ -13,16 +15,21 @@ enum SIMD_ISA
1315

1416
static int inline count_trailing_zeros(unsigned long long resmask)
1517
{
18+
assert(resmask != 0ULL); // result is undefined if resmask is zero
19+
1620
#ifdef _WIN64
1721
unsigned long offset = 0;
18-
const unsigned char dummyIsNonZero =_BitScanForward64(&offset, resmask); // resmask can't be 0 in this if
22+
(void)_BitScanForward64(&offset, resmask);
1923
#elif _WIN32
2024
unsigned long offset = 0;
21-
const unsigned char isNonZero = _BitScanForward(&offset, (unsigned long)(resmask >> 32U)); // resmask can't be 0 in this if
22-
if (isNonZero) {
25+
unsigned long resmaskLsb = (unsigned long)(resmask & 0xFFFFFFFFULL);
26+
if (resmaskLsb != 0U) {
27+
(void)_BitScanForward(&offset, resmaskLsb);
28+
}
29+
else {
30+
unsigned long resmaskMsb = (unsigned long)(resmask >> 32U);
31+
(void)_BitScanForward(&offset, resmaskMsb);
2332
offset += 32U;
24-
} else {
25-
_BitScanForward(&offset, (unsigned long)resmask); // resmask can't be 0 in this if
2633
}
2734
#else
2835
int offset = __builtin_ctzll(resmask);
@@ -32,4 +39,4 @@ static int inline count_trailing_zeros(unsigned long long resmask)
3239

3340
SIMD_ISA check_simd_support();
3441

35-
#endif
42+
#endif

0 commit comments

Comments
 (0)