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 · 1 comment
Open

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

PazerOP opened this issue Jan 1, 2020 · 1 comment

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.

@sharkautarch
Copy link

sharkautarch commented Sep 12, 2024

@PazerOP
I’ve made a draft pr that implements support for using simd and constexpr simultaneously: #1313

note that you have to set this define before including glm headers for it to work:
#define GLM_SIMD_CONSTEXPR 1
Also three other things to note:

  • this pr makes use of gcc’s vector extensions for the simd part of the constructors, so it’s only compatible with gcc and clang
  • Only constexpr constructor support is added, the operator overloads don’t currently work at compile time
  • Directly using swizzling expressions when calling c-style va_arg functions is broken (ex: printf("%f\n", v.y); always prints 0.0000). C++-style variadic template functions should work fine tho <- fixed

UPDATE: I've now added constexpr support to the operators as well.
I think my draft PR is definitely getting closer to being able to become a real PR, I think I just need to clean up the code a bit more...

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

3 participants