LLVM  14.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 // Utilities for dealing with flags related to floating point mode controls.
10 //
11 //===----------------------------------------------------------------------===/
12 
13 #ifndef LLVM_ADT_FLOATINGPOINTMODE_H
14 #define LLVM_ADT_FLOATINGPOINTMODE_H
15 
16 #include "llvm/ADT/StringSwitch.h"
18 
19 namespace llvm {
20 
21 /// Rounding mode.
22 ///
23 /// Enumerates supported rounding modes, as well as some special values. The set
24 /// of the modes must agree with IEEE-754, 4.3.1 and 4.3.2. The constants
25 /// assigned to the IEEE rounding modes must agree with the values used by
26 /// FLT_ROUNDS (C11, 5.2.4.2.2p8).
27 ///
28 /// This value is packed into bitfield in some cases, including \c FPOptions, so
29 /// the rounding mode values and the special value \c Dynamic must fit into the
30 /// the bit field (now - 3 bits). The value \c Invalid is used only in values
31 /// returned by intrinsics to indicate errors, it should never be stored as
32 /// rounding mode value, so it does not need to fit the bit fields.
33 ///
34 enum class RoundingMode : int8_t {
35  // Rounding mode defined in IEEE-754.
36  TowardZero = 0, ///< roundTowardZero.
37  NearestTiesToEven = 1, ///< roundTiesToEven.
38  TowardPositive = 2, ///< roundTowardPositive.
39  TowardNegative = 3, ///< roundTowardNegative.
40  NearestTiesToAway = 4, ///< roundTiesToAway.
41 
42  // Special values.
43  Dynamic = 7, ///< Denotes mode unknown at compile time.
44  Invalid = -1 ///< Denotes invalid value.
45 };
46 
47 /// Returns text representation of the given rounding mode.
49  switch (RM) {
50  case RoundingMode::TowardZero: return "towardzero";
51  case RoundingMode::NearestTiesToEven: return "tonearest";
52  case RoundingMode::TowardPositive: return "upward";
53  case RoundingMode::TowardNegative: return "downward";
54  case RoundingMode::NearestTiesToAway: return "tonearestaway";
55  case RoundingMode::Dynamic: return "dynamic";
56  default: return "invalid";
57  }
58 }
59 
61  OS << spell(RM);
62  return OS;
63 }
64 
65 /// Represent subnormal handling kind for floating point instruction inputs and
66 /// outputs.
67 struct DenormalMode {
68  /// Represent handled modes for denormal (aka subnormal) modes in the floating
69  /// point environment.
70  enum DenormalModeKind : int8_t {
71  Invalid = -1,
72 
73  /// IEEE-754 denormal numbers preserved.
75 
76  /// The sign of a flushed-to-zero number is preserved in the sign of 0
78 
79  /// Denormals are flushed to positive zero.
81  };
82 
83  /// Denormal flushing mode for floating point instruction results in the
84  /// default floating point environment.
86 
87  /// Denormal treatment kind for floating point instruction inputs in the
88  /// default floating-point environment. If this is not DenormalModeKind::IEEE,
89  /// floating-point instructions implicitly treat the input value as 0.
91 
92  constexpr DenormalMode() = default;
94  Output(Out), Input(In) {}
95 
96 
97  static constexpr DenormalMode getInvalid() {
99  }
100 
101  static constexpr DenormalMode getIEEE() {
102  return DenormalMode(DenormalModeKind::IEEE, DenormalModeKind::IEEE);
103  }
104 
105  static constexpr DenormalMode getPreserveSign() {
106  return DenormalMode(DenormalModeKind::PreserveSign,
107  DenormalModeKind::PreserveSign);
108  }
109 
110  static constexpr DenormalMode getPositiveZero() {
113  }
114 
115  bool operator==(DenormalMode Other) const {
116  return Output == Other.Output && Input == Other.Input;
117  }
118 
119  bool operator!=(DenormalMode Other) const {
120  return !(*this == Other);
121  }
122 
123  bool isSimple() const {
124  return Input == Output;
125  }
126 
127  bool isValid() const {
128  return Output != DenormalModeKind::Invalid &&
130  }
131 
132  inline void print(raw_ostream &OS) const;
133 
134  inline std::string str() const {
135  std::string storage;
136  raw_string_ostream OS(storage);
137  print(OS);
138  return OS.str();
139  }
140 };
141 
143  Mode.print(OS);
144  return OS;
145 }
146 
147 /// Parse the expected names from the denormal-fp-math attribute.
150  // Assume ieee on unspecified attribute.
152  .Cases("", "ieee", DenormalMode::IEEE)
153  .Case("preserve-sign", DenormalMode::PreserveSign)
154  .Case("positive-zero", DenormalMode::PositiveZero)
156 }
157 
158 /// Return the name used for the denormal handling mode used by the the
159 /// expected names from the denormal-fp-math attribute.
161  switch (Mode) {
162  case DenormalMode::IEEE:
163  return "ieee";
165  return "preserve-sign";
167  return "positive-zero";
168  default:
169  return "";
170  }
171 }
172 
173 /// Returns the denormal mode to use for inputs and outputs.
175  StringRef OutputStr, InputStr;
176  std::tie(OutputStr, InputStr) = Str.split(',');
177 
179  Mode.Output = parseDenormalFPAttributeComponent(OutputStr);
180 
181  // Maintain compatability with old form of the attribute which only specified
182  // one component.
183  Mode.Input = InputStr.empty() ? Mode.Output :
185 
186  return Mode;
187 }
188 
191 }
192 
193 }
194 
195 #endif // LLVM_ADT_FLOATINGPOINTMODE_H
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
llvm::DenormalMode::operator!=
bool operator!=(DenormalMode Other) const
Definition: FloatingPointMode.h:119
llvm::parseDenormalFPAttributeComponent
DenormalMode::DenormalModeKind parseDenormalFPAttributeComponent(StringRef Str)
Parse the expected names from the denormal-fp-math attribute.
Definition: FloatingPointMode.h:149
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::StringRef::empty
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
llvm::StringSwitch::Default
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:625
llvm::DenormalMode::Invalid
@ Invalid
Definition: FloatingPointMode.h:71
llvm::parseDenormalFPAttribute
DenormalMode parseDenormalFPAttribute(StringRef Str)
Returns the denormal mode to use for inputs and outputs.
Definition: FloatingPointMode.h:174
llvm::DenormalMode::Input
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
Definition: FloatingPointMode.h:90
llvm::RoundingMode
RoundingMode
Rounding mode.
Definition: FloatingPointMode.h:34
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::DenormalMode::DenormalMode
constexpr DenormalMode()=default
llvm::operator<<
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:230
llvm::DenormalMode::str
std::string str() const
Definition: FloatingPointMode.h:134
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:70
llvm::tgtok::In
@ In
Definition: TGLexer.h:51
llvm::DenormalMode::getPositiveZero
static constexpr DenormalMode getPositiveZero()
Definition: FloatingPointMode.h:110
llvm::DenormalMode
Represent subnormal handling kind for floating point instruction inputs and outputs.
Definition: FloatingPointMode.h:67
llvm::DenormalMode::DenormalMode
constexpr DenormalMode(DenormalModeKind Out, DenormalModeKind In)
Definition: FloatingPointMode.h:93
Mode
SI Whole Quad Mode
Definition: SIWholeQuadMode.cpp:262
llvm::DenormalMode::IEEE
@ IEEE
IEEE-754 denormal numbers preserved.
Definition: FloatingPointMode.h:74
llvm::RoundingMode::TowardNegative
@ TowardNegative
roundTowardNegative.
llvm::ARMBuildAttrs::PositiveZero
@ PositiveZero
Definition: ARMBuildAttributes.h:188
llvm::AArch64::RM
@ RM
Definition: AArch64ISelLowering.h:472
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:80
llvm::StringSwitch::Cases
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
Definition: StringSwitch.h:88
llvm::DenormalMode::getInvalid
static constexpr DenormalMode getInvalid()
Definition: FloatingPointMode.h:97
llvm::RoundingMode::NearestTiesToAway
@ NearestTiesToAway
roundTiesToAway.
llvm::spell
StringRef spell(RoundingMode RM)
Returns text representation of the given rounding mode.
Definition: FloatingPointMode.h:48
llvm::RoundingMode::TowardZero
@ TowardZero
roundTowardZero.
llvm::DenormalMode::operator==
bool operator==(DenormalMode Other) const
Definition: FloatingPointMode.h:115
llvm::DenormalMode::print
void print(raw_ostream &OS) const
Definition: FloatingPointMode.h:189
llvm::DenormalMode::getPreserveSign
static constexpr DenormalMode getPreserveSign()
Definition: FloatingPointMode.h:105
llvm::DenormalMode::isValid
bool isValid() const
Definition: FloatingPointMode.h:127
StringSwitch.h
Invalid
@ Invalid
Definition: AArch64ISelLowering.cpp:8264
llvm::DenormalMode::getIEEE
static constexpr DenormalMode getIEEE()
Definition: FloatingPointMode.h:101
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:85
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
llvm::DenormalMode::PreserveSign
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
Definition: FloatingPointMode.h:77
raw_ostream.h
llvm::raw_string_ostream::str
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
Definition: raw_ostream.h:643
llvm::DenormalMode::isSimple
bool isSimple() const
Definition: FloatingPointMode.h:123
llvm::RoundingMode::NearestTiesToEven
@ NearestTiesToEven
roundTiesToEven.
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1172
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:160