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

Bug report - compiler isn't aware that popcnt affects ZF

rbarreira
Beginner
454 Views
Hi,

I'm using Intel C++ Compiler version 11.1 under 64-bit Nehalem architecture, with -xSSE4.2 -O3 as optimization options.

I have some code which looks like:

[cpp]int var1 = __builtin_popcountll (x);
int var2 = (y != 0ULL);[/cpp]

The compiler uses testq/cmovne for the second statement, and popcntq for the first. But due to instruction reordering, testq is placed before the popcntq instruction (cmovne after popcntq). According to the Intel instruction set reference, popcnt instructions affect ZF (and other flags too), so this results in wrong behavior (var2 is assigned an incorrect value).

If you need sample source code I will try to provide a small example based on my source code which demonstrates the bug.

Thanks,
Ricardo Barreira

PS: If -xSSE3 or -xSSE4.1 is used the bug does not happen, since the popcntq instruction is not used in that case.
0 Kudos
5 Replies
rbarreira
Beginner
454 Views
OK I managed to get a small program which demonstrates the bug:

> icc -V
Intel C Intel 64 Compiler Professional for applications running on Intel 64, Version 11.1 Build 20100414 Package ID: l_cproc_p_11.1.072

> cat test_bug.c

[cpp]#include 

int main (void)
{
int var1, var2;
unsigned long long x, y;

scanf ("%llu %llu", &x, &y);
var1 = __builtin_popcountll (x);
var2 = (y != 0);
var1 += __builtin_popcountll (var1);

printf ("%d %dn", var1, var2);
return 0;
}[/cpp]


> icc -xSSE4.2 -O3 test_bug.c -o test_bug

> ./test_bug
1 0
2 1

> icc -xSSE4.1 -O3 test_bug.c -o test_bug

> ./test_bug
1 0
2 0


Fragment of incorrect code:

[cpp]   0x000000000040098b <+75>:    mov    $0x1,%r8d
   0x0000000000400991 <+81>:    xor    %edx,%edx
   0x0000000000400993 <+83>:    test   %rcx,%rcx
   0x0000000000400996 <+86>:    popcnt %rsi,%r9
   0x000000000040099b <+91>:    cmovne %r8d,%edx[/cpp]

0 Kudos
JenniferJ
Moderator
454 Views
Thank you so much for the small testcase!

I'll send it to compiler team, if there is any progress or news, I'll let you know.

Regards,
Jennifer
0 Kudos
Mark_S_Intel1
Employee
454 Views
Thanks for the test case. I reproduced the problem and filed a report on this issue. We will let you know when the problem is resolved.

--mark
0 Kudos
rbarreira
Beginner
454 Views
Thanks for submitting the bug.

I saw there's a new compiler update, but I couldn't find this issue in there. Is it fixed or will it only be in a future update?
0 Kudos
Mark_S_Intel1
Employee
454 Views

Yes. This problem has been resolved inicc 11.1 build 20100806.

--mark

$ icc -xSSE4.2 -O2 test_bug.c -o test_bug -g
[msabahi@dpd26 74776]$ icc -V -xSSE4.2 -O2 test_bug.c -o test_bug -g
Intel C Intel 64 Compiler Professional for applications running on Intel 64, Version 11.1 Build 20100806 Package ID: l_cproc_p_11.1.073
Copyright (C) 1985-2010 Intel Corporation. All rights reserved.

Edison Design Group C/C++ Front End, version 3.10.1 (Aug 6 2010 19:14:21)
Copyright 1988-2007 Edison Design Group, Inc.

GNU ld version 2.18.50.0.6-7.fc9 20080403
[msabahi@dpd26 74776]$ ./test_bug
1 0
2 0

0 Kudos
Reply