Skip to content

Commit

Permalink
gen/ctfloat: make CTFloat big-endian aware
Browse files Browse the repository at this point in the history
  • Loading branch information
liushuyu committed Jan 9, 2025
1 parent f96db33 commit 496476a
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions gen/ctfloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ void CTFloat::toAPFloat(const real_t src, APFloat &dst) {
CTFloatUnion u;
u.fp = src;

#ifdef __FLOAT_WORD_ORDER
#if __FLOAT_WORD_ORDER == __ORDER_BIG_ENDIAN__
std::swap(u.bits[0], u.bits[1]);
#endif // __FLOAT_WORD_ORDER == __ORDER_BIG_ENDIAN__
#endif // __FLOAT_WORD_ORDER

const unsigned sizeInBits = APFloat::getSizeInBits(*apSemantics);
const APInt bits = APInt(sizeInBits, numUint64Parts, u.bits);

Expand All @@ -97,11 +103,20 @@ real_t CTFloat::fromAPFloat(const APFloat &src_) {
src.convert(*apSemantics, APFloat::rmNearestTiesToEven, &ignored);
}

#if LDC_LLVM_VER >= 2001 && defined(HAS_IEE754_FLOAT128)
return src.convertToQuad();
#else
const APInt bits = src.bitcastToAPInt();

CTFloatUnion u;
memcpy(u.bits, bits.getRawData(), bits.getBitWidth() / 8);
CTFloatUnion u{};
memcpy(u.bits, bits.getRawData(),
std::min(static_cast<size_t>(bits.getNumWords()) * 8, sizeof(u.bits)));
#ifdef __FLOAT_WORD_ORDER
#if __FLOAT_WORD_ORDER == __ORDER_BIG_ENDIAN__
std::swap(u.bits[0], u.bits[1]);
#endif // __FLOAT_WORD_ORDER == __ORDER_BIG_ENDIAN__
#endif // __FLOAT_WORD_ORDER
return u.fp;
#endif
}

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 496476a

Please sign in to comment.