/
promotion.hpp
180 lines (159 loc) · 8.3 KB
/
promotion.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// boost\math\tools\promotion.hpp
// Copyright John Maddock 2006.
// Copyright Paul A. Bristow 2006.
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
// Promote arguments functions to allow math functions to have arguments
// provided as integer OR real (floating-point, built-in or UDT)
// (called ArithmeticType in functions that use promotion)
// that help to reduce the risk of creating multiple instantiations.
// Allows creation of an inline wrapper that forwards to a foo(RT, RT) function,
// so you never get to instantiate any mixed foo(RT, IT) functions.
#ifndef BOOST_MATH_PROMOTION_HPP
#define BOOST_MATH_PROMOTION_HPP
#ifdef _MSC_VER
#pragma once
#endif
#include <boost/math/tools/config.hpp>
#include <type_traits>
namespace boost
{
namespace math
{
namespace tools
{
// If either T1 or T2 is an integer type,
// pretend it was a double (for the purposes of further analysis).
// Then pick the wider of the two floating-point types
// as the actual signature to forward to.
// For example:
// foo(int, short) -> double foo(double, double);
// foo(int, float) -> double foo(double, double);
// Note: NOT float foo(float, float)
// foo(int, double) -> foo(double, double);
// foo(double, float) -> double foo(double, double);
// foo(double, float) -> double foo(double, double);
// foo(any-int-or-float-type, long double) -> foo(long double, long double);
// but ONLY float foo(float, float) is unchanged.
// So the only way to get an entirely float version is to call foo(1.F, 2.F),
// But since most (all?) the math functions convert to double internally,
// probably there would not be the hoped-for gain by using float here.
// This follows the C-compatible conversion rules of pow, etc
// where pow(int, float) is converted to pow(double, double).
template <class T>
struct promote_arg
{ // If T is integral type, then promote to double.
using type = typename std::conditional<std::is_integral<T>::value, double, T>::type;
};
// These full specialisations reduce std::conditional usage and speed up
// compilation:
template <> struct promote_arg<float> { using type = float; };
template <> struct promote_arg<double>{ using type = double; };
template <> struct promote_arg<long double> { using type = long double; };
template <> struct promote_arg<int> { using type = double; };
template <typename T>
using promote_arg_t = typename promote_arg<T>::type;
template <class T1, class T2>
struct promote_args_2
{ // Promote, if necessary, & pick the wider of the two floating-point types.
// for both parameter types, if integral promote to double.
using T1P = typename promote_arg<T1>::type; // T1 perhaps promoted.
using T2P = typename promote_arg<T2>::type; // T2 perhaps promoted.
using type = typename std::conditional<
std::is_floating_point<T1P>::value && std::is_floating_point<T2P>::value, // both T1P and T2P are floating-point?
#ifdef BOOST_MATH_USE_FLOAT128
typename std::conditional<std::is_same<__float128, T1P>::value || std::is_same<__float128, T2P>::value, // either long double?
__float128,
#endif
typename std::conditional<std::is_same<long double, T1P>::value || std::is_same<long double, T2P>::value, // either long double?
long double, // then result type is long double.
typename std::conditional<std::is_same<double, T1P>::value || std::is_same<double, T2P>::value, // either double?
double, // result type is double.
float // else result type is float.
>::type
#ifdef BOOST_MATH_USE_FLOAT128
>::type
#endif
>::type,
// else one or the other is a user-defined type:
typename std::conditional<!std::is_floating_point<T2P>::value && std::is_convertible<T1P, T2P>::value, T2P, T1P>::type>::type;
}; // promote_arg2
// These full specialisations reduce std::conditional usage and speed up
// compilation:
template <> struct promote_args_2<float, float> { using type = float; };
template <> struct promote_args_2<double, double>{ using type = double; };
template <> struct promote_args_2<long double, long double> { using type = long double; };
template <> struct promote_args_2<int, int> { using type = double; };
template <> struct promote_args_2<int, float> { using type = double; };
template <> struct promote_args_2<float, int> { using type = double; };
template <> struct promote_args_2<int, double> { using type = double; };
template <> struct promote_args_2<double, int> { using type = double; };
template <> struct promote_args_2<int, long double> { using type = long double; };
template <> struct promote_args_2<long double, int> { using type = long double; };
template <> struct promote_args_2<float, double> { using type = double; };
template <> struct promote_args_2<double, float> { using type = double; };
template <> struct promote_args_2<float, long double> { using type = long double; };
template <> struct promote_args_2<long double, float> { using type = long double; };
template <> struct promote_args_2<double, long double> { using type = long double; };
template <> struct promote_args_2<long double, double> { using type = long double; };
template <typename T, typename U>
using promote_args_2_t = typename promote_args_2<T, U>::type;
template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
struct promote_args
{
using type = typename promote_args_2<
typename std::remove_cv<T1>::type,
typename promote_args_2<
typename std::remove_cv<T2>::type,
typename promote_args_2<
typename std::remove_cv<T3>::type,
typename promote_args_2<
typename std::remove_cv<T4>::type,
typename promote_args_2<
typename std::remove_cv<T5>::type, typename std::remove_cv<T6>::type
>::type
>::type
>::type
>::type
>::type;
#if defined(BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS)
//
// Guard against use of long double if it's not supported:
//
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.");
#endif
};
template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
using promote_args_t = typename promote_args<T1, T2, T3, T4, T5, T6>::type;
//
// This struct is the same as above, but has no static assert on long double usage,
// it should be used only on functions that can be implemented for long double
// even when std lib support is missing or broken for that type.
//
template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
struct promote_args_permissive
{
using type = typename promote_args_2<
typename std::remove_cv<T1>::type,
typename promote_args_2<
typename std::remove_cv<T2>::type,
typename promote_args_2<
typename std::remove_cv<T3>::type,
typename promote_args_2<
typename std::remove_cv<T4>::type,
typename promote_args_2<
typename std::remove_cv<T5>::type, typename std::remove_cv<T6>::type
>::type
>::type
>::type
>::type
>::type;
};
template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
using promote_args_permissive_t = typename promote_args_permissive<T1, T2, T3, T4, T5, T6>::type;
} // namespace tools
} // namespace math
} // namespace boost
#endif // BOOST_MATH_PROMOTION_HPP