Skip to content

[clang][bytecode] Implement fixed-point shifts #110429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,10 @@ bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
return ConvertResult(this->emitMulFixedPoint(E));
case BO_Div:
return ConvertResult(this->emitDivFixedPoint(E));
case BO_Shl:
return ConvertResult(this->emitShiftFixedPoint(/*Left=*/true, E));
case BO_Shr:
return ConvertResult(this->emitShiftFixedPoint(/*Left=*/false, E));

default:
return this->emitInvalid(E);
Expand Down
17 changes: 17 additions & 0 deletions clang/lib/AST/ByteCode/FixedPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class FixedPoint final {
bool *Overflow) const {
return FixedPoint(V.convert(Sem, Overflow));
}
llvm::FixedPointSemantics getSemantics() const { return V.getSemantics(); }

llvm::APFloat toFloat(const llvm::fltSemantics *Sem) const {
return V.convertToFloat(*Sem);
Expand Down Expand Up @@ -120,6 +121,22 @@ class FixedPoint final {
*R = FixedPoint(A.V.div(B.V, &Overflow));
return Overflow;
}

static bool shiftLeft(const FixedPoint A, const FixedPoint B, unsigned OpBits,
FixedPoint *R) {
unsigned Amt = B.V.getValue().getLimitedValue(OpBits);
bool Overflow;
*R = FixedPoint(A.V.shl(Amt, &Overflow));
return Overflow;
}
static bool shiftRight(const FixedPoint A, const FixedPoint B,
unsigned OpBits, FixedPoint *R) {
unsigned Amt = B.V.getValue().getLimitedValue(OpBits);
bool Overflow;
*R = FixedPoint(A.V.shr(Amt, &Overflow));
return Overflow;
}

static bool rem(const FixedPoint A, const FixedPoint B, unsigned Bits,
FixedPoint *R) {
llvm_unreachable("Rem doesn't exist for fixed point values");
Expand Down
36 changes: 36 additions & 0 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2579,6 +2579,42 @@ inline bool Shl(InterpState &S, CodePtr OpPC) {
return DoShift<LT, RT, ShiftDir::Left>(S, OpPC, LHS, RHS);
}

static inline bool ShiftFixedPoint(InterpState &S, CodePtr OpPC, bool Left) {
const auto &RHS = S.Stk.pop<FixedPoint>();
const auto &LHS = S.Stk.pop<FixedPoint>();
llvm::FixedPointSemantics LHSSema = LHS.getSemantics();

unsigned ShiftBitWidth =
LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding() - 1;

// Embedded-C 4.1.6.2.2:
// The right operand must be nonnegative and less than the total number
// of (nonpadding) bits of the fixed-point operand ...
if (RHS.isNegative()) {
S.CCEDiag(S.Current->getLocation(OpPC), diag::note_constexpr_negative_shift)
<< RHS.toAPSInt();
} else if (static_cast<unsigned>(RHS.toAPSInt().getLimitedValue(
ShiftBitWidth)) != RHS.toAPSInt()) {
const Expr *E = S.Current->getExpr(OpPC);
S.CCEDiag(E, diag::note_constexpr_large_shift)
<< RHS.toAPSInt() << E->getType() << ShiftBitWidth;
}

FixedPoint Result;
if (Left) {
if (FixedPoint::shiftLeft(LHS, RHS, ShiftBitWidth, &Result) &&
!handleFixedPointOverflow(S, OpPC, Result))
return false;
} else {
if (FixedPoint::shiftRight(LHS, RHS, ShiftBitWidth, &Result) &&
!handleFixedPointOverflow(S, OpPC, Result))
return false;
}

S.Stk.push<FixedPoint>(Result);
return true;
}

//===----------------------------------------------------------------------===//
// NoRet
//===----------------------------------------------------------------------===//
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/AST/ByteCode/Opcodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,9 @@ def CastFixedPointIntegral : Opcode {
let Types = [FixedSizeIntegralTypes];
let HasGroup = 1;
}
def ShiftFixedPoint : Opcode {
let Args = [ArgBool];
}

def PtrPtrCast : Opcode {
let Args = [ArgBool];
Expand Down
1 change: 1 addition & 0 deletions clang/test/Frontend/fixed_point_errors.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -verify -ffixed-point %s
// RUN: %clang_cc1 -verify -ffixed-point %s -fexperimental-new-constant-interpreter

/* We do not yet support long long. No recommended bit widths are given for this
* size. */
Expand Down
Loading