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

C++20 support for constexpr and SIMD simultaneously #987

Open
PazerOP opened this issue Jan 1, 2020 · 0 comments
Open

C++20 support for constexpr and SIMD simultaneously #987

PazerOP opened this issue Jan 1, 2020 · 0 comments

Comments

@PazerOP
Copy link

PazerOP commented Jan 1, 2020

With C++20 and std::is_constant_evaluated(), it becomes possible to provide both simd and constexpr:

namespace detail
{
	constexpr bool is_constant_evaluated()
	{
#if __cpp_lib_is_constant_evaluated >= 201811
		return std::is_constant_evaluated();
#else
		// The user's compiler doesn't support std::is_constant_evaluated(). 
		// This should be a user-configurable switch to decide if they want
		// SIMD or constexpr (since they have to choose one or the other).
		return true;
#endif
	}
}

constexpr glm::vec4 some_function()
{
	if (detail::is_constant_evaluated())
	{ 
		// standard c++ implementation
	}
	else
	{
		// SIMD implementation, you can do anything here, intrinsics, reinterpret_cast, etc
	}
}

In the above example, if the user's compiler supports std::is_constant_evaluated(), this works exactly as expected:

	constexpr auto slow_but_compile_time_version = some_function();
	const auto fast_runtime_version = some_function();

On older compilers, depending on the value of the user-configurable switch, fast_runtime_version would become "slow" version, or slow_but_compile_time_version would not compile. This is the current behavior, forcing the user to decide between constexpr and SIMD.

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

No branches or pull requests

2 participants