_BitScanForward64 bug

_BitScanForward64 bug

Dmitry Vyukov's picture

Intel C++ Compiler 11.1.071 [Intel 64]

Following program compiles clean on /W5:

#include 
#include 

int main()
{
    unsigned __int64 value = 1000;
    unsigned long index [2] = {123, 456};
    printf("%u %un", (unsigned)index[0], (unsigned)index[1]);
    _BitScanForward64(&index[0], value);
    printf("%u %un", (unsigned)index[0], (unsigned)index[1]);
}

The output is:

123 456
3 0

Expected output is:

123 456
3 456

I.e. _BitScanForward64() treats index as __int64, thus overwrites memory after it.

Documentation clearly states that for both 32 and 64 bit versions index is unsigned long:

unsigned char _BitScanForward(
unsigned long * Index,
unsigned long Mask
);
unsigned char_BitScanForward64(
unsigned long * Index,
unsigned __int64 Mask
);

The same applies to _BitScanReverse64().

All about lock-free algorithms, multicore, scalability, parallel computing and related topics: http://www.1024cores.net
8 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
Dmitry Vyukov's picture

Is it appropriate place for posting error reports? Maybe I need to send it somewhere else?

All about lock-free algorithms, multicore, scalability, parallel computing and related topics: http://www.1024cores.net
Feilong H (Intel)'s picture

Hi Dmitriy,

Thanks for your problem report. I'm looking into this issue now. Will get back to you soon.

Thank you.
--
Feilong H.
Intel Developer Support

Tools Knowledge Base: http://software.intel.com/en-us/articles/tools

Feilong H (Intel)'s picture

I tested with VS2005. Same output. I'm looking into further. Any input is welcome.

C:\>type test.c
#include
#include

#pragma intrinsic(_BitScanForward64)

int main()
{
unsigned __int64 value = 1000;
unsigned long index[2] = {123, 456};
printf("%u %u\n", (unsigned)index[0], (unsigned)index[1]);
_BitScanForward64(&index[0], value);
printf("%u %u\n", (unsigned)index[0], (unsigned)index[1]);
}
C:\>cl test.c
Microsoft C/C++ Optimizing Compiler Version 14.00.50727.762 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

test.c
Microsoft Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.

/out:test.exe
test.obj

C:\>test
123 456
3 0

C:\>

Dmitry Vyukov's picture

That just suggests that VC2005 has the same bug. Such behavior can't be correct in any circumstances - compiler silently machine-guns random memory (we, C/C++ developers, like to do that by ourselfs, but we can't permit a compiler to do the same).

Anyway, my VC2008 outputs:

123 456
3 456

And when I recompile my real program with ICC it firmly crashes.

All about lock-free algorithms, multicore, scalability, parallel computing and related topics: http://www.1024cores.net
Feilong H (Intel)'s picture

Dmitriy,

Thanks for your input. I just entered this issue to our problem-tracking database. I'll let you know when there is an update regarding this issue.

Thanks,

Feilong

Feilong H (Intel)'s picture

Just an update. Engineering has implemented a fix for this issue. I'll let you know when a compiler update contains this fix is available for download.

Feilong

Feilong H (Intel)'s picture

Compiler 11.1 Update 6 (Package ID: w_cproc_p_11.1.065) contains the fix for this issue. You may download it at https://registrationcenter.intel.com.

Thanks,
Feilong

Login to leave a comment.