1// boost\math\tools\promotion.hpp
2
3// Copyright John Maddock 2006.
4// Copyright Paul A. Bristow 2006.
5// Copyright Matt Borland 2023.
6
7// Use, modification and distribution are subject to the
8// Boost Software License, Version 1.0.
9// (See accompanying file LICENSE_1_0.txt
10// or copy at http://www.boost.org/LICENSE_1_0.txt)
11
12// Promote arguments functions to allow math functions to have arguments
13// provided as integer OR real (floating-point, built-in or UDT)
14// (called ArithmeticType in functions that use promotion)
15// that help to reduce the risk of creating multiple instantiations.
16// Allows creation of an inline wrapper that forwards to a foo(RT, RT) function,
17// so you never get to instantiate any mixed foo(RT, IT) functions.
18
19#ifndef BOOST_MATH_PROMOTION_HPP
20#define BOOST_MATH_PROMOTION_HPP
21
22#ifdef _MSC_VER
23#pragma once
24#endif
25
26#include <boost/math/tools/config.hpp>
27#include <type_traits>
28
29#if __has_include(<stdfloat>)
30# include <stdfloat>
31#endif
32
33namespace boost
34{
35 namespace math
36 {
37 namespace tools
38 {
39 // If either T1 or T2 is an integer type,
40 // pretend it was a double (for the purposes of further analysis).
41 // Then pick the wider of the two floating-point types
42 // as the actual signature to forward to.
43 // For example:
44 // foo(int, short) -> double foo(double, double);
45 // foo(int, float) -> double foo(double, double);
46 // Note: NOT float foo(float, float)
47 // foo(int, double) -> foo(double, double);
48 // foo(double, float) -> double foo(double, double);
49 // foo(double, float) -> double foo(double, double);
50 // foo(any-int-or-float-type, long double) -> foo(long double, long double);
51 // but ONLY float foo(float, float) is unchanged.
52 // So the only way to get an entirely float version is to call foo(1.F, 2.F),
53 // But since most (all?) the math functions convert to double internally,
54 // probably there would not be the hoped-for gain by using float here.
55
56 // This follows the C-compatible conversion rules of pow, etc
57 // where pow(int, float) is converted to pow(double, double).
58
59 template <class T>
60 struct promote_arg
61 { // If T is integral type, then promote to double.
62 using type = typename std::conditional<std::is_integral<T>::value, double, T>::type;
63 };
64 // These full specialisations reduce std::conditional usage and speed up
65 // compilation:
66 template <> struct promote_arg<float> { using type = float; };
67 template <> struct promote_arg<double>{ using type = double; };
68 template <> struct promote_arg<long double> { using type = long double; };
69 template <> struct promote_arg<int> { using type = double; };
70
71 #ifdef __STDCPP_FLOAT16_T__
72 template <> struct promote_arg<std::float16_t> { using type = std::float16_t; };
73 #endif
74 #ifdef __STDCPP_FLOAT32_T__
75 template <> struct promote_arg<std::float32_t> { using type = std::float32_t; };
76 #endif
77 #ifdef __STDCPP_FLOAT64_T__
78 template <> struct promote_arg<std::float64_t> { using type = std::float64_t; };
79 #endif
80 #ifdef __STDCPP_FLOAT128_T__
81 template <> struct promote_arg<std::float128_t> { using type = std::float128_t; };
82 #endif
83
84 template <typename T>
85 using promote_arg_t = typename promote_arg<T>::type;
86
87 template <class T1, class T2>
88 struct promote_args_2
89 { // Promote, if necessary, & pick the wider of the two floating-point types.
90 // for both parameter types, if integral promote to double.
91 using T1P = typename promote_arg<T1>::type; // T1 perhaps promoted.
92 using T2P = typename promote_arg<T2>::type; // T2 perhaps promoted.
93 using intermediate_type = typename std::conditional<
94 std::is_floating_point<T1P>::value && std::is_floating_point<T2P>::value, // both T1P and T2P are floating-point?
95#ifdef __STDCPP_FLOAT128_T__
96 typename std::conditional<std::is_same<std::float128_t, T1P>::value || std::is_same<std::float128_t, T2P>::value, // either long double?
97 std::float128_t,
98#endif
99#ifdef BOOST_MATH_USE_FLOAT128
100 typename std::conditional<std::is_same<__float128, T1P>::value || std::is_same<__float128, T2P>::value, // either long double?
101 __float128,
102#endif
103 typename std::conditional<std::is_same<long double, T1P>::value || std::is_same<long double, T2P>::value, // either long double?
104 long double, // then result type is long double.
105#ifdef __STDCPP_FLOAT64_T__
106 typename std::conditional<std::is_same<std::float64_t, T1P>::value || std::is_same<std::float64_t, T2P>::value, // either float64?
107 std::float64_t, // then result type is float64_t.
108#endif
109 typename std::conditional<std::is_same<double, T1P>::value || std::is_same<double, T2P>::value, // either double?
110 double, // result type is double.
111#ifdef __STDCPP_FLOAT32_T__
112 typename std::conditional<std::is_same<std::float32_t, T1P>::value || std::is_same<std::float32_t, T2P>::value, // either float32?
113 std::float32_t, // then result type is float32_t.
114#endif
115 float // else result type is float.
116 >::type
117#ifdef BOOST_MATH_USE_FLOAT128
118 >::type
119#endif
120#ifdef __STDCPP_FLOAT128_T__
121 >::type
122#endif
123#ifdef __STDCPP_FLOAT64_T__
124 >::type
125#endif
126#ifdef __STDCPP_FLOAT32_T__
127 >::type
128#endif
129 >::type,
130 // else one or the other is a user-defined type:
131 typename std::conditional<!std::is_floating_point<T2P>::value && std::is_convertible<T1P, T2P>::value, T2P, T1P>::type>::type;
132
133#ifdef __STDCPP_FLOAT64_T__
134 // If long doubles are doubles then we should prefer to use std::float64_t when available
135 using type = std::conditional_t<(sizeof(double) == sizeof(long double) && std::is_same<intermediate_type, long double>::value), std::float64_t, intermediate_type>;
136#else
137 using type = intermediate_type;
138#endif
139 }; // promote_arg2
140 // These full specialisations reduce std::conditional usage and speed up
141 // compilation:
142 template <> struct promote_args_2<float, float> { using type = float; };
143 template <> struct promote_args_2<double, double>{ using type = double; };
144 template <> struct promote_args_2<long double, long double> { using type = long double; };
145 template <> struct promote_args_2<int, int> { using type = double; };
146 template <> struct promote_args_2<int, float> { using type = double; };
147 template <> struct promote_args_2<float, int> { using type = double; };
148 template <> struct promote_args_2<int, double> { using type = double; };
149 template <> struct promote_args_2<double, int> { using type = double; };
150 template <> struct promote_args_2<int, long double> { using type = long double; };
151 template <> struct promote_args_2<long double, int> { using type = long double; };
152 template <> struct promote_args_2<float, double> { using type = double; };
153 template <> struct promote_args_2<double, float> { using type = double; };
154 template <> struct promote_args_2<float, long double> { using type = long double; };
155 template <> struct promote_args_2<long double, float> { using type = long double; };
156 template <> struct promote_args_2<double, long double> { using type = long double; };
157 template <> struct promote_args_2<long double, double> { using type = long double; };
158
159 #ifdef __STDCPP_FLOAT128_T__
160 template <> struct promote_args_2<int, std::float128_t> { using type = std::float128_t; };
161 template <> struct promote_args_2<std::float128_t, int> { using type = std::float128_t; };
162 template <> struct promote_args_2<std::float128_t, float> { using type = std::float128_t; };
163 template <> struct promote_args_2<float, std::float128_t> { using type = std::float128_t; };
164 template <> struct promote_args_2<std::float128_t, double> { using type = std::float128_t; };
165 template <> struct promote_args_2<double, std::float128_t> { using type = std::float128_t; };
166 template <> struct promote_args_2<std::float128_t, long double> { using type = std::float128_t; };
167 template <> struct promote_args_2<long double, std::float128_t> { using type = std::float128_t; };
168
169 #ifdef __STDCPP_FLOAT16_T__
170 template <> struct promote_args_2<std::float128_t, std::float16_t> { using type = std::float128_t; };
171 template <> struct promote_args_2<std::float16_t, std::float128_t> { using type = std::float128_t; };
172 #endif
173
174 #ifdef __STDCPP_FLOAT32_T__
175 template <> struct promote_args_2<std::float128_t, std::float32_t> { using type = std::float128_t; };
176 template <> struct promote_args_2<std::float32_t, std::float128_t> { using type = std::float128_t; };
177 #endif
178
179 #ifdef __STDCPP_FLOAT64_T__
180 template <> struct promote_args_2<std::float128_t, std::float64_t> { using type = std::float128_t; };
181 template <> struct promote_args_2<std::float64_t, std::float128_t> { using type = std::float128_t; };
182 #endif
183
184 template <> struct promote_args_2<std::float128_t, std::float128_t> { using type = std::float128_t; };
185 #endif
186
187 #ifdef __STDCPP_FLOAT64_T__
188 template <> struct promote_args_2<int, std::float64_t> { using type = std::float64_t; };
189 template <> struct promote_args_2<std::float64_t, int> { using type = std::float64_t; };
190 template <> struct promote_args_2<std::float64_t, float> { using type = std::float64_t; };
191 template <> struct promote_args_2<float, std::float64_t> { using type = std::float64_t; };
192 template <> struct promote_args_2<std::float64_t, double> { using type = std::float64_t; };
193 template <> struct promote_args_2<double, std::float64_t> { using type = std::float64_t; };
194 template <> struct promote_args_2<std::float64_t, long double> { using type = long double; };
195 template <> struct promote_args_2<long double, std::float64_t> { using type = long double; };
196
197 #ifdef __STDCPP_FLOAT16_T__
198 template <> struct promote_args_2<std::float64_t, std::float16_t> { using type = std::float64_t; };
199 template <> struct promote_args_2<std::float16_t, std::float64_t> { using type = std::float64_t; };
200 #endif
201
202 #ifdef __STDCPP_FLOAT32_T__
203 template <> struct promote_args_2<std::float64_t, std::float32_t> { using type = std::float64_t; };
204 template <> struct promote_args_2<std::float32_t, std::float64_t> { using type = std::float64_t; };
205 #endif
206
207 template <> struct promote_args_2<std::float64_t, std::float64_t> { using type = std::float64_t; };
208 #endif
209
210 #ifdef __STDCPP_FLOAT32_T__
211 template <> struct promote_args_2<int, std::float32_t> { using type = std::float32_t; };
212 template <> struct promote_args_2<std::float32_t, int> { using type = std::float32_t; };
213 template <> struct promote_args_2<std::float32_t, float> { using type = std::float32_t; };
214 template <> struct promote_args_2<float, std::float32_t> { using type = std::float32_t; };
215 template <> struct promote_args_2<std::float32_t, double> { using type = double; };
216 template <> struct promote_args_2<double, std::float32_t> { using type = double; };
217 template <> struct promote_args_2<std::float32_t, long double> { using type = long double; };
218 template <> struct promote_args_2<long double, std::float32_t> { using type = long double; };
219
220 #ifdef __STDCPP_FLOAT16_T__
221 template <> struct promote_args_2<std::float32_t, std::float16_t> { using type = std::float32_t; };
222 template <> struct promote_args_2<std::float16_t, std::float32_t> { using type = std::float32_t; };
223 #endif
224
225 template <> struct promote_args_2<std::float32_t, std::float32_t> { using type = std::float32_t; };
226 #endif
227
228 #ifdef __STDCPP_FLOAT16_T__
229 template <> struct promote_args_2<int, std::float16_t> { using type = std::float16_t; };
230 template <> struct promote_args_2<std::float16_t, int> { using type = std::float16_t; };
231 template <> struct promote_args_2<std::float16_t, float> { using type = float; };
232 template <> struct promote_args_2<float, std::float16_t> { using type = float; };
233 template <> struct promote_args_2<std::float16_t, double> { using type = double; };
234 template <> struct promote_args_2<double, std::float16_t> { using type = double; };
235 template <> struct promote_args_2<std::float16_t, long double> { using type = long double; };
236 template <> struct promote_args_2<long double, std::float16_t> { using type = long double; };
237
238 template <> struct promote_args_2<std::float16_t, std::float16_t> { using type = std::float16_t; };
239 #endif
240
241 template <typename T, typename U>
242 using promote_args_2_t = typename promote_args_2<T, U>::type;
243
244 template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
245 struct promote_args
246 {
247 using type = typename promote_args_2<
248 typename std::remove_cv<T1>::type,
249 typename promote_args_2<
250 typename std::remove_cv<T2>::type,
251 typename promote_args_2<
252 typename std::remove_cv<T3>::type,
253 typename promote_args_2<
254 typename std::remove_cv<T4>::type,
255 typename promote_args_2<
256 typename std::remove_cv<T5>::type, typename std::remove_cv<T6>::type
257 >::type
258 >::type
259 >::type
260 >::type
261 >::type;
262
263#if defined(BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS)
264 //
265 // Guard against use of long double if it's not supported:
266 //
267 static_assert((0 == std::is_same<type, long double>::value), "Sorry, but this platform does not have sufficient long double support for the special functions to be reliably implemented.");
268#endif
269 };
270
271 template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
272 using promote_args_t = typename promote_args<T1, T2, T3, T4, T5, T6>::type;
273
274 //
275 // This struct is the same as above, but has no static assert on long double usage,
276 // it should be used only on functions that can be implemented for long double
277 // even when std lib support is missing or broken for that type.
278 //
279 template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
280 struct promote_args_permissive
281 {
282 using type = typename promote_args_2<
283 typename std::remove_cv<T1>::type,
284 typename promote_args_2<
285 typename std::remove_cv<T2>::type,
286 typename promote_args_2<
287 typename std::remove_cv<T3>::type,
288 typename promote_args_2<
289 typename std::remove_cv<T4>::type,
290 typename promote_args_2<
291 typename std::remove_cv<T5>::type, typename std::remove_cv<T6>::type
292 >::type
293 >::type
294 >::type
295 >::type
296 >::type;
297 };
298
299 template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
300 using promote_args_permissive_t = typename promote_args_permissive<T1, T2, T3, T4, T5, T6>::type;
301
302 } // namespace tools
303 } // namespace math
304} // namespace boost
305
306#endif // BOOST_MATH_PROMOTION_HPP
307
308

source code of include/boost/math/tools/promotion.hpp