xref: /freebsd/contrib/llvm-project/clang/lib/AST/ByteCode/FixedPoint.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1*700637cbSDimitry Andric //===------- FixedPoint.h - Fixedd point types for the VM -------*- C++ -*-===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric 
9*700637cbSDimitry Andric #ifndef LLVM_CLANG_AST_INTERP_FIXED_POINT_H
10*700637cbSDimitry Andric #define LLVM_CLANG_AST_INTERP_FIXED_POINT_H
11*700637cbSDimitry Andric 
12*700637cbSDimitry Andric #include "clang/AST/APValue.h"
13*700637cbSDimitry Andric #include "clang/AST/ComparisonCategories.h"
14*700637cbSDimitry Andric #include "llvm/ADT/APFixedPoint.h"
15*700637cbSDimitry Andric 
16*700637cbSDimitry Andric namespace clang {
17*700637cbSDimitry Andric namespace interp {
18*700637cbSDimitry Andric 
19*700637cbSDimitry Andric using APInt = llvm::APInt;
20*700637cbSDimitry Andric using APSInt = llvm::APSInt;
21*700637cbSDimitry Andric 
22*700637cbSDimitry Andric /// Wrapper around fixed point types.
23*700637cbSDimitry Andric class FixedPoint final {
24*700637cbSDimitry Andric private:
25*700637cbSDimitry Andric   llvm::APFixedPoint V;
26*700637cbSDimitry Andric 
27*700637cbSDimitry Andric public:
FixedPoint(llvm::APFixedPoint && V)28*700637cbSDimitry Andric   FixedPoint(llvm::APFixedPoint &&V) : V(std::move(V)) {}
FixedPoint(llvm::APFixedPoint & V)29*700637cbSDimitry Andric   FixedPoint(llvm::APFixedPoint &V) : V(V) {}
FixedPoint(APInt V,llvm::FixedPointSemantics Sem)30*700637cbSDimitry Andric   FixedPoint(APInt V, llvm::FixedPointSemantics Sem) : V(V, Sem) {}
31*700637cbSDimitry Andric   // This needs to be default-constructible so llvm::endian::read works.
FixedPoint()32*700637cbSDimitry Andric   FixedPoint()
33*700637cbSDimitry Andric       : V(APInt(0, 0ULL, false),
34*700637cbSDimitry Andric           llvm::FixedPointSemantics(0, 0, false, false, false)) {}
35*700637cbSDimitry Andric 
zero(llvm::FixedPointSemantics Sem)36*700637cbSDimitry Andric   static FixedPoint zero(llvm::FixedPointSemantics Sem) {
37*700637cbSDimitry Andric     return FixedPoint(APInt(Sem.getWidth(), 0ULL, Sem.isSigned()), Sem);
38*700637cbSDimitry Andric   }
39*700637cbSDimitry Andric 
from(const APSInt & I,llvm::FixedPointSemantics Sem,bool * Overflow)40*700637cbSDimitry Andric   static FixedPoint from(const APSInt &I, llvm::FixedPointSemantics Sem,
41*700637cbSDimitry Andric                          bool *Overflow) {
42*700637cbSDimitry Andric     return FixedPoint(llvm::APFixedPoint::getFromIntValue(I, Sem, Overflow));
43*700637cbSDimitry Andric   }
from(const llvm::APFloat & I,llvm::FixedPointSemantics Sem,bool * Overflow)44*700637cbSDimitry Andric   static FixedPoint from(const llvm::APFloat &I, llvm::FixedPointSemantics Sem,
45*700637cbSDimitry Andric                          bool *Overflow) {
46*700637cbSDimitry Andric     return FixedPoint(llvm::APFixedPoint::getFromFloatValue(I, Sem, Overflow));
47*700637cbSDimitry Andric   }
48*700637cbSDimitry Andric 
49*700637cbSDimitry Andric   operator bool() const { return V.getBoolValue(); }
print(llvm::raw_ostream & OS)50*700637cbSDimitry Andric   void print(llvm::raw_ostream &OS) const { OS << V; }
51*700637cbSDimitry Andric 
toAPValue(const ASTContext &)52*700637cbSDimitry Andric   APValue toAPValue(const ASTContext &) const { return APValue(V); }
53*700637cbSDimitry Andric   APSInt toAPSInt(unsigned BitWidth = 0) const { return V.getValue(); }
54*700637cbSDimitry Andric 
bitWidth()55*700637cbSDimitry Andric   unsigned bitWidth() const { return V.getWidth(); }
isSigned()56*700637cbSDimitry Andric   bool isSigned() const { return V.isSigned(); }
isZero()57*700637cbSDimitry Andric   bool isZero() const { return V.getValue().isZero(); }
isNegative()58*700637cbSDimitry Andric   bool isNegative() const { return V.getValue().isNegative(); }
isPositive()59*700637cbSDimitry Andric   bool isPositive() const { return V.getValue().isNonNegative(); }
isMin()60*700637cbSDimitry Andric   bool isMin() const {
61*700637cbSDimitry Andric     return V == llvm::APFixedPoint::getMin(V.getSemantics());
62*700637cbSDimitry Andric   }
isMinusOne()63*700637cbSDimitry Andric   bool isMinusOne() const { return V.isSigned() && V.getValue() == -1; }
64*700637cbSDimitry Andric 
truncate(unsigned BitWidth)65*700637cbSDimitry Andric   FixedPoint truncate(unsigned BitWidth) const { return *this; }
66*700637cbSDimitry Andric 
toSemantics(const llvm::FixedPointSemantics & Sem,bool * Overflow)67*700637cbSDimitry Andric   FixedPoint toSemantics(const llvm::FixedPointSemantics &Sem,
68*700637cbSDimitry Andric                          bool *Overflow) const {
69*700637cbSDimitry Andric     return FixedPoint(V.convert(Sem, Overflow));
70*700637cbSDimitry Andric   }
getSemantics()71*700637cbSDimitry Andric   llvm::FixedPointSemantics getSemantics() const { return V.getSemantics(); }
72*700637cbSDimitry Andric 
toFloat(const llvm::fltSemantics * Sem)73*700637cbSDimitry Andric   llvm::APFloat toFloat(const llvm::fltSemantics *Sem) const {
74*700637cbSDimitry Andric     return V.convertToFloat(*Sem);
75*700637cbSDimitry Andric   }
76*700637cbSDimitry Andric 
toInt(unsigned BitWidth,bool Signed,bool * Overflow)77*700637cbSDimitry Andric   llvm::APSInt toInt(unsigned BitWidth, bool Signed, bool *Overflow) const {
78*700637cbSDimitry Andric     return V.convertToInt(BitWidth, Signed, Overflow);
79*700637cbSDimitry Andric   }
80*700637cbSDimitry Andric 
toDiagnosticString(const ASTContext & Ctx)81*700637cbSDimitry Andric   std::string toDiagnosticString(const ASTContext &Ctx) const {
82*700637cbSDimitry Andric     return V.toString();
83*700637cbSDimitry Andric   }
84*700637cbSDimitry Andric 
compare(const FixedPoint & Other)85*700637cbSDimitry Andric   ComparisonCategoryResult compare(const FixedPoint &Other) const {
86*700637cbSDimitry Andric     int c = V.compare(Other.V);
87*700637cbSDimitry Andric     if (c == 0)
88*700637cbSDimitry Andric       return ComparisonCategoryResult::Equal;
89*700637cbSDimitry Andric     else if (c < 0)
90*700637cbSDimitry Andric       return ComparisonCategoryResult::Less;
91*700637cbSDimitry Andric     return ComparisonCategoryResult::Greater;
92*700637cbSDimitry Andric   }
93*700637cbSDimitry Andric 
bytesToSerialize()94*700637cbSDimitry Andric   size_t bytesToSerialize() const {
95*700637cbSDimitry Andric     return sizeof(uint32_t) + (V.getValue().getBitWidth() / CHAR_BIT);
96*700637cbSDimitry Andric   }
97*700637cbSDimitry Andric 
serialize(std::byte * Buff)98*700637cbSDimitry Andric   void serialize(std::byte *Buff) const {
99*700637cbSDimitry Andric     // Semantics followed by APInt.
100*700637cbSDimitry Andric     uint32_t SemI = V.getSemantics().toOpaqueInt();
101*700637cbSDimitry Andric     std::memcpy(Buff, &SemI, sizeof(SemI));
102*700637cbSDimitry Andric 
103*700637cbSDimitry Andric     llvm::APInt API = V.getValue();
104*700637cbSDimitry Andric     llvm::StoreIntToMemory(API, (uint8_t *)(Buff + sizeof(SemI)),
105*700637cbSDimitry Andric                            bitWidth() / 8);
106*700637cbSDimitry Andric   }
107*700637cbSDimitry Andric 
deserialize(const std::byte * Buff)108*700637cbSDimitry Andric   static FixedPoint deserialize(const std::byte *Buff) {
109*700637cbSDimitry Andric     auto Sem = llvm::FixedPointSemantics::getFromOpaqueInt(
110*700637cbSDimitry Andric         *reinterpret_cast<const uint32_t *>(Buff));
111*700637cbSDimitry Andric     unsigned BitWidth = Sem.getWidth();
112*700637cbSDimitry Andric     APInt I(BitWidth, 0ull, !Sem.isSigned());
113*700637cbSDimitry Andric     llvm::LoadIntFromMemory(
114*700637cbSDimitry Andric         I, reinterpret_cast<const uint8_t *>(Buff + sizeof(uint32_t)),
115*700637cbSDimitry Andric         BitWidth / CHAR_BIT);
116*700637cbSDimitry Andric 
117*700637cbSDimitry Andric     return FixedPoint(I, Sem);
118*700637cbSDimitry Andric   }
119*700637cbSDimitry Andric 
neg(const FixedPoint & A,FixedPoint * R)120*700637cbSDimitry Andric   static bool neg(const FixedPoint &A, FixedPoint *R) {
121*700637cbSDimitry Andric     bool Overflow = false;
122*700637cbSDimitry Andric     *R = FixedPoint(A.V.negate(&Overflow));
123*700637cbSDimitry Andric     return Overflow;
124*700637cbSDimitry Andric   }
125*700637cbSDimitry Andric 
add(const FixedPoint A,const FixedPoint B,unsigned Bits,FixedPoint * R)126*700637cbSDimitry Andric   static bool add(const FixedPoint A, const FixedPoint B, unsigned Bits,
127*700637cbSDimitry Andric                   FixedPoint *R) {
128*700637cbSDimitry Andric     bool Overflow = false;
129*700637cbSDimitry Andric     *R = FixedPoint(A.V.add(B.V, &Overflow));
130*700637cbSDimitry Andric     return Overflow;
131*700637cbSDimitry Andric   }
sub(const FixedPoint A,const FixedPoint B,unsigned Bits,FixedPoint * R)132*700637cbSDimitry Andric   static bool sub(const FixedPoint A, const FixedPoint B, unsigned Bits,
133*700637cbSDimitry Andric                   FixedPoint *R) {
134*700637cbSDimitry Andric     bool Overflow = false;
135*700637cbSDimitry Andric     *R = FixedPoint(A.V.sub(B.V, &Overflow));
136*700637cbSDimitry Andric     return Overflow;
137*700637cbSDimitry Andric   }
mul(const FixedPoint A,const FixedPoint B,unsigned Bits,FixedPoint * R)138*700637cbSDimitry Andric   static bool mul(const FixedPoint A, const FixedPoint B, unsigned Bits,
139*700637cbSDimitry Andric                   FixedPoint *R) {
140*700637cbSDimitry Andric     bool Overflow = false;
141*700637cbSDimitry Andric     *R = FixedPoint(A.V.mul(B.V, &Overflow));
142*700637cbSDimitry Andric     return Overflow;
143*700637cbSDimitry Andric   }
div(const FixedPoint A,const FixedPoint B,unsigned Bits,FixedPoint * R)144*700637cbSDimitry Andric   static bool div(const FixedPoint A, const FixedPoint B, unsigned Bits,
145*700637cbSDimitry Andric                   FixedPoint *R) {
146*700637cbSDimitry Andric     bool Overflow = false;
147*700637cbSDimitry Andric     *R = FixedPoint(A.V.div(B.V, &Overflow));
148*700637cbSDimitry Andric     return Overflow;
149*700637cbSDimitry Andric   }
150*700637cbSDimitry Andric 
shiftLeft(const FixedPoint A,const FixedPoint B,unsigned OpBits,FixedPoint * R)151*700637cbSDimitry Andric   static bool shiftLeft(const FixedPoint A, const FixedPoint B, unsigned OpBits,
152*700637cbSDimitry Andric                         FixedPoint *R) {
153*700637cbSDimitry Andric     unsigned Amt = B.V.getValue().getLimitedValue(OpBits);
154*700637cbSDimitry Andric     bool Overflow;
155*700637cbSDimitry Andric     *R = FixedPoint(A.V.shl(Amt, &Overflow));
156*700637cbSDimitry Andric     return Overflow;
157*700637cbSDimitry Andric   }
shiftRight(const FixedPoint A,const FixedPoint B,unsigned OpBits,FixedPoint * R)158*700637cbSDimitry Andric   static bool shiftRight(const FixedPoint A, const FixedPoint B,
159*700637cbSDimitry Andric                          unsigned OpBits, FixedPoint *R) {
160*700637cbSDimitry Andric     unsigned Amt = B.V.getValue().getLimitedValue(OpBits);
161*700637cbSDimitry Andric     bool Overflow;
162*700637cbSDimitry Andric     *R = FixedPoint(A.V.shr(Amt, &Overflow));
163*700637cbSDimitry Andric     return Overflow;
164*700637cbSDimitry Andric   }
165*700637cbSDimitry Andric 
rem(const FixedPoint A,const FixedPoint B,unsigned Bits,FixedPoint * R)166*700637cbSDimitry Andric   static bool rem(const FixedPoint A, const FixedPoint B, unsigned Bits,
167*700637cbSDimitry Andric                   FixedPoint *R) {
168*700637cbSDimitry Andric     llvm_unreachable("Rem doesn't exist for fixed point values");
169*700637cbSDimitry Andric     return true;
170*700637cbSDimitry Andric   }
bitAnd(const FixedPoint A,const FixedPoint B,unsigned Bits,FixedPoint * R)171*700637cbSDimitry Andric   static bool bitAnd(const FixedPoint A, const FixedPoint B, unsigned Bits,
172*700637cbSDimitry Andric                      FixedPoint *R) {
173*700637cbSDimitry Andric     return true;
174*700637cbSDimitry Andric   }
bitOr(const FixedPoint A,const FixedPoint B,unsigned Bits,FixedPoint * R)175*700637cbSDimitry Andric   static bool bitOr(const FixedPoint A, const FixedPoint B, unsigned Bits,
176*700637cbSDimitry Andric                     FixedPoint *R) {
177*700637cbSDimitry Andric     return true;
178*700637cbSDimitry Andric   }
bitXor(const FixedPoint A,const FixedPoint B,unsigned Bits,FixedPoint * R)179*700637cbSDimitry Andric   static bool bitXor(const FixedPoint A, const FixedPoint B, unsigned Bits,
180*700637cbSDimitry Andric                      FixedPoint *R) {
181*700637cbSDimitry Andric     return true;
182*700637cbSDimitry Andric   }
183*700637cbSDimitry Andric 
increment(const FixedPoint & A,FixedPoint * R)184*700637cbSDimitry Andric   static bool increment(const FixedPoint &A, FixedPoint *R) { return true; }
decrement(const FixedPoint & A,FixedPoint * R)185*700637cbSDimitry Andric   static bool decrement(const FixedPoint &A, FixedPoint *R) { return true; }
186*700637cbSDimitry Andric };
187*700637cbSDimitry Andric 
getSwappedBytes(FixedPoint F)188*700637cbSDimitry Andric inline FixedPoint getSwappedBytes(FixedPoint F) { return F; }
189*700637cbSDimitry Andric 
190*700637cbSDimitry Andric inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, FixedPoint F) {
191*700637cbSDimitry Andric   F.print(OS);
192*700637cbSDimitry Andric   return OS;
193*700637cbSDimitry Andric }
194*700637cbSDimitry Andric 
195*700637cbSDimitry Andric } // namespace interp
196*700637cbSDimitry Andric } // namespace clang
197*700637cbSDimitry Andric 
198*700637cbSDimitry Andric #endif
199