clang 20.0.0git
FixedPoint.h
Go to the documentation of this file.
1//===------- FixedPoint.h - Fixedd point types for the VM -------*- 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#ifndef LLVM_CLANG_AST_INTERP_FIXED_POINT_H
10#define LLVM_CLANG_AST_INTERP_FIXED_POINT_H
11
12#include "clang/AST/APValue.h"
14#include "llvm/ADT/APFixedPoint.h"
15
16namespace clang {
17namespace interp {
18
19using APInt = llvm::APInt;
20using APSInt = llvm::APSInt;
21
22/// Wrapper around fixed point types.
23class FixedPoint final {
24private:
25 llvm::APFixedPoint V;
26
27public:
28 FixedPoint(llvm::APFixedPoint &&V) : V(std::move(V)) {}
29 FixedPoint(llvm::APFixedPoint &V) : V(V) {}
30 FixedPoint(APInt V, llvm::FixedPointSemantics Sem) : V(V, Sem) {}
31 // This needs to be default-constructible so llvm::endian::read works.
33 : V(APInt(0, 0ULL, false),
35
36 static FixedPoint zero(llvm::FixedPointSemantics Sem) {
37 return FixedPoint(APInt(Sem.getWidth(), 0ULL, Sem.isSigned()), Sem);
38 }
39
40 static FixedPoint from(const APSInt &I, llvm::FixedPointSemantics Sem,
41 bool *Overflow) {
42 return FixedPoint(llvm::APFixedPoint::getFromIntValue(I, Sem, Overflow));
43 }
44 static FixedPoint from(const llvm::APFloat &I, llvm::FixedPointSemantics Sem,
45 bool *Overflow) {
46 return FixedPoint(llvm::APFixedPoint::getFromFloatValue(I, Sem, Overflow));
47 }
48
49 operator bool() const { return V.getBoolValue(); }
50 void print(llvm::raw_ostream &OS) const { OS << V; }
51
52 APValue toAPValue(const ASTContext &) const { return APValue(V); }
53 APSInt toAPSInt(unsigned BitWidth = 0) const { return V.getValue(); }
54
55 unsigned bitWidth() const { return V.getWidth(); }
56 bool isSigned() const { return V.isSigned(); }
57 bool isZero() const { return V.getValue().isZero(); }
58 bool isNegative() const { return V.getValue().isNegative(); }
59 bool isPositive() const { return V.getValue().isNonNegative(); }
60 bool isMin() const {
61 return V == llvm::APFixedPoint::getMin(V.getSemantics());
62 }
63 bool isMinusOne() const { return V.isSigned() && V.getValue() == -1; }
64
65 FixedPoint truncate(unsigned BitWidth) const { return *this; }
66
67 FixedPoint toSemantics(const llvm::FixedPointSemantics &Sem,
68 bool *Overflow) const {
69 return FixedPoint(V.convert(Sem, Overflow));
70 }
71 llvm::FixedPointSemantics getSemantics() const { return V.getSemantics(); }
72
73 llvm::APFloat toFloat(const llvm::fltSemantics *Sem) const {
74 return V.convertToFloat(*Sem);
75 }
76
77 llvm::APSInt toInt(unsigned BitWidth, bool Signed, bool *Overflow) const {
78 return V.convertToInt(BitWidth, Signed, Overflow);
79 }
80
81 std::string toDiagnosticString(const ASTContext &Ctx) const {
82 return V.toString();
83 }
84
86 int c = V.compare(Other.V);
87 if (c == 0)
89 else if (c < 0)
92 }
93
94 static bool neg(const FixedPoint &A, FixedPoint *R) {
95 bool Overflow = false;
96 *R = FixedPoint(A.V.negate(&Overflow));
97 return Overflow;
98 }
99
100 static bool add(const FixedPoint A, const FixedPoint B, unsigned Bits,
101 FixedPoint *R) {
102 bool Overflow = false;
103 *R = FixedPoint(A.V.add(B.V, &Overflow));
104 return Overflow;
105 }
106 static bool sub(const FixedPoint A, const FixedPoint B, unsigned Bits,
107 FixedPoint *R) {
108 bool Overflow = false;
109 *R = FixedPoint(A.V.sub(B.V, &Overflow));
110 return Overflow;
111 }
112 static bool mul(const FixedPoint A, const FixedPoint B, unsigned Bits,
113 FixedPoint *R) {
114 bool Overflow = false;
115 *R = FixedPoint(A.V.mul(B.V, &Overflow));
116 return Overflow;
117 }
118 static bool div(const FixedPoint A, const FixedPoint B, unsigned Bits,
119 FixedPoint *R) {
120 bool Overflow = false;
121 *R = FixedPoint(A.V.div(B.V, &Overflow));
122 return Overflow;
123 }
124
125 static bool shiftLeft(const FixedPoint A, const FixedPoint B, unsigned OpBits,
126 FixedPoint *R) {
127 unsigned Amt = B.V.getValue().getLimitedValue(OpBits);
128 bool Overflow;
129 *R = FixedPoint(A.V.shl(Amt, &Overflow));
130 return Overflow;
131 }
132 static bool shiftRight(const FixedPoint A, const FixedPoint B,
133 unsigned OpBits, FixedPoint *R) {
134 unsigned Amt = B.V.getValue().getLimitedValue(OpBits);
135 bool Overflow;
136 *R = FixedPoint(A.V.shr(Amt, &Overflow));
137 return Overflow;
138 }
139
140 static bool rem(const FixedPoint A, const FixedPoint B, unsigned Bits,
141 FixedPoint *R) {
142 llvm_unreachable("Rem doesn't exist for fixed point values");
143 return true;
144 }
145 static bool bitAnd(const FixedPoint A, const FixedPoint B, unsigned Bits,
146 FixedPoint *R) {
147 return true;
148 }
149 static bool bitOr(const FixedPoint A, const FixedPoint B, unsigned Bits,
150 FixedPoint *R) {
151 return true;
152 }
153 static bool bitXor(const FixedPoint A, const FixedPoint B, unsigned Bits,
154 FixedPoint *R) {
155 return true;
156 }
157
158 static bool increment(const FixedPoint &A, FixedPoint *R) { return true; }
159 static bool decrement(const FixedPoint &A, FixedPoint *R) { return true; }
160};
161
162inline FixedPoint getSwappedBytes(FixedPoint F) { return F; }
163
164inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, FixedPoint F) {
165 F.print(OS);
166 return OS;
167}
168
169} // namespace interp
170} // namespace clang
171
172#endif
#define V(N, I)
Definition: ASTContext.h:3443
__device__ __2f16 float c
#define bool
Definition: amdgpuintrin.h:20
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
Wrapper around fixed point types.
Definition: FixedPoint.h:23
static bool sub(const FixedPoint A, const FixedPoint B, unsigned Bits, FixedPoint *R)
Definition: FixedPoint.h:106
static bool add(const FixedPoint A, const FixedPoint B, unsigned Bits, FixedPoint *R)
Definition: FixedPoint.h:100
FixedPoint(llvm::APFixedPoint &V)
Definition: FixedPoint.h:29
llvm::FixedPointSemantics getSemantics() const
Definition: FixedPoint.h:71
FixedPoint toSemantics(const llvm::FixedPointSemantics &Sem, bool *Overflow) const
Definition: FixedPoint.h:67
static bool shiftRight(const FixedPoint A, const FixedPoint B, unsigned OpBits, FixedPoint *R)
Definition: FixedPoint.h:132
bool isPositive() const
Definition: FixedPoint.h:59
llvm::APSInt toInt(unsigned BitWidth, bool Signed, bool *Overflow) const
Definition: FixedPoint.h:77
static bool shiftLeft(const FixedPoint A, const FixedPoint B, unsigned OpBits, FixedPoint *R)
Definition: FixedPoint.h:125
llvm::APFloat toFloat(const llvm::fltSemantics *Sem) const
Definition: FixedPoint.h:73
static bool rem(const FixedPoint A, const FixedPoint B, unsigned Bits, FixedPoint *R)
Definition: FixedPoint.h:140
static FixedPoint from(const APSInt &I, llvm::FixedPointSemantics Sem, bool *Overflow)
Definition: FixedPoint.h:40
static bool neg(const FixedPoint &A, FixedPoint *R)
Definition: FixedPoint.h:94
FixedPoint truncate(unsigned BitWidth) const
Definition: FixedPoint.h:65
ComparisonCategoryResult compare(const FixedPoint &Other) const
Definition: FixedPoint.h:85
static bool increment(const FixedPoint &A, FixedPoint *R)
Definition: FixedPoint.h:158
static FixedPoint from(const llvm::APFloat &I, llvm::FixedPointSemantics Sem, bool *Overflow)
Definition: FixedPoint.h:44
static bool bitOr(const FixedPoint A, const FixedPoint B, unsigned Bits, FixedPoint *R)
Definition: FixedPoint.h:149
static bool div(const FixedPoint A, const FixedPoint B, unsigned Bits, FixedPoint *R)
Definition: FixedPoint.h:118
static bool mul(const FixedPoint A, const FixedPoint B, unsigned Bits, FixedPoint *R)
Definition: FixedPoint.h:112
bool isNegative() const
Definition: FixedPoint.h:58
APSInt toAPSInt(unsigned BitWidth=0) const
Definition: FixedPoint.h:53
FixedPoint(APInt V, llvm::FixedPointSemantics Sem)
Definition: FixedPoint.h:30
std::string toDiagnosticString(const ASTContext &Ctx) const
Definition: FixedPoint.h:81
bool isMinusOne() const
Definition: FixedPoint.h:63
static bool bitAnd(const FixedPoint A, const FixedPoint B, unsigned Bits, FixedPoint *R)
Definition: FixedPoint.h:145
static bool decrement(const FixedPoint &A, FixedPoint *R)
Definition: FixedPoint.h:159
FixedPoint(llvm::APFixedPoint &&V)
Definition: FixedPoint.h:28
static bool bitXor(const FixedPoint A, const FixedPoint B, unsigned Bits, FixedPoint *R)
Definition: FixedPoint.h:153
void print(llvm::raw_ostream &OS) const
Definition: FixedPoint.h:50
static FixedPoint zero(llvm::FixedPointSemantics Sem)
Definition: FixedPoint.h:36
APValue toAPValue(const ASTContext &) const
Definition: FixedPoint.h:52
unsigned bitWidth() const
Definition: FixedPoint.h:55
llvm::APInt APInt
Definition: FixedPoint.h:19
FixedPoint getSwappedBytes(FixedPoint F)
Definition: FixedPoint.h:162
llvm::FixedPointSemantics FixedPointSemantics
Definition: Interp.h:43
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition: Boolean.h:160
llvm::APSInt APSInt
Definition: FixedPoint.h:20
The JSON file list parser is used to communicate input to InstallAPI.
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
@ Other
Other implicit parameter.
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
#define false
Definition: stdbool.h:26
A quantity in bits.
Definition: BitcastBuffer.h:24