From: <sv...@op...> - 2024-08-16 08:48:13
|
Author: manx Date: Fri Aug 16 10:48:01 2024 New Revision: 21473 URL: https://source.openmpt.org/browse/openmpt/?op=revision&rev=21473 Log: [Ref] mpt/base/float.hpp: Integrate with C++23 <stdfloat> and provide optional mpt::stdfloatN types. [Ref] mpt/base/float.hpp: Provide mpt::fastfloat32 and mpt::fastfloat64 which match semantics of previous mpt::somefloat32 and mpt::somefloat64. The latter now try harder to match the requested size by also trying mpt::stdfloatN types. Modified: trunk/OpenMPT/src/mpt/base/detect_quirks.hpp trunk/OpenMPT/src/mpt/base/float.hpp Modified: trunk/OpenMPT/src/mpt/base/detect_quirks.hpp ============================================================================== --- trunk/OpenMPT/src/mpt/base/detect_quirks.hpp Fri Aug 16 10:33:52 2024 (r21472) +++ trunk/OpenMPT/src/mpt/base/detect_quirks.hpp Fri Aug 16 10:48:01 2024 (r21473) @@ -335,4 +335,10 @@ +#if MPT_LIBCXX_GNU_BEFORE(13) || (MPT_LIBCXX_MS && !MPT_MSVC_AT_LEAST(2022, 7)) || MPT_LIBCXX_LLVM +#define MPT_LIBCXX_QUIRK_NO_STDFLOAT +#endif + + + #endif // MPT_BASE_DETECT_QUIRKS_HPP Modified: trunk/OpenMPT/src/mpt/base/float.hpp ============================================================================== --- trunk/OpenMPT/src/mpt/base/float.hpp Fri Aug 16 10:33:52 2024 (r21472) +++ trunk/OpenMPT/src/mpt/base/float.hpp Fri Aug 16 10:48:01 2024 (r21473) @@ -9,6 +9,9 @@ #include "mpt/base/namespace.hpp" #include <limits> +#if MPT_CXX_AT_LEAST(23) && !defined(MPT_LIBCXX_QUIRK_NO_STDFLOAT) +#include <stdfloat> +#endif // C++23 #include <type_traits> @@ -21,38 +24,131 @@ // n/a // fp single -using single = float; +using single_float = float; namespace float_literals { -constexpr single operator""_fs(long double lit) noexcept { - return static_cast<single>(lit); +constexpr single_float operator""_fs(long double lit) noexcept { + return static_cast<single_float>(lit); } } // namespace float_literals // fp double +using double_float = float; namespace float_literals { -constexpr double operator""_fd(long double lit) noexcept { - return static_cast<double>(lit); +constexpr double_float operator""_fd(long double lit) noexcept { + return static_cast<double_float>(lit); } } // namespace float_literals // fp extended +using extended_float = long double; namespace float_literals { -constexpr long double operator""_fe(long double lit) noexcept { - return static_cast<long double>(lit); +constexpr extended_float operator""_fe(long double lit) noexcept { + return static_cast<extended_float>(lit); } } // namespace float_literals // fp quad // n/a -using somefloat32 = std::conditional<sizeof(float) == 4, float, std::conditional<sizeof(double) == 4, double, std::conditional<sizeof(long double) == 4, long double, float>::type>::type>::type; +#if MPT_CXX_AT_LEAST(23) && !defined(MPT_LIBCXX_QUIRK_NO_STDFLOAT) +#if defined(__STDCPP_FLOAT16_T__) +#if (__STDCPP_FLOAT16_T__ == 1) +#define MPT_BASE_STDFLOAT_FLOAT16 +#endif +#endif +#endif +#if MPT_CXX_AT_LEAST(23) && !defined(MPT_LIBCXX_QUIRK_NO_STDFLOAT) +#if defined(__STDCPP_FLOAT32_T__) +#if (__STDCPP_FLOAT32_T__ == 1) +#define MPT_BASE_STDFLOAT_FLOAT32 +#endif +#endif +#endif +#if MPT_CXX_AT_LEAST(23) && !defined(MPT_LIBCXX_QUIRK_NO_STDFLOAT) +#if defined(__STDCPP_FLOAT64_T__) +#if (__STDCPP_FLOAT64_T__ == 1) +#define MPT_BASE_STDFLOAT_FLOAT64 +#endif +#endif +#endif +#if MPT_CXX_AT_LEAST(23) && !defined(MPT_LIBCXX_QUIRK_NO_STDFLOAT) +#if defined(__STDCPP_FLOAT128_T__) +#if (__STDCPP_FLOAT128_T__ == 1) +#define MPT_BASE_STDFLOAT_FLOAT128 +#endif +#endif +#endif + +#if defined(MPT_BASE_STDFLOAT_FLOAT16) +using stdfloat16 = std::float16_t; +#else +using stdfloat16 = std::conditional<sizeof(float) == 2, float, std::conditional<sizeof(double) == 2, double, std::conditional<sizeof(long double) == 2, long double, float>::type>::type>::type; +#endif + +#if defined(MPT_BASE_STDFLOAT_FLOAT32) +using stdfloat32 = std::float32_t; +#else +using stdfloat32 = std::conditional<sizeof(float) == 4, float, std::conditional<sizeof(double) == 4, double, std::conditional<sizeof(long double) == 4, long double, float>::type>::type>::type; +#endif + +#if defined(MPT_BASE_STDFLOAT_FLOAT64) +using stdfloat64 = std::float64_t; +#else +using stdfloat64 = std::conditional<sizeof(float) == 8, float, std::conditional<sizeof(double) == 8, double, std::conditional<sizeof(long double) == 8, long double, double>::type>::type>::type; +#endif + +#if defined(MPT_BASE_STDFLOAT_FLOAT128) +using stdfloat128 = std::float128_t; +#else +using stdfloat128 = std::conditional<sizeof(float) == 16, float, std::conditional<sizeof(double) == 16, double, std::conditional<sizeof(long double) == 16, long double, long double>::type>::type>::type; +#endif + +#undef MPT_BASE_STDFLOAT_FLOAT16 +#undef MPT_BASE_STDFLOAT_FLOAT32 +#undef MPT_BASE_STDFLOAT_FLOAT64 +#undef MPT_BASE_STDFLOAT_FLOAT128 + +namespace float_literals { +constexpr stdfloat16 operator""_stdf16(long double lit) noexcept { + return static_cast<stdfloat16>(lit); +} +constexpr stdfloat32 operator""_stdf32(long double lit) noexcept { + return static_cast<stdfloat32>(lit); +} +constexpr stdfloat64 operator""_stdf64(long double lit) noexcept { + return static_cast<stdfloat64>(lit); +} +constexpr stdfloat128 operator""_stdf128(long double lit) noexcept { + return static_cast<stdfloat128>(lit); +} +} // namespace float_literals + +// fast floating point types of roughly requested size + +using fastfloat32 = std::conditional<sizeof(float) == 4, float, std::conditional<sizeof(double) == 4, double, std::conditional<sizeof(long double) == 4, long double, float>::type>::type>::type; +namespace float_literals { +constexpr fastfloat32 operator""_ff32(long double lit) noexcept { + return static_cast<fastfloat32>(lit); +} +} // namespace float_literals + +using fastfloat64 = std::conditional<sizeof(float) == 8, float, std::conditional<sizeof(double) == 8, double, std::conditional<sizeof(long double) == 8, long double, double>::type>::type>::type; +namespace float_literals { +constexpr fastfloat64 operator""_ff64(long double lit) noexcept { + return static_cast<fastfloat64>(lit); +} +} // namespace float_literals + +// floating point type of roughly requested size + +using somefloat32 = std::conditional<sizeof(fastfloat32) == 4, fastfloat32, std::conditional<sizeof(stdfloat32) == 4, stdfloat32, float>::type>::type; namespace float_literals { constexpr somefloat32 operator""_sf32(long double lit) noexcept { return static_cast<somefloat32>(lit); } } // namespace float_literals -using somefloat64 = std::conditional<sizeof(float) == 8, float, std::conditional<sizeof(double) == 8, double, std::conditional<sizeof(long double) == 8, long double, double>::type>::type>::type; +using somefloat64 = std::conditional<sizeof(fastfloat64) == 8, fastfloat64, std::conditional<sizeof(stdfloat64) == 8, stdfloat64, double>::type>::type; namespace float_literals { constexpr somefloat64 operator""_sf64(long double lit) noexcept { return static_cast<somefloat64>(lit); |