Visual C ++ 2015,舍入模式和优化问题 [英] Visual C++ 2015, rounding mode and optimization issue
问题描述
大家好,
我需要使用visual c ++以特定的舍入模式(朝向负无穷大)执行计算。我的程序的行为从vs2013改为vs2015,它似乎与在vs2013中没有执行的优化有关。
I need to perform computations with specific rounding mode (toward minus infinity) with visual c++. The behaviour of my program changed from vs2013 to vs2015 and it seems to be related to an optimization that was not performed in vs2013.
这是一个最小的例子(用cl example.cpp /编译) fp:strict / O2):
Here is a minimal example (compiled with cl example.cpp /fp:strict /O2):
#include <iostream>
int main(){
union{
long long i;
double d;
} value;
//rounding mode to minus infinity
_MM_SET_ROUNDING_MODE((unsigned int) _MM_ROUND_DOWN);
_control87(_DN_FLUSH | _RC_DOWN | _PC_53, _MCW_DN | _MCW_RC | _MCW_PC);
value.i = 0x3ff0000000000000;
double x = value.d;
value.i = 0x7feffffc57ca82ae;
double y = value.d;
value.d = -((-x) + (-y));
std::cout << std::hex << value.i << std::endl; //expect 7feffffc57ca82af
value.d = x + y;
std::cout << std::hex << value.i << std::endl; // expect 7feffffc57ca82ae
}
VS2010和2013得到预期的结果,但vs2015和vs2017总是显示7feffffc57ca82ae,好像在我的第一次计算中简化了减号。
VS2010 and 2013 get the expected results but vs2015 and vs2017 always display 7feffffc57ca82ae as if the minus signs were simplified in my first computation.
根据我对IEEE-754和/ fp:strict选项的理解,预期结果应该是vs2013的结果,而不是vs2015的结果。
In my understanding of IEEE-754 and the /fp:strict option, the expected results should be the results of vs2013 and not the results of vs2015.
如果我删除优化标志(/ O2),两个编译器都会返回预期值。
If I remove the optimization flag (/O2) both compilers return the expected values.
推荐答案
>根据我对IEEE-754和/ fp:strict选项的理解,预期结果应该是vs2013的结果,而不是vs2015的结果。
>In my understanding of IEEE-754 and the /fp:strict option, the expected results should be the results of vs2013 and not the results of vs2015.
使用VS2017帮助,发送反馈,报告问题工具向MS报告,以了解他们对此有何看法。
Report it to MS using the VS2017 Help, Send Feedback, Report a Problem facility to see what they have to say about it.
Dave
这篇关于Visual C ++ 2015,舍入模式和优化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!