Debian Codegraph @

src/mpt/base/float.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_BASE_FLOAT_HPP
4 #define MPT_BASE_FLOAT_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9 #include "mpt/base/macros.hpp"
10 #include "mpt/base/namespace.hpp"
11
12 #include <limits>
13 #if MPT_CXX_AT_LEAST(23) && !defined(MPT_LIBCXX_QUIRK_NO_STDFLOAT)
14 #include <stdfloat>
15 #endif // C++23
16 #include <type_traits>
17
18
19
20 namespace mpt {
21 inline namespace MPT_INLINE_NS {
22
23
24 // fp half
25 // n/a
26
27 // fp single
28 using single_float = float;
29 namespace float_literals {
30 MPT_CONSTEVAL single_float operator""_fs(long double lit) noexcept {
31 return static_cast<single_float>(lit);
32 }
33 } // namespace float_literals
34
35 // fp double
36 using double_float = double;
37 namespace float_literals {
38 MPT_CONSTEVAL double_float operator""_fd(long double lit) noexcept {
39 return static_cast<double_float>(lit);
40 }
41 } // namespace float_literals
42
43 // fp extended
44 using extended_float = long double;
45 namespace float_literals {
46 MPT_CONSTEVAL extended_float operator""_fe(long double lit) noexcept {
47 return static_cast<extended_float>(lit);
48 }
49 } // namespace float_literals
50
51 // fp quad
52 // n/a
53
54 #if MPT_CXX_AT_LEAST(23) && !defined(MPT_LIBCXX_QUIRK_NO_STDFLOAT)
55 #if defined(__STDCPP_FLOAT16_T__)
56 #if (__STDCPP_FLOAT16_T__ == 1)
57 #define MPT_BASE_STDFLOAT_FLOAT16
58 #endif
59 #endif
60 #endif
61 #if MPT_CXX_AT_LEAST(23) && !defined(MPT_LIBCXX_QUIRK_NO_STDFLOAT)
62 #if defined(__STDCPP_FLOAT32_T__)
63 #if (__STDCPP_FLOAT32_T__ == 1)
64 #define MPT_BASE_STDFLOAT_FLOAT32
65 #endif
66 #endif
67 #endif
68 #if MPT_CXX_AT_LEAST(23) && !defined(MPT_LIBCXX_QUIRK_NO_STDFLOAT)
69 #if defined(__STDCPP_FLOAT64_T__)
70 #if (__STDCPP_FLOAT64_T__ == 1)
71 #define MPT_BASE_STDFLOAT_FLOAT64
72 #endif
73 #endif
74 #endif
75 #if MPT_CXX_AT_LEAST(23) && !defined(MPT_LIBCXX_QUIRK_NO_STDFLOAT)
76 #if defined(__STDCPP_FLOAT128_T__)
77 #if (__STDCPP_FLOAT128_T__ == 1)
78 #define MPT_BASE_STDFLOAT_FLOAT128
79 #endif
80 #endif
81 #endif
82
83 #if defined(MPT_BASE_STDFLOAT_FLOAT16)
84 using stdfloat16 = std::float16_t;
85 #else
86 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;
87 #endif
88
89 #if defined(MPT_BASE_STDFLOAT_FLOAT32)
90 using stdfloat32 = std::float32_t;
91 #else
92 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;
93 #endif
94
95 #if defined(MPT_BASE_STDFLOAT_FLOAT64)
96 using stdfloat64 = std::float64_t;
97 #else
98 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;
99 #endif
100
101 #if defined(MPT_BASE_STDFLOAT_FLOAT128)
102 using stdfloat128 = std::float128_t;
103 #else
104 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;
105 #endif
106
107 #undef MPT_BASE_STDFLOAT_FLOAT16
108 #undef MPT_BASE_STDFLOAT_FLOAT32
109 #undef MPT_BASE_STDFLOAT_FLOAT64
110 #undef MPT_BASE_STDFLOAT_FLOAT128
111
112 namespace float_literals {
113 MPT_CONSTEVAL stdfloat16 operator""_stdf16(long double lit) noexcept {
114 return static_cast<stdfloat16>(lit);
115 }
116 MPT_CONSTEVAL stdfloat32 operator""_stdf32(long double lit) noexcept {
117 return static_cast<stdfloat32>(lit);
118 }
119 MPT_CONSTEVAL stdfloat64 operator""_stdf64(long double lit) noexcept {
120 return static_cast<stdfloat64>(lit);
121 }
122 MPT_CONSTEVAL stdfloat128 operator""_stdf128(long double lit) noexcept {
123 return static_cast<stdfloat128>(lit);
124 }
125 } // namespace float_literals
126
127 // fast floating point types of roughly requested size
128
129 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;
130 namespace float_literals {
131 MPT_CONSTEVAL fastfloat32 operator""_ff32(long double lit) noexcept {
132 return static_cast<fastfloat32>(lit);
133 }
134 } // namespace float_literals
135
136 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;
137 namespace float_literals {
138 MPT_CONSTEVAL fastfloat64 operator""_ff64(long double lit) noexcept {
139 return static_cast<fastfloat64>(lit);
140 }
141 } // namespace float_literals
142
143 // floating point type of roughly requested size
144
145 using somefloat32 = std::conditional<sizeof(fastfloat32) == 4, fastfloat32, std::conditional<sizeof(stdfloat32) == 4, stdfloat32, float>::type>::type;
146 namespace float_literals {
147 MPT_CONSTEVAL somefloat32 operator""_sf32(long double lit) noexcept {
148 return static_cast<somefloat32>(lit);
149 }
150 } // namespace float_literals
151
152 using somefloat64 = std::conditional<sizeof(fastfloat64) == 8, fastfloat64, std::conditional<sizeof(stdfloat64) == 8, stdfloat64, double>::type>::type;
153 namespace float_literals {
154 MPT_CONSTEVAL somefloat64 operator""_sf64(long double lit) noexcept {
155 return static_cast<somefloat64>(lit);
156 }
157 } // namespace float_literals
158
159 template <typename T>
160 struct float_traits {
161 static constexpr bool is_float = !std::numeric_limits<T>::is_integer;
162 static constexpr bool is_hard = is_float && !MPT_COMPILER_QUIRK_FLOAT_EMULATED;
163 static constexpr bool is_soft = is_float && MPT_COMPILER_QUIRK_FLOAT_EMULATED;
164 static constexpr bool is_float16 = is_float && (sizeof(T) == 2);
165 static constexpr bool is_float32 = is_float && (sizeof(T) == 4);
166 static constexpr bool is_float64 = is_float && (sizeof(T) == 8);
167 static constexpr bool is_float128 = is_float && (sizeof(T) == 16);
168 static constexpr bool is_native_endian = is_float && !MPT_COMPILER_QUIRK_FLOAT_NOTNATIVEENDIAN;
169 static constexpr bool is_ieee754_binary = is_float && std::numeric_limits<T>::is_iec559 && !MPT_COMPILER_QUIRK_FLOAT_NOTIEEE754;
170 static constexpr bool is_preferred = is_float && ((is_float32 && MPT_COMPILER_QUIRK_FLOAT_PREFER32) || (is_float64 && MPT_COMPILER_QUIRK_FLOAT_PREFER64));
171 };
172
173 // prefer smaller floats, but try to use IEEE754 floats
174 using nativefloat =
175 std::conditional<mpt::float_traits<somefloat32>::is_preferred, somefloat32, std::conditional<mpt::float_traits<somefloat64>::is_preferred, somefloat64, std::conditional<std::numeric_limits<float>::is_iec559, float, std::conditional<std::numeric_limits<double>::is_iec559, double, std::conditional<std::numeric_limits<long double>::is_iec559, long double, float>::type>::type>::type>::type>::type;
176 namespace float_literals {
177 MPT_CONSTEVAL nativefloat operator""_nf(long double lit) noexcept {
178 return static_cast<nativefloat>(lit);
179 }
180 } // namespace float_literals
181
182
183 } // namespace MPT_INLINE_NS
184 } // namespace mpt
185
186
187
188 #endif // MPT_BASE_FLOAT_HPP