Different optimizations in VS2015 and VS2013 cause a floating point exception

I have a small example of the problem that occurred when switching from VS2013 to VS2015. In VS2015, the following code example causes an invalid floating point operation.

enter image description here

int main()
{
    unsigned int enableBits = _EM_OVERFLOW | _EM_ZERODIVIDE | _EM_INVALID;

    _clearfp();
    _controlfp_s(0, ~enableBits, enableBits);

    int count = 100;
    float array[100];

    for (int i = 0; i < count; ++i)
    {
        array[i] = (float)pow((float)(count - 1 - i) / count, 4); //this causes exception in VS2015
    }

    return 0;
}

This only happens in release mode, so it’s probably due to another optimization. Is there something wrong with this code or is this a bug in VS 2015?

It is difficult to find such problems on the entire code base, so I'm looking for some kind of systematic fix, and not a workaround (for example, use another variable instead of i that works)

, , , VS2013 128- 4 . VS2015, , 2 , ( ), , , .

, , .

VS2013 VS2013

VS2015 enter image description here

. .

+4
1

, .

, 2 ( ), divps, 4 ( 4 XMM). 2 XMM . , . , , op, , , .

- , , /fp: strict, , ( , , ) controlfp.

+1

Source: https://habr.com/ru/post/1657375/


All Articles