aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/clang/lib/AST/Interp/Floating.h
blob: e4ac76d8509fb838086580cff0f3837ceeaba453 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//===--- Floating.h - Types for the constexpr VM ----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Defines the VM types and helpers operating on types.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_AST_INTERP_FLOATING_H
#define LLVM_CLANG_AST_INTERP_FLOATING_H

#include "Primitives.h"
#include "clang/AST/APValue.h"
#include "llvm/ADT/APFloat.h"

namespace clang {
namespace interp {

using APFloat = llvm::APFloat;
using APSInt = llvm::APSInt;

class Floating final {
private:
  // The underlying value storage.
  APFloat F;

public:
  /// Zero-initializes a Floating.
  Floating() : F(0.0f) {}
  Floating(const APFloat &F) : F(F) {}

  // Static constructors for special floating point values.
  static Floating getInf(const llvm::fltSemantics &Sem) {
    return Floating(APFloat::getInf(Sem));
  }
  const APFloat &getAPFloat() const { return F; }

  bool operator<(Floating RHS) const { return F < RHS.F; }
  bool operator>(Floating RHS) const { return F > RHS.F; }
  bool operator<=(Floating RHS) const { return F <= RHS.F; }
  bool operator>=(Floating RHS) const { return F >= RHS.F; }
  bool operator==(Floating RHS) const { return F == RHS.F; }
  bool operator!=(Floating RHS) const { return F != RHS.F; }
  Floating operator-() const { return Floating(-F); }

  APFloat::opStatus convertToInteger(APSInt &Result) const {
    bool IsExact;
    return F.convertToInteger(Result, llvm::APFloat::rmTowardZero, &IsExact);
  }

  Floating toSemantics(const llvm::fltSemantics *Sem,
                       llvm::RoundingMode RM) const {
    APFloat Copy = F;
    bool LosesInfo;
    Copy.convert(*Sem, RM, &LosesInfo);
    (void)LosesInfo;
    return Floating(Copy);
  }

  /// Convert this Floating to one with the same semantics as \Other.
  Floating toSemantics(const Floating &Other, llvm::RoundingMode RM) const {
    return toSemantics(&Other.F.getSemantics(), RM);
  }

  APSInt toAPSInt(unsigned NumBits = 0) const {
    return APSInt(F.bitcastToAPInt());
  }
  APValue toAPValue() const { return APValue(F); }
  void print(llvm::raw_ostream &OS) const {
    // Can't use APFloat::print() since it appends a newline.
    SmallVector<char, 16> Buffer;
    F.toString(Buffer);
    OS << Buffer;
  }
  std::string toDiagnosticString(const ASTContext &Ctx) const {
    std::string NameStr;
    llvm::raw_string_ostream OS(NameStr);
    print(OS);
    return NameStr;
  }

  unsigned bitWidth() const { return F.semanticsSizeInBits(F.getSemantics()); }

  bool isSigned() const { return true; }
  bool isNegative() const { return F.isNegative(); }
  bool isPositive() const { return !F.isNegative(); }
  bool isZero() const { return F.isZero(); }
  bool isNonZero() const { return F.isNonZero(); }
  bool isMin() const { return F.isSmallest(); }
  bool isMinusOne() const { return F.isExactlyValue(-1.0); }
  bool isNan() const { return F.isNaN(); }
  bool isSignaling() const { return F.isSignaling(); }
  bool isInf() const { return F.isInfinity(); }
  bool isFinite() const { return F.isFinite(); }
  bool isNormal() const { return F.isNormal(); }
  bool isDenormal() const { return F.isDenormal(); }
  llvm::FPClassTest classify() const { return F.classify(); }
  APFloat::fltCategory getCategory() const { return F.getCategory(); }

  ComparisonCategoryResult compare(const Floating &RHS) const {
    llvm::APFloatBase::cmpResult CmpRes = F.compare(RHS.F);
    switch (CmpRes) {
    case llvm::APFloatBase::cmpLessThan:
      return ComparisonCategoryResult::Less;
    case llvm::APFloatBase::cmpEqual:
      return ComparisonCategoryResult::Equal;
    case llvm::APFloatBase::cmpGreaterThan:
      return ComparisonCategoryResult::Greater;
    case llvm::APFloatBase::cmpUnordered:
      return ComparisonCategoryResult::Unordered;
    }
    llvm_unreachable("Inavlid cmpResult value");
  }

  static APFloat::opStatus fromIntegral(APSInt Val,
                                        const llvm::fltSemantics &Sem,
                                        llvm::RoundingMode RM,
                                        Floating &Result) {
    APFloat F = APFloat(Sem);
    APFloat::opStatus Status = F.convertFromAPInt(Val, Val.isSigned(), RM);
    Result = Floating(F);
    return Status;
  }

  static Floating bitcastFromMemory(const std::byte *Buff,
                                    const llvm::fltSemantics &Sem) {
    size_t Size = APFloat::semanticsSizeInBits(Sem);
    llvm::APInt API(Size, true);
    llvm::LoadIntFromMemory(API, (const uint8_t *)Buff, Size / 8);

    return Floating(APFloat(Sem, API));
  }

  // === Serialization support ===
  size_t bytesToSerialize() const {
    return sizeof(llvm::fltSemantics *) +
           (APFloat::semanticsSizeInBits(F.getSemantics()) / 8);
  }

  void serialize(std::byte *Buff) const {
    // Semantics followed by an APInt.
    *reinterpret_cast<const llvm::fltSemantics **>(Buff) = &F.getSemantics();

    llvm::APInt API = F.bitcastToAPInt();
    llvm::StoreIntToMemory(API, (uint8_t *)(Buff + sizeof(void *)),
                           bitWidth() / 8);
  }

  static Floating deserialize(const std::byte *Buff) {
    const llvm::fltSemantics *Sem;
    std::memcpy((void *)&Sem, Buff, sizeof(void *));
    return bitcastFromMemory(Buff + sizeof(void *), *Sem);
  }

  static Floating abs(const Floating &F) {
    APFloat V = F.F;
    if (V.isNegative())
      V.changeSign();
    return Floating(V);
  }

  // -------

  static APFloat::opStatus add(const Floating &A, const Floating &B,
                               llvm::RoundingMode RM, Floating *R) {
    *R = Floating(A.F);
    return R->F.add(B.F, RM);
  }

  static APFloat::opStatus increment(const Floating &A, llvm::RoundingMode RM,
                                     Floating *R) {
    APFloat One(A.F.getSemantics(), 1);
    *R = Floating(A.F);
    return R->F.add(One, RM);
  }

  static APFloat::opStatus sub(const Floating &A, const Floating &B,
                               llvm::RoundingMode RM, Floating *R) {
    *R = Floating(A.F);
    return R->F.subtract(B.F, RM);
  }

  static APFloat::opStatus decrement(const Floating &A, llvm::RoundingMode RM,
                                     Floating *R) {
    APFloat One(A.F.getSemantics(), 1);
    *R = Floating(A.F);
    return R->F.subtract(One, RM);
  }

  static APFloat::opStatus mul(const Floating &A, const Floating &B,
                               llvm::RoundingMode RM, Floating *R) {
    *R = Floating(A.F);
    return R->F.multiply(B.F, RM);
  }

  static APFloat::opStatus div(const Floating &A, const Floating &B,
                               llvm::RoundingMode RM, Floating *R) {
    *R = Floating(A.F);
    return R->F.divide(B.F, RM);
  }

  static bool neg(const Floating &A, Floating *R) {
    *R = -A;
    return false;
  }
};

llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, Floating F);
Floating getSwappedBytes(Floating F);

} // namespace interp
} // namespace clang

#endif