最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Good practices guidelines for `ffast-math` - Stack Overflow

programmeradmin3浏览0评论

I am writing C++ header-only library doing some floating-point math. Since the library is header-only I realized that the library user sooner or later will include it into his project where -ffast-math is enabled. And I cannot prevent the user from doing it except of writing in documentation: "Please, don't do it". The question is what can I do from my side to better support -ffast-math?

I've tested my code and probably all rare issues are related to -finite-math-only. It is not surprising since I don't do anything special like Kahan summation, but it is still an issue. If I understand -finite-math-only correctly, then every x * y multiplication for unbounded values x and y is UB, powering unbounded value to the power greater than 1 is UB, exp(x) is UB, etc.

I am writing C++ header-only library doing some floating-point math. Since the library is header-only I realized that the library user sooner or later will include it into his project where -ffast-math is enabled. And I cannot prevent the user from doing it except of writing in documentation: "Please, don't do it". The question is what can I do from my side to better support -ffast-math?

I've tested my code and probably all rare issues are related to -finite-math-only. It is not surprising since I don't do anything special like Kahan summation, but it is still an issue. If I understand -finite-math-only correctly, then every x * y multiplication for unbounded values x and y is UB, powering unbounded value to the power greater than 1 is UB, exp(x) is UB, etc.

Share Improve this question asked Mar 17 at 15:12 0x22070x2207 1,0321 gold badge8 silver badges26 bronze badges 5
  • 2 This might help: stackoverflow/a/22135559/4342498 – NathanOliver Commented Mar 17 at 15:17
  • I think the best you can do is write in the documentation: Please, don't do it. and call it a day. HC SVNT DRACONES. caveat elit (developer beware). – Eljay Commented Mar 17 at 15:18
  • 2 Which compiler(s) are you targeting? --ffast-math is both GCC and CLang; MSVC has /fp:fast which is similar in spirit. But it's not at all obvious which compiler(s) will have issues with your cod.e – MSalters Commented Mar 17 at 15:37
  • Note that you cannot prevent using any compilation options anyway, for a header-only library or not. Any form of source code. So, yes, the documentation or better support are the only options. I would only advise you to remove the words “best practices”. In Stackoverflow, there are many reports against posts “inviting opinions”, so some questions are closed as “opinion-based”. I'm not saying it is good, this is what it is. One possible title could be “How to support -ffast-math and -finite-math-only?” Also, some code samples would be helpful. – Sergey A Kryukov Commented Mar 17 at 15:37
  • maybe you can a bullet to your question, specifically to prompt readers to respond to: • is there a way to use static_assert on some specific constexpr math expression such that the static_assert would fail only when some behavior of ffast-math is enabled? (likely not, because it seems that ffast-math behaviors are runtime and not compile time, but if there is any compile time change that could be inspected, then that would help you) – pestophagous Commented Mar 17 at 15:44
Add a comment  | 

1 Answer 1

Reset to default 7

And I cannot prevent the user from doing it [...]

Yes, you can.

#ifdef __FAST_MATH__
#error -ffast-math is not supported
#endif

Try on godbolt

See also how to use the gcc preprocessor macro __FAST_MATH__?

There are also (undocumented) predefined macros __ASSOCIATIVE_MATH__, __FINITE_MATH_ONLY__, __NO_MATH_ERRNO__, __NO_SIGNED_ZEROS__, __NO_TRAPPING_MATH__, and __RECIPROCAL_MATH__ for the corresponding options, so to be a bit more careful, you could also test some or all of those.

发布评论

评论列表(0)

  1. 暂无评论