LLVM 23.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
109
110 /// Return the assumed default mode for a function without denormal-fp-math.
111 static constexpr DenormalMode getDefault() {
112 return getIEEE();
113 }
114
118
123
128
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 input denormals may be implicitly treated as 0.
157 constexpr bool inputsMayBeZero() const {
159 }
160
161 /// Return true if output denormals should be flushed to 0.
162 constexpr bool outputsAreZero() const {
165 }
166
167 /// Return true if output denormals may be implicitly treated as 0.
168 constexpr bool outputsMayBeZero() const {
170 }
171
172 /// Return true if input denormals could be flushed to +0.
173 constexpr bool inputsMayBePositiveZero() const {
176 }
177
178 /// Return true if output denormals could be flushed to +0.
179 constexpr bool outputsMayBePositiveZero() const {
182 }
183
184 /// Get the effective denormal mode if the mode if this caller calls into a
185 /// function with \p Callee. This promotes dynamic modes to the mode of the
186 /// caller.
188 DenormalMode MergedMode = Callee;
189 if (Callee.Input == DenormalMode::Dynamic)
190 MergedMode.Input = Input;
191 if (Callee.Output == DenormalMode::Dynamic)
192 MergedMode.Output = Output;
193 return MergedMode;
194 }
195
196 inline void print(raw_ostream &OS) const;
197
198 inline std::string str() const {
199 std::string storage;
200 raw_string_ostream OS(storage);
201 print(OS);
202 return storage;
203 }
204};
205
207 Mode.print(OS);
208 return OS;
209}
210
211/// Parse the expected names from the denormal-fp-math attribute.
214 // Assume ieee on unspecified attribute.
216 .Cases({"", "ieee"}, DenormalMode::IEEE)
217 .Case("preserve-sign", DenormalMode::PreserveSign)
218 .Case("positive-zero", DenormalMode::PositiveZero)
219 .Case("dynamic", DenormalMode::Dynamic)
221}
222
223/// Return the name used for the denormal handling mode used by the
224/// expected names from the denormal-fp-math attribute.
226 switch (Mode) {
228 return "ieee";
230 return "preserve-sign";
232 return "positive-zero";
234 return "dynamic";
235 default:
236 return "";
237 }
238}
239
240/// Returns the denormal mode to use for inputs and outputs.
242 StringRef OutputStr, InputStr;
243 std::tie(OutputStr, InputStr) = Str.split(',');
244
246 Mode.Output = parseDenormalFPAttributeComponent(OutputStr);
247
248 // Maintain compatibility with old form of the attribute which only specified
249 // one component.
250 Mode.Input = InputStr.empty() ? Mode.Output :
252
253 return Mode;
254}
255
259
260/// Floating-point class tests, supported by 'is_fpclass' intrinsic. Actual
261/// test may be an OR combination of basic tests.
289
291
292/// Return the test mask which returns true if the value's sign bit is flipped.
294
295/// Return the test mask which returns true after fabs is applied to the value.
297
298/// Return the test mask which returns true if the value could have the same set
299/// of classes, but with a different sign.
301
302/// Write a human readable form of \p Mask to \p OS
304
305/// Returns true if all values in \p LHS must be less than or equal to those in
306/// \p RHS. That is, the comparison `fcmp ogt LHS, RHS` will always return
307/// false.
308///
309/// If \p OrderedZeroSign is true, -0 will be treated as ordered less than +0,
310/// unlike fcmp.
312 bool OrderedZeroSign = false);
313
314/// Returns true if all values in \p LHS must be less than those in \p RHS. That
315/// is, the comparison `fcmp oge LHS, RHS` will always return false.
316//
317// If \p OrderedZeroSign is true, -0 will be treated as ordered less than +0,
318// unlike fcmp.
320 bool OrderedZeroSign = false);
321
322/// Returns true if all values in \p LHS must be greater than or equal to those
323/// in \p RHS. That is, the comparison `fcmp olt LHS, RHS` will always return
324/// false.
325///
326/// If \p OrderedZeroSign is true, -0 will be treated as ordered less than +0,
327/// unlike fcmp.
329 bool OrderedZeroSign = false);
330
331/// Returns true if all values in \p LHS must be greater than to those in \p
332/// RHS. That is, the comparison `fcmp ole LHS, RHS` will always return false.
333///
334/// If \p OrderedZeroSign is true, -0 will be treated as ordered less than +0,
335/// unlike fcmp.
337 bool OrderedZeroSign = false);
338
339} // namespace llvm
340
341#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")))
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
Value * RHS
Value * LHS
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:712
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
StringSwitch & Cases(std::initializer_list< StringLiteral > CaseStrings, T Value)
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.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
DenormalMode::DenormalModeKind parseDenormalFPAttributeComponent(StringRef Str)
Parse the expected names from the denormal-fp-math attribute.
LLVM_ABI bool cannotOrderStrictlyGreaterEq(FPClassTest LHS, FPClassTest RHS, bool OrderedZeroSign=false)
Returns true if all values in LHS must be less than those in RHS.
LLVM_ABI bool cannotOrderStrictlyLess(FPClassTest LHS, FPClassTest RHS, bool OrderedZeroSign=false)
Returns true if all values in LHS must be greater than or equal to those in RHS.
LLVM_ABI bool cannotOrderStrictlyLessEq(FPClassTest LHS, FPClassTest RHS, bool OrderedZeroSign=false)
Returns true if all values in LHS must be greater than to those in RHS.
LLVM_ABI bool cannotOrderStrictlyGreater(FPClassTest LHS, FPClassTest RHS, bool OrderedZeroSign=false)
Returns true if all values in LHS must be less than or equal to those in RHS.
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.
Definition ModRef.h:68
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)
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.
constexpr bool inputsMayBePositiveZero() const
Return true if input denormals could be flushed to +0.
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.
constexpr bool inputsMayBeZero() const
Return true if input denormals may be implicitly treated as 0.
std::string str() const
constexpr bool outputsMayBePositiveZero() const
Return true if output denormals could be flushed to +0.
constexpr bool outputsMayBeZero() const
Return true if output denormals may be implicitly treated as 0.
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()