Skip to content

Commit 496476a

Browse files
committed
gen/ctfloat: make CTFloat big-endian aware
1 parent f96db33 commit 496476a

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

gen/ctfloat.cpp

+18-3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ void CTFloat::toAPFloat(const real_t src, APFloat &dst) {
8282
CTFloatUnion u;
8383
u.fp = src;
8484

85+
#ifdef __FLOAT_WORD_ORDER
86+
#if __FLOAT_WORD_ORDER == __ORDER_BIG_ENDIAN__
87+
std::swap(u.bits[0], u.bits[1]);
88+
#endif // __FLOAT_WORD_ORDER == __ORDER_BIG_ENDIAN__
89+
#endif // __FLOAT_WORD_ORDER
90+
8591
const unsigned sizeInBits = APFloat::getSizeInBits(*apSemantics);
8692
const APInt bits = APInt(sizeInBits, numUint64Parts, u.bits);
8793

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

106+
#if LDC_LLVM_VER >= 2001 && defined(HAS_IEE754_FLOAT128)
107+
return src.convertToQuad();
108+
#else
100109
const APInt bits = src.bitcastToAPInt();
101-
102-
CTFloatUnion u;
103-
memcpy(u.bits, bits.getRawData(), bits.getBitWidth() / 8);
110+
CTFloatUnion u{};
111+
memcpy(u.bits, bits.getRawData(),
112+
std::min(static_cast<size_t>(bits.getNumWords()) * 8, sizeof(u.bits)));
113+
#ifdef __FLOAT_WORD_ORDER
114+
#if __FLOAT_WORD_ORDER == __ORDER_BIG_ENDIAN__
115+
std::swap(u.bits[0], u.bits[1]);
116+
#endif // __FLOAT_WORD_ORDER == __ORDER_BIG_ENDIAN__
117+
#endif // __FLOAT_WORD_ORDER
104118
return u.fp;
119+
#endif
105120
}
106121

107122
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)