Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

_BitScanForward64 bug

Dmitry_Vyukov
Valued Contributor I
666 Views

Intel C++ Compiler 11.1.071 [Intel 64]

Following program compiles clean on /W5:

[bash]#include 
#include 

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]);
}
[/bash]

The output is:

[bash]123 456
3 0[/bash]

Expected output is:

[bash]123 456
3 456[/bash]

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().

0 Kudos
7 Replies
Dmitry_Vyukov
Valued Contributor I
666 Views

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

0 Kudos
Feilong_H_Intel
Employee
666 Views

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
0 Kudos
Feilong_H_Intel
Employee
666 Views

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:\>

0 Kudos
Dmitry_Vyukov
Valued Contributor I
666 Views

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.

0 Kudos
Feilong_H_Intel
Employee
666 Views

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

0 Kudos
Feilong_H_Intel
Employee
666 Views

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

0 Kudos
Feilong_H_Intel
Employee
666 Views
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
0 Kudos
Reply