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

Bug using conditinoal operator and bit-manipulations

Stoyanov__MIroslav
340 Views

Using Intel ICC 18, I encountered the following bug in 

int p = ((l == 0) ? 0 : (1 << l));

Here is how to reproduce:

[<snip> Intel18]$ cat main.cpp 
#include <iostream>

int main(int, char**){

    // for l = 0, 1, 2, 3, ...
    // we want to output the sequence
    //     p = 0, 2, 4, 8, ...

    for(int l=0; l<4; l++){
        int p = ((l == 0) ? 0 : (1 << l));
        std::cout << p << " ";
    }
    std::cout << std::endl;

    return 0;
}

[<snip> Intel18]$ icc -v
icc version 18.0.1 (gcc version 6.3.0 compatibility)
[<snip> Intel18]$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=<snip>
Target: x86_64-pc-linux-gnu
Configured with: <snip> --disable-multilib --enable-languages=c,c++,fortran
Thread model: posix
gcc version 6.3.0 (GCC) 
[<snip> Intel18]$ icc main.cpp -o run_intel
[<snip> Intel18]$ ./run_intel
1 2 4 8 
[<snip> Intel18]$ g++ main.cpp -o run_gcc
[<snip> Intel18]$ ./run_gcc 
0 2 4 8

Workaround is to flip the if-statement:

int p = ((l > 0) ? (1 << l) : 0);

Does anyone know the "official" way of reporting such bugs?

0 Kudos
2 Replies
Viet_H_Intel
Moderator
340 Views

This seems to be fixed in 19.0 Update 3:

$  rm run_intel ; icpc t.cpp -o run_intel&& ./run_intel
0 2 4 8
$ icc -v
icc version 19.0.3.199 (gcc version 6.3.0 compatibility)
$ cat t.cpp
#include <iostream>

int main(int, char**){
    for(int l=0; l<4; l++){
        int p = ((l == 0) ? 0 : (1 << l));
        std::cout << p << " ";
    }
    std::cout << std::endl;

    return 0;
}
 

0 Kudos
Stoyanov__MIroslav
340 Views

Thank for checking on this, Viet!

I'll see when our IT is planning on upgrading the clusters to Intel 19.

Best!

0 Kudos
Reply