Long time MSVC user, new to gcc (so bear with me).
I am using the rubenvb version of c++ (see version in subject, yes I'm building for 64bit) on Windows 7 and I'm having a problem using _BitScanForward64. Some sample code looks like this:
int __cdecl main(int argc, char* argv[])
{
DWORD d = (DWORD)atoi(argv[1]);
DWORD ix, ix2;
ix2 = _BitScanForward64(&ix, d);
printf("bsf %u %u\n", ix, ix2);
}
I am compiling with:
"C:\Program Files\gcc2\mingw64\bin\c++.exe" -o iTot.exe -mno-ms-bitfields -march=native -momit-leaf-frame-pointer -mwin32 -Os -fomit-frame-pointer -m64 -msse4 -mpopcnt -D WINDOWS main.cpp
When I run iTot.exe using the parameter 8, I expected that _BitScanForward64 would set ix to 3. That's what MSVC does. However, ix is 0 and ix2 is 1.
Also, looking at the assembler, I see:
bsfq QWORD PTR 44[rsp],rax # MEM[(volatile LONG64 *)&ix], Mask
Under the circumstances, why does gcc force a memory write+read here?
So, a few questions:
- Is _BitScanForward64 somehow supposed to be called differently under gcc? If I'm just calling it wrong, that would be good to know (although the incompatibility with MSVC would be a pain).
- Why does the _BitScanForward64 intrinsic force a memory write?
Staring at the assembler output from -S, I couldn't see anything wrong with the code being generated. However, using objdump.exe -d -Mintel, I see that rather than using the asm code above (which seems like it would work), it actually produced the reverse:
bsf rax,QWORD PTR [rsp+0x2c]
WTF? Why is -S lying to me?
Like I said, I'm new to gcc, so if I'm just doing something dumb, be gentle with me. Thanks.