Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4623,10 +4623,12 @@ struct IncDecSubobjectHandler {
if (Old) *Old = APValue(Value);

APFloat One(Value.getSemantics(), 1);
llvm::RoundingMode RM =
E->getFPFeaturesInEffect(Info.Ctx.getLangOpts()).getRoundingMode();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this use getActiveRoundingMode()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it should. Thank you!

if (AccessKind == AK_Increment)
Value.add(One, APFloat::rmNearestTiesToEven);
Value.add(One, RM);
else
Value.subtract(One, APFloat::rmNearestTiesToEven);
Value.subtract(One, RM);
return true;
}
bool foundPointer(APValue &Subobj, QualType SubobjType) {
Expand Down
21 changes: 21 additions & 0 deletions clang/test/SemaCXX/rounding-math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,24 @@ struct S1d {
int f;
};
static_assert(sizeof(S1d) == sizeof(int), "");

constexpr float incr_down(float k) {
float x = k;
++x;
return x;
}

// 0x1.0p23 = 8388608.0, inc(8388608.0) = 8388609.0
static_assert(incr_down(0x1.0p23F) == 0x1.000002p23F, "");
// 0x1.0p24 = 16777216.0, inc(16777216.0) = 16777217.0 -> round down -> 16777216.0
static_assert(incr_down(0x1.0p24F) == 0x1.0p24F, "");

#pragma STDC FENV_ROUND FE_UPWARD
constexpr float incr_up(float k) {
float x = k;
++x;
return x;
}
static_assert(incr_up(0x1.0p23F) == 0x1.000002p23F, "");
// 0x1.0p24 = 16777216.0, inc(16777216.0) = 16777217.0 -> round up -> 16777218.0
static_assert(incr_up(0x1.0p24F) == 0x1.000002p24F, "");