/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ #ifndef MPT_BASE_FLOAT_HPP #define MPT_BASE_FLOAT_HPP #include "mpt/base/detect.hpp" #include "mpt/base/macros.hpp" #include "mpt/base/namespace.hpp" #include #if MPT_CXX_AT_LEAST(23) && !defined(MPT_LIBCXX_QUIRK_NO_STDFLOAT) #include #endif // C++23 #include namespace mpt { inline namespace MPT_INLINE_NS { // fp half // n/a // fp single using single_float = float; namespace float_literals { MPT_CONSTEVAL single_float operator""_fs(long double lit) noexcept { return static_cast(lit); } } // namespace float_literals // fp double using double_float = double; namespace float_literals { MPT_CONSTEVAL double_float operator""_fd(long double lit) noexcept { return static_cast(lit); } } // namespace float_literals // fp extended using extended_float = long double; namespace float_literals { MPT_CONSTEVAL extended_float operator""_fe(long double lit) noexcept { return static_cast(lit); } } // namespace float_literals // fp quad // n/a #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::type>::type>::type; #endif #if defined(MPT_BASE_STDFLOAT_FLOAT32) using stdfloat32 = std::float32_t; #else using stdfloat32 = std::conditional::type>::type>::type; #endif #if defined(MPT_BASE_STDFLOAT_FLOAT64) using stdfloat64 = std::float64_t; #else using stdfloat64 = std::conditional::type>::type>::type; #endif #if defined(MPT_BASE_STDFLOAT_FLOAT128) using stdfloat128 = std::float128_t; #else using stdfloat128 = std::conditional::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 { MPT_CONSTEVAL stdfloat16 operator""_stdf16(long double lit) noexcept { return static_cast(lit); } MPT_CONSTEVAL stdfloat32 operator""_stdf32(long double lit) noexcept { return static_cast(lit); } MPT_CONSTEVAL stdfloat64 operator""_stdf64(long double lit) noexcept { return static_cast(lit); } MPT_CONSTEVAL stdfloat128 operator""_stdf128(long double lit) noexcept { return static_cast(lit); } } // namespace float_literals // fast floating point types of roughly requested size using fastfloat32 = std::conditional::type>::type>::type; namespace float_literals { MPT_CONSTEVAL fastfloat32 operator""_ff32(long double lit) noexcept { return static_cast(lit); } } // namespace float_literals using fastfloat64 = std::conditional::type>::type>::type; namespace float_literals { MPT_CONSTEVAL fastfloat64 operator""_ff64(long double lit) noexcept { return static_cast(lit); } } // namespace float_literals // floating point type of roughly requested size using somefloat32 = std::conditional::type>::type; namespace float_literals { MPT_CONSTEVAL somefloat32 operator""_sf32(long double lit) noexcept { return static_cast(lit); } } // namespace float_literals using somefloat64 = std::conditional::type>::type; namespace float_literals { MPT_CONSTEVAL somefloat64 operator""_sf64(long double lit) noexcept { return static_cast(lit); } } // namespace float_literals template struct float_traits { static constexpr bool is_float = !std::numeric_limits::is_integer; static constexpr bool is_hard = is_float && !MPT_COMPILER_QUIRK_FLOAT_EMULATED; static constexpr bool is_soft = is_float && MPT_COMPILER_QUIRK_FLOAT_EMULATED; static constexpr bool is_float16 = is_float && (sizeof(T) == 2); static constexpr bool is_float32 = is_float && (sizeof(T) == 4); static constexpr bool is_float64 = is_float && (sizeof(T) == 8); static constexpr bool is_float128 = is_float && (sizeof(T) == 16); static constexpr bool is_native_endian = is_float && !MPT_COMPILER_QUIRK_FLOAT_NOTNATIVEENDIAN; static constexpr bool is_ieee754_binary = is_float && std::numeric_limits::is_iec559 && !MPT_COMPILER_QUIRK_FLOAT_NOTIEEE754; static constexpr bool is_preferred = is_float && ((is_float32 && MPT_COMPILER_QUIRK_FLOAT_PREFER32) || (is_float64 && MPT_COMPILER_QUIRK_FLOAT_PREFER64)); }; // prefer smaller floats, but try to use IEEE754 floats using nativefloat = std::conditional::is_preferred, somefloat32, std::conditional::is_preferred, somefloat64, std::conditional::is_iec559, float, std::conditional::is_iec559, double, std::conditional::is_iec559, long double, float>::type>::type>::type>::type>::type; namespace float_literals { MPT_CONSTEVAL nativefloat operator""_nf(long double lit) noexcept { return static_cast(lit); } } // namespace float_literals } // namespace MPT_INLINE_NS } // namespace mpt #endif // MPT_BASE_FLOAT_HPP