9#ifndef FORTRAN_EVALUATE_HOST_H_
10#define FORTRAN_EVALUATE_HOST_H_
21#include "quadmath_wrapper.h"
22#include "flang/Common/float128.h"
24#include "flang/Evaluate/type.h"
39 bool hasSubnormalFlushingHardwareControl()
const {
40 return hasSubnormalFlushingHardwareControl_;
42 void SetFlag(RealFlag flag) { flags_.set(flag); }
43 bool hardwareFlagsAreReliable()
const {
return hardwareFlagsAreReliable_; }
46 std::fenv_t originalFenv_;
48 unsigned int originalMxcsr;
51 bool hasSubnormalFlushingHardwareControl_{
false};
52 bool hardwareFlagsAreReliable_{
true};
61template <
typename FTN_T>
using HostType =
typename HostTypeHelper<FTN_T>::Type;
63template <
typename... T>
constexpr inline bool HostTypeExists() {
72template <
typename FTN_T>
73inline constexpr Scalar<FTN_T> CastHostToFortran(
const HostType<FTN_T> &x) {
74 static_assert(HostTypeExists<FTN_T>());
75 if constexpr (FTN_T::category == TypeCategory::Complex &&
76 sizeof(Scalar<FTN_T>) !=
sizeof(HostType<FTN_T>)) {
79 return Scalar<FTN_T>{CastHostToFortran<typename FTN_T::Part>(std::real(x)),
80 CastHostToFortran<typename FTN_T::Part>(std::imag(x))};
82 return *
reinterpret_cast<const Scalar<FTN_T> *
>(&x);
87template <
typename FTN_T>
88inline constexpr HostType<FTN_T> CastFortranToHost(
const Scalar<FTN_T> &x) {
89 static_assert(HostTypeExists<FTN_T>());
90 if constexpr (FTN_T::category == TypeCategory::Complex) {
91 using FortranPartType =
typename FTN_T::Part;
92 return HostType<FTN_T>{CastFortranToHost<FortranPartType>(x.REAL()),
93 CastFortranToHost<FortranPartType>(x.AIMAG())};
94 }
else if constexpr (std::is_same_v<FTN_T, Type<TypeCategory::Real, 10>>) {
99 std::memcpy(&y, &x,
sizeof x);
102 static_assert(
sizeof x ==
sizeof(HostType<FTN_T>));
103 return *
reinterpret_cast<const HostType<FTN_T> *
>(&x);
108 using Type = std::int8_t;
112 using Type = std::int16_t;
116 using Type = std::int32_t;
120 using Type = std::int64_t;
124#if (defined(__GNUC__) || defined(__clang__)) && defined(__SIZEOF_INT128__)
125 using Type = __int128_t;
136 Type<TypeCategory::Real, common::RealKindForPrecision(24)>> {
138 using Type = std::conditional_t<
sizeof(float) == 4 &&
139 std::numeric_limits<float>::is_iec559,
145 Type<TypeCategory::Real, common::RealKindForPrecision(53)>> {
147 using Type = std::conditional_t<
sizeof(double) == 8 &&
148 std::numeric_limits<double>::is_iec559,
154 Type<TypeCategory::Real, common::RealKindForPrecision(64)>> {
156 using Type = std::conditional_t<
sizeof(
long double) >= 10 &&
157 std::numeric_limits<long double>::digits == 64 &&
158 std::numeric_limits<long double>::max_exponent == 16384,
165 using Type = __float128;
170 using Type = std::conditional_t<
sizeof(
long double) == 16 &&
171 std::numeric_limits<long double>::digits == 113 &&
172 std::numeric_limits<long double>::max_exponent == 16384,
179 using Type = std::conditional_t<HostTypeExists<RealT>(),
186 using Type = __complex128;
191 using Type = std::conditional_t<KIND <= 8, std::uint8_t, UnsupportedType>;
196 Scalar<typename Fortran::evaluate::Type<TypeCategory::Character, KIND>>;
202template <
typename T,
typename... TT>
204 static constexpr int value{common::TypeIndex<T, TT...>};
208 using HostTypeMapping =
209 common::MapTemplate<HostType, AllIntrinsicTypes, std::tuple>;
210 static constexpr int index{
214 using Type = std::conditional_t<index >= 0,
215 std::tuple_element_t<(index >= 0) ? index : 0, AllIntrinsicTypes>,
219template <
typename HOST_T>
220using FortranType =
typename FortranTypeHelper<HOST_T>::Type;
222template <
typename... HT>
constexpr inline bool FortranTypeExists() {
223 return (... && (!std::is_same_v<FortranType<HT>,
UnknownType>));