LLVM  12.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_FLOATINGPOINTMODE_H
14 #define LLVM_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 
116  return Output == Other.Output && Input == Other.Input;
117  }
118 
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_FLOATINGPOINTMODE_H
bool operator==(DenormalMode Other) const
SI Whole Quad Mode
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static constexpr DenormalMode getIEEE()
StringRef denormalModeKindName(DenormalMode::DenormalModeKind Mode)
Return the name used for the denormal handling mode used by the the expected names from the denormal-...
constexpr DenormalMode(DenormalModeKind Out, DenormalModeKind In)
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
void print(raw_ostream &OS) const
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
constexpr DenormalMode()=default
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:156
StringRef spell(RoundingMode RM)
Returns text representation of the given rounding mode.
Denotes invalid value.
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
Definition: StringSwitch.h:88
DenormalMode::DenormalModeKind parseDenormalFPAttributeComponent(StringRef Str)
Parse the expected names from the denormal-fp-math attribute.
DenormalMode parseDenormalFPAttribute(StringRef Str)
Returns the denormal mode to use for inputs and outputs.
IEEE-754 denormal numbers preserved.
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
Definition: raw_ostream.h:625
static constexpr DenormalMode getPreserveSign()
Represent subnormal handling kind for floating point instruction inputs and outputs.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:230
Denotes mode unknown at compile time.
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:730
DenormalModeKind
Represent handled modes for denormal (aka subnormal) modes in the floating point environment.
The sign of a flushed-to-zero number is preserved in the sign of 0.
std::string str() const
RoundingMode
Rounding mode.
static constexpr DenormalMode getPositiveZero()
static constexpr DenormalMode getInvalid()
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:607
bool operator!=(DenormalMode Other) const
Denormals are flushed to positive zero.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57