diff --git a/lib/IR/Constants.cpp b/lib/IR/Constants.cpp index e87aa3d92ed..1c93265c79d 100644 --- a/lib/IR/Constants.cpp +++ b/lib/IR/Constants.cpp @@ -2715,18 +2715,15 @@ uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const { /// type, return the specified element as an APFloat. APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const { const char *EltPtr = getElementPointer(Elt); + auto EltVal = *reinterpret_cast(EltPtr); switch (getElementType()->getTypeID()) { default: llvm_unreachable("Accessor can only be used when element is float/double!"); - case Type::FloatTyID: { - const float *FloatPrt = reinterpret_cast(EltPtr); - return APFloat(*const_cast(FloatPrt)); - } - case Type::DoubleTyID: { - const double *DoublePtr = reinterpret_cast(EltPtr); - return APFloat(*const_cast(DoublePtr)); - } + case Type::FloatTyID: + return APFloat(APFloat::IEEEsingle, APInt(32, EltVal)); + case Type::DoubleTyID: + return APFloat(APFloat::IEEEdouble, APInt(64, EltVal)); } }