Question: Can IL2CPP ldsfld BitConverter.IsLittleEndian be made constexpr?

C++20 adds a new feature std::endian.

Current IL2CPP BitConverter.IsLittleEndian is assgned at runtime.
And if statement optimization has not been performed.

Can the ldsfld BitConverter.IsLittleEndian be replaced?

#if __cpp_lib_endian
#include <bit>
#endif

#if __cpp_lib_endian
std::endian::native == std::endian::little;
#else
((BitConverter_t8DCBA24B909F1B221372AF2B37C76DCF614BA654_StaticFields*)il2cpp_codegen_static_fields_for(BitConverter_t8DCBA24B909F1B221372AF2B37C76DCF614BA654_il2cpp_TypeInfo_var))->get_IsLittleEndian_0();
#endif

While I think this is possible, IL2CPP currently targets C++11, because we try to stick with the lowest common C++ version among the compilers that IL2CPP supports. It would be possible to use a #if statement like this to allow the feature where it is supported by the C++ compiler, but we don’t currently emit much code like this.

I don’t think this is a feature we will be adding anytime soon, as we won’t likely have all compilers on C++20 for 5+years.

Thank you for the answer.

This is very useful information.

MSVC and Clang? I don’t know the compilers of video game consoles. Anyway, I’ll wait for the next 5+ years.

Another question:
If I come up with a similar optimization (replacement IL2CPP) suggestion, should I post a new thread or add replies to this thread?

IL2CPP currently supports various versions of MSVC, Clang, and GCC, as well as forks of them and separate compilers for some platforms, so it is really all over the place.

I’d recommend a new thread for each independent one.

1 Like

Thanks!
Oh, GCC. I’ve forgot that.
It must be really hard to take care of so many compilers. Thank you for your efforts.

OK! Thanks!