Path: blob/main/contrib/llvm-project/clang/lib/AST/ByteCode/FixedPoint.h
213799 views
//===------- FixedPoint.h - Fixedd point types for the VM -------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_CLANG_AST_INTERP_FIXED_POINT_H9#define LLVM_CLANG_AST_INTERP_FIXED_POINT_H1011#include "clang/AST/APValue.h"12#include "clang/AST/ComparisonCategories.h"13#include "llvm/ADT/APFixedPoint.h"1415namespace clang {16namespace interp {1718using APInt = llvm::APInt;19using APSInt = llvm::APSInt;2021/// Wrapper around fixed point types.22class FixedPoint final {23private:24llvm::APFixedPoint V;2526public:27FixedPoint(llvm::APFixedPoint &&V) : V(std::move(V)) {}28FixedPoint(llvm::APFixedPoint &V) : V(V) {}29FixedPoint(APInt V, llvm::FixedPointSemantics Sem) : V(V, Sem) {}30// This needs to be default-constructible so llvm::endian::read works.31FixedPoint()32: V(APInt(0, 0ULL, false),33llvm::FixedPointSemantics(0, 0, false, false, false)) {}3435static FixedPoint zero(llvm::FixedPointSemantics Sem) {36return FixedPoint(APInt(Sem.getWidth(), 0ULL, Sem.isSigned()), Sem);37}3839static FixedPoint from(const APSInt &I, llvm::FixedPointSemantics Sem,40bool *Overflow) {41return FixedPoint(llvm::APFixedPoint::getFromIntValue(I, Sem, Overflow));42}43static FixedPoint from(const llvm::APFloat &I, llvm::FixedPointSemantics Sem,44bool *Overflow) {45return FixedPoint(llvm::APFixedPoint::getFromFloatValue(I, Sem, Overflow));46}4748operator bool() const { return V.getBoolValue(); }49void print(llvm::raw_ostream &OS) const { OS << V; }5051APValue toAPValue(const ASTContext &) const { return APValue(V); }52APSInt toAPSInt(unsigned BitWidth = 0) const { return V.getValue(); }5354unsigned bitWidth() const { return V.getWidth(); }55bool isSigned() const { return V.isSigned(); }56bool isZero() const { return V.getValue().isZero(); }57bool isNegative() const { return V.getValue().isNegative(); }58bool isPositive() const { return V.getValue().isNonNegative(); }59bool isMin() const {60return V == llvm::APFixedPoint::getMin(V.getSemantics());61}62bool isMinusOne() const { return V.isSigned() && V.getValue() == -1; }6364FixedPoint truncate(unsigned BitWidth) const { return *this; }6566FixedPoint toSemantics(const llvm::FixedPointSemantics &Sem,67bool *Overflow) const {68return FixedPoint(V.convert(Sem, Overflow));69}70llvm::FixedPointSemantics getSemantics() const { return V.getSemantics(); }7172llvm::APFloat toFloat(const llvm::fltSemantics *Sem) const {73return V.convertToFloat(*Sem);74}7576llvm::APSInt toInt(unsigned BitWidth, bool Signed, bool *Overflow) const {77return V.convertToInt(BitWidth, Signed, Overflow);78}7980std::string toDiagnosticString(const ASTContext &Ctx) const {81return V.toString();82}8384ComparisonCategoryResult compare(const FixedPoint &Other) const {85int c = V.compare(Other.V);86if (c == 0)87return ComparisonCategoryResult::Equal;88else if (c < 0)89return ComparisonCategoryResult::Less;90return ComparisonCategoryResult::Greater;91}9293size_t bytesToSerialize() const {94return sizeof(uint32_t) + (V.getValue().getBitWidth() / CHAR_BIT);95}9697void serialize(std::byte *Buff) const {98// Semantics followed by APInt.99uint32_t SemI = V.getSemantics().toOpaqueInt();100std::memcpy(Buff, &SemI, sizeof(SemI));101102llvm::APInt API = V.getValue();103llvm::StoreIntToMemory(API, (uint8_t *)(Buff + sizeof(SemI)),104bitWidth() / 8);105}106107static FixedPoint deserialize(const std::byte *Buff) {108auto Sem = llvm::FixedPointSemantics::getFromOpaqueInt(109*reinterpret_cast<const uint32_t *>(Buff));110unsigned BitWidth = Sem.getWidth();111APInt I(BitWidth, 0ull, !Sem.isSigned());112llvm::LoadIntFromMemory(113I, reinterpret_cast<const uint8_t *>(Buff + sizeof(uint32_t)),114BitWidth / CHAR_BIT);115116return FixedPoint(I, Sem);117}118119static bool neg(const FixedPoint &A, FixedPoint *R) {120bool Overflow = false;121*R = FixedPoint(A.V.negate(&Overflow));122return Overflow;123}124125static bool add(const FixedPoint A, const FixedPoint B, unsigned Bits,126FixedPoint *R) {127bool Overflow = false;128*R = FixedPoint(A.V.add(B.V, &Overflow));129return Overflow;130}131static bool sub(const FixedPoint A, const FixedPoint B, unsigned Bits,132FixedPoint *R) {133bool Overflow = false;134*R = FixedPoint(A.V.sub(B.V, &Overflow));135return Overflow;136}137static bool mul(const FixedPoint A, const FixedPoint B, unsigned Bits,138FixedPoint *R) {139bool Overflow = false;140*R = FixedPoint(A.V.mul(B.V, &Overflow));141return Overflow;142}143static bool div(const FixedPoint A, const FixedPoint B, unsigned Bits,144FixedPoint *R) {145bool Overflow = false;146*R = FixedPoint(A.V.div(B.V, &Overflow));147return Overflow;148}149150static bool shiftLeft(const FixedPoint A, const FixedPoint B, unsigned OpBits,151FixedPoint *R) {152unsigned Amt = B.V.getValue().getLimitedValue(OpBits);153bool Overflow;154*R = FixedPoint(A.V.shl(Amt, &Overflow));155return Overflow;156}157static bool shiftRight(const FixedPoint A, const FixedPoint B,158unsigned OpBits, FixedPoint *R) {159unsigned Amt = B.V.getValue().getLimitedValue(OpBits);160bool Overflow;161*R = FixedPoint(A.V.shr(Amt, &Overflow));162return Overflow;163}164165static bool rem(const FixedPoint A, const FixedPoint B, unsigned Bits,166FixedPoint *R) {167llvm_unreachable("Rem doesn't exist for fixed point values");168return true;169}170static bool bitAnd(const FixedPoint A, const FixedPoint B, unsigned Bits,171FixedPoint *R) {172return true;173}174static bool bitOr(const FixedPoint A, const FixedPoint B, unsigned Bits,175FixedPoint *R) {176return true;177}178static bool bitXor(const FixedPoint A, const FixedPoint B, unsigned Bits,179FixedPoint *R) {180return true;181}182183static bool increment(const FixedPoint &A, FixedPoint *R) { return true; }184static bool decrement(const FixedPoint &A, FixedPoint *R) { return true; }185};186187inline FixedPoint getSwappedBytes(FixedPoint F) { return F; }188189inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, FixedPoint F) {190F.print(OS);191return OS;192}193194} // namespace interp195} // namespace clang196197#endif198199200