GCC用-O0和-O2给出不同的数值结果

时间:2021-09-06 19:56:12

I'm using 4.1.2. Does anyone have any ideas of the best places in my code to look? Experience with common causes? There are some ugly pointer casts (ie, d = (double) (* (float *) p), where p is pointer-to-int) that I'm working on eliminating, but no luck yet.
For what it's worth, -O0 is giving the correct answer. Thanks for any help.

我正在使用4.1.2。有没有人对我的代码中最好的地方有任何想法?有共同原因的经验?有一些丑陋的指针转换(即,d =(double)(*(float *)p),其中p是指向int的指针)我正在努力消除,但还没有运气。对于它的价值,-O0给出了正确的答案。谢谢你的帮助。

2 个解决方案

#1


I'd check for strict aliasing issues, as demonstrated here: http://www.cellperformance.com/mike_acton/2006/06/understanding_strict_aliasing.html

我将检查严格的别名问题,如下所示:http://www.cellperformance.com/mike_acton/2006/06/understanding_strict_aliasing.html

Without knowing exactly what your code does, the mention of "ugly pointer casts" make me suspect aliasing problems.

如果不知道你的代码究竟做了什么,提到“丑陋的指针转换”会让我怀疑是否存在别名问题。

It would be helpful for you, and make it easier for us to answer, if you provided some code that demonstrated the issue.

如果你提供了一些证明问题的代码,它对你有帮助,并使我们更容易回答。

#2


Thanks everyone, -fno-strict-aliasing (suggested by several) solved my problem. Thanks for all your help.
Lesson learned: Always compile with warning flags.

谢谢大家,-fno-strict-aliasing(由几个人建议)解决了我的问题。感谢你的帮助。获得的经验:始终使用警告标志进行编译。

#1


I'd check for strict aliasing issues, as demonstrated here: http://www.cellperformance.com/mike_acton/2006/06/understanding_strict_aliasing.html

我将检查严格的别名问题,如下所示:http://www.cellperformance.com/mike_acton/2006/06/understanding_strict_aliasing.html

Without knowing exactly what your code does, the mention of "ugly pointer casts" make me suspect aliasing problems.

如果不知道你的代码究竟做了什么,提到“丑陋的指针转换”会让我怀疑是否存在别名问题。

It would be helpful for you, and make it easier for us to answer, if you provided some code that demonstrated the issue.

如果你提供了一些证明问题的代码,它对你有帮助,并使我们更容易回答。

#2


Thanks everyone, -fno-strict-aliasing (suggested by several) solved my problem. Thanks for all your help.
Lesson learned: Always compile with warning flags.

谢谢大家,-fno-strict-aliasing(由几个人建议)解决了我的问题。感谢你的帮助。获得的经验:始终使用警告标志进行编译。