9#ifndef FORTRAN_EVALUATE_HOST_H_
10#define FORTRAN_EVALUATE_HOST_H_
22#include "flang/Common/float128.h"
24#include "flang/Evaluate/type.h"
40 bool hasSubnormalFlushingHardwareControl()
const {
41 return hasSubnormalFlushingHardwareControl_;
43 void SetFlag(RealFlag flag) { flags_.set(flag); }
44 bool hardwareFlagsAreReliable()
const {
return hardwareFlagsAreReliable_; }
47 std::fenv_t originalFenv_;
49 unsigned int originalMxcsr;
52 bool hasSubnormalFlushingHardwareControl_{
false};
53 bool hardwareFlagsAreReliable_{
true};
64template <
typename... T>
constexpr inline bool HostTypeExists() {
73template <
typename FTN_T>
74inline constexpr Scalar<FTN_T> CastHostToFortran(
const HostType<FTN_T> &x) {
75 static_assert(HostTypeExists<FTN_T>());
76 if constexpr (FTN_T::category == TypeCategory::Complex &&
77 sizeof(Scalar<FTN_T>) !=
sizeof(HostType<FTN_T>)) {
80 return Scalar<FTN_T>{CastHostToFortran<typename FTN_T::Part>(std::real(x)),
81 CastHostToFortran<typename FTN_T::Part>(std::imag(x))};
83 return *
reinterpret_cast<const Scalar<FTN_T> *
>(&x);
88template <
typename FTN_T>
89inline constexpr HostType<FTN_T> CastFortranToHost(
const Scalar<FTN_T> &x) {
90 static_assert(HostTypeExists<FTN_T>());
91 if constexpr (FTN_T::category == TypeCategory::Complex) {
92 using FortranPartType =
typename FTN_T::Part;
93 return HostType<FTN_T>{CastFortranToHost<FortranPartType>(x.REAL()),
94 CastFortranToHost<FortranPartType>(x.AIMAG())};
95 }
else if constexpr (std::is_same_v<FTN_T, Type<TypeCategory::Real, 10>>) {
100 std::memcpy(&y, &x,
sizeof x);
103 static_assert(
sizeof x ==
sizeof(HostType<FTN_T>));
104 return *
reinterpret_cast<const HostType<FTN_T> *
>(&x);
109 using Type = std::int8_t;
113 using Type = std::int16_t;
117 using Type = std::int32_t;
121 using Type = std::int64_t;
125#if (defined(__GNUC__) || defined(__clang__)) && defined(__SIZEOF_INT128__)
126 using Type = __int128_t;
137 Type<TypeCategory::Real, common::RealKindForPrecision(24)>> {
139 using Type = std::conditional_t<
sizeof(float) == 4 &&
140 std::numeric_limits<float>::is_iec559,
146 Type<TypeCategory::Real, common::RealKindForPrecision(53)>> {
148 using Type = std::conditional_t<
sizeof(double) == 8 &&
149 std::numeric_limits<double>::is_iec559,
155 Type<TypeCategory::Real, common::RealKindForPrecision(64)>> {
157 using Type = std::conditional_t<
sizeof(
long double) >= 10 &&
158 std::numeric_limits<long double>::digits == 64 &&
159 std::numeric_limits<long double>::max_exponent == 16384,
166 using Type = __float128;
171 using Type = std::conditional_t<
sizeof(
long double) == 16 &&
172 std::numeric_limits<long double>::digits == 113 &&
173 std::numeric_limits<long double>::max_exponent == 16384,
180 using Type = std::conditional_t<HostTypeExists<RealT>(),
187 using Type = __complex128;
192 using Type = std::conditional_t<KIND <= 8, std::uint8_t, UnsupportedType>;
197 Scalar<typename Fortran::evaluate::Type<TypeCategory::Character, KIND>>;
203template <
typename T,
typename... TT>
205 static constexpr int value{common::TypeIndex<T, TT...>};
209 using HostTypeMapping =
210 common::MapTemplate<HostType, AllIntrinsicTypes, std::tuple>;
211 static constexpr int index{
215 using Type = std::conditional_t<index >= 0,
216 std::tuple_element_t<(index >= 0) ? index : 0, AllIntrinsicTypes>,
220template <
typename HOST_T>
221using FortranType =
typename FortranTypeHelper<HOST_T>::Type;
223template <
typename... HT>
constexpr inline bool FortranTypeExists() {
224 return (... && (!std::is_same_v<FortranType<HT>,
UnknownType>));