Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code behaves differently if compiled with MSVC vs GCC #35

Open
olologin opened this issue May 17, 2023 · 1 comment
Open

Code behaves differently if compiled with MSVC vs GCC #35

olologin opened this issue May 17, 2023 · 1 comment

Comments

@olologin
Copy link

olologin commented May 17, 2023

VS 19 vs GCC 12
If you build and run the following sample with both compilers:

#include <fenv.h>
#include <iostream>
#pragma warning(push)
#pragma warning(disable : 4702)
#include <boost/numeric/interval.hpp>
#pragma warning(pop)

/// An interval of doubles [lower,upper)
using Interval = boost::numeric::interval<double,boost::numeric::interval_lib::policies<boost::numeric::interval_lib::rounded_math<double>,boost::numeric::interval_lib::checking_strict<double>>>;

/// An interval from -DBL_MAX to +DBL_MAX
const inline Interval InfiniteInterval =
	Interval(std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max());

int main(void)
{
#ifdef _WIN32
	unsigned int current_word = 0;
	_controlfp_s(&current_word, 0, _MCW_EM);
#else
	feenableexcept(FE_ALL_EXCEPT);
#endif
	auto i = InfiniteInterval;
	boost::numeric::width(i);
	std::cerr << "The end" << std::endl;
}

You will see how it fails with FP exception on linux, but never fails on windows.
The reason is simple:
On linux we adjust only rounding mode

static void set_rounding_mode(rounding_mode mode) { fesetround(mode); }

But on windows in addition to rounding we also mask all the FP exceptions (turn off corresponding flags) because we use _MCW_EM | _MCW_RC instead of _MCW_RC only.

Shouldn't both systems set the same FP settings? If yes - what should they adjust? Both systems should adjust rounding only, or both should adjust rounding and exception masking?
If we only want to change rounding control bits - it is enough to remove _MCW_EM | part of that line and both systems will throw FP exception.

@olologin
Copy link
Author

Maybe I can help with PR if you decide on what should be the preferred behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant