llvm.org GIT mirror llvm / f0dc104
[APInt] Use MathExtras.h BitsToFloat/Double and Float/DoubleToBits instead of type punning through a union The functions in MathExtras.h uses a safer memcpy instead of going through a union. Differential Revision: https://reviews.llvm.org/D33116 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302916 91177308-0d34-0410-b5e6-96231b3b80d8 Craig Topper 3 years ago
1 changed file(s) with 4 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
16561656 /// re-interprets the bits as a double. Note that it is valid to do this on
16571657 /// any bit width. Exactly 64 bits will be translated.
16581658 double bitsToDouble() const {
1659 union {
1660 uint64_t I;
1661 double D;
1662 } T;
1663 T.I = (isSingleWord() ? U.VAL : U.pVal[0]);
1664 return T.D;
1659 return BitsToDouble(getWord(0));
16651660 }
16661661
16671662 /// \brief Converts APInt bits to a double
16701665 /// re-interprets the bits as a float. Note that it is valid to do this on
16711666 /// any bit width. Exactly 32 bits will be translated.
16721667 float bitsToFloat() const {
1673 union {
1674 unsigned I;
1675 float F;
1676 } T;
1677 T.I = unsigned((isSingleWord() ? U.VAL : U.pVal[0]));
1678 return T.F;
1668 return BitsToFloat(getWord(0));
16791669 }
16801670
16811671 /// \brief Converts a double to APInt bits.
16831673 /// The conversion does not do a translation from double to integer, it just
16841674 /// re-interprets the bits of the double.
16851675 static APInt doubleToBits(double V) {
1686 union {
1687 uint64_t I;
1688 double D;
1689 } T;
1690 T.D = V;
1691 return APInt(sizeof T * CHAR_BIT, T.I);
1676 return APInt(sizeof(double) * CHAR_BIT, DoubleToBits(V));
16921677 }
16931678
16941679 /// \brief Converts a float to APInt bits.
16961681 /// The conversion does not do a translation from float to integer, it just
16971682 /// re-interprets the bits of the float.
16981683 static APInt floatToBits(float V) {
1699 union {
1700 unsigned I;
1701 float F;
1702 } T;
1703 T.F = V;
1704 return APInt(sizeof T * CHAR_BIT, T.I);
1684 return APInt(sizeof(float) * CHAR_BIT, FloatToBits(V));
17051685 }
17061686
17071687 /// @}