1 //===-- lib/Evaluate/host.h -------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef FORTRAN_EVALUATE_HOST_H_
10 #define FORTRAN_EVALUATE_HOST_H_
11
12 // Define a compile-time mapping between Fortran intrinsic types and host
13 // hardware types if possible. The purpose is to avoid having to do any kind of
14 // assumption on whether a "float" matches the Scalar<Type<TypeCategory::Real,
15 // 4>> outside of this header. The main tools are HostTypeExists<T> and
16 // HostType<T>. HostTypeExists<T>() will return true if and only if a host
17 // hardware type maps to Fortran intrinsic type T. Then HostType<T> can be used
18 // to safely refer to this hardware type.
19
20 #include "flang/Evaluate/type.h"
21 #include <cfenv>
22 #include <complex>
23 #include <cstdint>
24 #include <limits>
25 #include <string>
26 #include <type_traits>
27
28 namespace Fortran::evaluate {
29 namespace host {
30
31 // Helper class to handle host runtime traps, status flag and errno
32 class HostFloatingPointEnvironment {
33 public:
34 void SetUpHostFloatingPointEnvironment(FoldingContext &);
35 void CheckAndRestoreFloatingPointEnvironment(FoldingContext &);
hasSubnormalFlushingHardwareControl()36 bool hasSubnormalFlushingHardwareControl() const {
37 return hasSubnormalFlushingHardwareControl_;
38 }
SetFlag(RealFlag flag)39 void SetFlag(RealFlag flag) { flags_.set(flag); }
hardwareFlagsAreReliable()40 bool hardwareFlagsAreReliable() const { return hardwareFlagsAreReliable_; }
41
42 private:
43 std::fenv_t originalFenv_;
44 #if __x86_64__
45 unsigned int originalMxcsr;
46 #endif
47 RealFlags flags_;
48 bool hasSubnormalFlushingHardwareControl_{false};
49 bool hardwareFlagsAreReliable_{true};
50 };
51
52 // Type mapping from F18 types to host types
53 struct UnsupportedType {}; // There is no host type for the F18 type
54
55 template <typename FTN_T> struct HostTypeHelper {
56 using Type = UnsupportedType;
57 };
58 template <typename FTN_T> using HostType = typename HostTypeHelper<FTN_T>::Type;
59
HostTypeExists()60 template <typename... T> constexpr inline bool HostTypeExists() {
61 return (... && (!std::is_same_v<HostType<T>, UnsupportedType>));
62 }
63
64 // Type mapping from host types to F18 types FortranType<HOST_T> is defined
65 // after all HosTypeHelper definition because it reverses them to avoid
66 // duplication.
67
68 // Scalar conversion utilities from host scalars to F18 scalars
69 template <typename FTN_T>
CastHostToFortran(const HostType<FTN_T> & x)70 inline constexpr Scalar<FTN_T> CastHostToFortran(const HostType<FTN_T> &x) {
71 static_assert(HostTypeExists<FTN_T>());
72 if constexpr (FTN_T::category == TypeCategory::Complex &&
73 sizeof(Scalar<FTN_T>) != sizeof(HostType<FTN_T>)) {
74 // X87 is usually padded to 12 or 16bytes. Need to cast piecewise for
75 // complex
76 return Scalar<FTN_T>{CastHostToFortran<typename FTN_T::Part>(std::real(x)),
77 CastHostToFortran<typename FTN_T::Part>(std::imag(x))};
78 } else {
79 return *reinterpret_cast<const Scalar<FTN_T> *>(&x);
80 }
81 }
82
83 // Scalar conversion utilities from F18 scalars to host scalars.
84 template <typename FTN_T>
CastFortranToHost(const Scalar<FTN_T> & x)85 inline constexpr HostType<FTN_T> CastFortranToHost(const Scalar<FTN_T> &x) {
86 static_assert(HostTypeExists<FTN_T>());
87 if constexpr (FTN_T::category == TypeCategory::Complex) {
88 using FortranPartType = typename FTN_T::Part;
89 return HostType<FTN_T>{CastFortranToHost<FortranPartType>(x.REAL()),
90 CastFortranToHost<FortranPartType>(x.AIMAG())};
91 } else if constexpr (std::is_same_v<FTN_T, Type<TypeCategory::Real, 10>>) {
92 // x87 80-bit floating-point occupies 16 bytes as a C "long double";
93 // copy the data to avoid a legitimate (but benign due to little-endianness)
94 // warning from GCC >= 11.2.0.
95 HostType<FTN_T> y;
96 std::memcpy(&y, &x, sizeof x);
97 return y;
98 } else {
99 static_assert(sizeof x == sizeof(HostType<FTN_T>));
100 return *reinterpret_cast<const HostType<FTN_T> *>(&x);
101 }
102 }
103
104 template <> struct HostTypeHelper<Type<TypeCategory::Integer, 1>> {
105 using Type = std::int8_t;
106 };
107
108 template <> struct HostTypeHelper<Type<TypeCategory::Integer, 2>> {
109 using Type = std::int16_t;
110 };
111
112 template <> struct HostTypeHelper<Type<TypeCategory::Integer, 4>> {
113 using Type = std::int32_t;
114 };
115
116 template <> struct HostTypeHelper<Type<TypeCategory::Integer, 8>> {
117 using Type = std::int64_t;
118 };
119
120 template <> struct HostTypeHelper<Type<TypeCategory::Integer, 16>> {
121 #if (defined(__GNUC__) || defined(__clang__)) && defined(__SIZEOF_INT128__)
122 using Type = __int128_t;
123 #else
124 using Type = UnsupportedType;
125 #endif
126 };
127
128 // TODO no mapping to host types are defined currently for 16bits float
129 // It should be defined when gcc/clang have a better support for it.
130
131 template <>
132 struct HostTypeHelper<
133 Type<TypeCategory::Real, common::RealKindForPrecision(24)>> {
134 // IEEE 754 32bits
135 using Type = std::conditional_t<sizeof(float) == 4 &&
136 std::numeric_limits<float>::is_iec559,
137 float, UnsupportedType>;
138 };
139
140 template <>
141 struct HostTypeHelper<
142 Type<TypeCategory::Real, common::RealKindForPrecision(53)>> {
143 // IEEE 754 64bits
144 using Type = std::conditional_t<sizeof(double) == 8 &&
145 std::numeric_limits<double>::is_iec559,
146 double, UnsupportedType>;
147 };
148
149 template <>
150 struct HostTypeHelper<
151 Type<TypeCategory::Real, common::RealKindForPrecision(64)>> {
152 // X87 80bits
153 using Type = std::conditional_t<sizeof(long double) >= 10 &&
154 std::numeric_limits<long double>::digits == 64 &&
155 std::numeric_limits<long double>::max_exponent == 16384,
156 long double, UnsupportedType>;
157 };
158
159 template <>
160 struct HostTypeHelper<
161 Type<TypeCategory::Real, common::RealKindForPrecision(113)>> {
162 // IEEE 754 128bits
163 using Type = std::conditional_t<sizeof(long double) == 16 &&
164 std::numeric_limits<long double>::digits == 113 &&
165 std::numeric_limits<long double>::max_exponent == 16384,
166 long double, UnsupportedType>;
167 };
168
169 template <int KIND> struct HostTypeHelper<Type<TypeCategory::Complex, KIND>> {
170 using RealT = Fortran::evaluate::Type<TypeCategory::Real, KIND>;
171 using Type = std::conditional_t<HostTypeExists<RealT>(),
172 std::complex<HostType<RealT>>, UnsupportedType>;
173 };
174
175 template <int KIND> struct HostTypeHelper<Type<TypeCategory::Logical, KIND>> {
176 using Type = std::conditional_t<KIND <= 8, std::uint8_t, UnsupportedType>;
177 };
178
179 template <int KIND> struct HostTypeHelper<Type<TypeCategory::Character, KIND>> {
180 using Type =
181 Scalar<typename Fortran::evaluate::Type<TypeCategory::Character, KIND>>;
182 };
183
184 // Type mapping from host types to F18 types. This need to be placed after all
185 // HostTypeHelper specializations.
186 template <typename T, typename... TT> struct IndexInTupleHelper {};
187 template <typename T, typename... TT>
188 struct IndexInTupleHelper<T, std::tuple<TT...>> {
189 static constexpr int value{common::TypeIndex<T, TT...>};
190 };
191 struct UnknownType {}; // the host type does not match any F18 types
192 template <typename HOST_T> struct FortranTypeHelper {
193 using HostTypeMapping =
194 common::MapTemplate<HostType, AllIntrinsicTypes, std::tuple>;
195 static constexpr int index{
196 IndexInTupleHelper<HOST_T, HostTypeMapping>::value};
197 // Both conditional types are "instantiated", so a valid type must be
198 // created for invalid index even if not used.
199 using Type = std::conditional_t<index >= 0,
200 std::tuple_element_t<(index >= 0) ? index : 0, AllIntrinsicTypes>,
201 UnknownType>;
202 };
203
204 template <typename HOST_T>
205 using FortranType = typename FortranTypeHelper<HOST_T>::Type;
206
207 template <typename... HT> constexpr inline bool FortranTypeExists() {
208 return (... && (!std::is_same_v<FortranType<HT>, UnknownType>));
209 }
210
211 } // namespace host
212 } // namespace Fortran::evaluate
213
214 #endif // FORTRAN_EVALUATE_HOST_H_
215