Skip to content

[Flang] Shift the data from lower to higher order bits in the big endian environment #73670

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 4 commits into from
Dec 28, 2023
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
32 changes: 22 additions & 10 deletions flang/include/flang/Common/uint128.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ template <bool IS_SIGNED = false> class Int128 {
constexpr Int128(unsigned n) : low_{n} {}
constexpr Int128(unsigned long n) : low_{n} {}
constexpr Int128(unsigned long long n) : low_{n} {}
constexpr Int128(int n)
: low_{static_cast<std::uint64_t>(n)}, high_{-static_cast<std::uint64_t>(
n < 0)} {}
constexpr Int128(long n)
: low_{static_cast<std::uint64_t>(n)}, high_{-static_cast<std::uint64_t>(
n < 0)} {}
constexpr Int128(long long n)
: low_{static_cast<std::uint64_t>(n)}, high_{-static_cast<std::uint64_t>(
n < 0)} {}
constexpr Int128(int n) {
low_ = static_cast<std::uint64_t>(n);
high_ = -static_cast<std::uint64_t>(n < 0);
}
constexpr Int128(long n) {
low_ = static_cast<std::uint64_t>(n);
high_ = -static_cast<std::uint64_t>(n < 0);
}
constexpr Int128(long long n) {
low_ = static_cast<std::uint64_t>(n);
high_ = -static_cast<std::uint64_t>(n < 0);
}
constexpr Int128(const Int128 &) = default;
constexpr Int128(Int128 &&) = default;
constexpr Int128 &operator=(const Int128 &) = default;
Expand Down Expand Up @@ -246,7 +249,10 @@ template <bool IS_SIGNED = false> class Int128 {
}

private:
constexpr Int128(std::uint64_t hi, std::uint64_t lo) : low_{lo}, high_{hi} {}
constexpr Int128(std::uint64_t hi, std::uint64_t lo) {
low_ = lo;
high_ = hi;
}
constexpr int LeadingZeroes() const {
if (high_ == 0) {
return 64 + LeadingZeroBitCount(low_);
Expand All @@ -255,7 +261,13 @@ template <bool IS_SIGNED = false> class Int128 {
}
}
static constexpr std::uint64_t topBit{std::uint64_t{1} << 63};
#if FLANG_LITTLE_ENDIAN
std::uint64_t low_{0}, high_{0};
#elif FLANG_BIG_ENDIAN
std::uint64_t high_{0}, low_{0};
#else
#error host endianness is not known
#endif
};

using UnsignedInt128 = Int128<false>;
Expand Down
11 changes: 10 additions & 1 deletion flang/runtime/edit-input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,16 @@ bool EditIntegerInput(
value = -value;
}
if (any || !io.GetConnectionState().IsAtEOF()) {
std::memcpy(n, &value, kind); // a blank field means zero
// The value is stored in the lower order bits on big endian platform.
// When memcpy, shift the value to the higher order bit.
auto shft{static_cast<int>(sizeof(value.low())) - kind};
// For kind==8 (i.e. shft==0), the value is stored in low_ in big endian.
if (!isHostLittleEndian && shft >= 0) {
auto l{value.low() << (8 * shft)};
std::memcpy(n, &l, kind);
} else {
std::memcpy(n, &value, kind); // a blank field means zero
}
}
return any;
}
Expand Down