LLVM  15.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 
18 #include "llvm/ADT/StringSwitch.h"
20 
21 namespace llvm {
22 
23 /// Rounding mode.
24 ///
25 /// Enumerates supported rounding modes, as well as some special values. The set
26 /// of the modes must agree with IEEE-754, 4.3.1 and 4.3.2. The constants
27 /// assigned to the IEEE rounding modes must agree with the values used by
28 /// FLT_ROUNDS (C11, 5.2.4.2.2p8).
29 ///
30 /// This value is packed into bitfield in some cases, including \c FPOptions, so
31 /// the rounding mode values and the special value \c Dynamic must fit into the
32 /// the bit field (now - 3 bits). The value \c Invalid is used only in values
33 /// returned by intrinsics to indicate errors, it should never be stored as
34 /// rounding mode value, so it does not need to fit the bit fields.
35 ///
36 enum class RoundingMode : int8_t {
37  // Rounding mode defined in IEEE-754.
38  TowardZero = 0, ///< roundTowardZero.
39  NearestTiesToEven = 1, ///< roundTiesToEven.
40  TowardPositive = 2, ///< roundTowardPositive.
41  TowardNegative = 3, ///< roundTowardNegative.
42  NearestTiesToAway = 4, ///< roundTiesToAway.
43 
44  // Special values.
45  Dynamic = 7, ///< Denotes mode unknown at compile time.
46  Invalid = -1 ///< Denotes invalid value.
47 };
48 
49 /// Returns text representation of the given rounding mode.
51  switch (RM) {
52  case RoundingMode::TowardZero: return "towardzero";
53  case RoundingMode::NearestTiesToEven: return "tonearest";
54  case RoundingMode::TowardPositive: return "upward";
55  case RoundingMode::TowardNegative: return "downward";
56  case RoundingMode::NearestTiesToAway: return "tonearestaway";
57  case RoundingMode::Dynamic: return "dynamic";
58  default: return "invalid";
59  }
60 }
61 
63  OS << spell(RM);
64  return OS;
65 }
66 
67 /// Represent subnormal handling kind for floating point instruction inputs and
68 /// outputs.
69 struct DenormalMode {
70  /// Represent handled modes for denormal (aka subnormal) modes in the floating
71  /// point environment.
72  enum DenormalModeKind : int8_t {
73  Invalid = -1,
74 
75  /// IEEE-754 denormal numbers preserved.
77 
78  /// The sign of a flushed-to-zero number is preserved in the sign of 0
80 
81  /// Denormals are flushed to positive zero.
83  };
84 
85  /// Denormal flushing mode for floating point instruction results in the
86  /// default floating point environment.
88 
89  /// Denormal treatment kind for floating point instruction inputs in the
90  /// default floating-point environment. If this is not DenormalModeKind::IEEE,
91  /// floating-point instructions implicitly treat the input value as 0.
93 
94  constexpr DenormalMode() = default;
96  Output(Out), Input(In) {}
97 
98 
99  static constexpr DenormalMode getInvalid() {
101  }
102 
103  static constexpr DenormalMode getIEEE() {
104  return DenormalMode(DenormalModeKind::IEEE, DenormalModeKind::IEEE);
105  }
106 
107  static constexpr DenormalMode getPreserveSign() {
108  return DenormalMode(DenormalModeKind::PreserveSign,
109  DenormalModeKind::PreserveSign);
110  }
111 
112  static constexpr DenormalMode getPositiveZero() {
115  }
116 
117  bool operator==(DenormalMode Other) const {
118  return Output == Other.Output && Input == Other.Input;
119  }
120 
121  bool operator!=(DenormalMode Other) const {
122  return !(*this == Other);
123  }
124 
125  bool isSimple() const {
126  return Input == Output;
127  }
128 
129  bool isValid() const {
130  return Output != DenormalModeKind::Invalid &&
132  }
133 
134  inline void print(raw_ostream &OS) const;
135 
136  inline std::string str() const {
137  std::string storage;
138  raw_string_ostream OS(storage);
139  print(OS);
140  return OS.str();
141  }
142 };
143 
145  Mode.print(OS);
146  return OS;
147 }
148 
149 /// Parse the expected names from the denormal-fp-math attribute.
152  // Assume ieee on unspecified attribute.
154  .Cases("", "ieee", DenormalMode::IEEE)
155  .Case("preserve-sign", DenormalMode::PreserveSign)
156  .Case("positive-zero", DenormalMode::PositiveZero)
158 }
159 
160 /// Return the name used for the denormal handling mode used by the the
161 /// expected names from the denormal-fp-math attribute.
163  switch (Mode) {
164  case DenormalMode::IEEE:
165  return "ieee";
167  return "preserve-sign";
169  return "positive-zero";
170  default:
171  return "";
172  }
173 }
174 
175 /// Returns the denormal mode to use for inputs and outputs.
177  StringRef OutputStr, InputStr;
178  std::tie(OutputStr, InputStr) = Str.split(',');
179 
181  Mode.Output = parseDenormalFPAttributeComponent(OutputStr);
182 
183  // Maintain compatability with old form of the attribute which only specified
184  // one component.
185  Mode.Input = InputStr.empty() ? Mode.Output :
187 
188  return Mode;
189 }
190 
193 }
194 
195 }
196 
197 /// Floating-point class tests, supported by 'is_fpclass' intrinsic. Actual
198 /// test may be an OR combination of basic tests.
200  fcSNan = 0x0001,
201  fcQNan = 0x0002,
202  fcNegInf = 0x0004,
203  fcNegNormal = 0x0008,
204  fcNegSubnormal = 0x0010,
205  fcNegZero = 0x0020,
206  fcPosZero = 0x0040,
207  fcPosSubnormal = 0x0080,
208  fcPosNormal = 0x0100,
209  fcPosInf = 0x0200,
210 
220 };
221 
222 #endif // LLVM_ADT_FLOATINGPOINTMODE_H
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
llvm::DenormalMode::operator!=
bool operator!=(DenormalMode Other) const
Definition: FloatingPointMode.h:121
llvm::parseDenormalFPAttributeComponent
DenormalMode::DenormalModeKind parseDenormalFPAttributeComponent(StringRef Str)
Parse the expected names from the denormal-fp-math attribute.
Definition: FloatingPointMode.h:151
fcQNan
@ fcQNan
Definition: FloatingPointMode.h:201
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::StringSwitch::Default
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:183
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:632
fcPosZero
@ fcPosZero
Definition: FloatingPointMode.h:206
fcNormal
@ fcNormal
Definition: FloatingPointMode.h:213
fcNan
@ fcNan
Definition: FloatingPointMode.h:211
fcPosNormal
@ fcPosNormal
Definition: FloatingPointMode.h:208
fcPosInf
@ fcPosInf
Definition: FloatingPointMode.h:209
llvm::DenormalMode::Invalid
@ Invalid
Definition: FloatingPointMode.h:73
fcPosFinite
@ fcPosFinite
Definition: FloatingPointMode.h:216
llvm::parseDenormalFPAttribute
DenormalMode parseDenormalFPAttribute(StringRef Str)
Returns the denormal mode to use for inputs and outputs.
Definition: FloatingPointMode.h:176
fcNegZero
@ fcNegZero
Definition: FloatingPointMode.h:205
fcAllFlags
@ fcAllFlags
Definition: FloatingPointMode.h:219
llvm::DenormalMode::Input
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
Definition: FloatingPointMode.h:92
llvm::RoundingMode
RoundingMode
Rounding mode.
Definition: FloatingPointMode.h:36
fcNegFinite
@ fcNegFinite
Definition: FloatingPointMode.h:217
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::DenormalMode::DenormalMode
constexpr DenormalMode()=default
llvm::operator<<
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:230
fcNegSubnormal
@ fcNegSubnormal
Definition: FloatingPointMode.h:204
llvm::DenormalMode::str
std::string str() const
Definition: FloatingPointMode.h:136
llvm::RoundingMode::Invalid
@ Invalid
Denotes invalid value.
llvm::DenormalMode::DenormalModeKind
DenormalModeKind
Represent handled modes for denormal (aka subnormal) modes in the floating point environment.
Definition: FloatingPointMode.h:72
llvm::tgtok::In
@ In
Definition: TGLexer.h:51
FPClassTest
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
Definition: FloatingPointMode.h:199
llvm::DenormalMode::getPositiveZero
static constexpr DenormalMode getPositiveZero()
Definition: FloatingPointMode.h:112
llvm::StringRef::empty
constexpr LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
fcZero
@ fcZero
Definition: FloatingPointMode.h:215
fcInf
@ fcInf
Definition: FloatingPointMode.h:212
llvm::DenormalMode
Represent subnormal handling kind for floating point instruction inputs and outputs.
Definition: FloatingPointMode.h:69
fcPosSubnormal
@ fcPosSubnormal
Definition: FloatingPointMode.h:207
fcSNan
@ fcSNan
Definition: FloatingPointMode.h:200
llvm::DenormalMode::DenormalMode
constexpr DenormalMode(DenormalModeKind Out, DenormalModeKind In)
Definition: FloatingPointMode.h:95
Mode
SI Whole Quad Mode
Definition: SIWholeQuadMode.cpp:262
llvm::DenormalMode::IEEE
@ IEEE
IEEE-754 denormal numbers preserved.
Definition: FloatingPointMode.h:76
llvm::RoundingMode::TowardNegative
@ TowardNegative
roundTowardNegative.
llvm::AArch64::RM
@ RM
Definition: AArch64ISelLowering.h:469
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::RoundingMode::Dynamic
@ Dynamic
Denotes mode unknown at compile time.
llvm::DenormalMode::PositiveZero
@ PositiveZero
Denormals are flushed to positive zero.
Definition: FloatingPointMode.h:82
llvm::StringSwitch::Cases
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
Definition: StringSwitch.h:90
llvm::ARMBuildAttrs::PositiveZero
@ PositiveZero
Definition: ARMBuildAttributes.h:193
llvm::DenormalMode::getInvalid
static constexpr DenormalMode getInvalid()
Definition: FloatingPointMode.h:99
llvm::RoundingMode::NearestTiesToAway
@ NearestTiesToAway
roundTiesToAway.
llvm::spell
StringRef spell(RoundingMode RM)
Returns text representation of the given rounding mode.
Definition: FloatingPointMode.h:50
llvm::RoundingMode::TowardZero
@ TowardZero
roundTowardZero.
llvm::DenormalMode::operator==
bool operator==(DenormalMode Other) const
Definition: FloatingPointMode.h:117
llvm::DenormalMode::print
void print(raw_ostream &OS) const
Definition: FloatingPointMode.h:191
llvm::DenormalMode::getPreserveSign
static constexpr DenormalMode getPreserveSign()
Definition: FloatingPointMode.h:107
fcSubnormal
@ fcSubnormal
Definition: FloatingPointMode.h:214
llvm::DenormalMode::isValid
bool isValid() const
Definition: FloatingPointMode.h:129
StringSwitch.h
fcFinite
@ fcFinite
Definition: FloatingPointMode.h:218
fcNegNormal
@ fcNegNormal
Definition: FloatingPointMode.h:203
Invalid
@ Invalid
Definition: AArch64ISelLowering.cpp:8807
fcNegInf
@ fcNegInf
Definition: FloatingPointMode.h:202
llvm::DenormalMode::getIEEE
static constexpr DenormalMode getIEEE()
Definition: FloatingPointMode.h:103
llvm::RoundingMode::TowardPositive
@ TowardPositive
roundTowardPositive.
llvm::DenormalMode::Output
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
Definition: FloatingPointMode.h:87
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
llvm::DenormalMode::PreserveSign
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
Definition: FloatingPointMode.h:79
raw_ostream.h
llvm::raw_string_ostream::str
std::string & str()
Returns the string's reference.
Definition: raw_ostream.h:650
llvm::DenormalMode::isSimple
bool isSimple() const
Definition: FloatingPointMode.h:125
llvm::RoundingMode::NearestTiesToEven
@ NearestTiesToEven
roundTiesToEven.
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1236
llvm::denormalModeKindName
StringRef denormalModeKindName(DenormalMode::DenormalModeKind Mode)
Return the name used for the denormal handling mode used by the the expected names from the denormal-...
Definition: FloatingPointMode.h:162