File tree Expand file tree Collapse file tree 1 file changed +10
-2
lines changed
flang/include/flang/Evaluate Expand file tree Collapse file tree 1 file changed +10
-2
lines changed Original file line number Diff line number Diff line change @@ -150,7 +150,10 @@ class Integer {
150
150
}
151
151
}
152
152
} else {
153
- INT signExtension{-(n < 0 )};
153
+ // Avoid left shifts of negative signed values (that's an undefined
154
+ // behavior in C++).
155
+ auto signExtension{std::make_unsigned_t <INT>(n < 0 )};
156
+ signExtension = ~signExtension + 1 ;
154
157
static_assert (nBits >= partBits);
155
158
if constexpr (nBits > partBits) {
156
159
signExtension <<= nBits - partBits;
@@ -474,7 +477,12 @@ class Integer {
474
477
SINT n = ToUInt<UINT>();
475
478
constexpr std::size_t maxBits{CHAR_BIT * sizeof n};
476
479
if constexpr (bits < maxBits) {
477
- n |= -(n >> (bits - 1 )) << bits;
480
+ // Avoid left shifts of negative signed values (that's an undefined
481
+ // behavior in C++).
482
+ auto u{std::make_unsigned_t <SINT>(ToUInt ())};
483
+ u = (u >> (bits - 1 )) << (bits - 1 ); // Get the sign bit only.
484
+ u = ~u + 1 ; // Negate top bits if not 0.
485
+ n |= static_cast <SINT>(u);
478
486
}
479
487
return n;
480
488
}
You can’t perform that action at this time.
0 commit comments