xref: /llvm-project/llvm/lib/Support/FloatingPointMode.cpp (revision 0eb59cab95a01fa84188049fa08348c3bf97e356)
1 //===- FloatingPointMode.cpp ------------------------------------*- 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 #include "llvm/ADT/FloatingPointMode.h"
10 #include "llvm/ADT/StringExtras.h"
11 
12 using namespace llvm;
13 
14 // Every bitfield has a unique name and one or more aliasing names that cover
15 // multiple bits. Names should be listed in order of preference, with higher
16 // popcounts listed first.
17 //
18 // Bits are consumed as printed. Each field should only be represented in one
19 // printed field.
20 static constexpr std::pair<FPClassTest, StringLiteral> NoFPClassName[] = {
21   {fcAllFlags, "all"},
22   {fcNan, "nan"},
23   {fcSNan, "snan"},
24   {fcQNan, "qnan"},
25   {fcInf, "inf"},
26   {fcNegInf, "ninf"},
27   {fcPosInf, "pinf"},
28   {fcZero, "zero"},
29   {fcNegZero, "nzero"},
30   {fcPosZero, "pzero"},
31   {fcSubnormal, "sub"},
32   {fcNegSubnormal, "nsub"},
33   {fcPosSubnormal, "psub"},
34   {fcNormal, "norm"},
35   {fcNegNormal, "nnorm"},
36   {fcPosNormal, "pnorm"}
37 };
38 
39 raw_ostream &llvm::operator<<(raw_ostream &OS, FPClassTest Mask) {
40   OS << '(';
41 
42   if (Mask == fcNone) {
43     OS << "none)";
44     return OS;
45   }
46 
47   ListSeparator LS(" ");
48   for (auto [BitTest, Name] : NoFPClassName) {
49     if ((Mask & BitTest) == BitTest) {
50       OS << LS << Name;
51 
52       // Clear the bits so we don't print any aliased names later.
53       Mask &= ~BitTest;
54     }
55   }
56 
57   assert(Mask == 0 && "didn't print some mask bits");
58 
59   OS << ')';
60   return OS;
61 }
62