LLVM 22.0.0git
FloatingPointMode.h
Go to the documentation of this file.
1//===- llvm/Support/FloatingPointMode.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/// \file
10/// Utilities for dealing with flags related to floating point properties and
11/// mode controls.
12///
13//===----------------------------------------------------------------------===/
14
15#ifndef LLVM_ADT_FLOATINGPOINTMODE_H
16#define LLVM_ADT_FLOATINGPOINTMODE_H
17
22
23namespace llvm {
24
25/// Rounding mode.
26///
27/// Enumerates supported rounding modes, as well as some special values. The set
28/// of the modes must agree with IEEE-754, 4.3.1 and 4.3.2. The constants
29/// assigned to the IEEE rounding modes must agree with the values used by
30/// FLT_ROUNDS (C11, 5.2.4.2.2p8).
31///
32/// This value is packed into bitfield in some cases, including \c FPOptions, so
33/// the rounding mode values and the special value \c Dynamic must fit into the
34/// the bit field (now - 3 bits). The value \c Invalid is used only in values
35/// returned by intrinsics to indicate errors, it should never be stored as
36/// rounding mode value, so it does not need to fit the bit fields.
37///
38enum class RoundingMode : int8_t {
39 // Rounding mode defined in IEEE-754.
40 TowardZero = 0, ///< roundTowardZero.
41 NearestTiesToEven = 1, ///< roundTiesToEven.
42 TowardPositive = 2, ///< roundTowardPositive.
43 TowardNegative = 3, ///< roundTowardNegative.
44 NearestTiesToAway = 4, ///< roundTiesToAway.
45
46 // Special values.
47 Dynamic = 7, ///< Denotes mode unknown at compile time.
48 Invalid = -1 ///< Denotes invalid value.
49};
50
51/// Returns text representation of the given rounding mode.
53 switch (RM) {
54 case RoundingMode::TowardZero: return "towardzero";
55 case RoundingMode::NearestTiesToEven: return "tonearest";
56 case RoundingMode::TowardPositive: return "upward";
57 case RoundingMode::TowardNegative: return "downward";
58 case RoundingMode::NearestTiesToAway: return "tonearestaway";
59 case RoundingMode::Dynamic: return "dynamic";
60 default: return "invalid";
61 }
62}
63
65 OS << spell(RM);
66 return OS;
67}
68
69/// Represent subnormal handling kind for floating point instruction inputs and
70/// outputs.
72 /// Represent handled modes for denormal (aka subnormal) modes in the floating
73 /// point environment.
74 enum DenormalModeKind : int8_t {
75 Invalid = -1,
76
77 /// IEEE-754 denormal numbers preserved.
79
80 /// The sign of a flushed-to-zero number is preserved in the sign of 0
82
83 /// Denormals are flushed to positive zero.
85
86 /// Denormals have unknown treatment.
88 };
89
90 /// Denormal flushing mode for floating point instruction results in the
91 /// default floating point environment.
93
94 /// Denormal treatment kind for floating point instruction inputs in the
95 /// default floating-point environment. If this is not DenormalModeKind::IEEE,
96 /// floating-point instructions implicitly treat the input value as 0.
98
99 constexpr DenormalMode() = default;
100 constexpr DenormalMode(const DenormalMode &) = default;
102 Output(Out), Input(In) {}
103
105
106 static constexpr DenormalMode getInvalid() {
108 }
109
110 /// Return the assumed default mode for a function without denormal-fp-math.
111 static constexpr DenormalMode getDefault() {
112 return getIEEE();
113 }
114
115 static constexpr DenormalMode getIEEE() {
117 }
118
119 static constexpr DenormalMode getPreserveSign() {
122 }
123
124 static constexpr DenormalMode getPositiveZero() {
127 }
128
129 static constexpr DenormalMode getDynamic() {
131 }
132
134 return Output == Other.Output && Input == Other.Input;
135 }
136
138 return !(*this == Other);
139 }
140
141 bool isSimple() const {
142 return Input == Output;
143 }
144
145 bool isValid() const {
148 }
149
150 /// Return true if input denormals must be implicitly treated as 0.
151 constexpr bool inputsAreZero() const {
154 }
155
156 /// Return true if output denormals should be flushed to 0.
157 constexpr bool outputsAreZero() const {
160 }
161
162 /// Get the effective denormal mode if the mode if this caller calls into a
163 /// function with \p Callee. This promotes dynamic modes to the mode of the
164 /// caller.
166 DenormalMode MergedMode = Callee;
167 if (Callee.Input == DenormalMode::Dynamic)
168 MergedMode.Input = Input;
169 if (Callee.Output == DenormalMode::Dynamic)
170 MergedMode.Output = Output;
171 return MergedMode;
172 }
173
174 inline void print(raw_ostream &OS) const;
175
176 inline std::string str() const {
177 std::string storage;
178 raw_string_ostream OS(storage);
179 print(OS);
180 return storage;
181 }
182};
183
185 Mode.print(OS);
186 return OS;
187}
188
189/// Parse the expected names from the denormal-fp-math attribute.
192 // Assume ieee on unspecified attribute.
194 .Cases("", "ieee", DenormalMode::IEEE)
195 .Case("preserve-sign", DenormalMode::PreserveSign)
196 .Case("positive-zero", DenormalMode::PositiveZero)
197 .Case("dynamic", DenormalMode::Dynamic)
199}
200
201/// Return the name used for the denormal handling mode used by the
202/// expected names from the denormal-fp-math attribute.
204 switch (Mode) {
206 return "ieee";
208 return "preserve-sign";
210 return "positive-zero";
212 return "dynamic";
213 default:
214 return "";
215 }
216}
217
218/// Returns the denormal mode to use for inputs and outputs.
220 StringRef OutputStr, InputStr;
221 std::tie(OutputStr, InputStr) = Str.split(',');
222
224 Mode.Output = parseDenormalFPAttributeComponent(OutputStr);
225
226 // Maintain compatibility with old form of the attribute which only specified
227 // one component.
228 Mode.Input = InputStr.empty() ? Mode.Output :
230
231 return Mode;
232}
233
236}
237
238/// Floating-point class tests, supported by 'is_fpclass' intrinsic. Actual
239/// test may be an OR combination of basic tests.
240enum FPClassTest : unsigned {
242
243 fcSNan = 0x0001,
244 fcQNan = 0x0002,
245 fcNegInf = 0x0004,
246 fcNegNormal = 0x0008,
248 fcNegZero = 0x0020,
249 fcPosZero = 0x0040,
251 fcPosNormal = 0x0100,
252 fcPosInf = 0x0200,
253
264
266};
267
269
270/// Return the test mask which returns true if the value's sign bit is flipped.
272
273/// Return the test mask which returns true after fabs is applied to the value.
275
276/// Return the test mask which returns true if the value could have the same set
277/// of classes, but with a different sign.
279
280/// Write a human readable form of \p Mask to \p OS
282
283} // namespace llvm
284
285#endif // LLVM_ADT_FLOATINGPOINTMODE_H
#define LLVM_DECLARE_ENUM_AS_BITMASK(Enum, LargestValue)
LLVM_DECLARE_ENUM_AS_BITMASK can be used to declare an enum type as a bit set, so that bitwise operat...
Definition: BitmaskEnum.h:66
#define LLVM_ABI
Definition: Compiler.h:213
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
raw_pwrite_stream & OS
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:151
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:68
R Default(T Value)
Definition: StringSwitch.h:177
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
Definition: StringSwitch.h:87
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:662
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
DenormalMode::DenormalModeKind parseDenormalFPAttributeComponent(StringRef Str)
Parse the expected names from the denormal-fp-math attribute.
LLVM_ABI FPClassTest fneg(FPClassTest Mask)
Return the test mask which returns true if the value's sign bit is flipped.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI FPClassTest inverse_fabs(FPClassTest Mask)
Return the test mask which returns true after fabs is applied to the value.
@ Other
Any other memory.
LLVM_ABI FPClassTest unknown_sign(FPClassTest Mask)
Return the test mask which returns true if the value could have the same set of classes,...
RoundingMode
Rounding mode.
@ TowardZero
roundTowardZero.
@ NearestTiesToEven
roundTiesToEven.
@ Dynamic
Denotes mode unknown at compile time.
@ TowardPositive
roundTowardPositive.
@ NearestTiesToAway
roundTiesToAway.
@ TowardNegative
roundTowardNegative.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:312
DenormalMode parseDenormalFPAttribute(StringRef Str)
Returns the denormal mode to use for inputs and outputs.
StringRef denormalModeKindName(DenormalMode::DenormalModeKind Mode)
Return the name used for the denormal handling mode used by the expected names from the denormal-fp-m...
StringRef spell(RoundingMode RM)
Returns text representation of the given rounding mode.
Represent subnormal handling kind for floating point instruction inputs and outputs.
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
constexpr bool outputsAreZero() const
Return true if output denormals should be flushed to 0.
DenormalModeKind
Represent handled modes for denormal (aka subnormal) modes in the floating point environment.
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ Dynamic
Denormals have unknown treatment.
@ IEEE
IEEE-754 denormal numbers preserved.
static constexpr DenormalMode getPositiveZero()
constexpr DenormalMode(const DenormalMode &)=default
void print(raw_ostream &OS) const
bool operator!=(DenormalMode Other) const
static constexpr DenormalMode getDefault()
Return the assumed default mode for a function without denormal-fp-math.
std::string str() const
static constexpr DenormalMode getInvalid()
constexpr bool inputsAreZero() const
Return true if input denormals must be implicitly treated as 0.
static constexpr DenormalMode getPreserveSign()
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
bool operator==(DenormalMode Other) const
DenormalMode & operator=(const DenormalMode &)=default
constexpr DenormalMode(DenormalModeKind Out, DenormalModeKind In)
constexpr DenormalMode()=default
DenormalMode mergeCalleeMode(DenormalMode Callee) const
Get the effective denormal mode if the mode if this caller calls into a function with Callee.
static constexpr DenormalMode getDynamic()
static constexpr DenormalMode getIEEE()