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

static_cast and optimization

eckardt4
Beginner
360 Views

Hi,

In my application I have the following code for the calculation of the number of rows/columns of a symmetric square matrix which is stored as upper triangle matrix

[cpp]#include 
#include 
int main()
{
    int exclude_matrix[] =
        {
            1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
              1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
                1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
                  1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
                    1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
                      1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
                        1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,
                          1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
                            1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
                              1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
                                1,0,1,1,1,1,1,1,1,1,1,1,1,1,
                                  1,1,1,1,1,1,1,1,1,1,1,1,1,
                                    1,0,0,0,0,0,0,0,0,0,0,0,
                                      1,0,0,0,0,0,0,0,0,0,0,
                                        1,0,0,0,0,0,0,0,0,0,
                                          1,0,0,0,0,0,0,0,0,
                                            1,0,0,0,0,0,0,0,
                                              1,0,0,0,0,0,0,
                                                1,0,1,1,1,0,
                                                  1,1,1,1,1,
                                                    1,0,0,1,
                                                      1,0,1,
                                                        1,1,
                                                          1
            
       };
    double tmp = (sqrt(2.*sizeof(exclude_matrix)/sizeof(int)+.25)-.5);
    std::cout << tmp << std::endl;
    std::cout << static_cast(tmp) << std::endl;
    return 0;
}
[/cpp]

This code is compiled with the Intel cpp-compiler, version 10.1.018 running on Intel 64.

Using -O0 or -O1 as optimization the right result (24) is obtained.
With -O2 and -O3 the result after casting the double value to an unsigned short value is 23 and not 24.
The value of tmp before casting is still 24.

Thanks.

Stefan

0 Kudos
1 Solution
jimdempseyatthecove
Honored Contributor III
360 Views

The static_cast is returning the integer portion of the number which may be one (or two) bits below 24.0withing the precision of the double.

Remove the -.5 from your expression evaluating tmp and you will receive the correct value in the integer portion of tmp.

Jim Dempsey

View solution in original post

0 Kudos
1 Reply
jimdempseyatthecove
Honored Contributor III
361 Views

The static_cast is returning the integer portion of the number which may be one (or two) bits below 24.0withing the precision of the double.

Remove the -.5 from your expression evaluating tmp and you will receive the correct value in the integer portion of tmp.

Jim Dempsey

0 Kudos
Reply