FLANG
real-value.h
1//===-- include/flang/Evaluate/real-value.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_REAL_VALUE_H_
10#define FORTRAN_EVALUATE_REAL_VALUE_H_
11
12#include "flang/Evaluate/integer-value.h"
13#include "flang/Evaluate/object-sizes.h"
14#include "flang/Evaluate/target.h"
15#include "llvm/Support/Compiler.h"
16#include <cstddef>
17
18// Some environments, viz. glibc 2.17 and *BSD, allow the macro HUGE
19// to leak out of <math.h>.
20#undef HUGE
21
22namespace Fortran::evaluate::value {
23class RealValueImpl;
24
32class RealValue {
33public:
34 using Word = IntegerValue;
35
36 RealValue();
37#if FLANG_TRIVIALLY_COPYABLE_VALUE_IMPL
38 ~RealValue() = default;
39 RealValue(const RealValue &) = default;
40 RealValue(RealValue &&) = default;
41 RealValue &operator=(const RealValue &) = default;
42 RealValue &operator=(RealValue &&) = default;
43#else
44 ~RealValue();
45 RealValue(const RealValue &);
46 RealValue(RealValue &&);
47 RealValue &operator=(const RealValue &);
48 RealValue &operator=(RealValue &&);
49#endif
50
51 RealValue(int kind, const RealValue &v) : RealValue(v) {
52 CHECK(kind == v.kind());
53 }
54 RealValue(int kind, RealValue &&v) : RealValue(std::move(v)) {
55 CHECK(kind == v.kind());
56 }
57
59 RealValue(int kind, const Word &w);
60
65 RealValue(int kind, double x);
66
69 static RealValue Zero(int kind);
70
72 static RealValue NegativeZero(int kind);
73
74 static RealValue Infinity(int kind, bool negative = false);
75
76 void print(llvm::raw_ostream &os) const;
77
78#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
79 LLVM_DUMP_METHOD void dump() const;
80#endif
81
84 bool IsNull() const;
85
87 int kind() const;
88
89 int bits() const { return bits(kind()); }
90 static constexpr int bits(int kind) { return bytesStored(kind) * 8; }
91
93 std::size_t bytesStored() const { return bytesStored(kind()); }
94 static constexpr std::size_t bytesStored(int kind) {
95 switch (kind) {
96 case 3:
97 return 2;
98 case 10:
99 return 16;
100 default:
101 return kind;
102 }
103 }
104
105 bool operator==(const RealValue &y) const;
106 bool operator!=(const RealValue &y) const { return !operator==(y); }
107
108 bool IsNegative() const;
109
110 bool IsNotANumber() const;
111
112 bool IsSignalingNaN() const;
113
114 bool IsInfinite() const;
115
116 bool IsFinite() const;
117
118 bool IsZero() const;
119
120 bool IsNormal() const;
121
122 RealValue ABS() const;
123
124 RealValue SetSign(bool toNegative) const;
125
126 RealValue SIGN(const RealValue &x) const;
127
128 RealValue Negate() const;
129
130 Relation Compare(const RealValue &y) const;
131
132 ValueWithRealFlags<RealValue> Add(const RealValue &y,
133 Rounding rounding = TargetCharacteristics::defaultRounding) const;
134
135 ValueWithRealFlags<RealValue> Subtract(const RealValue &y,
136 Rounding rounding = TargetCharacteristics::defaultRounding) const;
137
138 ValueWithRealFlags<RealValue> Multiply(const RealValue &y,
139 Rounding rounding = TargetCharacteristics::defaultRounding) const;
140
141 ValueWithRealFlags<RealValue> Divide(const RealValue &y,
142 Rounding rounding = TargetCharacteristics::defaultRounding) const;
143
144 ValueWithRealFlags<RealValue> SQRT(
145 Rounding rounding = TargetCharacteristics::defaultRounding) const;
146
148 ValueWithRealFlags<RealValue> NEAREST(bool upward) const;
149
152 ValueWithRealFlags<RealValue> HYPOT(const RealValue &y,
153 Rounding rounding = TargetCharacteristics::defaultRounding) const;
154
156 ValueWithRealFlags<RealValue> DIM(const RealValue &y,
157 Rounding rounding = TargetCharacteristics::defaultRounding) const;
158
160 ValueWithRealFlags<RealValue> MOD(const RealValue &y,
161 Rounding rounding = TargetCharacteristics::defaultRounding) const;
162
164 ValueWithRealFlags<RealValue> MODULO(const RealValue &y,
165 Rounding rounding = TargetCharacteristics::defaultRounding) const;
166
167 ValueWithRealFlags<RealValue> KahanSummation(const RealValue &y,
168 RealValue &correction,
169 Rounding rounding = TargetCharacteristics::defaultRounding) const;
170
171 IntegerValue EXPONENT(int kind) const;
172
173 static RealValue EPSILON(int kind);
174
175 static RealValue HUGE(int kind);
176
177 static RealValue TINY(int kind);
178
179 static int DIGITS(int kind);
180
181 static int PRECISION(int kind);
182
183 static int RANGE(int kind);
184
185 static int MAXEXPONENT(int kind);
186
187 static int MINEXPONENT(int kind);
188
189 RealValue RRSPACING() const;
190
191 RealValue SPACING() const;
192
193 RealValue SET_EXPONENT(std::int64_t e) const;
194
195 RealValue FRACTION() const;
196
198 ValueWithRealFlags<RealValue> SCALE(const IntegerValue &by,
199 Rounding rounding = TargetCharacteristics::defaultRounding) const;
200
201 RealValue FlushSubnormalToZero() const;
202
203 // TODO: Configurable NotANumber representations
204 static RealValue NotANumber(int kind);
205
206 static ValueWithRealFlags<RealValue> FromInteger(int kind,
207 const IntegerValue &n, bool isUnsigned = false,
208 Rounding rounding = TargetCharacteristics::defaultRounding);
209
211 ValueWithRealFlags<RealValue> ToWholeNumber(
212 common::RoundingMode mode = common::RoundingMode::ToZero) const;
213
215 ValueWithRealFlags<IntegerValue> ToInteger(
216 common::RoundingMode mode = common::RoundingMode::ToZero,
217 int toBits = 0) const;
218
219 static ValueWithRealFlags<RealValue> Convert(int kind, const RealValue &from,
220 Rounding rounding = TargetCharacteristics::defaultRounding);
221
222 Word RawBits() const;
223
225 int Exponent() const;
226
227 static ValueWithRealFlags<RealValue> Read(int kind, const char *&pp,
228 Rounding rounding = TargetCharacteristics::defaultRounding);
229
230 std::string DumpHexadecimal() const;
231
234 llvm::raw_ostream &AsFortran(
235 llvm::raw_ostream &o, int kind, bool minimal = false) const;
236
237 static RealValue FromRawBytes(
238 int kind, const void *raw, std::size_t expectedSize);
239
240 void StoreRawBytes(void *dst, size_t size, bool *changed = nullptr) const;
241
242private:
243 static RealValue FromImpl(const RealValueImpl &x);
244 static RealValue FromImpl(RealValueImpl &&x);
245 static ValueWithRealFlags<RealValue> FromImpl(
246 const ValueWithRealFlags<RealValueImpl> &x);
247 static ValueWithRealFlags<RealValue> FromImpl(
248 ValueWithRealFlags<RealValueImpl> &&x);
249
250 RealValueImpl &impl();
251 const RealValueImpl &impl() const;
252
253 // On AIX, -Waix-compat diagnoses passing by value a struct with a 16-byte
254 // aligned member as not binary compatible with IBM XL C/C++ for AIX 16.1.0
255 // or older. The alignment is required: it is that of RealValueImpl, which
256 // comes from Real<>::word_ (see real.h, which suppresses the same warning).
257 // Compatibility with those compilers is not a concern: they cannot compile
258 // this C++17 code.
259#ifdef __clang__
260#pragma clang diagnostic push
261#pragma clang diagnostic ignored "-Wunknown-warning-option"
262#pragma clang diagnostic ignored "-Waix-compat"
263#endif
264 alignas(detail::kRealObjectAlign) std::byte opaque_[detail::kRealObjectSize];
265#ifdef __clang__
266#pragma clang diagnostic pop
267#endif
268};
269
270} // namespace Fortran::evaluate::value
271
272namespace llvm {
274inline raw_ostream &operator<<(
275 raw_ostream &os, const Fortran::evaluate::value::RealValue &v) {
276 v.print(os);
277 return os;
278}
279} // namespace llvm
280
281#endif // FORTRAN_EVALUATE_REAL_VALUE_H_
Definition integer-value.h:35
Definition real-value-impl.h:29
ValueWithRealFlags< RealValue > DIM(const RealValue &y, Rounding rounding=TargetCharacteristics::defaultRounding) const
DIM(X,Y) = MAX(X-Y, 0)
Definition real-value.cpp:143
int Exponent() const
Extracts "raw" biased exponent field.
Definition real-value.cpp:234
ValueWithRealFlags< RealValue > ToWholeNumber(common::RoundingMode mode=common::RoundingMode::ToZero) const
Conversion to integer in the same real format (AINT(), ANINT())
Definition real-value.cpp:218
ValueWithRealFlags< RealValue > HYPOT(const RealValue &y, Rounding rounding=TargetCharacteristics::defaultRounding) const
Definition real-value.cpp:139
ValueWithRealFlags< RealValue > SCALE(const IntegerValue &by, Rounding rounding=TargetCharacteristics::defaultRounding) const
SCALE(); also known as IEEE_SCALB and (in IEEE-754 '08) ScaleB.
Definition real-value.cpp:200
llvm::raw_ostream & AsFortran(llvm::raw_ostream &o, int kind, bool minimal=false) const
Definition real-value.cpp:245
ValueWithRealFlags< RealValue > MOD(const RealValue &y, Rounding rounding=TargetCharacteristics::defaultRounding) const
MOD(x,y) = x - AINT(x/y)*y (in the standard)
Definition real-value.cpp:147
ValueWithRealFlags< RealValue > MODULO(const RealValue &y, Rounding rounding=TargetCharacteristics::defaultRounding) const
MODULO(x,y) = x - FLOOR(x/y)*y (in the standard)
Definition real-value.cpp:151
int kind() const
The kind of the value currently stored.
Definition real-value.cpp:75
bool IsNull() const
Definition real-value.cpp:73
std::size_t bytesStored() const
Number of bytes accessed by FromRawBytes/StoreRawBytes.
Definition real-value.h:93
static RealValue NegativeZero(int kind)
Creates a floating-point with value -0.0 of a given kind.
Definition real-value.cpp:65
ValueWithRealFlags< RealValue > NEAREST(bool upward) const
NEAREST(), IEEE_NEXT_AFTER(), IEEE_NEXT_UP(), and IEEE_NEXT_DOWN()
Definition real-value.cpp:136
static RealValue Zero(int kind)
Definition real-value.cpp:61
ValueWithRealFlags< IntegerValue > ToInteger(common::RoundingMode mode=common::RoundingMode::ToZero, int toBits=0) const
Conversion to an integer (INT(), NINT(), FLOOR(), CEILING())
Definition real-value.cpp:222
Definition target-rounding.h:18