FLANG
host.h
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#if HAS_QUADMATHLIB
21#include "quadmath_wrapper.h"
22#include "flang/Common/float128.h"
23#endif
24#include "flang/Evaluate/type.h"
25#include <cfenv>
26#include <complex>
27#include <cstdint>
28#include <limits>
29#include <type_traits>
30
31namespace Fortran::evaluate {
32namespace host {
33
34// Helper class to handle host runtime traps, status flag and errno
36public:
37 void SetUpHostFloatingPointEnvironment(FoldingContext &);
38 void CheckAndRestoreFloatingPointEnvironment(FoldingContext &);
39 bool hasSubnormalFlushingHardwareControl() const {
40 return hasSubnormalFlushingHardwareControl_;
41 }
42 void SetFlag(RealFlag flag) { flags_.set(flag); }
43 bool hardwareFlagsAreReliable() const { return hardwareFlagsAreReliable_; }
44
45private:
46 std::fenv_t originalFenv_;
47#if __x86_64__
48 unsigned int originalMxcsr;
49#endif
50 RealFlags flags_;
51 bool hasSubnormalFlushingHardwareControl_{false};
52 bool hardwareFlagsAreReliable_{true};
53};
54
55// Type mapping from F18 types to host types
56struct UnsupportedType {}; // There is no host type for the F18 type
57
58template <typename FTN_T> struct HostTypeHelper {
59 using Type = UnsupportedType;
60};
61template <typename FTN_T> using HostType = typename HostTypeHelper<FTN_T>::Type;
62
63template <typename... T> constexpr inline bool HostTypeExists() {
64 return (... && (!std::is_same_v<HostType<T>, UnsupportedType>));
65}
66
67// Type mapping from host types to F18 types FortranType<HOST_T> is defined
68// after all HosTypeHelper definition because it reverses them to avoid
69// duplication.
70
71// Scalar conversion utilities from host scalars to F18 scalars
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>)) {
77 // X87 is usually padded to 12 or 16bytes. Need to cast piecewise for
78 // complex
79 return Scalar<FTN_T>{CastHostToFortran<typename FTN_T::Part>(std::real(x)),
80 CastHostToFortran<typename FTN_T::Part>(std::imag(x))};
81 } else {
82 return *reinterpret_cast<const Scalar<FTN_T> *>(&x);
83 }
84}
85
86// Scalar conversion utilities from F18 scalars to host scalars.
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>>) {
95 // x87 80-bit floating-point occupies 16 bytes as a C "long double";
96 // copy the data to avoid a legitimate (but benign due to little-endianness)
97 // warning from GCC >= 11.2.0.
98 HostType<FTN_T> y;
99 std::memcpy(&y, &x, sizeof x);
100 return y;
101 } else {
102 static_assert(sizeof x == sizeof(HostType<FTN_T>));
103 return *reinterpret_cast<const HostType<FTN_T> *>(&x);
104 }
105}
106
107template <> struct HostTypeHelper<Type<TypeCategory::Integer, 1>> {
108 using Type = std::int8_t;
109};
110
111template <> struct HostTypeHelper<Type<TypeCategory::Integer, 2>> {
112 using Type = std::int16_t;
113};
114
115template <> struct HostTypeHelper<Type<TypeCategory::Integer, 4>> {
116 using Type = std::int32_t;
117};
118
119template <> struct HostTypeHelper<Type<TypeCategory::Integer, 8>> {
120 using Type = std::int64_t;
121};
122
123template <> struct HostTypeHelper<Type<TypeCategory::Integer, 16>> {
124#if (defined(__GNUC__) || defined(__clang__)) && defined(__SIZEOF_INT128__)
125 using Type = __int128_t;
126#else
127 using Type = UnsupportedType;
128#endif
129};
130
131// TODO no mapping to host types are defined currently for 16bits float
132// It should be defined when gcc/clang have a better support for it.
133
134template <>
136 Type<TypeCategory::Real, common::RealKindForPrecision(24)>> {
137 // IEEE 754 32bits
138 using Type = std::conditional_t<sizeof(float) == 4 &&
139 std::numeric_limits<float>::is_iec559,
140 float, UnsupportedType>;
141};
142
143template <>
145 Type<TypeCategory::Real, common::RealKindForPrecision(53)>> {
146 // IEEE 754 64bits
147 using Type = std::conditional_t<sizeof(double) == 8 &&
148 std::numeric_limits<double>::is_iec559,
149 double, UnsupportedType>;
150};
151
152template <>
154 Type<TypeCategory::Real, common::RealKindForPrecision(64)>> {
155 // X87 80bits
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,
159 long double, UnsupportedType>;
160};
161
162#if HAS_QUADMATHLIB
163template <> struct HostTypeHelper<Type<TypeCategory::Real, 16>> {
164 // IEEE 754 128bits
165 using Type = __float128;
166};
167#else
168template <> struct HostTypeHelper<Type<TypeCategory::Real, 16>> {
169 // IEEE 754 128bits
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,
173 long double, UnsupportedType>;
174};
175#endif
176
177template <int KIND> struct HostTypeHelper<Type<TypeCategory::Complex, KIND>> {
179 using Type = std::conditional_t<HostTypeExists<RealT>(),
180 std::complex<HostType<RealT>>, UnsupportedType>;
181};
182
183#if HAS_QUADMATHLIB
184template <> struct HostTypeHelper<Type<TypeCategory::Complex, 16>> {
186 using Type = __complex128;
187};
188#endif
189
190template <int KIND> struct HostTypeHelper<Type<TypeCategory::Logical, KIND>> {
191 using Type = std::conditional_t<KIND <= 8, std::uint8_t, UnsupportedType>;
192};
193
194template <int KIND> struct HostTypeHelper<Type<TypeCategory::Character, KIND>> {
195 using Type =
196 Scalar<typename Fortran::evaluate::Type<TypeCategory::Character, KIND>>;
197};
198
199// Type mapping from host types to F18 types. This need to be placed after all
200// HostTypeHelper specializations.
201template <typename T, typename... TT> struct IndexInTupleHelper {};
202template <typename T, typename... TT>
203struct IndexInTupleHelper<T, std::tuple<TT...>> {
204 static constexpr int value{common::TypeIndex<T, TT...>};
205};
206struct UnknownType {}; // the host type does not match any F18 types
207template <typename HOST_T> struct FortranTypeHelper {
208 using HostTypeMapping =
209 common::MapTemplate<HostType, AllIntrinsicTypes, std::tuple>;
210 static constexpr int index{
212 // Both conditional types are "instantiated", so a valid type must be
213 // created for invalid index even if not used.
214 using Type = std::conditional_t<index >= 0,
215 std::tuple_element_t<(index >= 0) ? index : 0, AllIntrinsicTypes>,
217};
218
219template <typename HOST_T>
220using FortranType = typename FortranTypeHelper<HOST_T>::Type;
221
222template <typename... HT> constexpr inline bool FortranTypeExists() {
223 return (... && (!std::is_same_v<FortranType<HT>, UnknownType>));
224}
225
226} // namespace host
227} // namespace Fortran::evaluate
228
229#endif // FORTRAN_EVALUATE_HOST_H_
Definition common.h:217
Definition type.h:56
Definition call.h:34