Skip to content

Commit 6b62e04

Browse files
authored
[clang][bytecode] Implement (N)EQ between fixed point and integral (#110358)
Convert the non-fixed-point side to a fixed-point type before doing the comparison.
1 parent 2f7ccaf commit 6b62e04

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,8 @@ bool Compiler<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
773773
RHS->getType()->isAnyComplexType()) &&
774774
BO->isComparisonOp())
775775
return this->emitComplexComparison(LHS, RHS, BO);
776+
if (LHS->getType()->isFixedPointType() || RHS->getType()->isFixedPointType())
777+
return this->VisitFixedPointBinOp(BO);
776778

777779
if (BO->isPtrMemOp()) {
778780
if (!this->visit(LHS))
@@ -1469,6 +1471,55 @@ bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) {
14691471
return true;
14701472
}
14711473

1474+
template <class Emitter>
1475+
bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
1476+
const Expr *LHS = E->getLHS();
1477+
const Expr *RHS = E->getRHS();
1478+
1479+
assert(LHS->getType()->isFixedPointType() ||
1480+
RHS->getType()->isFixedPointType());
1481+
1482+
if (!this->visit(LHS))
1483+
return false;
1484+
if (!LHS->getType()->isFixedPointType()) {
1485+
auto Sem = Ctx.getASTContext().getFixedPointSemantics(LHS->getType());
1486+
uint32_t I;
1487+
std::memcpy(&I, &Sem, sizeof(Sem));
1488+
if (!this->emitCastIntegralFixedPoint(classifyPrim(LHS->getType()), I, E))
1489+
return false;
1490+
}
1491+
if (!this->visit(RHS))
1492+
return false;
1493+
if (!RHS->getType()->isFixedPointType()) {
1494+
auto Sem = Ctx.getASTContext().getFixedPointSemantics(RHS->getType());
1495+
uint32_t I;
1496+
std::memcpy(&I, &Sem, sizeof(Sem));
1497+
if (!this->emitCastIntegralFixedPoint(classifyPrim(RHS->getType()), I, E))
1498+
return false;
1499+
}
1500+
1501+
switch (E->getOpcode()) {
1502+
case BO_EQ:
1503+
return this->emitEQFixedPoint(E);
1504+
case BO_NE:
1505+
return this->emitNEFixedPoint(E);
1506+
#if 0
1507+
case BO_LT:
1508+
return this->emitLTFixedPoint(E);
1509+
case BO_LE:
1510+
return this->emitLEFixedPoint(E);
1511+
case BO_GT:
1512+
return this->emitGTFixedPoint(E);
1513+
case BO_GE:
1514+
return this->emitGEFixedPoint(E);
1515+
#endif
1516+
default:
1517+
return this->emitInvalid(E);
1518+
}
1519+
1520+
llvm_unreachable("unhandled binop opcode");
1521+
}
1522+
14721523
template <class Emitter>
14731524
bool Compiler<Emitter>::VisitImplicitValueInitExpr(
14741525
const ImplicitValueInitExpr *E) {

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
132132
bool VisitPointerArithBinOp(const BinaryOperator *E);
133133
bool VisitComplexBinOp(const BinaryOperator *E);
134134
bool VisitVectorBinOp(const BinaryOperator *E);
135+
bool VisitFixedPointBinOp(const BinaryOperator *E);
135136
bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E);
136137
bool VisitCallExpr(const CallExpr *E);
137138
bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinID);

clang/test/AST/ByteCode/fixed-point.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@ static_assert(!((bool)0.0k));
66
static_assert((bool)0.0k); // both-error {{static assertion failed}}
77

88
static_assert(1.0k == 1.0k);
9+
static_assert(1.0k == 1);
910
static_assert(1.0k != 1.0k); // both-error {{failed due to requirement '1.0k != 1.0k'}}
11+
static_assert(1.0k != 1); // both-error {{failed due to requirement '1.0k != 1'}}
1012
static_assert(-12.0k == -(-(-12.0k)));
1113

1214
/// Zero-init.
1315
constexpr _Accum A{};
1416
static_assert(A == 0.0k);
17+
static_assert(A == 0);
1518

1619
namespace IntToFixedPointCast {
1720
constexpr _Accum B = 13;
1821
static_assert(B == 13.0k);
22+
static_assert(B == 13);
1923

2024
constexpr _Fract sf = -1;
2125
static_assert(sf == -1.0k);
26+
static_assert(sf == -1);
2227
}

0 commit comments

Comments
 (0)