LLVM 17.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
21
22namespace llvm {
23
24/// Rounding mode.
25///
26/// Enumerates supported rounding modes, as well as some special values. The set
27/// of the modes must agree with IEEE-754, 4.3.1 and 4.3.2. The constants
28/// assigned to the IEEE rounding modes must agree with the values used by
29/// FLT_ROUNDS (C11, 5.2.4.2.2p8).
30///
31/// This value is packed into bitfield in some cases, including \c FPOptions, so
32/// the rounding mode values and the special value \c Dynamic must fit into the
33/// the bit field (now - 3 bits). The value \c Invalid is used only in values
34/// returned by intrinsics to indicate errors, it should never be stored as
35/// rounding mode value, so it does not need to fit the bit fields.
36///
37enum class RoundingMode : int8_t {
38 // Rounding mode defined in IEEE-754.
39 TowardZero = 0, ///< roundTowardZero.
40 NearestTiesToEven = 1, ///< roundTiesToEven.
41 TowardPositive = 2, ///< roundTowardPositive.
42 TowardNegative = 3, ///< roundTowardNegative.
43 NearestTiesToAway = 4, ///< roundTiesToAway.
44
45 // Special values.
46 Dynamic = 7, ///< Denotes mode unknown at compile time.
47 Invalid = -1 ///< Denotes invalid value.
48};
49
50/// Returns text representation of the given rounding mode.
52 switch (RM) {
53 case RoundingMode::TowardZero: return "towardzero";
54 case RoundingMode::NearestTiesToEven: return "tonearest";
55 case RoundingMode::TowardPositive: return "upward";
56 case RoundingMode::TowardNegative: return "downward";
57 case RoundingMode::NearestTiesToAway: return "tonearestaway";
58 case RoundingMode::Dynamic: return "dynamic";
59 default: return "invalid";
60 }
61}
62
64 OS << spell(RM);
65 return OS;
66}
67
68/// Represent subnormal handling kind for floating point instruction inputs and
69/// outputs.
71 /// Represent handled modes for denormal (aka subnormal) modes in the floating
72 /// point environment.
73 enum DenormalModeKind : int8_t {
74 Invalid = -1,
75
76 /// IEEE-754 denormal numbers preserved.
78
79 /// The sign of a flushed-to-zero number is preserved in the sign of 0
81
82 /// Denormals are flushed to positive zero.
84 };
85
86 /// Denormal flushing mode for floating point instruction results in the
87 /// default floating point environment.
89
90 /// Denormal treatment kind for floating point instruction inputs in the
91 /// default floating-point environment. If this is not DenormalModeKind::IEEE,
92 /// floating-point instructions implicitly treat the input value as 0.
94
95 constexpr DenormalMode() = default;
97 Output(Out), Input(In) {}
98
99
100 static constexpr DenormalMode getInvalid() {
102 }
103
104 static constexpr DenormalMode getIEEE() {
106 }
107
108 static constexpr DenormalMode getPreserveSign() {
111 }
112
113 static constexpr DenormalMode getPositiveZero() {
116 }
117
119 return Output == Other.Output && Input == Other.Input;
120 }
121
123 return !(*this == Other);
124 }
125
126 bool isSimple() const {
127 return Input == Output;
128 }
129
130 bool isValid() const {
133 }
134
135 inline void print(raw_ostream &OS) const;
136
137 inline std::string str() const {
138 std::string storage;
139 raw_string_ostream OS(storage);
140 print(OS);
141 return OS.str();
142 }
143};
144
146 Mode.print(OS);
147 return OS;
148}
149
150/// Parse the expected names from the denormal-fp-math attribute.
153 // Assume ieee on unspecified attribute.
155 .Cases("", "ieee", DenormalMode::IEEE)
156 .Case("preserve-sign", DenormalMode::PreserveSign)
157 .Case("positive-zero", DenormalMode::PositiveZero)
159}
160
161/// Return the name used for the denormal handling mode used by the the
162/// expected names from the denormal-fp-math attribute.
164 switch (Mode) {
166 return "ieee";
168 return "preserve-sign";
170 return "positive-zero";
171 default:
172 return "";
173 }
174}
175
176/// Returns the denormal mode to use for inputs and outputs.
178 StringRef OutputStr, InputStr;
179 std::tie(OutputStr, InputStr) = Str.split(',');
180
182 Mode.Output = parseDenormalFPAttributeComponent(OutputStr);
183
184 // Maintain compatability with old form of the attribute which only specified
185 // one component.
186 Mode.Input = InputStr.empty() ? Mode.Output :
188
189 return Mode;
190}
191
194}
195
196/// Floating-point class tests, supported by 'is_fpclass' intrinsic. Actual
197/// test may be an OR combination of basic tests.
198enum FPClassTest : unsigned {
200
201 fcSNan = 0x0001,
202 fcQNan = 0x0002,
203 fcNegInf = 0x0004,
204 fcNegNormal = 0x0008,
206 fcNegZero = 0x0020,
207 fcPosZero = 0x0040,
209 fcPosNormal = 0x0100,
210 fcPosInf = 0x0200,
211
222
224};
225
227
228/// Return the test mask which returns true if the value's sign bit is flipped.
230
231/// Return the test mask which returns true if the value's sign bit is cleared.
233
234/// Write a human readable form of \p Mask to \p OS
236
237} // namespace llvm
238
239#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:65
static cl::opt< RegAllocEvictionAdvisorAnalysis::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysis::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:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
Definition: StringSwitch.h:90
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:642
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.
FPClassTest fabs(FPClassTest Mask)
Return the test mask which returns true if the value's sign bit is cleared.
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.
RoundingMode
Rounding mode.
@ TowardZero
roundTowardZero.
@ NearestTiesToEven
roundTiesToEven.
@ Invalid
Denotes invalid value.
@ 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:292
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 the expected names from the denormal-...
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...
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.
@ IEEE
IEEE-754 denormal numbers preserved.
static constexpr DenormalMode getPositiveZero()
void print(raw_ostream &OS) const
bool operator!=(DenormalMode Other) const
std::string str() const
static constexpr DenormalMode getInvalid()
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
constexpr DenormalMode(DenormalModeKind Out, DenormalModeKind In)
constexpr DenormalMode()=default
static constexpr DenormalMode getIEEE()