@@ -1502,25 +1502,41 @@ bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
15021502 assert (LHS->getType ()->isFixedPointType () ||
15031503 RHS->getType ()->isFixedPointType ());
15041504
1505+ auto LHSSema = Ctx.getASTContext ().getFixedPointSemantics (LHS->getType ());
1506+ auto RHSSema = Ctx.getASTContext ().getFixedPointSemantics (RHS->getType ());
1507+
15051508 if (!this ->visit (LHS))
15061509 return false ;
15071510 if (!LHS->getType ()->isFixedPointType ()) {
1508- auto Sem = Ctx.getASTContext ().getFixedPointSemantics (LHS->getType ());
15091511 uint32_t I;
1510- std::memcpy (&I, &Sem , sizeof (Sem ));
1512+ std::memcpy (&I, &LHSSema , sizeof (llvm::FixedPointSemantics ));
15111513 if (!this ->emitCastIntegralFixedPoint (classifyPrim (LHS->getType ()), I, E))
15121514 return false ;
15131515 }
1516+
15141517 if (!this ->visit (RHS))
15151518 return false ;
15161519 if (!RHS->getType ()->isFixedPointType ()) {
1517- auto Sem = Ctx.getASTContext ().getFixedPointSemantics (RHS->getType ());
15181520 uint32_t I;
1519- std::memcpy (&I, &Sem , sizeof (Sem ));
1521+ std::memcpy (&I, &RHSSema , sizeof (llvm::FixedPointSemantics ));
15201522 if (!this ->emitCastIntegralFixedPoint (classifyPrim (RHS->getType ()), I, E))
15211523 return false ;
15221524 }
15231525
1526+ // Convert the result to the target semantics.
1527+ auto ConvertResult = [&](bool R) -> bool {
1528+ if (!R)
1529+ return false ;
1530+ auto ResultSema = Ctx.getASTContext ().getFixedPointSemantics (E->getType ());
1531+ auto CommonSema = LHSSema.getCommonSemantics (RHSSema);
1532+ if (ResultSema != CommonSema) {
1533+ uint32_t I;
1534+ std::memcpy (&I, &ResultSema, sizeof (ResultSema));
1535+ return this ->emitCastFixedPoint (I, E);
1536+ }
1537+ return true ;
1538+ };
1539+
15241540 switch (E->getOpcode ()) {
15251541 case BO_EQ:
15261542 return this ->emitEQFixedPoint (E);
@@ -1537,7 +1553,7 @@ bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
15371553 return this->emitGEFixedPoint(E);
15381554#endif
15391555 case BO_Add:
1540- return this ->emitAddFixedPoint (E);
1556+ return ConvertResult ( this ->emitAddFixedPoint (E) );
15411557
15421558 default :
15431559 return this ->emitInvalid (E);
0 commit comments