Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1 | //===-- APFloat.cpp - Implement APFloat class -----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a class to represent arbitrary precision floating |
| 11 | // point values and provide a variety of arithmetic operations on them. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | e525675 | 2007-12-08 19:00:03 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/APFloat.h" |
Jeffrey Yasskin | 03b81a2 | 2011-07-15 07:04:56 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/APSInt.h" |
Mehdi Amini | 47b292d | 2016-04-16 07:51:28 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/ArrayRef.h" |
Ted Kremenek | 6f30a07 | 2008-02-11 17:24:50 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/FoldingSet.h" |
Chandler Carruth | 71bd7d1 | 2012-03-04 12:02:57 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Hashing.h" |
Jordan Rose | e1f7658 | 2013-01-18 21:45:30 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 71bd7d1 | 2012-03-04 12:02:57 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringRef.h" |
Tim Shen | 44bde89 | 2016-12-12 21:59:30 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Dale Johannesen | 918c33c | 2007-08-24 05:08:11 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MathExtras.h" |
Tim Shen | 44bde89 | 2016-12-12 21:59:30 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 26 | #include <cstring> |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 27 | #include <limits.h> |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 28 | |
Tim Shen | 601ba8c | 2017-01-27 02:11:07 +0000 | [diff] [blame] | 29 | #define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \ |
| 30 | do { \ |
| 31 | if (usesLayout<IEEEFloat>(getSemantics())) \ |
| 32 | return U.IEEE.METHOD_CALL; \ |
| 33 | if (usesLayout<DoubleAPFloat>(getSemantics())) \ |
| 34 | return U.Double.METHOD_CALL; \ |
| 35 | llvm_unreachable("Unexpected semantics"); \ |
| 36 | } while (false) |
| 37 | |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Craig Topper | 55229b7 | 2017-04-02 19:17:22 +0000 | [diff] [blame] | 40 | // TODO: Remove these and use APInt qualified types directly. |
| 41 | typedef APInt::WordType integerPart; |
| 42 | const unsigned int integerPartWidth = APInt::APINT_BITS_PER_WORD; |
| 43 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 44 | /// A macro used to combine two fcCategory enums into one key which can be used |
| 45 | /// in a switch statement to classify how the interaction of two APFloat's |
| 46 | /// categories affects an operation. |
| 47 | /// |
| 48 | /// TODO: If clang source code is ever allowed to use constexpr in its own |
| 49 | /// codebase, change this into a static inline function. |
| 50 | #define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs)) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 51 | |
Neil Booth | 8f1946f | 2007-10-03 22:26:02 +0000 | [diff] [blame] | 52 | /* Assumed in hexadecimal significand parsing, and conversion to |
| 53 | hexadecimal strings. */ |
Benjamin Kramer | 7000ca3 | 2014-10-12 17:56:40 +0000 | [diff] [blame] | 54 | static_assert(integerPartWidth % 4 == 0, "Part width must be divisible by 4!"); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 55 | |
| 56 | namespace llvm { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 57 | /* Represents floating point arithmetic semantics. */ |
| 58 | struct fltSemantics { |
| 59 | /* The largest E such that 2^E is representable; this matches the |
| 60 | definition of IEEE 754. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 61 | APFloatBase::ExponentType maxExponent; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 62 | |
| 63 | /* The smallest E such that 2^E is a normalized number; this |
| 64 | matches the definition of IEEE 754. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 65 | APFloatBase::ExponentType minExponent; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 66 | |
| 67 | /* Number of bits in the significand. This includes the integer |
| 68 | bit. */ |
Neil Booth | 146fdb3 | 2007-10-12 15:33:27 +0000 | [diff] [blame] | 69 | unsigned int precision; |
Tamas Berghammer | b6b0ddf | 2015-07-09 10:13:39 +0000 | [diff] [blame] | 70 | |
| 71 | /* Number of bits actually used in the semantics. */ |
| 72 | unsigned int sizeInBits; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 75 | static const fltSemantics semIEEEhalf = {15, -14, 11, 16}; |
| 76 | static const fltSemantics semIEEEsingle = {127, -126, 24, 32}; |
| 77 | static const fltSemantics semIEEEdouble = {1023, -1022, 53, 64}; |
| 78 | static const fltSemantics semIEEEquad = {16383, -16382, 113, 128}; |
| 79 | static const fltSemantics semX87DoubleExtended = {16383, -16382, 64, 80}; |
| 80 | static const fltSemantics semBogus = {0, 0, 0, 0}; |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 81 | |
Tim Shen | 7117e69 | 2017-01-26 00:11:07 +0000 | [diff] [blame] | 82 | /* The IBM double-double semantics. Such a number consists of a pair of IEEE |
| 83 | 64-bit doubles (Hi, Lo), where |Hi| > |Lo|, and if normal, |
| 84 | (double)(Hi + Lo) == Hi. The numeric value it's modeling is Hi + Lo. |
| 85 | Therefore it has two 53-bit mantissa parts that aren't necessarily adjacent |
| 86 | to each other, and two 11-bit exponents. |
Michal Gorny | 21c1204 | 2017-01-03 16:33:50 +0000 | [diff] [blame] | 87 | |
| 88 | Note: we need to make the value different from semBogus as otherwise |
| 89 | an unsafe optimization may collapse both values to a single address, |
| 90 | and we heavily rely on them having distinct addresses. */ |
| 91 | static const fltSemantics semPPCDoubleDouble = {-1, 0, 0, 0}; |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 92 | |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 93 | /* These are legacy semantics for the fallback, inaccrurate implementation of |
| 94 | IBM double-double, if the accurate semPPCDoubleDouble doesn't handle the |
Tim Shen | 7117e69 | 2017-01-26 00:11:07 +0000 | [diff] [blame] | 95 | operation. It's equivalent to having an IEEE number with consecutive 106 |
| 96 | bits of mantissa and 11 bits of exponent. |
| 97 | |
| 98 | It's not equivalent to IBM double-double. For example, a legit IBM |
| 99 | double-double, 1 + epsilon: |
| 100 | |
| 101 | 1 + epsilon = 1 + (1 >> 1076) |
| 102 | |
| 103 | is not representable by a consecutive 106 bits of mantissa. |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 104 | |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 105 | Currently, these semantics are used in the following way: |
| 106 | |
Tim Shen | 7117e69 | 2017-01-26 00:11:07 +0000 | [diff] [blame] | 107 | semPPCDoubleDouble -> (IEEEdouble, IEEEdouble) -> |
| 108 | (64-bit APInt, 64-bit APInt) -> (128-bit APInt) -> |
| 109 | semPPCDoubleDoubleLegacy -> IEEE operations |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 110 | |
| 111 | We use bitcastToAPInt() to get the bit representation (in APInt) of the |
| 112 | underlying IEEEdouble, then use the APInt constructor to construct the |
| 113 | legacy IEEE float. |
| 114 | |
| 115 | TODO: Implement all operations in semPPCDoubleDouble, and delete these |
| 116 | semantics. */ |
| 117 | static const fltSemantics semPPCDoubleDoubleLegacy = {1023, -1022 + 53, |
| 118 | 53 + 53, 128}; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 119 | |
| 120 | const fltSemantics &APFloatBase::IEEEhalf() { |
| 121 | return semIEEEhalf; |
| 122 | } |
| 123 | const fltSemantics &APFloatBase::IEEEsingle() { |
| 124 | return semIEEEsingle; |
| 125 | } |
| 126 | const fltSemantics &APFloatBase::IEEEdouble() { |
| 127 | return semIEEEdouble; |
| 128 | } |
| 129 | const fltSemantics &APFloatBase::IEEEquad() { |
| 130 | return semIEEEquad; |
| 131 | } |
| 132 | const fltSemantics &APFloatBase::x87DoubleExtended() { |
| 133 | return semX87DoubleExtended; |
| 134 | } |
| 135 | const fltSemantics &APFloatBase::Bogus() { |
| 136 | return semBogus; |
| 137 | } |
| 138 | const fltSemantics &APFloatBase::PPCDoubleDouble() { |
| 139 | return semPPCDoubleDouble; |
| 140 | } |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 141 | |
| 142 | /* A tight upper bound on number of parts required to hold the value |
| 143 | pow(5, power) is |
| 144 | |
Neil Booth | 9130551 | 2007-10-15 15:00:55 +0000 | [diff] [blame] | 145 | power * 815 / (351 * integerPartWidth) + 1 |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 146 | |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 147 | However, whilst the result may require only this many parts, |
| 148 | because we are multiplying two values to get it, the |
| 149 | multiplication may require an extra part with the excess part |
| 150 | being zero (consider the trivial case of 1 * 1, tcFullMultiply |
| 151 | requires two parts to hold the single-part result). So we add an |
| 152 | extra one to guarantee enough space whilst multiplying. */ |
| 153 | const unsigned int maxExponent = 16383; |
| 154 | const unsigned int maxPrecision = 113; |
| 155 | const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1; |
Neil Booth | 9130551 | 2007-10-15 15:00:55 +0000 | [diff] [blame] | 156 | const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815) |
| 157 | / (351 * integerPartWidth)); |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 158 | |
| 159 | unsigned int APFloatBase::semanticsPrecision(const fltSemantics &semantics) { |
| 160 | return semantics.precision; |
| 161 | } |
| 162 | APFloatBase::ExponentType |
| 163 | APFloatBase::semanticsMaxExponent(const fltSemantics &semantics) { |
| 164 | return semantics.maxExponent; |
| 165 | } |
| 166 | APFloatBase::ExponentType |
| 167 | APFloatBase::semanticsMinExponent(const fltSemantics &semantics) { |
| 168 | return semantics.minExponent; |
| 169 | } |
| 170 | unsigned int APFloatBase::semanticsSizeInBits(const fltSemantics &semantics) { |
| 171 | return semantics.sizeInBits; |
| 172 | } |
| 173 | |
| 174 | unsigned APFloatBase::getSizeInBits(const fltSemantics &Sem) { |
| 175 | return Sem.sizeInBits; |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 176 | } |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 178 | /* A bunch of private, handy routines. */ |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 179 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 180 | static inline unsigned int |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 181 | partCountForBits(unsigned int bits) |
| 182 | { |
| 183 | return ((bits) + integerPartWidth - 1) / integerPartWidth; |
| 184 | } |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 185 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 186 | /* Returns 0U-9U. Return values >= 10U are not digits. */ |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 187 | static inline unsigned int |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 188 | decDigitValue(unsigned int c) |
| 189 | { |
| 190 | return c - '0'; |
| 191 | } |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 193 | /* Return the value of a decimal exponent of the form |
| 194 | [+-]ddddddd. |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 196 | If the exponent overflows, returns a large exponent with the |
| 197 | appropriate sign. */ |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 198 | static int |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 199 | readExponent(StringRef::iterator begin, StringRef::iterator end) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 200 | { |
| 201 | bool isNegative; |
| 202 | unsigned int absExponent; |
| 203 | const unsigned int overlargeExponent = 24000; /* FIXME. */ |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 204 | StringRef::iterator p = begin; |
| 205 | |
| 206 | assert(p != end && "Exponent has no digits"); |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 208 | isNegative = (*p == '-'); |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 209 | if (*p == '-' || *p == '+') { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 210 | p++; |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 211 | assert(p != end && "Exponent has no digits"); |
| 212 | } |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 214 | absExponent = decDigitValue(*p++); |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 215 | assert(absExponent < 10U && "Invalid character in exponent"); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 216 | |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 217 | for (; p != end; ++p) { |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 218 | unsigned int value; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 220 | value = decDigitValue(*p); |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 221 | assert(value < 10U && "Invalid character in exponent"); |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 222 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 223 | value += absExponent * 10; |
| 224 | if (absExponent >= overlargeExponent) { |
| 225 | absExponent = overlargeExponent; |
Dale Johannesen | 370c77c | 2010-08-19 17:58:35 +0000 | [diff] [blame] | 226 | p = end; /* outwit assert below */ |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 227 | break; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 228 | } |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 229 | absExponent = value; |
| 230 | } |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 231 | |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 232 | assert(p == end && "Invalid exponent in exponent"); |
| 233 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 234 | if (isNegative) |
| 235 | return -(int) absExponent; |
| 236 | else |
| 237 | return (int) absExponent; |
| 238 | } |
| 239 | |
| 240 | /* This is ugly and needs cleaning up, but I don't immediately see |
| 241 | how whilst remaining safe. */ |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 242 | static int |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 243 | totalExponent(StringRef::iterator p, StringRef::iterator end, |
| 244 | int exponentAdjustment) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 245 | { |
| 246 | int unsignedExponent; |
| 247 | bool negative, overflow; |
Ted Kremenek | 3c4408c | 2011-01-23 17:05:06 +0000 | [diff] [blame] | 248 | int exponent = 0; |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 249 | |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 250 | assert(p != end && "Exponent has no digits"); |
| 251 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 252 | negative = *p == '-'; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 253 | if (*p == '-' || *p == '+') { |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 254 | p++; |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 255 | assert(p != end && "Exponent has no digits"); |
| 256 | } |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 257 | |
| 258 | unsignedExponent = 0; |
| 259 | overflow = false; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 260 | for (; p != end; ++p) { |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 261 | unsigned int value; |
| 262 | |
| 263 | value = decDigitValue(*p); |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 264 | assert(value < 10U && "Invalid character in exponent"); |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 266 | unsignedExponent = unsignedExponent * 10 + value; |
Richard Smith | 156d920 | 2012-08-24 00:01:19 +0000 | [diff] [blame] | 267 | if (unsignedExponent > 32767) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 268 | overflow = true; |
Richard Smith | 156d920 | 2012-08-24 00:01:19 +0000 | [diff] [blame] | 269 | break; |
| 270 | } |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Abramo Bagnara | a41d7ae | 2011-01-06 16:55:14 +0000 | [diff] [blame] | 273 | if (exponentAdjustment > 32767 || exponentAdjustment < -32768) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 274 | overflow = true; |
| 275 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 276 | if (!overflow) { |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 277 | exponent = unsignedExponent; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 278 | if (negative) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 279 | exponent = -exponent; |
| 280 | exponent += exponentAdjustment; |
Abramo Bagnara | a41d7ae | 2011-01-06 16:55:14 +0000 | [diff] [blame] | 281 | if (exponent > 32767 || exponent < -32768) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 282 | overflow = true; |
| 283 | } |
| 284 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 285 | if (overflow) |
Abramo Bagnara | a41d7ae | 2011-01-06 16:55:14 +0000 | [diff] [blame] | 286 | exponent = negative ? -32768: 32767; |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 287 | |
| 288 | return exponent; |
| 289 | } |
| 290 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 291 | static StringRef::iterator |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 292 | skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end, |
| 293 | StringRef::iterator *dot) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 294 | { |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 295 | StringRef::iterator p = begin; |
| 296 | *dot = end; |
Nick Lewycky | 095b92e | 2014-09-06 01:16:42 +0000 | [diff] [blame] | 297 | while (p != end && *p == '0') |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 298 | p++; |
| 299 | |
Nick Lewycky | 095b92e | 2014-09-06 01:16:42 +0000 | [diff] [blame] | 300 | if (p != end && *p == '.') { |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 301 | *dot = p++; |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 302 | |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 303 | assert(end - begin != 1 && "Significand has no digits"); |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 304 | |
Nick Lewycky | 095b92e | 2014-09-06 01:16:42 +0000 | [diff] [blame] | 305 | while (p != end && *p == '0') |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 306 | p++; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 309 | return p; |
| 310 | } |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 312 | /* Given a normal decimal floating point number of the form |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 313 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 314 | dddd.dddd[eE][+-]ddd |
Neil Booth | 9130551 | 2007-10-15 15:00:55 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 316 | where the decimal point and exponent are optional, fill out the |
| 317 | structure D. Exponent is appropriate if the significand is |
| 318 | treated as an integer, and normalizedExponent if the significand |
| 319 | is taken to have the decimal point after a single leading |
| 320 | non-zero digit. |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 322 | If the value is zero, V->firstSigDigit points to a non-digit, and |
| 323 | the return exponent is zero. |
| 324 | */ |
| 325 | struct decimalInfo { |
| 326 | const char *firstSigDigit; |
| 327 | const char *lastSigDigit; |
| 328 | int exponent; |
| 329 | int normalizedExponent; |
| 330 | }; |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 331 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 332 | static void |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 333 | interpretDecimal(StringRef::iterator begin, StringRef::iterator end, |
| 334 | decimalInfo *D) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 335 | { |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 336 | StringRef::iterator dot = end; |
| 337 | StringRef::iterator p = skipLeadingZeroesAndAnyDot (begin, end, &dot); |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 338 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 339 | D->firstSigDigit = p; |
| 340 | D->exponent = 0; |
| 341 | D->normalizedExponent = 0; |
| 342 | |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 343 | for (; p != end; ++p) { |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 344 | if (*p == '.') { |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 345 | assert(dot == end && "String contains multiple dots"); |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 346 | dot = p++; |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 347 | if (p == end) |
| 348 | break; |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 349 | } |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 350 | if (decDigitValue(*p) >= 10U) |
| 351 | break; |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 352 | } |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 353 | |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 354 | if (p != end) { |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 355 | assert((*p == 'e' || *p == 'E') && "Invalid character in significand"); |
| 356 | assert(p != begin && "Significand has no digits"); |
| 357 | assert((dot == end || p - begin != 1) && "Significand has no digits"); |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 358 | |
| 359 | /* p points to the first non-digit in the string */ |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 360 | D->exponent = readExponent(p + 1, end); |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 361 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 362 | /* Implied decimal point? */ |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 363 | if (dot == end) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 364 | dot = p; |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 365 | } |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 366 | |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 367 | /* If number is all zeroes accept any exponent. */ |
| 368 | if (p != D->firstSigDigit) { |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 369 | /* Drop insignificant trailing zeroes. */ |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 370 | if (p != begin) { |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 371 | do |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 372 | do |
| 373 | p--; |
| 374 | while (p != begin && *p == '0'); |
| 375 | while (p != begin && *p == '.'); |
| 376 | } |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 377 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 378 | /* Adjust the exponents for any decimal point. */ |
Michael Gottesman | 9dc9833 | 2013-06-24 04:06:23 +0000 | [diff] [blame] | 379 | D->exponent += static_cast<APFloat::ExponentType>((dot - p) - (dot > p)); |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 380 | D->normalizedExponent = (D->exponent + |
Michael Gottesman | 9dc9833 | 2013-06-24 04:06:23 +0000 | [diff] [blame] | 381 | static_cast<APFloat::ExponentType>((p - D->firstSigDigit) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 382 | - (dot > D->firstSigDigit && dot < p))); |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 385 | D->lastSigDigit = p; |
| 386 | } |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 387 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 388 | /* Return the trailing fraction of a hexadecimal number. |
| 389 | DIGITVALUE is the first hex digit of the fraction, P points to |
| 390 | the next digit. */ |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 391 | static lostFraction |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 392 | trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end, |
| 393 | unsigned int digitValue) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 394 | { |
| 395 | unsigned int hexDigit; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 396 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 397 | /* If the first trailing digit isn't 0 or 8 we can work out the |
| 398 | fraction immediately. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 399 | if (digitValue > 8) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 400 | return lfMoreThanHalf; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 401 | else if (digitValue < 8 && digitValue > 0) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 402 | return lfLessThanHalf; |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 403 | |
Eli Friedman | d2eb07a | 2013-07-17 22:17:29 +0000 | [diff] [blame] | 404 | // Otherwise we need to find the first non-zero digit. |
| 405 | while (p != end && (*p == '0' || *p == '.')) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 406 | p++; |
| 407 | |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 408 | assert(p != end && "Invalid trailing hexadecimal fraction!"); |
| 409 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 410 | hexDigit = hexDigitValue(*p); |
| 411 | |
| 412 | /* If we ran off the end it is exactly zero or one-half, otherwise |
| 413 | a little more. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 414 | if (hexDigit == -1U) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 415 | return digitValue == 0 ? lfExactlyZero: lfExactlyHalf; |
| 416 | else |
| 417 | return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf; |
| 418 | } |
| 419 | |
| 420 | /* Return the fraction lost were a bignum truncated losing the least |
| 421 | significant BITS bits. */ |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 422 | static lostFraction |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 423 | lostFractionThroughTruncation(const integerPart *parts, |
| 424 | unsigned int partCount, |
| 425 | unsigned int bits) |
| 426 | { |
| 427 | unsigned int lsb; |
| 428 | |
| 429 | lsb = APInt::tcLSB(parts, partCount); |
| 430 | |
| 431 | /* Note this is guaranteed true if bits == 0, or LSB == -1U. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 432 | if (bits <= lsb) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 433 | return lfExactlyZero; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 434 | if (bits == lsb + 1) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 435 | return lfExactlyHalf; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 436 | if (bits <= partCount * integerPartWidth && |
| 437 | APInt::tcExtractBit(parts, bits - 1)) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 438 | return lfMoreThanHalf; |
| 439 | |
| 440 | return lfLessThanHalf; |
| 441 | } |
| 442 | |
| 443 | /* Shift DST right BITS bits noting lost fraction. */ |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 444 | static lostFraction |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 445 | shiftRight(integerPart *dst, unsigned int parts, unsigned int bits) |
| 446 | { |
| 447 | lostFraction lost_fraction; |
| 448 | |
| 449 | lost_fraction = lostFractionThroughTruncation(dst, parts, bits); |
| 450 | |
| 451 | APInt::tcShiftRight(dst, parts, bits); |
| 452 | |
| 453 | return lost_fraction; |
| 454 | } |
| 455 | |
| 456 | /* Combine the effect of two lost fractions. */ |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 457 | static lostFraction |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 458 | combineLostFractions(lostFraction moreSignificant, |
| 459 | lostFraction lessSignificant) |
| 460 | { |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 461 | if (lessSignificant != lfExactlyZero) { |
| 462 | if (moreSignificant == lfExactlyZero) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 463 | moreSignificant = lfLessThanHalf; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 464 | else if (moreSignificant == lfExactlyHalf) |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 465 | moreSignificant = lfMoreThanHalf; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 468 | return moreSignificant; |
| 469 | } |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 470 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 471 | /* The error from the true value, in half-ulps, on multiplying two |
| 472 | floating point numbers, which differ from the value they |
| 473 | approximate by at most HUE1 and HUE2 half-ulps, is strictly less |
| 474 | than the returned value. |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 475 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 476 | See "How to Read Floating Point Numbers Accurately" by William D |
| 477 | Clinger. */ |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 478 | static unsigned int |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 479 | HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2) |
| 480 | { |
| 481 | assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8)); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 482 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 483 | if (HUerr1 + HUerr2 == 0) |
| 484 | return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */ |
| 485 | else |
| 486 | return inexactMultiply + 2 * (HUerr1 + HUerr2); |
| 487 | } |
Neil Booth | 8f1946f | 2007-10-03 22:26:02 +0000 | [diff] [blame] | 488 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 489 | /* The number of ulps from the boundary (zero, or half if ISNEAREST) |
| 490 | when the least significant BITS are truncated. BITS cannot be |
| 491 | zero. */ |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 492 | static integerPart |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 493 | ulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest) |
| 494 | { |
| 495 | unsigned int count, partBits; |
| 496 | integerPart part, boundary; |
Neil Booth | d398592 | 2007-10-07 08:51:21 +0000 | [diff] [blame] | 497 | |
Evan Cheng | 67c9021 | 2009-10-27 21:35:42 +0000 | [diff] [blame] | 498 | assert(bits != 0); |
Neil Booth | 8f1946f | 2007-10-03 22:26:02 +0000 | [diff] [blame] | 499 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 500 | bits--; |
| 501 | count = bits / integerPartWidth; |
| 502 | partBits = bits % integerPartWidth + 1; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 503 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 504 | part = parts[count] & (~(integerPart) 0 >> (integerPartWidth - partBits)); |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 505 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 506 | if (isNearest) |
| 507 | boundary = (integerPart) 1 << (partBits - 1); |
| 508 | else |
| 509 | boundary = 0; |
| 510 | |
| 511 | if (count == 0) { |
| 512 | if (part - boundary <= boundary - part) |
| 513 | return part - boundary; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 514 | else |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 515 | return boundary - part; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 518 | if (part == boundary) { |
| 519 | while (--count) |
| 520 | if (parts[count]) |
| 521 | return ~(integerPart) 0; /* A lot. */ |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 522 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 523 | return parts[0]; |
| 524 | } else if (part == boundary - 1) { |
| 525 | while (--count) |
| 526 | if (~parts[count]) |
| 527 | return ~(integerPart) 0; /* A lot. */ |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 528 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 529 | return -parts[0]; |
| 530 | } |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 531 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 532 | return ~(integerPart) 0; /* A lot. */ |
| 533 | } |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 534 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 535 | /* Place pow(5, power) in DST, and return the number of parts used. |
| 536 | DST must be at least one part larger than size of the answer. */ |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 537 | static unsigned int |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 538 | powerOf5(integerPart *dst, unsigned int power) |
| 539 | { |
| 540 | static const integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125, |
| 541 | 15625, 78125 }; |
Chris Lattner | b858c0e | 2009-03-13 00:24:01 +0000 | [diff] [blame] | 542 | integerPart pow5s[maxPowerOfFiveParts * 2 + 5]; |
| 543 | pow5s[0] = 78125 * 5; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 544 | |
Chris Lattner | 0bf1869 | 2009-03-13 00:03:51 +0000 | [diff] [blame] | 545 | unsigned int partsCount[16] = { 1 }; |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 546 | integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5; |
| 547 | unsigned int result; |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 548 | assert(power <= maxExponent); |
| 549 | |
| 550 | p1 = dst; |
| 551 | p2 = scratch; |
| 552 | |
| 553 | *p1 = firstEightPowers[power & 7]; |
| 554 | power >>= 3; |
| 555 | |
| 556 | result = 1; |
| 557 | pow5 = pow5s; |
| 558 | |
| 559 | for (unsigned int n = 0; power; power >>= 1, n++) { |
| 560 | unsigned int pc; |
| 561 | |
| 562 | pc = partsCount[n]; |
| 563 | |
| 564 | /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */ |
| 565 | if (pc == 0) { |
| 566 | pc = partsCount[n - 1]; |
| 567 | APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc); |
| 568 | pc *= 2; |
| 569 | if (pow5[pc - 1] == 0) |
| 570 | pc--; |
| 571 | partsCount[n] = pc; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 574 | if (power & 1) { |
| 575 | integerPart *tmp; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 576 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 577 | APInt::tcFullMultiply(p2, p1, pow5, result, pc); |
| 578 | result += pc; |
| 579 | if (p2[result - 1] == 0) |
| 580 | result--; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 581 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 582 | /* Now result is in p1 with partsCount parts and p2 is scratch |
| 583 | space. */ |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 584 | tmp = p1; |
| 585 | p1 = p2; |
| 586 | p2 = tmp; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 589 | pow5 += pc; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 592 | if (p1 != dst) |
| 593 | APInt::tcAssign(dst, p1, result); |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 594 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 595 | return result; |
| 596 | } |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 598 | /* Zero at the end to avoid modular arithmetic when adding one; used |
| 599 | when rounding up during hexadecimal output. */ |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 600 | static const char hexDigitsLower[] = "0123456789abcdef0"; |
| 601 | static const char hexDigitsUpper[] = "0123456789ABCDEF0"; |
| 602 | static const char infinityL[] = "infinity"; |
| 603 | static const char infinityU[] = "INFINITY"; |
| 604 | static const char NaNL[] = "nan"; |
| 605 | static const char NaNU[] = "NAN"; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 606 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 607 | /* Write out an integerPart in hexadecimal, starting with the most |
| 608 | significant nibble. Write out exactly COUNT hexdigits, return |
| 609 | COUNT. */ |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 610 | static unsigned int |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 611 | partAsHex (char *dst, integerPart part, unsigned int count, |
| 612 | const char *hexDigitChars) |
| 613 | { |
| 614 | unsigned int result = count; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 615 | |
Evan Cheng | 67c9021 | 2009-10-27 21:35:42 +0000 | [diff] [blame] | 616 | assert(count != 0 && count <= integerPartWidth / 4); |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 617 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 618 | part >>= (integerPartWidth - 4 * count); |
| 619 | while (count--) { |
| 620 | dst[count] = hexDigitChars[part & 0xf]; |
| 621 | part >>= 4; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 624 | return result; |
| 625 | } |
Neil Booth | 8f1946f | 2007-10-03 22:26:02 +0000 | [diff] [blame] | 626 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 627 | /* Write out an unsigned decimal integer. */ |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 628 | static char * |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 629 | writeUnsignedDecimal (char *dst, unsigned int n) |
| 630 | { |
| 631 | char buff[40], *p; |
Neil Booth | 8f1946f | 2007-10-03 22:26:02 +0000 | [diff] [blame] | 632 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 633 | p = buff; |
| 634 | do |
| 635 | *p++ = '0' + n % 10; |
| 636 | while (n /= 10); |
Neil Booth | 8f1946f | 2007-10-03 22:26:02 +0000 | [diff] [blame] | 637 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 638 | do |
| 639 | *dst++ = *--p; |
| 640 | while (p != buff); |
Neil Booth | 8f1946f | 2007-10-03 22:26:02 +0000 | [diff] [blame] | 641 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 642 | return dst; |
| 643 | } |
Neil Booth | 8f1946f | 2007-10-03 22:26:02 +0000 | [diff] [blame] | 644 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 645 | /* Write out a signed decimal integer. */ |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 646 | static char * |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 647 | writeSignedDecimal (char *dst, int value) |
| 648 | { |
| 649 | if (value < 0) { |
| 650 | *dst++ = '-'; |
| 651 | dst = writeUnsignedDecimal(dst, -(unsigned) value); |
| 652 | } else |
| 653 | dst = writeUnsignedDecimal(dst, value); |
Neil Booth | 8f1946f | 2007-10-03 22:26:02 +0000 | [diff] [blame] | 654 | |
Chris Lattner | 9170209 | 2009-03-12 23:59:55 +0000 | [diff] [blame] | 655 | return dst; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 658 | namespace detail { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 659 | /* Constructors. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 660 | void IEEEFloat::initialize(const fltSemantics *ourSemantics) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 661 | unsigned int count; |
| 662 | |
| 663 | semantics = ourSemantics; |
| 664 | count = partCount(); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 665 | if (count > 1) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 666 | significand.parts = new integerPart[count]; |
| 667 | } |
| 668 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 669 | void IEEEFloat::freeSignificand() { |
Manuel Klimek | d0cf5b2 | 2013-06-03 13:03:05 +0000 | [diff] [blame] | 670 | if (needsCleanup()) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 671 | delete [] significand.parts; |
| 672 | } |
| 673 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 674 | void IEEEFloat::assign(const IEEEFloat &rhs) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 675 | assert(semantics == rhs.semantics); |
| 676 | |
| 677 | sign = rhs.sign; |
| 678 | category = rhs.category; |
| 679 | exponent = rhs.exponent; |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 680 | if (isFiniteNonZero() || category == fcNaN) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 681 | copySignificand(rhs); |
| 682 | } |
| 683 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 684 | void IEEEFloat::copySignificand(const IEEEFloat &rhs) { |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 685 | assert(isFiniteNonZero() || category == fcNaN); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 686 | assert(rhs.partCount() >= partCount()); |
| 687 | |
| 688 | APInt::tcAssign(significandParts(), rhs.significandParts(), |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 689 | partCount()); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Neil Booth | 5fe658b | 2007-10-14 10:39:51 +0000 | [diff] [blame] | 692 | /* Make this number a NaN, with an arbitrary but deterministic value |
Dale Johannesen | 1f86498 | 2009-01-21 20:32:55 +0000 | [diff] [blame] | 693 | for the significand. If double or longer, this is a signalling NaN, |
Mike Stump | 799bf58 | 2009-05-30 03:49:43 +0000 | [diff] [blame] | 694 | which may not be ideal. If float, this is QNaN(0). */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 695 | void IEEEFloat::makeNaN(bool SNaN, bool Negative, const APInt *fill) { |
Neil Booth | 5fe658b | 2007-10-14 10:39:51 +0000 | [diff] [blame] | 696 | category = fcNaN; |
John McCall | dcb9a7a | 2010-02-28 02:51:25 +0000 | [diff] [blame] | 697 | sign = Negative; |
| 698 | |
John McCall | c12b133 | 2010-02-28 12:49:50 +0000 | [diff] [blame] | 699 | integerPart *significand = significandParts(); |
| 700 | unsigned numParts = partCount(); |
| 701 | |
John McCall | dcb9a7a | 2010-02-28 02:51:25 +0000 | [diff] [blame] | 702 | // Set the significand bits to the fill. |
John McCall | c12b133 | 2010-02-28 12:49:50 +0000 | [diff] [blame] | 703 | if (!fill || fill->getNumWords() < numParts) |
| 704 | APInt::tcSet(significand, 0, numParts); |
| 705 | if (fill) { |
John McCall | c6dbe30 | 2010-03-01 18:38:45 +0000 | [diff] [blame] | 706 | APInt::tcAssign(significand, fill->getRawData(), |
| 707 | std::min(fill->getNumWords(), numParts)); |
John McCall | c12b133 | 2010-02-28 12:49:50 +0000 | [diff] [blame] | 708 | |
| 709 | // Zero out the excess bits of the significand. |
| 710 | unsigned bitsToPreserve = semantics->precision - 1; |
| 711 | unsigned part = bitsToPreserve / 64; |
| 712 | bitsToPreserve %= 64; |
| 713 | significand[part] &= ((1ULL << bitsToPreserve) - 1); |
| 714 | for (part++; part != numParts; ++part) |
| 715 | significand[part] = 0; |
| 716 | } |
| 717 | |
| 718 | unsigned QNaNBit = semantics->precision - 2; |
John McCall | dcb9a7a | 2010-02-28 02:51:25 +0000 | [diff] [blame] | 719 | |
| 720 | if (SNaN) { |
| 721 | // We always have to clear the QNaN bit to make it an SNaN. |
John McCall | c12b133 | 2010-02-28 12:49:50 +0000 | [diff] [blame] | 722 | APInt::tcClearBit(significand, QNaNBit); |
John McCall | dcb9a7a | 2010-02-28 02:51:25 +0000 | [diff] [blame] | 723 | |
| 724 | // If there are no bits set in the payload, we have to set |
| 725 | // *something* to make it a NaN instead of an infinity; |
| 726 | // conventionally, this is the next bit down from the QNaN bit. |
John McCall | c12b133 | 2010-02-28 12:49:50 +0000 | [diff] [blame] | 727 | if (APInt::tcIsZero(significand, numParts)) |
| 728 | APInt::tcSetBit(significand, QNaNBit - 1); |
John McCall | dcb9a7a | 2010-02-28 02:51:25 +0000 | [diff] [blame] | 729 | } else { |
| 730 | // We always have to set the QNaN bit to make it a QNaN. |
John McCall | c12b133 | 2010-02-28 12:49:50 +0000 | [diff] [blame] | 731 | APInt::tcSetBit(significand, QNaNBit); |
John McCall | dcb9a7a | 2010-02-28 02:51:25 +0000 | [diff] [blame] | 732 | } |
John McCall | c12b133 | 2010-02-28 12:49:50 +0000 | [diff] [blame] | 733 | |
| 734 | // For x87 extended precision, we want to make a NaN, not a |
| 735 | // pseudo-NaN. Maybe we should expose the ability to make |
| 736 | // pseudo-NaNs? |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 737 | if (semantics == &semX87DoubleExtended) |
John McCall | c12b133 | 2010-02-28 12:49:50 +0000 | [diff] [blame] | 738 | APInt::tcSetBit(significand, QNaNBit + 1); |
John McCall | dcb9a7a | 2010-02-28 02:51:25 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 741 | IEEEFloat &IEEEFloat::operator=(const IEEEFloat &rhs) { |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 742 | if (this != &rhs) { |
| 743 | if (semantics != rhs.semantics) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 744 | freeSignificand(); |
| 745 | initialize(rhs.semantics); |
| 746 | } |
| 747 | assign(rhs); |
| 748 | } |
| 749 | |
| 750 | return *this; |
| 751 | } |
| 752 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 753 | IEEEFloat &IEEEFloat::operator=(IEEEFloat &&rhs) { |
Benjamin Kramer | 06f4778 | 2014-03-04 20:26:51 +0000 | [diff] [blame] | 754 | freeSignificand(); |
| 755 | |
| 756 | semantics = rhs.semantics; |
| 757 | significand = rhs.significand; |
| 758 | exponent = rhs.exponent; |
| 759 | category = rhs.category; |
| 760 | sign = rhs.sign; |
| 761 | |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 762 | rhs.semantics = &semBogus; |
Benjamin Kramer | 06f4778 | 2014-03-04 20:26:51 +0000 | [diff] [blame] | 763 | return *this; |
| 764 | } |
| 765 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 766 | bool IEEEFloat::isDenormal() const { |
Michael Gottesman | 3cb77ab | 2013-06-19 21:23:18 +0000 | [diff] [blame] | 767 | return isFiniteNonZero() && (exponent == semantics->minExponent) && |
Simon Pilgrim | a284959 | 2017-03-20 13:53:59 +0000 | [diff] [blame] | 768 | (APInt::tcExtractBit(significandParts(), |
Shuxin Yang | 4fb504f | 2013-01-07 18:59:35 +0000 | [diff] [blame] | 769 | semantics->precision - 1) == 0); |
| 770 | } |
| 771 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 772 | bool IEEEFloat::isSmallest() const { |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 773 | // The smallest number by magnitude in our format will be the smallest |
Michael Gottesman | a7cc124 | 2013-06-19 07:34:21 +0000 | [diff] [blame] | 774 | // denormal, i.e. the floating point number with exponent being minimum |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 775 | // exponent and significand bitwise equal to 1 (i.e. with MSB equal to 0). |
Michael Gottesman | 3cb77ab | 2013-06-19 21:23:18 +0000 | [diff] [blame] | 776 | return isFiniteNonZero() && exponent == semantics->minExponent && |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 777 | significandMSB() == 0; |
| 778 | } |
| 779 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 780 | bool IEEEFloat::isSignificandAllOnes() const { |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 781 | // Test if the significand excluding the integral bit is all ones. This allows |
| 782 | // us to test for binade boundaries. |
| 783 | const integerPart *Parts = significandParts(); |
| 784 | const unsigned PartCount = partCount(); |
| 785 | for (unsigned i = 0; i < PartCount - 1; i++) |
| 786 | if (~Parts[i]) |
| 787 | return false; |
| 788 | |
| 789 | // Set the unused high bits to all ones when we compare. |
| 790 | const unsigned NumHighBits = |
| 791 | PartCount*integerPartWidth - semantics->precision + 1; |
| 792 | assert(NumHighBits <= integerPartWidth && "Can not have more high bits to " |
| 793 | "fill than integerPartWidth"); |
| 794 | const integerPart HighBitFill = |
| 795 | ~integerPart(0) << (integerPartWidth - NumHighBits); |
| 796 | if (~(Parts[PartCount - 1] | HighBitFill)) |
| 797 | return false; |
| 798 | |
| 799 | return true; |
| 800 | } |
| 801 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 802 | bool IEEEFloat::isSignificandAllZeros() const { |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 803 | // Test if the significand excluding the integral bit is all zeros. This |
| 804 | // allows us to test for binade boundaries. |
| 805 | const integerPart *Parts = significandParts(); |
| 806 | const unsigned PartCount = partCount(); |
| 807 | |
| 808 | for (unsigned i = 0; i < PartCount - 1; i++) |
| 809 | if (Parts[i]) |
| 810 | return false; |
| 811 | |
| 812 | const unsigned NumHighBits = |
| 813 | PartCount*integerPartWidth - semantics->precision + 1; |
| 814 | assert(NumHighBits <= integerPartWidth && "Can not have more high bits to " |
| 815 | "clear than integerPartWidth"); |
| 816 | const integerPart HighBitMask = ~integerPart(0) >> NumHighBits; |
| 817 | |
| 818 | if (Parts[PartCount - 1] & HighBitMask) |
| 819 | return false; |
| 820 | |
| 821 | return true; |
| 822 | } |
| 823 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 824 | bool IEEEFloat::isLargest() const { |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 825 | // The largest number by magnitude in our format will be the floating point |
| 826 | // number with maximum exponent and with significand that is all ones. |
Michael Gottesman | 3cb77ab | 2013-06-19 21:23:18 +0000 | [diff] [blame] | 827 | return isFiniteNonZero() && exponent == semantics->maxExponent |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 828 | && isSignificandAllOnes(); |
| 829 | } |
| 830 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 831 | bool IEEEFloat::isInteger() const { |
Stephen Canon | 1bfc89b | 2015-11-16 21:52:48 +0000 | [diff] [blame] | 832 | // This could be made more efficient; I'm going for obviously correct. |
| 833 | if (!isFinite()) return false; |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 834 | IEEEFloat truncated = *this; |
Stephen Canon | 1bfc89b | 2015-11-16 21:52:48 +0000 | [diff] [blame] | 835 | truncated.roundToIntegral(rmTowardZero); |
| 836 | return compare(truncated) == cmpEqual; |
| 837 | } |
| 838 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 839 | bool IEEEFloat::bitwiseIsEqual(const IEEEFloat &rhs) const { |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 840 | if (this == &rhs) |
| 841 | return true; |
| 842 | if (semantics != rhs.semantics || |
Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 843 | category != rhs.category || |
| 844 | sign != rhs.sign) |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 845 | return false; |
Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 846 | if (category==fcZero || category==fcInfinity) |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 847 | return true; |
Benjamin Kramer | 103fc94 | 2015-08-21 16:44:52 +0000 | [diff] [blame] | 848 | |
| 849 | if (isFiniteNonZero() && exponent != rhs.exponent) |
Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 850 | return false; |
Benjamin Kramer | 103fc94 | 2015-08-21 16:44:52 +0000 | [diff] [blame] | 851 | |
| 852 | return std::equal(significandParts(), significandParts() + partCount(), |
| 853 | rhs.significandParts()); |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 856 | IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, integerPart value) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 857 | initialize(&ourSemantics); |
| 858 | sign = 0; |
Michael Gottesman | 30a90eb | 2013-07-27 21:49:21 +0000 | [diff] [blame] | 859 | category = fcNormal; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 860 | zeroSignificand(); |
| 861 | exponent = ourSemantics.precision - 1; |
| 862 | significandParts()[0] = value; |
| 863 | normalize(rmNearestTiesToEven, lfExactlyZero); |
| 864 | } |
| 865 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 866 | IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics) { |
Chris Lattner | ac6271e | 2009-09-17 01:08:43 +0000 | [diff] [blame] | 867 | initialize(&ourSemantics); |
| 868 | category = fcZero; |
| 869 | sign = false; |
| 870 | } |
| 871 | |
Tim Shen | b4991548 | 2016-10-28 22:45:33 +0000 | [diff] [blame] | 872 | // Delegate to the previous constructor, because later copy constructor may |
| 873 | // actually inspects category, which can't be garbage. |
| 874 | IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, uninitializedTag tag) |
Tim Shen | 1bab9cf | 2016-10-29 00:51:41 +0000 | [diff] [blame] | 875 | : IEEEFloat(ourSemantics) {} |
Chris Lattner | ac6271e | 2009-09-17 01:08:43 +0000 | [diff] [blame] | 876 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 877 | IEEEFloat::IEEEFloat(const IEEEFloat &rhs) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 878 | initialize(rhs.semantics); |
| 879 | assign(rhs); |
| 880 | } |
| 881 | |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 882 | IEEEFloat::IEEEFloat(IEEEFloat &&rhs) : semantics(&semBogus) { |
Benjamin Kramer | 06f4778 | 2014-03-04 20:26:51 +0000 | [diff] [blame] | 883 | *this = std::move(rhs); |
| 884 | } |
| 885 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 886 | IEEEFloat::~IEEEFloat() { freeSignificand(); } |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 887 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 888 | unsigned int IEEEFloat::partCount() const { |
Dale Johannesen | 146a0ea | 2007-09-20 23:47:58 +0000 | [diff] [blame] | 889 | return partCountForBits(semantics->precision + 1); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 892 | const integerPart *IEEEFloat::significandParts() const { |
| 893 | return const_cast<IEEEFloat *>(this)->significandParts(); |
JF Bastien | a1d3c24 | 2015-08-26 02:32:45 +0000 | [diff] [blame] | 894 | } |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 895 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 896 | integerPart *IEEEFloat::significandParts() { |
Evan Cheng | 67c9021 | 2009-10-27 21:35:42 +0000 | [diff] [blame] | 897 | if (partCount() > 1) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 898 | return significand.parts; |
| 899 | else |
| 900 | return &significand.part; |
| 901 | } |
| 902 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 903 | void IEEEFloat::zeroSignificand() { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 904 | APInt::tcSet(significandParts(), 0, partCount()); |
| 905 | } |
| 906 | |
| 907 | /* Increment an fcNormal floating point number's significand. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 908 | void IEEEFloat::incrementSignificand() { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 909 | integerPart carry; |
| 910 | |
| 911 | carry = APInt::tcIncrement(significandParts(), partCount()); |
| 912 | |
| 913 | /* Our callers should never cause us to overflow. */ |
| 914 | assert(carry == 0); |
Duncan Sands | a41634e | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 915 | (void)carry; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | /* Add the significand of the RHS. Returns the carry flag. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 919 | integerPart IEEEFloat::addSignificand(const IEEEFloat &rhs) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 920 | integerPart *parts; |
| 921 | |
| 922 | parts = significandParts(); |
| 923 | |
| 924 | assert(semantics == rhs.semantics); |
| 925 | assert(exponent == rhs.exponent); |
| 926 | |
| 927 | return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount()); |
| 928 | } |
| 929 | |
| 930 | /* Subtract the significand of the RHS with a borrow flag. Returns |
| 931 | the borrow flag. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 932 | integerPart IEEEFloat::subtractSignificand(const IEEEFloat &rhs, |
| 933 | integerPart borrow) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 934 | integerPart *parts; |
| 935 | |
| 936 | parts = significandParts(); |
| 937 | |
| 938 | assert(semantics == rhs.semantics); |
| 939 | assert(exponent == rhs.exponent); |
| 940 | |
| 941 | return APInt::tcSubtract(parts, rhs.significandParts(), borrow, |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 942 | partCount()); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | /* Multiply the significand of the RHS. If ADDEND is non-NULL, add it |
| 946 | on to the full-precision result of the multiplication. Returns the |
| 947 | lost fraction. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 948 | lostFraction IEEEFloat::multiplySignificand(const IEEEFloat &rhs, |
| 949 | const IEEEFloat *addend) { |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 950 | unsigned int omsb; // One, not zero, based MSB. |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 951 | unsigned int partsCount, newPartsCount, precision; |
| 952 | integerPart *lhsSignificand; |
| 953 | integerPart scratch[4]; |
| 954 | integerPart *fullSignificand; |
| 955 | lostFraction lost_fraction; |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 956 | bool ignored; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 957 | |
| 958 | assert(semantics == rhs.semantics); |
| 959 | |
| 960 | precision = semantics->precision; |
Lang Hames | 56c0eb2 | 2014-11-19 19:15:41 +0000 | [diff] [blame] | 961 | |
| 962 | // Allocate space for twice as many bits as the original significand, plus one |
| 963 | // extra bit for the addition to overflow into. |
| 964 | newPartsCount = partCountForBits(precision * 2 + 1); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 965 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 966 | if (newPartsCount > 4) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 967 | fullSignificand = new integerPart[newPartsCount]; |
| 968 | else |
| 969 | fullSignificand = scratch; |
| 970 | |
| 971 | lhsSignificand = significandParts(); |
| 972 | partsCount = partCount(); |
| 973 | |
| 974 | APInt::tcFullMultiply(fullSignificand, lhsSignificand, |
Neil Booth | 0ea72a9 | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 975 | rhs.significandParts(), partsCount, partsCount); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 976 | |
| 977 | lost_fraction = lfExactlyZero; |
| 978 | omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1; |
| 979 | exponent += rhs.exponent; |
| 980 | |
Shuxin Yang | bbddbac | 2013-05-13 18:03:12 +0000 | [diff] [blame] | 981 | // Assume the operands involved in the multiplication are single-precision |
| 982 | // FP, and the two multiplicants are: |
| 983 | // *this = a23 . a22 ... a0 * 2^e1 |
| 984 | // rhs = b23 . b22 ... b0 * 2^e2 |
| 985 | // the result of multiplication is: |
Lang Hames | 56c0eb2 | 2014-11-19 19:15:41 +0000 | [diff] [blame] | 986 | // *this = c48 c47 c46 . c45 ... c0 * 2^(e1+e2) |
Simon Pilgrim | a284959 | 2017-03-20 13:53:59 +0000 | [diff] [blame] | 987 | // Note that there are three significant bits at the left-hand side of the |
Lang Hames | 56c0eb2 | 2014-11-19 19:15:41 +0000 | [diff] [blame] | 988 | // radix point: two for the multiplication, and an overflow bit for the |
| 989 | // addition (that will always be zero at this point). Move the radix point |
| 990 | // toward left by two bits, and adjust exponent accordingly. |
| 991 | exponent += 2; |
Shuxin Yang | bbddbac | 2013-05-13 18:03:12 +0000 | [diff] [blame] | 992 | |
Hal Finkel | 171c2ec | 2014-10-14 19:23:07 +0000 | [diff] [blame] | 993 | if (addend && addend->isNonZero()) { |
Simon Pilgrim | a284959 | 2017-03-20 13:53:59 +0000 | [diff] [blame] | 994 | // The intermediate result of the multiplication has "2 * precision" |
Shuxin Yang | bbddbac | 2013-05-13 18:03:12 +0000 | [diff] [blame] | 995 | // signicant bit; adjust the addend to be consistent with mul result. |
| 996 | // |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 997 | Significand savedSignificand = significand; |
| 998 | const fltSemantics *savedSemantics = semantics; |
| 999 | fltSemantics extendedSemantics; |
| 1000 | opStatus status; |
| 1001 | unsigned int extendedPrecision; |
| 1002 | |
Lang Hames | 56c0eb2 | 2014-11-19 19:15:41 +0000 | [diff] [blame] | 1003 | // Normalize our MSB to one below the top bit to allow for overflow. |
| 1004 | extendedPrecision = 2 * precision + 1; |
| 1005 | if (omsb != extendedPrecision - 1) { |
Shuxin Yang | bbddbac | 2013-05-13 18:03:12 +0000 | [diff] [blame] | 1006 | assert(extendedPrecision > omsb); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1007 | APInt::tcShiftLeft(fullSignificand, newPartsCount, |
Lang Hames | 56c0eb2 | 2014-11-19 19:15:41 +0000 | [diff] [blame] | 1008 | (extendedPrecision - 1) - omsb); |
| 1009 | exponent -= (extendedPrecision - 1) - omsb; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1010 | } |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1011 | |
| 1012 | /* Create new semantics. */ |
| 1013 | extendedSemantics = *semantics; |
| 1014 | extendedSemantics.precision = extendedPrecision; |
| 1015 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1016 | if (newPartsCount == 1) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1017 | significand.part = fullSignificand[0]; |
| 1018 | else |
| 1019 | significand.parts = fullSignificand; |
| 1020 | semantics = &extendedSemantics; |
| 1021 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1022 | IEEEFloat extendedAddend(*addend); |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 1023 | status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1024 | assert(status == opOK); |
Duncan Sands | a41634e | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 1025 | (void)status; |
Lang Hames | 56c0eb2 | 2014-11-19 19:15:41 +0000 | [diff] [blame] | 1026 | |
| 1027 | // Shift the significand of the addend right by one bit. This guarantees |
| 1028 | // that the high bit of the significand is zero (same as fullSignificand), |
| 1029 | // so the addition will overflow (if it does overflow at all) into the top bit. |
| 1030 | lost_fraction = extendedAddend.shiftSignificandRight(1); |
| 1031 | assert(lost_fraction == lfExactlyZero && |
| 1032 | "Lost precision while shifting addend for fused-multiply-add."); |
| 1033 | |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1034 | lost_fraction = addOrSubtractSignificand(extendedAddend, false); |
| 1035 | |
| 1036 | /* Restore our state. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1037 | if (newPartsCount == 1) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1038 | fullSignificand[0] = significand.part; |
| 1039 | significand = savedSignificand; |
| 1040 | semantics = savedSemantics; |
| 1041 | |
| 1042 | omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1; |
| 1043 | } |
| 1044 | |
Shuxin Yang | bbddbac | 2013-05-13 18:03:12 +0000 | [diff] [blame] | 1045 | // Convert the result having "2 * precision" significant-bits back to the one |
Simon Pilgrim | a284959 | 2017-03-20 13:53:59 +0000 | [diff] [blame] | 1046 | // having "precision" significant-bits. First, move the radix point from |
Shuxin Yang | bbddbac | 2013-05-13 18:03:12 +0000 | [diff] [blame] | 1047 | // poision "2*precision - 1" to "precision - 1". The exponent need to be |
| 1048 | // adjusted by "2*precision - 1" - "precision - 1" = "precision". |
Lang Hames | 56c0eb2 | 2014-11-19 19:15:41 +0000 | [diff] [blame] | 1049 | exponent -= precision + 1; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1050 | |
Shuxin Yang | bbddbac | 2013-05-13 18:03:12 +0000 | [diff] [blame] | 1051 | // In case MSB resides at the left-hand side of radix point, shift the |
| 1052 | // mantissa right by some amount to make sure the MSB reside right before |
| 1053 | // the radix point (i.e. "MSB . rest-significant-bits"). |
| 1054 | // |
| 1055 | // Note that the result is not normalized when "omsb < precision". So, the |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1056 | // caller needs to call IEEEFloat::normalize() if normalized value is |
| 1057 | // expected. |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1058 | if (omsb > precision) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1059 | unsigned int bits, significantParts; |
| 1060 | lostFraction lf; |
| 1061 | |
| 1062 | bits = omsb - precision; |
| 1063 | significantParts = partCountForBits(omsb); |
| 1064 | lf = shiftRight(fullSignificand, significantParts, bits); |
| 1065 | lost_fraction = combineLostFractions(lf, lost_fraction); |
| 1066 | exponent += bits; |
| 1067 | } |
| 1068 | |
| 1069 | APInt::tcAssign(lhsSignificand, fullSignificand, partsCount); |
| 1070 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1071 | if (newPartsCount > 4) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1072 | delete [] fullSignificand; |
| 1073 | |
| 1074 | return lost_fraction; |
| 1075 | } |
| 1076 | |
| 1077 | /* Multiply the significands of LHS and RHS to DST. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1078 | lostFraction IEEEFloat::divideSignificand(const IEEEFloat &rhs) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1079 | unsigned int bit, i, partsCount; |
| 1080 | const integerPart *rhsSignificand; |
| 1081 | integerPart *lhsSignificand, *dividend, *divisor; |
| 1082 | integerPart scratch[4]; |
| 1083 | lostFraction lost_fraction; |
| 1084 | |
| 1085 | assert(semantics == rhs.semantics); |
| 1086 | |
| 1087 | lhsSignificand = significandParts(); |
| 1088 | rhsSignificand = rhs.significandParts(); |
| 1089 | partsCount = partCount(); |
| 1090 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1091 | if (partsCount > 2) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1092 | dividend = new integerPart[partsCount * 2]; |
| 1093 | else |
| 1094 | dividend = scratch; |
| 1095 | |
| 1096 | divisor = dividend + partsCount; |
| 1097 | |
| 1098 | /* Copy the dividend and divisor as they will be modified in-place. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1099 | for (i = 0; i < partsCount; i++) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1100 | dividend[i] = lhsSignificand[i]; |
| 1101 | divisor[i] = rhsSignificand[i]; |
| 1102 | lhsSignificand[i] = 0; |
| 1103 | } |
| 1104 | |
| 1105 | exponent -= rhs.exponent; |
| 1106 | |
| 1107 | unsigned int precision = semantics->precision; |
| 1108 | |
| 1109 | /* Normalize the divisor. */ |
| 1110 | bit = precision - APInt::tcMSB(divisor, partsCount) - 1; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1111 | if (bit) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1112 | exponent += bit; |
| 1113 | APInt::tcShiftLeft(divisor, partsCount, bit); |
| 1114 | } |
| 1115 | |
| 1116 | /* Normalize the dividend. */ |
| 1117 | bit = precision - APInt::tcMSB(dividend, partsCount) - 1; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1118 | if (bit) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1119 | exponent -= bit; |
| 1120 | APInt::tcShiftLeft(dividend, partsCount, bit); |
| 1121 | } |
| 1122 | |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 1123 | /* Ensure the dividend >= divisor initially for the loop below. |
| 1124 | Incidentally, this means that the division loop below is |
| 1125 | guaranteed to set the integer bit to one. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1126 | if (APInt::tcCompare(dividend, divisor, partsCount) < 0) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1127 | exponent--; |
| 1128 | APInt::tcShiftLeft(dividend, partsCount, 1); |
| 1129 | assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0); |
| 1130 | } |
| 1131 | |
| 1132 | /* Long division. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1133 | for (bit = precision; bit; bit -= 1) { |
| 1134 | if (APInt::tcCompare(dividend, divisor, partsCount) >= 0) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1135 | APInt::tcSubtract(dividend, divisor, 0, partsCount); |
| 1136 | APInt::tcSetBit(lhsSignificand, bit - 1); |
| 1137 | } |
| 1138 | |
| 1139 | APInt::tcShiftLeft(dividend, partsCount, 1); |
| 1140 | } |
| 1141 | |
| 1142 | /* Figure out the lost fraction. */ |
| 1143 | int cmp = APInt::tcCompare(dividend, divisor, partsCount); |
| 1144 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1145 | if (cmp > 0) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1146 | lost_fraction = lfMoreThanHalf; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1147 | else if (cmp == 0) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1148 | lost_fraction = lfExactlyHalf; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1149 | else if (APInt::tcIsZero(dividend, partsCount)) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1150 | lost_fraction = lfExactlyZero; |
| 1151 | else |
| 1152 | lost_fraction = lfLessThanHalf; |
| 1153 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1154 | if (partsCount > 2) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1155 | delete [] dividend; |
| 1156 | |
| 1157 | return lost_fraction; |
| 1158 | } |
| 1159 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1160 | unsigned int IEEEFloat::significandMSB() const { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1161 | return APInt::tcMSB(significandParts(), partCount()); |
| 1162 | } |
| 1163 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1164 | unsigned int IEEEFloat::significandLSB() const { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1165 | return APInt::tcLSB(significandParts(), partCount()); |
| 1166 | } |
| 1167 | |
| 1168 | /* Note that a zero result is NOT normalized to fcZero. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1169 | lostFraction IEEEFloat::shiftSignificandRight(unsigned int bits) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1170 | /* Our exponent should not overflow. */ |
Michael Gottesman | 9dc9833 | 2013-06-24 04:06:23 +0000 | [diff] [blame] | 1171 | assert((ExponentType) (exponent + bits) >= exponent); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1172 | |
| 1173 | exponent += bits; |
| 1174 | |
| 1175 | return shiftRight(significandParts(), partCount(), bits); |
| 1176 | } |
| 1177 | |
| 1178 | /* Shift the significand left BITS bits, subtract BITS from its exponent. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1179 | void IEEEFloat::shiftSignificandLeft(unsigned int bits) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1180 | assert(bits < semantics->precision); |
| 1181 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1182 | if (bits) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1183 | unsigned int partsCount = partCount(); |
| 1184 | |
| 1185 | APInt::tcShiftLeft(significandParts(), partsCount, bits); |
| 1186 | exponent -= bits; |
| 1187 | |
| 1188 | assert(!APInt::tcIsZero(significandParts(), partsCount)); |
| 1189 | } |
| 1190 | } |
| 1191 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1192 | IEEEFloat::cmpResult |
| 1193 | IEEEFloat::compareAbsoluteValue(const IEEEFloat &rhs) const { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1194 | int compare; |
| 1195 | |
| 1196 | assert(semantics == rhs.semantics); |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 1197 | assert(isFiniteNonZero()); |
| 1198 | assert(rhs.isFiniteNonZero()); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1199 | |
| 1200 | compare = exponent - rhs.exponent; |
| 1201 | |
| 1202 | /* If exponents are equal, do an unsigned bignum comparison of the |
| 1203 | significands. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1204 | if (compare == 0) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1205 | compare = APInt::tcCompare(significandParts(), rhs.significandParts(), |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 1206 | partCount()); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1207 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1208 | if (compare > 0) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1209 | return cmpGreaterThan; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1210 | else if (compare < 0) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1211 | return cmpLessThan; |
| 1212 | else |
| 1213 | return cmpEqual; |
| 1214 | } |
| 1215 | |
| 1216 | /* Handle overflow. Sign is preserved. We either become infinity or |
| 1217 | the largest finite number. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1218 | IEEEFloat::opStatus IEEEFloat::handleOverflow(roundingMode rounding_mode) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1219 | /* Infinity? */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1220 | if (rounding_mode == rmNearestTiesToEven || |
| 1221 | rounding_mode == rmNearestTiesToAway || |
| 1222 | (rounding_mode == rmTowardPositive && !sign) || |
| 1223 | (rounding_mode == rmTowardNegative && sign)) { |
| 1224 | category = fcInfinity; |
| 1225 | return (opStatus) (opOverflow | opInexact); |
| 1226 | } |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1227 | |
| 1228 | /* Otherwise we become the largest finite number. */ |
| 1229 | category = fcNormal; |
| 1230 | exponent = semantics->maxExponent; |
| 1231 | APInt::tcSetLeastSignificantBits(significandParts(), partCount(), |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 1232 | semantics->precision); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1233 | |
| 1234 | return opInexact; |
| 1235 | } |
| 1236 | |
Neil Booth | 1ca1f80 | 2007-10-03 15:16:41 +0000 | [diff] [blame] | 1237 | /* Returns TRUE if, when truncating the current number, with BIT the |
| 1238 | new LSB, with the given lost fraction and rounding mode, the result |
| 1239 | would need to be rounded away from zero (i.e., by increasing the |
| 1240 | signficand). This routine must work for fcZero of both signs, and |
| 1241 | fcNormal numbers. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1242 | bool IEEEFloat::roundAwayFromZero(roundingMode rounding_mode, |
| 1243 | lostFraction lost_fraction, |
| 1244 | unsigned int bit) const { |
Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1245 | /* NaNs and infinities should not have lost fractions. */ |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 1246 | assert(isFiniteNonZero() || category == fcZero); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1247 | |
Neil Booth | 1ca1f80 | 2007-10-03 15:16:41 +0000 | [diff] [blame] | 1248 | /* Current callers never pass this so we don't handle it. */ |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1249 | assert(lost_fraction != lfExactlyZero); |
| 1250 | |
Mike Stump | 889285d | 2009-05-13 23:23:20 +0000 | [diff] [blame] | 1251 | switch (rounding_mode) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1252 | case rmNearestTiesToAway: |
| 1253 | return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf; |
| 1254 | |
| 1255 | case rmNearestTiesToEven: |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1256 | if (lost_fraction == lfMoreThanHalf) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1257 | return true; |
| 1258 | |
| 1259 | /* Our zeroes don't have a significand to test. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1260 | if (lost_fraction == lfExactlyHalf && category != fcZero) |
Neil Booth | 1ca1f80 | 2007-10-03 15:16:41 +0000 | [diff] [blame] | 1261 | return APInt::tcExtractBit(significandParts(), bit); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1262 | |
| 1263 | return false; |
| 1264 | |
| 1265 | case rmTowardZero: |
| 1266 | return false; |
| 1267 | |
| 1268 | case rmTowardPositive: |
David Blaikie | dc3f01e | 2015-03-09 01:57:13 +0000 | [diff] [blame] | 1269 | return !sign; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1270 | |
| 1271 | case rmTowardNegative: |
David Blaikie | dc3f01e | 2015-03-09 01:57:13 +0000 | [diff] [blame] | 1272 | return sign; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1273 | } |
Chandler Carruth | f3e8502 | 2012-01-10 18:08:01 +0000 | [diff] [blame] | 1274 | llvm_unreachable("Invalid rounding mode found"); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1277 | IEEEFloat::opStatus IEEEFloat::normalize(roundingMode rounding_mode, |
| 1278 | lostFraction lost_fraction) { |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 1279 | unsigned int omsb; /* One, not zero, based MSB. */ |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1280 | int exponentChange; |
| 1281 | |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 1282 | if (!isFiniteNonZero()) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1283 | return opOK; |
| 1284 | |
| 1285 | /* Before rounding normalize the exponent of fcNormal numbers. */ |
| 1286 | omsb = significandMSB() + 1; |
| 1287 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1288 | if (omsb) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1289 | /* OMSB is numbered from 1. We want to place it in the integer |
Nick Lewycky | f66daac | 2011-10-03 21:30:08 +0000 | [diff] [blame] | 1290 | bit numbered PRECISION if possible, with a compensating change in |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1291 | the exponent. */ |
| 1292 | exponentChange = omsb - semantics->precision; |
| 1293 | |
| 1294 | /* If the resulting exponent is too high, overflow according to |
| 1295 | the rounding mode. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1296 | if (exponent + exponentChange > semantics->maxExponent) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1297 | return handleOverflow(rounding_mode); |
| 1298 | |
| 1299 | /* Subnormal numbers have exponent minExponent, and their MSB |
| 1300 | is forced based on that. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1301 | if (exponent + exponentChange < semantics->minExponent) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1302 | exponentChange = semantics->minExponent - exponent; |
| 1303 | |
| 1304 | /* Shifting left is easy as we don't lose precision. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1305 | if (exponentChange < 0) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1306 | assert(lost_fraction == lfExactlyZero); |
| 1307 | |
| 1308 | shiftSignificandLeft(-exponentChange); |
| 1309 | |
| 1310 | return opOK; |
| 1311 | } |
| 1312 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1313 | if (exponentChange > 0) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1314 | lostFraction lf; |
| 1315 | |
| 1316 | /* Shift right and capture any new lost fraction. */ |
| 1317 | lf = shiftSignificandRight(exponentChange); |
| 1318 | |
| 1319 | lost_fraction = combineLostFractions(lf, lost_fraction); |
| 1320 | |
| 1321 | /* Keep OMSB up-to-date. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1322 | if (omsb > (unsigned) exponentChange) |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 1323 | omsb -= exponentChange; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1324 | else |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 1325 | omsb = 0; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | /* Now round the number according to rounding_mode given the lost |
| 1330 | fraction. */ |
| 1331 | |
| 1332 | /* As specified in IEEE 754, since we do not trap we do not report |
| 1333 | underflow for exact results. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1334 | if (lost_fraction == lfExactlyZero) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1335 | /* Canonicalize zeroes. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1336 | if (omsb == 0) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1337 | category = fcZero; |
| 1338 | |
| 1339 | return opOK; |
| 1340 | } |
| 1341 | |
| 1342 | /* Increment the significand if we're rounding away from zero. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1343 | if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) { |
| 1344 | if (omsb == 0) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1345 | exponent = semantics->minExponent; |
| 1346 | |
| 1347 | incrementSignificand(); |
| 1348 | omsb = significandMSB() + 1; |
| 1349 | |
| 1350 | /* Did the significand increment overflow? */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1351 | if (omsb == (unsigned) semantics->precision + 1) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1352 | /* Renormalize by incrementing the exponent and shifting our |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 1353 | significand right one. However if we already have the |
| 1354 | maximum exponent we overflow to infinity. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1355 | if (exponent == semantics->maxExponent) { |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 1356 | category = fcInfinity; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1357 | |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 1358 | return (opStatus) (opOverflow | opInexact); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | shiftSignificandRight(1); |
| 1362 | |
| 1363 | return opInexact; |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | /* The normal case - we were and are not denormal, and any |
| 1368 | significand increment above didn't overflow. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1369 | if (omsb == semantics->precision) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1370 | return opInexact; |
| 1371 | |
| 1372 | /* We have a non-zero denormal. */ |
| 1373 | assert(omsb < semantics->precision); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1374 | |
| 1375 | /* Canonicalize zeroes. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1376 | if (omsb == 0) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1377 | category = fcZero; |
| 1378 | |
| 1379 | /* The fcZero case is a denormal that underflowed to zero. */ |
| 1380 | return (opStatus) (opUnderflow | opInexact); |
| 1381 | } |
| 1382 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1383 | IEEEFloat::opStatus IEEEFloat::addOrSubtractSpecials(const IEEEFloat &rhs, |
| 1384 | bool subtract) { |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1385 | switch (PackCategoriesIntoKey(category, rhs.category)) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1386 | default: |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1387 | llvm_unreachable(nullptr); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1388 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1389 | case PackCategoriesIntoKey(fcNaN, fcZero): |
| 1390 | case PackCategoriesIntoKey(fcNaN, fcNormal): |
| 1391 | case PackCategoriesIntoKey(fcNaN, fcInfinity): |
| 1392 | case PackCategoriesIntoKey(fcNaN, fcNaN): |
| 1393 | case PackCategoriesIntoKey(fcNormal, fcZero): |
| 1394 | case PackCategoriesIntoKey(fcInfinity, fcNormal): |
| 1395 | case PackCategoriesIntoKey(fcInfinity, fcZero): |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1396 | return opOK; |
| 1397 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1398 | case PackCategoriesIntoKey(fcZero, fcNaN): |
| 1399 | case PackCategoriesIntoKey(fcNormal, fcNaN): |
| 1400 | case PackCategoriesIntoKey(fcInfinity, fcNaN): |
Stephen Canon | d327828 | 2014-06-08 16:53:31 +0000 | [diff] [blame] | 1401 | // We need to be sure to flip the sign here for subtraction because we |
| 1402 | // don't have a separate negate operation so -NaN becomes 0 - NaN here. |
| 1403 | sign = rhs.sign ^ subtract; |
Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1404 | category = fcNaN; |
| 1405 | copySignificand(rhs); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1406 | return opOK; |
| 1407 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1408 | case PackCategoriesIntoKey(fcNormal, fcInfinity): |
| 1409 | case PackCategoriesIntoKey(fcZero, fcInfinity): |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1410 | category = fcInfinity; |
| 1411 | sign = rhs.sign ^ subtract; |
| 1412 | return opOK; |
| 1413 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1414 | case PackCategoriesIntoKey(fcZero, fcNormal): |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1415 | assign(rhs); |
| 1416 | sign = rhs.sign ^ subtract; |
| 1417 | return opOK; |
| 1418 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1419 | case PackCategoriesIntoKey(fcZero, fcZero): |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1420 | /* Sign depends on rounding mode; handled by caller. */ |
| 1421 | return opOK; |
| 1422 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1423 | case PackCategoriesIntoKey(fcInfinity, fcInfinity): |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1424 | /* Differently signed infinities can only be validly |
| 1425 | subtracted. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1426 | if (((sign ^ rhs.sign)!=0) != subtract) { |
Neil Booth | 5fe658b | 2007-10-14 10:39:51 +0000 | [diff] [blame] | 1427 | makeNaN(); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1428 | return opInvalidOp; |
| 1429 | } |
| 1430 | |
| 1431 | return opOK; |
| 1432 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1433 | case PackCategoriesIntoKey(fcNormal, fcNormal): |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1434 | return opDivByZero; |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | /* Add or subtract two normal numbers. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1439 | lostFraction IEEEFloat::addOrSubtractSignificand(const IEEEFloat &rhs, |
| 1440 | bool subtract) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1441 | integerPart carry; |
| 1442 | lostFraction lost_fraction; |
| 1443 | int bits; |
| 1444 | |
| 1445 | /* Determine if the operation on the absolute values is effectively |
| 1446 | an addition or subtraction. */ |
Aaron Ballman | d5cc45f | 2015-03-24 12:47:51 +0000 | [diff] [blame] | 1447 | subtract ^= static_cast<bool>(sign ^ rhs.sign); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1448 | |
| 1449 | /* Are we bigger exponent-wise than the RHS? */ |
| 1450 | bits = exponent - rhs.exponent; |
| 1451 | |
| 1452 | /* Subtraction is more subtle than one might naively expect. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1453 | if (subtract) { |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1454 | IEEEFloat temp_rhs(rhs); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1455 | bool reverse; |
| 1456 | |
Chris Lattner | 3da18eb | 2007-08-24 03:02:34 +0000 | [diff] [blame] | 1457 | if (bits == 0) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1458 | reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan; |
| 1459 | lost_fraction = lfExactlyZero; |
Chris Lattner | 3da18eb | 2007-08-24 03:02:34 +0000 | [diff] [blame] | 1460 | } else if (bits > 0) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1461 | lost_fraction = temp_rhs.shiftSignificandRight(bits - 1); |
| 1462 | shiftSignificandLeft(1); |
| 1463 | reverse = false; |
Chris Lattner | 3da18eb | 2007-08-24 03:02:34 +0000 | [diff] [blame] | 1464 | } else { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1465 | lost_fraction = shiftSignificandRight(-bits - 1); |
| 1466 | temp_rhs.shiftSignificandLeft(1); |
| 1467 | reverse = true; |
| 1468 | } |
| 1469 | |
Chris Lattner | 3da18eb | 2007-08-24 03:02:34 +0000 | [diff] [blame] | 1470 | if (reverse) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1471 | carry = temp_rhs.subtractSignificand |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 1472 | (*this, lost_fraction != lfExactlyZero); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1473 | copySignificand(temp_rhs); |
| 1474 | sign = !sign; |
| 1475 | } else { |
| 1476 | carry = subtractSignificand |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 1477 | (temp_rhs, lost_fraction != lfExactlyZero); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | /* Invert the lost fraction - it was on the RHS and |
| 1481 | subtracted. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1482 | if (lost_fraction == lfLessThanHalf) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1483 | lost_fraction = lfMoreThanHalf; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1484 | else if (lost_fraction == lfMoreThanHalf) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1485 | lost_fraction = lfLessThanHalf; |
| 1486 | |
| 1487 | /* The code above is intended to ensure that no borrow is |
| 1488 | necessary. */ |
| 1489 | assert(!carry); |
Duncan Sands | a41634e | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 1490 | (void)carry; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1491 | } else { |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1492 | if (bits > 0) { |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1493 | IEEEFloat temp_rhs(rhs); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1494 | |
| 1495 | lost_fraction = temp_rhs.shiftSignificandRight(bits); |
| 1496 | carry = addSignificand(temp_rhs); |
| 1497 | } else { |
| 1498 | lost_fraction = shiftSignificandRight(-bits); |
| 1499 | carry = addSignificand(rhs); |
| 1500 | } |
| 1501 | |
| 1502 | /* We have a guard bit; generating a carry cannot happen. */ |
| 1503 | assert(!carry); |
Duncan Sands | a41634e | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 1504 | (void)carry; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | return lost_fraction; |
| 1508 | } |
| 1509 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1510 | IEEEFloat::opStatus IEEEFloat::multiplySpecials(const IEEEFloat &rhs) { |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1511 | switch (PackCategoriesIntoKey(category, rhs.category)) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1512 | default: |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1513 | llvm_unreachable(nullptr); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1514 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1515 | case PackCategoriesIntoKey(fcNaN, fcZero): |
| 1516 | case PackCategoriesIntoKey(fcNaN, fcNormal): |
| 1517 | case PackCategoriesIntoKey(fcNaN, fcInfinity): |
| 1518 | case PackCategoriesIntoKey(fcNaN, fcNaN): |
Michael Gottesman | b0e688e | 2013-07-27 21:49:25 +0000 | [diff] [blame] | 1519 | sign = false; |
Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1520 | return opOK; |
| 1521 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1522 | case PackCategoriesIntoKey(fcZero, fcNaN): |
| 1523 | case PackCategoriesIntoKey(fcNormal, fcNaN): |
| 1524 | case PackCategoriesIntoKey(fcInfinity, fcNaN): |
Michael Gottesman | b0e688e | 2013-07-27 21:49:25 +0000 | [diff] [blame] | 1525 | sign = false; |
Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1526 | category = fcNaN; |
| 1527 | copySignificand(rhs); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1528 | return opOK; |
| 1529 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1530 | case PackCategoriesIntoKey(fcNormal, fcInfinity): |
| 1531 | case PackCategoriesIntoKey(fcInfinity, fcNormal): |
| 1532 | case PackCategoriesIntoKey(fcInfinity, fcInfinity): |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1533 | category = fcInfinity; |
| 1534 | return opOK; |
| 1535 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1536 | case PackCategoriesIntoKey(fcZero, fcNormal): |
| 1537 | case PackCategoriesIntoKey(fcNormal, fcZero): |
| 1538 | case PackCategoriesIntoKey(fcZero, fcZero): |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1539 | category = fcZero; |
| 1540 | return opOK; |
| 1541 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1542 | case PackCategoriesIntoKey(fcZero, fcInfinity): |
| 1543 | case PackCategoriesIntoKey(fcInfinity, fcZero): |
Neil Booth | 5fe658b | 2007-10-14 10:39:51 +0000 | [diff] [blame] | 1544 | makeNaN(); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1545 | return opInvalidOp; |
| 1546 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1547 | case PackCategoriesIntoKey(fcNormal, fcNormal): |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1548 | return opOK; |
| 1549 | } |
| 1550 | } |
| 1551 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1552 | IEEEFloat::opStatus IEEEFloat::divideSpecials(const IEEEFloat &rhs) { |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1553 | switch (PackCategoriesIntoKey(category, rhs.category)) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1554 | default: |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1555 | llvm_unreachable(nullptr); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1556 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1557 | case PackCategoriesIntoKey(fcZero, fcNaN): |
| 1558 | case PackCategoriesIntoKey(fcNormal, fcNaN): |
| 1559 | case PackCategoriesIntoKey(fcInfinity, fcNaN): |
Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1560 | category = fcNaN; |
| 1561 | copySignificand(rhs); |
Michael Gottesman | b0e688e | 2013-07-27 21:49:25 +0000 | [diff] [blame] | 1562 | case PackCategoriesIntoKey(fcNaN, fcZero): |
| 1563 | case PackCategoriesIntoKey(fcNaN, fcNormal): |
| 1564 | case PackCategoriesIntoKey(fcNaN, fcInfinity): |
| 1565 | case PackCategoriesIntoKey(fcNaN, fcNaN): |
| 1566 | sign = false; |
| 1567 | case PackCategoriesIntoKey(fcInfinity, fcZero): |
| 1568 | case PackCategoriesIntoKey(fcInfinity, fcNormal): |
| 1569 | case PackCategoriesIntoKey(fcZero, fcInfinity): |
| 1570 | case PackCategoriesIntoKey(fcZero, fcNormal): |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1571 | return opOK; |
| 1572 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1573 | case PackCategoriesIntoKey(fcNormal, fcInfinity): |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1574 | category = fcZero; |
| 1575 | return opOK; |
| 1576 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1577 | case PackCategoriesIntoKey(fcNormal, fcZero): |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1578 | category = fcInfinity; |
| 1579 | return opDivByZero; |
| 1580 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1581 | case PackCategoriesIntoKey(fcInfinity, fcInfinity): |
| 1582 | case PackCategoriesIntoKey(fcZero, fcZero): |
Neil Booth | 5fe658b | 2007-10-14 10:39:51 +0000 | [diff] [blame] | 1583 | makeNaN(); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1584 | return opInvalidOp; |
| 1585 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1586 | case PackCategoriesIntoKey(fcNormal, fcNormal): |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1587 | return opOK; |
| 1588 | } |
| 1589 | } |
| 1590 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1591 | IEEEFloat::opStatus IEEEFloat::modSpecials(const IEEEFloat &rhs) { |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1592 | switch (PackCategoriesIntoKey(category, rhs.category)) { |
Dale Johannesen | b572163 | 2009-01-21 00:35:19 +0000 | [diff] [blame] | 1593 | default: |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1594 | llvm_unreachable(nullptr); |
Dale Johannesen | b572163 | 2009-01-21 00:35:19 +0000 | [diff] [blame] | 1595 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1596 | case PackCategoriesIntoKey(fcNaN, fcZero): |
| 1597 | case PackCategoriesIntoKey(fcNaN, fcNormal): |
| 1598 | case PackCategoriesIntoKey(fcNaN, fcInfinity): |
| 1599 | case PackCategoriesIntoKey(fcNaN, fcNaN): |
| 1600 | case PackCategoriesIntoKey(fcZero, fcInfinity): |
| 1601 | case PackCategoriesIntoKey(fcZero, fcNormal): |
| 1602 | case PackCategoriesIntoKey(fcNormal, fcInfinity): |
Dale Johannesen | b572163 | 2009-01-21 00:35:19 +0000 | [diff] [blame] | 1603 | return opOK; |
| 1604 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1605 | case PackCategoriesIntoKey(fcZero, fcNaN): |
| 1606 | case PackCategoriesIntoKey(fcNormal, fcNaN): |
| 1607 | case PackCategoriesIntoKey(fcInfinity, fcNaN): |
Michael Gottesman | b0e688e | 2013-07-27 21:49:25 +0000 | [diff] [blame] | 1608 | sign = false; |
Dale Johannesen | b572163 | 2009-01-21 00:35:19 +0000 | [diff] [blame] | 1609 | category = fcNaN; |
| 1610 | copySignificand(rhs); |
| 1611 | return opOK; |
| 1612 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1613 | case PackCategoriesIntoKey(fcNormal, fcZero): |
| 1614 | case PackCategoriesIntoKey(fcInfinity, fcZero): |
| 1615 | case PackCategoriesIntoKey(fcInfinity, fcNormal): |
| 1616 | case PackCategoriesIntoKey(fcInfinity, fcInfinity): |
| 1617 | case PackCategoriesIntoKey(fcZero, fcZero): |
Dale Johannesen | b572163 | 2009-01-21 00:35:19 +0000 | [diff] [blame] | 1618 | makeNaN(); |
| 1619 | return opInvalidOp; |
| 1620 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1621 | case PackCategoriesIntoKey(fcNormal, fcNormal): |
Dale Johannesen | b572163 | 2009-01-21 00:35:19 +0000 | [diff] [blame] | 1622 | return opOK; |
| 1623 | } |
| 1624 | } |
| 1625 | |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1626 | /* Change sign. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1627 | void IEEEFloat::changeSign() { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1628 | /* Look mummy, this one's easy. */ |
| 1629 | sign = !sign; |
| 1630 | } |
| 1631 | |
| 1632 | /* Normalized addition or subtraction. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1633 | IEEEFloat::opStatus IEEEFloat::addOrSubtract(const IEEEFloat &rhs, |
| 1634 | roundingMode rounding_mode, |
| 1635 | bool subtract) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1636 | opStatus fs; |
| 1637 | |
| 1638 | fs = addOrSubtractSpecials(rhs, subtract); |
| 1639 | |
| 1640 | /* This return code means it was not a simple case. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1641 | if (fs == opDivByZero) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1642 | lostFraction lost_fraction; |
| 1643 | |
| 1644 | lost_fraction = addOrSubtractSignificand(rhs, subtract); |
| 1645 | fs = normalize(rounding_mode, lost_fraction); |
| 1646 | |
| 1647 | /* Can only be zero if we lost no fraction. */ |
| 1648 | assert(category != fcZero || lost_fraction == lfExactlyZero); |
| 1649 | } |
| 1650 | |
| 1651 | /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a |
| 1652 | positive zero unless rounding to minus infinity, except that |
| 1653 | adding two like-signed zeroes gives that zero. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1654 | if (category == fcZero) { |
| 1655 | if (rhs.category != fcZero || (sign == rhs.sign) == subtract) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1656 | sign = (rounding_mode == rmTowardNegative); |
| 1657 | } |
| 1658 | |
| 1659 | return fs; |
| 1660 | } |
| 1661 | |
| 1662 | /* Normalized addition. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1663 | IEEEFloat::opStatus IEEEFloat::add(const IEEEFloat &rhs, |
| 1664 | roundingMode rounding_mode) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1665 | return addOrSubtract(rhs, rounding_mode, false); |
| 1666 | } |
| 1667 | |
| 1668 | /* Normalized subtraction. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1669 | IEEEFloat::opStatus IEEEFloat::subtract(const IEEEFloat &rhs, |
| 1670 | roundingMode rounding_mode) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1671 | return addOrSubtract(rhs, rounding_mode, true); |
| 1672 | } |
| 1673 | |
| 1674 | /* Normalized multiply. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1675 | IEEEFloat::opStatus IEEEFloat::multiply(const IEEEFloat &rhs, |
| 1676 | roundingMode rounding_mode) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1677 | opStatus fs; |
| 1678 | |
| 1679 | sign ^= rhs.sign; |
| 1680 | fs = multiplySpecials(rhs); |
| 1681 | |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 1682 | if (isFiniteNonZero()) { |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 1683 | lostFraction lost_fraction = multiplySignificand(rhs, nullptr); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1684 | fs = normalize(rounding_mode, lost_fraction); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1685 | if (lost_fraction != lfExactlyZero) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1686 | fs = (opStatus) (fs | opInexact); |
| 1687 | } |
| 1688 | |
| 1689 | return fs; |
| 1690 | } |
| 1691 | |
| 1692 | /* Normalized divide. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1693 | IEEEFloat::opStatus IEEEFloat::divide(const IEEEFloat &rhs, |
| 1694 | roundingMode rounding_mode) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1695 | opStatus fs; |
| 1696 | |
| 1697 | sign ^= rhs.sign; |
| 1698 | fs = divideSpecials(rhs); |
| 1699 | |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 1700 | if (isFiniteNonZero()) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1701 | lostFraction lost_fraction = divideSignificand(rhs); |
| 1702 | fs = normalize(rounding_mode, lost_fraction); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1703 | if (lost_fraction != lfExactlyZero) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1704 | fs = (opStatus) (fs | opInexact); |
| 1705 | } |
| 1706 | |
| 1707 | return fs; |
| 1708 | } |
| 1709 | |
Dale Johannesen | fe75017 | 2009-01-20 18:35:05 +0000 | [diff] [blame] | 1710 | /* Normalized remainder. This is not currently correct in all cases. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1711 | IEEEFloat::opStatus IEEEFloat::remainder(const IEEEFloat &rhs) { |
Dale Johannesen | fe75017 | 2009-01-20 18:35:05 +0000 | [diff] [blame] | 1712 | opStatus fs; |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1713 | IEEEFloat V = *this; |
Dale Johannesen | fe75017 | 2009-01-20 18:35:05 +0000 | [diff] [blame] | 1714 | unsigned int origSign = sign; |
| 1715 | |
Dale Johannesen | fe75017 | 2009-01-20 18:35:05 +0000 | [diff] [blame] | 1716 | fs = V.divide(rhs, rmNearestTiesToEven); |
| 1717 | if (fs == opDivByZero) |
| 1718 | return fs; |
| 1719 | |
| 1720 | int parts = partCount(); |
Dylan Noblesmith | 4e69e29 | 2014-08-26 02:03:33 +0000 | [diff] [blame] | 1721 | integerPart *x = new integerPart[parts]; |
Dale Johannesen | fe75017 | 2009-01-20 18:35:05 +0000 | [diff] [blame] | 1722 | bool ignored; |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 1723 | fs = V.convertToInteger(makeMutableArrayRef(x, parts), |
| 1724 | parts * integerPartWidth, true, rmNearestTiesToEven, |
| 1725 | &ignored); |
| 1726 | if (fs == opInvalidOp) { |
Sylvestre Ledru | 3ce346d | 2016-11-08 10:00:45 +0000 | [diff] [blame] | 1727 | delete[] x; |
Dale Johannesen | fe75017 | 2009-01-20 18:35:05 +0000 | [diff] [blame] | 1728 | return fs; |
Sylvestre Ledru | 3ce346d | 2016-11-08 10:00:45 +0000 | [diff] [blame] | 1729 | } |
Dale Johannesen | fe75017 | 2009-01-20 18:35:05 +0000 | [diff] [blame] | 1730 | |
| 1731 | fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true, |
| 1732 | rmNearestTiesToEven); |
| 1733 | assert(fs==opOK); // should always work |
| 1734 | |
| 1735 | fs = V.multiply(rhs, rmNearestTiesToEven); |
| 1736 | assert(fs==opOK || fs==opInexact); // should not overflow or underflow |
| 1737 | |
| 1738 | fs = subtract(V, rmNearestTiesToEven); |
| 1739 | assert(fs==opOK || fs==opInexact); // likewise |
| 1740 | |
| 1741 | if (isZero()) |
| 1742 | sign = origSign; // IEEE754 requires this |
Dylan Noblesmith | 4e69e29 | 2014-08-26 02:03:33 +0000 | [diff] [blame] | 1743 | delete[] x; |
Dale Johannesen | fe75017 | 2009-01-20 18:35:05 +0000 | [diff] [blame] | 1744 | return fs; |
| 1745 | } |
| 1746 | |
Stephen Canon | 157c869 | 2017-03-31 20:31:33 +0000 | [diff] [blame] | 1747 | /* Normalized llvm frem (C fmod). */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1748 | IEEEFloat::opStatus IEEEFloat::mod(const IEEEFloat &rhs) { |
Dale Johannesen | 689d17d | 2007-08-31 23:35:31 +0000 | [diff] [blame] | 1749 | opStatus fs; |
Dale Johannesen | b572163 | 2009-01-21 00:35:19 +0000 | [diff] [blame] | 1750 | fs = modSpecials(rhs); |
Dale Johannesen | 689d17d | 2007-08-31 23:35:31 +0000 | [diff] [blame] | 1751 | |
Stephen Canon | 157c869 | 2017-03-31 20:31:33 +0000 | [diff] [blame] | 1752 | while (isFiniteNonZero() && rhs.isFiniteNonZero() && |
| 1753 | compareAbsoluteValue(rhs) != cmpLessThan) { |
| 1754 | IEEEFloat V = scalbn(rhs, ilogb(*this) - ilogb(rhs), rmNearestTiesToEven); |
| 1755 | if (compareAbsoluteValue(V) == cmpLessThan) |
| 1756 | V = scalbn(V, -1, rmNearestTiesToEven); |
| 1757 | V.sign = sign; |
| 1758 | |
Stephen Canon | b12db0e | 2015-09-21 19:29:25 +0000 | [diff] [blame] | 1759 | fs = subtract(V, rmNearestTiesToEven); |
Stephen Canon | 157c869 | 2017-03-31 20:31:33 +0000 | [diff] [blame] | 1760 | assert(fs==opOK); |
Dale Johannesen | b572163 | 2009-01-21 00:35:19 +0000 | [diff] [blame] | 1761 | } |
Dale Johannesen | 689d17d | 2007-08-31 23:35:31 +0000 | [diff] [blame] | 1762 | return fs; |
| 1763 | } |
| 1764 | |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1765 | /* Normalized fused-multiply-add. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1766 | IEEEFloat::opStatus IEEEFloat::fusedMultiplyAdd(const IEEEFloat &multiplicand, |
| 1767 | const IEEEFloat &addend, |
| 1768 | roundingMode rounding_mode) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1769 | opStatus fs; |
| 1770 | |
| 1771 | /* Post-multiplication sign, before addition. */ |
| 1772 | sign ^= multiplicand.sign; |
| 1773 | |
| 1774 | /* If and only if all arguments are normal do we need to do an |
| 1775 | extended-precision calculation. */ |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 1776 | if (isFiniteNonZero() && |
| 1777 | multiplicand.isFiniteNonZero() && |
Hal Finkel | 171c2ec | 2014-10-14 19:23:07 +0000 | [diff] [blame] | 1778 | addend.isFinite()) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1779 | lostFraction lost_fraction; |
| 1780 | |
| 1781 | lost_fraction = multiplySignificand(multiplicand, &addend); |
| 1782 | fs = normalize(rounding_mode, lost_fraction); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1783 | if (lost_fraction != lfExactlyZero) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1784 | fs = (opStatus) (fs | opInexact); |
| 1785 | |
| 1786 | /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a |
| 1787 | positive zero unless rounding to minus infinity, except that |
| 1788 | adding two like-signed zeroes gives that zero. */ |
Lang Hames | 12b12e8 | 2015-01-04 01:20:55 +0000 | [diff] [blame] | 1789 | if (category == fcZero && !(fs & opUnderflow) && sign != addend.sign) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1790 | sign = (rounding_mode == rmTowardNegative); |
| 1791 | } else { |
| 1792 | fs = multiplySpecials(multiplicand); |
| 1793 | |
| 1794 | /* FS can only be opOK or opInvalidOp. There is no more work |
| 1795 | to do in the latter case. The IEEE-754R standard says it is |
| 1796 | implementation-defined in this case whether, if ADDEND is a |
Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1797 | quiet NaN, we raise invalid op; this implementation does so. |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1798 | |
| 1799 | If we need to do the addition we can do so with normal |
| 1800 | precision. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1801 | if (fs == opOK) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1802 | fs = addOrSubtract(addend, rounding_mode, false); |
| 1803 | } |
| 1804 | |
| 1805 | return fs; |
| 1806 | } |
| 1807 | |
Owen Anderson | a40319b | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 1808 | /* Rounding-mode corrrect round to integral value. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1809 | IEEEFloat::opStatus IEEEFloat::roundToIntegral(roundingMode rounding_mode) { |
Owen Anderson | a40319b | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 1810 | opStatus fs; |
Owen Anderson | a40319b | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 1811 | |
Owen Anderson | 352dfff | 2012-08-15 18:28:45 +0000 | [diff] [blame] | 1812 | // If the exponent is large enough, we know that this value is already |
| 1813 | // integral, and the arithmetic below would potentially cause it to saturate |
| 1814 | // to +/-Inf. Bail out early instead. |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 1815 | if (isFiniteNonZero() && exponent+1 >= (int)semanticsPrecision(*semantics)) |
Owen Anderson | 352dfff | 2012-08-15 18:28:45 +0000 | [diff] [blame] | 1816 | return opOK; |
| 1817 | |
Owen Anderson | a40319b | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 1818 | // The algorithm here is quite simple: we add 2^(p-1), where p is the |
| 1819 | // precision of our format, and then subtract it back off again. The choice |
| 1820 | // of rounding modes for the addition/subtraction determines the rounding mode |
| 1821 | // for our integral rounding as well. |
Owen Anderson | be7e297 | 2012-08-15 16:42:53 +0000 | [diff] [blame] | 1822 | // NOTE: When the input value is negative, we do subtraction followed by |
Owen Anderson | 1ff74b0 | 2012-08-15 05:39:46 +0000 | [diff] [blame] | 1823 | // addition instead. |
Owen Anderson | 0b35722 | 2012-08-14 18:51:15 +0000 | [diff] [blame] | 1824 | APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1); |
| 1825 | IntegerConstant <<= semanticsPrecision(*semantics)-1; |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1826 | IEEEFloat MagicConstant(*semantics); |
Owen Anderson | a40319b | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 1827 | fs = MagicConstant.convertFromAPInt(IntegerConstant, false, |
| 1828 | rmNearestTiesToEven); |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 1829 | MagicConstant.sign = sign; |
Owen Anderson | 1ff74b0 | 2012-08-15 05:39:46 +0000 | [diff] [blame] | 1830 | |
Owen Anderson | a40319b | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 1831 | if (fs != opOK) |
| 1832 | return fs; |
| 1833 | |
Owen Anderson | 1ff74b0 | 2012-08-15 05:39:46 +0000 | [diff] [blame] | 1834 | // Preserve the input sign so that we can handle 0.0/-0.0 cases correctly. |
| 1835 | bool inputSign = isNegative(); |
| 1836 | |
Owen Anderson | a40319b | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 1837 | fs = add(MagicConstant, rounding_mode); |
| 1838 | if (fs != opOK && fs != opInexact) |
| 1839 | return fs; |
| 1840 | |
| 1841 | fs = subtract(MagicConstant, rounding_mode); |
Owen Anderson | 1ff74b0 | 2012-08-15 05:39:46 +0000 | [diff] [blame] | 1842 | |
| 1843 | // Restore the input sign. |
| 1844 | if (inputSign != isNegative()) |
| 1845 | changeSign(); |
| 1846 | |
Owen Anderson | a40319b | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 1847 | return fs; |
| 1848 | } |
| 1849 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 1850 | |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1851 | /* Comparison requires normalized numbers. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1852 | IEEEFloat::cmpResult IEEEFloat::compare(const IEEEFloat &rhs) const { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1853 | cmpResult result; |
| 1854 | |
| 1855 | assert(semantics == rhs.semantics); |
| 1856 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1857 | switch (PackCategoriesIntoKey(category, rhs.category)) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1858 | default: |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1859 | llvm_unreachable(nullptr); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1860 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1861 | case PackCategoriesIntoKey(fcNaN, fcZero): |
| 1862 | case PackCategoriesIntoKey(fcNaN, fcNormal): |
| 1863 | case PackCategoriesIntoKey(fcNaN, fcInfinity): |
| 1864 | case PackCategoriesIntoKey(fcNaN, fcNaN): |
| 1865 | case PackCategoriesIntoKey(fcZero, fcNaN): |
| 1866 | case PackCategoriesIntoKey(fcNormal, fcNaN): |
| 1867 | case PackCategoriesIntoKey(fcInfinity, fcNaN): |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1868 | return cmpUnordered; |
| 1869 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1870 | case PackCategoriesIntoKey(fcInfinity, fcNormal): |
| 1871 | case PackCategoriesIntoKey(fcInfinity, fcZero): |
| 1872 | case PackCategoriesIntoKey(fcNormal, fcZero): |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1873 | if (sign) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1874 | return cmpLessThan; |
| 1875 | else |
| 1876 | return cmpGreaterThan; |
| 1877 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1878 | case PackCategoriesIntoKey(fcNormal, fcInfinity): |
| 1879 | case PackCategoriesIntoKey(fcZero, fcInfinity): |
| 1880 | case PackCategoriesIntoKey(fcZero, fcNormal): |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1881 | if (rhs.sign) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1882 | return cmpGreaterThan; |
| 1883 | else |
| 1884 | return cmpLessThan; |
| 1885 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1886 | case PackCategoriesIntoKey(fcInfinity, fcInfinity): |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1887 | if (sign == rhs.sign) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1888 | return cmpEqual; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1889 | else if (sign) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1890 | return cmpLessThan; |
| 1891 | else |
| 1892 | return cmpGreaterThan; |
| 1893 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1894 | case PackCategoriesIntoKey(fcZero, fcZero): |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1895 | return cmpEqual; |
| 1896 | |
Michael Gottesman | 9b877e1 | 2013-06-24 09:57:57 +0000 | [diff] [blame] | 1897 | case PackCategoriesIntoKey(fcNormal, fcNormal): |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1898 | break; |
| 1899 | } |
| 1900 | |
| 1901 | /* Two normal numbers. Do they have the same sign? */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1902 | if (sign != rhs.sign) { |
| 1903 | if (sign) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1904 | result = cmpLessThan; |
| 1905 | else |
| 1906 | result = cmpGreaterThan; |
| 1907 | } else { |
| 1908 | /* Compare absolute values; invert result if negative. */ |
| 1909 | result = compareAbsoluteValue(rhs); |
| 1910 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1911 | if (sign) { |
| 1912 | if (result == cmpLessThan) |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 1913 | result = cmpGreaterThan; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 1914 | else if (result == cmpGreaterThan) |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 1915 | result = cmpLessThan; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | return result; |
| 1920 | } |
| 1921 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1922 | /// IEEEFloat::convert - convert a value of one floating point type to another. |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 1923 | /// The return value corresponds to the IEEE754 exceptions. *losesInfo |
| 1924 | /// records whether the transformation lost information, i.e. whether |
| 1925 | /// converting the result back to the original type will produce the |
| 1926 | /// original value (this is almost the same as return value==fsOK, but there |
| 1927 | /// are edge cases where this is not so). |
| 1928 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 1929 | IEEEFloat::opStatus IEEEFloat::convert(const fltSemantics &toSemantics, |
| 1930 | roundingMode rounding_mode, |
| 1931 | bool *losesInfo) { |
Neil Booth | a8d7269 | 2007-09-22 02:56:19 +0000 | [diff] [blame] | 1932 | lostFraction lostFraction; |
| 1933 | unsigned int newPartCount, oldPartCount; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1934 | opStatus fs; |
Eli Friedman | a84ad7d | 2011-11-26 03:38:02 +0000 | [diff] [blame] | 1935 | int shift; |
| 1936 | const fltSemantics &fromSemantics = *semantics; |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 1937 | |
Neil Booth | a8d7269 | 2007-09-22 02:56:19 +0000 | [diff] [blame] | 1938 | lostFraction = lfExactlyZero; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1939 | newPartCount = partCountForBits(toSemantics.precision + 1); |
Neil Booth | a8d7269 | 2007-09-22 02:56:19 +0000 | [diff] [blame] | 1940 | oldPartCount = partCount(); |
Eli Friedman | a84ad7d | 2011-11-26 03:38:02 +0000 | [diff] [blame] | 1941 | shift = toSemantics.precision - fromSemantics.precision; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1942 | |
Eli Friedman | a84ad7d | 2011-11-26 03:38:02 +0000 | [diff] [blame] | 1943 | bool X86SpecialNan = false; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1944 | if (&fromSemantics == &semX87DoubleExtended && |
| 1945 | &toSemantics != &semX87DoubleExtended && category == fcNaN && |
Eli Friedman | a84ad7d | 2011-11-26 03:38:02 +0000 | [diff] [blame] | 1946 | (!(*significandParts() & 0x8000000000000000ULL) || |
| 1947 | !(*significandParts() & 0x4000000000000000ULL))) { |
| 1948 | // x86 has some unusual NaNs which cannot be represented in any other |
| 1949 | // format; note them here. |
| 1950 | X86SpecialNan = true; |
| 1951 | } |
| 1952 | |
Ulrich Weigand | 1d4dbda | 2013-07-16 13:03:25 +0000 | [diff] [blame] | 1953 | // If this is a truncation of a denormal number, and the target semantics |
| 1954 | // has larger exponent range than the source semantics (this can happen |
| 1955 | // when truncating from PowerPC double-double to double format), the |
| 1956 | // right shift could lose result mantissa bits. Adjust exponent instead |
| 1957 | // of performing excessive shift. |
| 1958 | if (shift < 0 && isFiniteNonZero()) { |
| 1959 | int exponentChange = significandMSB() + 1 - fromSemantics.precision; |
| 1960 | if (exponent + exponentChange < toSemantics.minExponent) |
| 1961 | exponentChange = toSemantics.minExponent - exponent; |
| 1962 | if (exponentChange < shift) |
| 1963 | exponentChange = shift; |
| 1964 | if (exponentChange < 0) { |
| 1965 | shift -= exponentChange; |
| 1966 | exponent += exponentChange; |
| 1967 | } |
| 1968 | } |
| 1969 | |
Eli Friedman | a84ad7d | 2011-11-26 03:38:02 +0000 | [diff] [blame] | 1970 | // If this is a truncation, perform the shift before we narrow the storage. |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 1971 | if (shift < 0 && (isFiniteNonZero() || category==fcNaN)) |
Eli Friedman | a84ad7d | 2011-11-26 03:38:02 +0000 | [diff] [blame] | 1972 | lostFraction = shiftRight(significandParts(), oldPartCount, -shift); |
| 1973 | |
| 1974 | // Fix the storage so it can hold to new value. |
Neil Booth | a8d7269 | 2007-09-22 02:56:19 +0000 | [diff] [blame] | 1975 | if (newPartCount > oldPartCount) { |
Eli Friedman | a84ad7d | 2011-11-26 03:38:02 +0000 | [diff] [blame] | 1976 | // The new type requires more storage; make it available. |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1977 | integerPart *newParts; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1978 | newParts = new integerPart[newPartCount]; |
| 1979 | APInt::tcSet(newParts, 0, newPartCount); |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 1980 | if (isFiniteNonZero() || category==fcNaN) |
Dale Johannesen | 4f55d9f | 2007-09-25 17:25:00 +0000 | [diff] [blame] | 1981 | APInt::tcAssign(newParts, significandParts(), oldPartCount); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1982 | freeSignificand(); |
| 1983 | significand.parts = newParts; |
Eli Friedman | a84ad7d | 2011-11-26 03:38:02 +0000 | [diff] [blame] | 1984 | } else if (newPartCount == 1 && oldPartCount != 1) { |
| 1985 | // Switch to built-in storage for a single part. |
| 1986 | integerPart newPart = 0; |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 1987 | if (isFiniteNonZero() || category==fcNaN) |
Eli Friedman | a84ad7d | 2011-11-26 03:38:02 +0000 | [diff] [blame] | 1988 | newPart = significandParts()[0]; |
| 1989 | freeSignificand(); |
| 1990 | significand.part = newPart; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Eli Friedman | a84ad7d | 2011-11-26 03:38:02 +0000 | [diff] [blame] | 1993 | // Now that we have the right storage, switch the semantics. |
| 1994 | semantics = &toSemantics; |
| 1995 | |
| 1996 | // If this is an extension, perform the shift now that the storage is |
| 1997 | // available. |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 1998 | if (shift > 0 && (isFiniteNonZero() || category==fcNaN)) |
Eli Friedman | a84ad7d | 2011-11-26 03:38:02 +0000 | [diff] [blame] | 1999 | APInt::tcShiftLeft(significandParts(), newPartCount, shift); |
| 2000 | |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 2001 | if (isFiniteNonZero()) { |
Neil Booth | a8d7269 | 2007-09-22 02:56:19 +0000 | [diff] [blame] | 2002 | fs = normalize(rounding_mode, lostFraction); |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 2003 | *losesInfo = (fs != opOK); |
Dale Johannesen | 4f55d9f | 2007-09-25 17:25:00 +0000 | [diff] [blame] | 2004 | } else if (category == fcNaN) { |
Eli Friedman | a84ad7d | 2011-11-26 03:38:02 +0000 | [diff] [blame] | 2005 | *losesInfo = lostFraction != lfExactlyZero || X86SpecialNan; |
Benjamin Kramer | b361adb | 2013-01-25 17:01:00 +0000 | [diff] [blame] | 2006 | |
| 2007 | // For x87 extended precision, we want to make a NaN, not a special NaN if |
| 2008 | // the input wasn't special either. |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2009 | if (!X86SpecialNan && semantics == &semX87DoubleExtended) |
Benjamin Kramer | b361adb | 2013-01-25 17:01:00 +0000 | [diff] [blame] | 2010 | APInt::tcSetBit(significandParts(), semantics->precision - 1); |
| 2011 | |
Dale Johannesen | 4f55d9f | 2007-09-25 17:25:00 +0000 | [diff] [blame] | 2012 | // gcc forces the Quiet bit on, which means (float)(double)(float_sNan) |
| 2013 | // does not give you back the same bits. This is dubious, and we |
| 2014 | // don't currently do it. You're really supposed to get |
| 2015 | // an invalid operation signal at runtime, but nobody does that. |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 2016 | fs = opOK; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2017 | } else { |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 2018 | *losesInfo = false; |
Eli Friedman | 31f0116 | 2011-11-28 18:50:37 +0000 | [diff] [blame] | 2019 | fs = opOK; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | return fs; |
| 2023 | } |
| 2024 | |
| 2025 | /* Convert a floating point number to an integer according to the |
| 2026 | rounding mode. If the rounded integer value is out of range this |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2027 | returns an invalid operation exception and the contents of the |
| 2028 | destination parts are unspecified. If the rounded value is in |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2029 | range but the floating point number is not the exact integer, the C |
| 2030 | standard doesn't require an inexact exception to be raised. IEEE |
| 2031 | 854 does require it so we do that. |
| 2032 | |
| 2033 | Note that for conversions to integer type the C standard requires |
| 2034 | round-to-zero to always be used. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2035 | IEEEFloat::opStatus IEEEFloat::convertToSignExtendedInteger( |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 2036 | MutableArrayRef<integerPart> parts, unsigned int width, bool isSigned, |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2037 | roundingMode rounding_mode, bool *isExact) const { |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2038 | lostFraction lost_fraction; |
| 2039 | const integerPart *src; |
| 2040 | unsigned int dstPartsCount, truncatedBits; |
| 2041 | |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 2042 | *isExact = false; |
| 2043 | |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2044 | /* Handle the three special cases first. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2045 | if (category == fcInfinity || category == fcNaN) |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2046 | return opInvalidOp; |
| 2047 | |
| 2048 | dstPartsCount = partCountForBits(width); |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 2049 | assert(dstPartsCount <= parts.size() && "Integer too big"); |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2050 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2051 | if (category == fcZero) { |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 2052 | APInt::tcSet(parts.data(), 0, dstPartsCount); |
Dale Johannesen | 7221af3 | 2008-10-07 00:40:01 +0000 | [diff] [blame] | 2053 | // Negative zero can't be represented as an int. |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 2054 | *isExact = !sign; |
| 2055 | return opOK; |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | src = significandParts(); |
| 2059 | |
| 2060 | /* Step 1: place our absolute value, with any fraction truncated, in |
| 2061 | the destination. */ |
| 2062 | if (exponent < 0) { |
| 2063 | /* Our absolute value is less than one; truncate everything. */ |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 2064 | APInt::tcSet(parts.data(), 0, dstPartsCount); |
Dale Johannesen | 740e987 | 2009-01-19 21:17:05 +0000 | [diff] [blame] | 2065 | /* For exponent -1 the integer bit represents .5, look at that. |
| 2066 | For smaller exponents leftmost truncated bit is 0. */ |
| 2067 | truncatedBits = semantics->precision -1U - exponent; |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2068 | } else { |
| 2069 | /* We want the most significant (exponent + 1) bits; the rest are |
| 2070 | truncated. */ |
| 2071 | unsigned int bits = exponent + 1U; |
| 2072 | |
| 2073 | /* Hopelessly large in magnitude? */ |
| 2074 | if (bits > width) |
| 2075 | return opInvalidOp; |
| 2076 | |
| 2077 | if (bits < semantics->precision) { |
| 2078 | /* We truncate (semantics->precision - bits) bits. */ |
| 2079 | truncatedBits = semantics->precision - bits; |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 2080 | APInt::tcExtract(parts.data(), dstPartsCount, src, bits, truncatedBits); |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2081 | } else { |
| 2082 | /* We want at least as many bits as are available. */ |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 2083 | APInt::tcExtract(parts.data(), dstPartsCount, src, semantics->precision, |
| 2084 | 0); |
| 2085 | APInt::tcShiftLeft(parts.data(), dstPartsCount, |
| 2086 | bits - semantics->precision); |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2087 | truncatedBits = 0; |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | /* Step 2: work out any lost fraction, and increment the absolute |
| 2092 | value if we would round away from zero. */ |
| 2093 | if (truncatedBits) { |
| 2094 | lost_fraction = lostFractionThroughTruncation(src, partCount(), |
| 2095 | truncatedBits); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2096 | if (lost_fraction != lfExactlyZero && |
| 2097 | roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) { |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 2098 | if (APInt::tcIncrement(parts.data(), dstPartsCount)) |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2099 | return opInvalidOp; /* Overflow. */ |
| 2100 | } |
| 2101 | } else { |
| 2102 | lost_fraction = lfExactlyZero; |
| 2103 | } |
| 2104 | |
| 2105 | /* Step 3: check if we fit in the destination. */ |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 2106 | unsigned int omsb = APInt::tcMSB(parts.data(), dstPartsCount) + 1; |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2107 | |
| 2108 | if (sign) { |
| 2109 | if (!isSigned) { |
| 2110 | /* Negative numbers cannot be represented as unsigned. */ |
| 2111 | if (omsb != 0) |
| 2112 | return opInvalidOp; |
| 2113 | } else { |
| 2114 | /* It takes omsb bits to represent the unsigned integer value. |
| 2115 | We lose a bit for the sign, but care is needed as the |
| 2116 | maximally negative integer is a special case. */ |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 2117 | if (omsb == width && |
| 2118 | APInt::tcLSB(parts.data(), dstPartsCount) + 1 != omsb) |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2119 | return opInvalidOp; |
| 2120 | |
| 2121 | /* This case can happen because of rounding. */ |
| 2122 | if (omsb > width) |
| 2123 | return opInvalidOp; |
| 2124 | } |
| 2125 | |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 2126 | APInt::tcNegate (parts.data(), dstPartsCount); |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2127 | } else { |
| 2128 | if (omsb >= width + !isSigned) |
| 2129 | return opInvalidOp; |
| 2130 | } |
| 2131 | |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 2132 | if (lost_fraction == lfExactlyZero) { |
| 2133 | *isExact = true; |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2134 | return opOK; |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 2135 | } else |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2136 | return opInexact; |
| 2137 | } |
| 2138 | |
| 2139 | /* Same as convertToSignExtendedInteger, except we provide |
| 2140 | deterministic values in case of an invalid operation exception, |
| 2141 | namely zero for NaNs and the minimal or maximal value respectively |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 2142 | for underflow or overflow. |
| 2143 | The *isExact output tells whether the result is exact, in the sense |
| 2144 | that converting it back to the original floating point type produces |
| 2145 | the original value. This is almost equivalent to result==opOK, |
| 2146 | except for negative zeroes. |
| 2147 | */ |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 2148 | IEEEFloat::opStatus |
| 2149 | IEEEFloat::convertToInteger(MutableArrayRef<integerPart> parts, |
| 2150 | unsigned int width, bool isSigned, |
| 2151 | roundingMode rounding_mode, bool *isExact) const { |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2152 | opStatus fs; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2153 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2154 | fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode, |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 2155 | isExact); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2156 | |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2157 | if (fs == opInvalidOp) { |
| 2158 | unsigned int bits, dstPartsCount; |
| 2159 | |
| 2160 | dstPartsCount = partCountForBits(width); |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 2161 | assert(dstPartsCount <= parts.size() && "Integer too big"); |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2162 | |
| 2163 | if (category == fcNaN) |
| 2164 | bits = 0; |
| 2165 | else if (sign) |
| 2166 | bits = isSigned; |
| 2167 | else |
| 2168 | bits = width - isSigned; |
| 2169 | |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 2170 | APInt::tcSetLeastSignificantBits(parts.data(), dstPartsCount, bits); |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2171 | if (sign && isSigned) |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 2172 | APInt::tcShiftLeft(parts.data(), dstPartsCount, width - 1); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
Neil Booth | 618d0fc | 2007-11-01 22:43:37 +0000 | [diff] [blame] | 2175 | return fs; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
Neil Booth | 6c1c858 | 2007-10-07 12:07:53 +0000 | [diff] [blame] | 2178 | /* Convert an unsigned integer SRC to a floating point number, |
| 2179 | rounding according to ROUNDING_MODE. The sign of the floating |
| 2180 | point number is not modified. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2181 | IEEEFloat::opStatus IEEEFloat::convertFromUnsignedParts( |
| 2182 | const integerPart *src, unsigned int srcCount, roundingMode rounding_mode) { |
Neil Booth | 49c6aab | 2007-10-08 14:39:42 +0000 | [diff] [blame] | 2183 | unsigned int omsb, precision, dstCount; |
Neil Booth | 6c1c858 | 2007-10-07 12:07:53 +0000 | [diff] [blame] | 2184 | integerPart *dst; |
Neil Booth | 49c6aab | 2007-10-08 14:39:42 +0000 | [diff] [blame] | 2185 | lostFraction lost_fraction; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2186 | |
| 2187 | category = fcNormal; |
Neil Booth | 49c6aab | 2007-10-08 14:39:42 +0000 | [diff] [blame] | 2188 | omsb = APInt::tcMSB(src, srcCount) + 1; |
Neil Booth | 6c1c858 | 2007-10-07 12:07:53 +0000 | [diff] [blame] | 2189 | dst = significandParts(); |
| 2190 | dstCount = partCount(); |
Neil Booth | 49c6aab | 2007-10-08 14:39:42 +0000 | [diff] [blame] | 2191 | precision = semantics->precision; |
Neil Booth | 6c1c858 | 2007-10-07 12:07:53 +0000 | [diff] [blame] | 2192 | |
Nick Lewycky | f66daac | 2011-10-03 21:30:08 +0000 | [diff] [blame] | 2193 | /* We want the most significant PRECISION bits of SRC. There may not |
Neil Booth | 49c6aab | 2007-10-08 14:39:42 +0000 | [diff] [blame] | 2194 | be that many; extract what we can. */ |
| 2195 | if (precision <= omsb) { |
| 2196 | exponent = omsb - 1; |
Neil Booth | 6c1c858 | 2007-10-07 12:07:53 +0000 | [diff] [blame] | 2197 | lost_fraction = lostFractionThroughTruncation(src, srcCount, |
Neil Booth | 49c6aab | 2007-10-08 14:39:42 +0000 | [diff] [blame] | 2198 | omsb - precision); |
| 2199 | APInt::tcExtract(dst, dstCount, src, precision, omsb - precision); |
| 2200 | } else { |
| 2201 | exponent = precision - 1; |
| 2202 | lost_fraction = lfExactlyZero; |
| 2203 | APInt::tcExtract(dst, dstCount, src, omsb, 0); |
Neil Booth | 6c1c858 | 2007-10-07 12:07:53 +0000 | [diff] [blame] | 2204 | } |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2205 | |
| 2206 | return normalize(rounding_mode, lost_fraction); |
| 2207 | } |
| 2208 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2209 | IEEEFloat::opStatus IEEEFloat::convertFromAPInt(const APInt &Val, bool isSigned, |
| 2210 | roundingMode rounding_mode) { |
Dan Gohman | 35723eb | 2008-02-29 01:26:11 +0000 | [diff] [blame] | 2211 | unsigned int partCount = Val.getNumWords(); |
| 2212 | APInt api = Val; |
| 2213 | |
| 2214 | sign = false; |
| 2215 | if (isSigned && api.isNegative()) { |
| 2216 | sign = true; |
| 2217 | api = -api; |
| 2218 | } |
| 2219 | |
| 2220 | return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode); |
| 2221 | } |
| 2222 | |
Neil Booth | 03f58ab | 2007-10-07 12:15:41 +0000 | [diff] [blame] | 2223 | /* Convert a two's complement integer SRC to a floating point number, |
| 2224 | rounding according to ROUNDING_MODE. ISSIGNED is true if the |
| 2225 | integer is signed, in which case it must be sign-extended. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2226 | IEEEFloat::opStatus |
| 2227 | IEEEFloat::convertFromSignExtendedInteger(const integerPart *src, |
| 2228 | unsigned int srcCount, bool isSigned, |
| 2229 | roundingMode rounding_mode) { |
Neil Booth | 03f58ab | 2007-10-07 12:15:41 +0000 | [diff] [blame] | 2230 | opStatus status; |
| 2231 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2232 | if (isSigned && |
| 2233 | APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) { |
Dylan Noblesmith | 4e69e29 | 2014-08-26 02:03:33 +0000 | [diff] [blame] | 2234 | integerPart *copy; |
Neil Booth | 03f58ab | 2007-10-07 12:15:41 +0000 | [diff] [blame] | 2235 | |
| 2236 | /* If we're signed and negative negate a copy. */ |
| 2237 | sign = true; |
Dylan Noblesmith | 4e69e29 | 2014-08-26 02:03:33 +0000 | [diff] [blame] | 2238 | copy = new integerPart[srcCount]; |
Neil Booth | 03f58ab | 2007-10-07 12:15:41 +0000 | [diff] [blame] | 2239 | APInt::tcAssign(copy, src, srcCount); |
| 2240 | APInt::tcNegate(copy, srcCount); |
| 2241 | status = convertFromUnsignedParts(copy, srcCount, rounding_mode); |
Dylan Noblesmith | 4e69e29 | 2014-08-26 02:03:33 +0000 | [diff] [blame] | 2242 | delete [] copy; |
Neil Booth | 03f58ab | 2007-10-07 12:15:41 +0000 | [diff] [blame] | 2243 | } else { |
| 2244 | sign = false; |
| 2245 | status = convertFromUnsignedParts(src, srcCount, rounding_mode); |
| 2246 | } |
| 2247 | |
| 2248 | return status; |
| 2249 | } |
| 2250 | |
Neil Booth | 5f00973 | 2007-10-07 11:45:55 +0000 | [diff] [blame] | 2251 | /* FIXME: should this just take a const APInt reference? */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2252 | IEEEFloat::opStatus |
| 2253 | IEEEFloat::convertFromZeroExtendedInteger(const integerPart *parts, |
| 2254 | unsigned int width, bool isSigned, |
| 2255 | roundingMode rounding_mode) { |
Dale Johannesen | 4230512 | 2007-09-21 22:09:37 +0000 | [diff] [blame] | 2256 | unsigned int partCount = partCountForBits(width); |
Jeffrey Yasskin | 7a16288 | 2011-07-18 21:45:40 +0000 | [diff] [blame] | 2257 | APInt api = APInt(width, makeArrayRef(parts, partCount)); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2258 | |
| 2259 | sign = false; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2260 | if (isSigned && APInt::tcExtractBit(parts, width - 1)) { |
Dale Johannesen | 28a2c4a | 2007-09-30 18:17:01 +0000 | [diff] [blame] | 2261 | sign = true; |
| 2262 | api = -api; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
Neil Booth | ba20522 | 2007-10-07 12:10:57 +0000 | [diff] [blame] | 2265 | return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2268 | IEEEFloat::opStatus |
| 2269 | IEEEFloat::convertFromHexadecimalString(StringRef s, |
| 2270 | roundingMode rounding_mode) { |
Erick Tryzelaar | a9680df | 2009-08-18 18:20:37 +0000 | [diff] [blame] | 2271 | lostFraction lost_fraction = lfExactlyZero; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2272 | |
Michael Gottesman | 30a90eb | 2013-07-27 21:49:21 +0000 | [diff] [blame] | 2273 | category = fcNormal; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2274 | zeroSignificand(); |
| 2275 | exponent = 0; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2276 | |
Eli Friedman | d2eb07a | 2013-07-17 22:17:29 +0000 | [diff] [blame] | 2277 | integerPart *significand = significandParts(); |
| 2278 | unsigned partsCount = partCount(); |
| 2279 | unsigned bitPos = partsCount * integerPartWidth; |
| 2280 | bool computedTrailingFraction = false; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2281 | |
Eli Friedman | d2eb07a | 2013-07-17 22:17:29 +0000 | [diff] [blame] | 2282 | // Skip leading zeroes and any (hexa)decimal point. |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 2283 | StringRef::iterator begin = s.begin(); |
| 2284 | StringRef::iterator end = s.end(); |
Eli Friedman | d2eb07a | 2013-07-17 22:17:29 +0000 | [diff] [blame] | 2285 | StringRef::iterator dot; |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 2286 | StringRef::iterator p = skipLeadingZeroesAndAnyDot(begin, end, &dot); |
Eli Friedman | d2eb07a | 2013-07-17 22:17:29 +0000 | [diff] [blame] | 2287 | StringRef::iterator firstSignificantDigit = p; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2288 | |
Eli Friedman | d2eb07a | 2013-07-17 22:17:29 +0000 | [diff] [blame] | 2289 | while (p != end) { |
Dale Johannesen | fa48372 | 2008-05-14 22:53:25 +0000 | [diff] [blame] | 2290 | integerPart hex_value; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2291 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2292 | if (*p == '.') { |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 2293 | assert(dot == end && "String contains multiple dots"); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2294 | dot = p++; |
Eli Friedman | d2eb07a | 2013-07-17 22:17:29 +0000 | [diff] [blame] | 2295 | continue; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | hex_value = hexDigitValue(*p); |
Eli Friedman | d2eb07a | 2013-07-17 22:17:29 +0000 | [diff] [blame] | 2299 | if (hex_value == -1U) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2300 | break; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2301 | |
| 2302 | p++; |
| 2303 | |
Eli Friedman | d2eb07a | 2013-07-17 22:17:29 +0000 | [diff] [blame] | 2304 | // Store the number while we have space. |
| 2305 | if (bitPos) { |
| 2306 | bitPos -= 4; |
| 2307 | hex_value <<= bitPos % integerPartWidth; |
| 2308 | significand[bitPos / integerPartWidth] |= hex_value; |
| 2309 | } else if (!computedTrailingFraction) { |
| 2310 | lost_fraction = trailingHexadecimalFraction(p, end, hex_value); |
| 2311 | computedTrailingFraction = true; |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2312 | } |
| 2313 | } |
| 2314 | |
| 2315 | /* Hex floats require an exponent but not a hexadecimal point. */ |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 2316 | assert(p != end && "Hex strings require an exponent"); |
| 2317 | assert((*p == 'p' || *p == 'P') && "Invalid character in significand"); |
| 2318 | assert(p != begin && "Significand has no digits"); |
| 2319 | assert((dot == end || p - begin != 1) && "Significand has no digits"); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2320 | |
| 2321 | /* Ignore the exponent if we are zero. */ |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2322 | if (p != firstSignificantDigit) { |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2323 | int expAdjustment; |
| 2324 | |
| 2325 | /* Implicit hexadecimal point? */ |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 2326 | if (dot == end) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2327 | dot = p; |
| 2328 | |
| 2329 | /* Calculate the exponent adjustment implicit in the number of |
| 2330 | significant digits. */ |
Evan Cheng | 82b9e96 | 2008-05-02 21:15:08 +0000 | [diff] [blame] | 2331 | expAdjustment = static_cast<int>(dot - firstSignificantDigit); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2332 | if (expAdjustment < 0) |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2333 | expAdjustment++; |
| 2334 | expAdjustment = expAdjustment * 4 - 1; |
| 2335 | |
| 2336 | /* Adjust for writing the significand starting at the most |
| 2337 | significant nibble. */ |
| 2338 | expAdjustment += semantics->precision; |
| 2339 | expAdjustment -= partsCount * integerPartWidth; |
| 2340 | |
| 2341 | /* Adjust for the given exponent. */ |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 2342 | exponent = totalExponent(p + 1, end, expAdjustment); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | return normalize(rounding_mode, lost_fraction); |
| 2346 | } |
| 2347 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2348 | IEEEFloat::opStatus |
| 2349 | IEEEFloat::roundSignificandWithExponent(const integerPart *decSigParts, |
| 2350 | unsigned sigPartCount, int exp, |
| 2351 | roundingMode rounding_mode) { |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2352 | unsigned int parts, pow5PartCount; |
Tamas Berghammer | b6b0ddf | 2015-07-09 10:13:39 +0000 | [diff] [blame] | 2353 | fltSemantics calcSemantics = { 32767, -32767, 0, 0 }; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2354 | integerPart pow5Parts[maxPowerOfFiveParts]; |
| 2355 | bool isNearest; |
| 2356 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2357 | isNearest = (rounding_mode == rmNearestTiesToEven || |
| 2358 | rounding_mode == rmNearestTiesToAway); |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2359 | |
| 2360 | parts = partCountForBits(semantics->precision + 11); |
| 2361 | |
| 2362 | /* Calculate pow(5, abs(exp)). */ |
| 2363 | pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp); |
| 2364 | |
| 2365 | for (;; parts *= 2) { |
| 2366 | opStatus sigStatus, powStatus; |
| 2367 | unsigned int excessPrecision, truncatedBits; |
| 2368 | |
| 2369 | calcSemantics.precision = parts * integerPartWidth - 1; |
| 2370 | excessPrecision = calcSemantics.precision - semantics->precision; |
| 2371 | truncatedBits = excessPrecision; |
| 2372 | |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 2373 | IEEEFloat decSig(calcSemantics, uninitialized); |
| 2374 | decSig.makeZero(sign); |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2375 | IEEEFloat pow5(calcSemantics); |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2376 | |
| 2377 | sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount, |
| 2378 | rmNearestTiesToEven); |
| 2379 | powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount, |
| 2380 | rmNearestTiesToEven); |
| 2381 | /* Add exp, as 10^n = 5^n * 2^n. */ |
| 2382 | decSig.exponent += exp; |
| 2383 | |
| 2384 | lostFraction calcLostFraction; |
Evan Cheng | 82b9e96 | 2008-05-02 21:15:08 +0000 | [diff] [blame] | 2385 | integerPart HUerr, HUdistance; |
| 2386 | unsigned int powHUerr; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2387 | |
| 2388 | if (exp >= 0) { |
| 2389 | /* multiplySignificand leaves the precision-th bit set to 1. */ |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 2390 | calcLostFraction = decSig.multiplySignificand(pow5, nullptr); |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2391 | powHUerr = powStatus != opOK; |
| 2392 | } else { |
| 2393 | calcLostFraction = decSig.divideSignificand(pow5); |
| 2394 | /* Denormal numbers have less precision. */ |
| 2395 | if (decSig.exponent < semantics->minExponent) { |
| 2396 | excessPrecision += (semantics->minExponent - decSig.exponent); |
| 2397 | truncatedBits = excessPrecision; |
| 2398 | if (excessPrecision > calcSemantics.precision) |
| 2399 | excessPrecision = calcSemantics.precision; |
| 2400 | } |
| 2401 | /* Extra half-ulp lost in reciprocal of exponent. */ |
Evan Cheng | 82b9e96 | 2008-05-02 21:15:08 +0000 | [diff] [blame] | 2402 | powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
| 2405 | /* Both multiplySignificand and divideSignificand return the |
| 2406 | result with the integer bit set. */ |
Evan Cheng | 67c9021 | 2009-10-27 21:35:42 +0000 | [diff] [blame] | 2407 | assert(APInt::tcExtractBit |
| 2408 | (decSig.significandParts(), calcSemantics.precision - 1) == 1); |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2409 | |
| 2410 | HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK, |
| 2411 | powHUerr); |
| 2412 | HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(), |
| 2413 | excessPrecision, isNearest); |
| 2414 | |
| 2415 | /* Are we guaranteed to round correctly if we truncate? */ |
| 2416 | if (HUdistance >= HUerr) { |
| 2417 | APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(), |
| 2418 | calcSemantics.precision - excessPrecision, |
| 2419 | excessPrecision); |
| 2420 | /* Take the exponent of decSig. If we tcExtract-ed less bits |
| 2421 | above we must adjust our exponent to compensate for the |
| 2422 | implicit right shift. */ |
| 2423 | exponent = (decSig.exponent + semantics->precision |
| 2424 | - (calcSemantics.precision - excessPrecision)); |
| 2425 | calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(), |
| 2426 | decSig.partCount(), |
| 2427 | truncatedBits); |
| 2428 | return normalize(rounding_mode, calcLostFraction); |
| 2429 | } |
| 2430 | } |
| 2431 | } |
| 2432 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2433 | IEEEFloat::opStatus |
| 2434 | IEEEFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode) { |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 2435 | decimalInfo D; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2436 | opStatus fs; |
| 2437 | |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 2438 | /* Scan the text. */ |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 2439 | StringRef::iterator p = str.begin(); |
| 2440 | interpretDecimal(p, str.end(), &D); |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2441 | |
Neil Booth | 9130551 | 2007-10-15 15:00:55 +0000 | [diff] [blame] | 2442 | /* Handle the quick cases. First the case of no significant digits, |
| 2443 | i.e. zero, and then exponents that are obviously too large or too |
| 2444 | small. Writing L for log 10 / log 2, a number d.ddddd*10^exp |
| 2445 | definitely overflows if |
| 2446 | |
| 2447 | (exp - 1) * L >= maxExponent |
| 2448 | |
| 2449 | and definitely underflows to zero where |
| 2450 | |
| 2451 | (exp + 1) * L <= minExponent - precision |
| 2452 | |
| 2453 | With integer arithmetic the tightest bounds for L are |
| 2454 | |
| 2455 | 93/28 < L < 196/59 [ numerator <= 256 ] |
| 2456 | 42039/12655 < L < 28738/8651 [ numerator <= 65536 ] |
| 2457 | */ |
| 2458 | |
Michael Gottesman | 228156c | 2013-07-01 23:54:08 +0000 | [diff] [blame] | 2459 | // Test if we have a zero number allowing for strings with no null terminators |
| 2460 | // and zero decimals with non-zero exponents. |
Simon Pilgrim | a284959 | 2017-03-20 13:53:59 +0000 | [diff] [blame] | 2461 | // |
Michael Gottesman | 228156c | 2013-07-01 23:54:08 +0000 | [diff] [blame] | 2462 | // We computed firstSigDigit by ignoring all zeros and dots. Thus if |
| 2463 | // D->firstSigDigit equals str.end(), every digit must be a zero and there can |
| 2464 | // be at most one dot. On the other hand, if we have a zero with a non-zero |
| 2465 | // exponent, then we know that D.firstSigDigit will be non-numeric. |
Michael Gottesman | 94d6195 | 2013-07-02 15:50:05 +0000 | [diff] [blame] | 2466 | if (D.firstSigDigit == str.end() || decDigitValue(*D.firstSigDigit) >= 10U) { |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2467 | category = fcZero; |
| 2468 | fs = opOK; |
John McCall | b42cc68 | 2010-02-26 22:20:41 +0000 | [diff] [blame] | 2469 | |
| 2470 | /* Check whether the normalized exponent is high enough to overflow |
| 2471 | max during the log-rebasing in the max-exponent check below. */ |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 2472 | } else if (D.normalizedExponent - 1 > INT_MAX / 42039) { |
John McCall | b42cc68 | 2010-02-26 22:20:41 +0000 | [diff] [blame] | 2473 | fs = handleOverflow(rounding_mode); |
| 2474 | |
| 2475 | /* If it wasn't, then it also wasn't high enough to overflow max |
| 2476 | during the log-rebasing in the min-exponent check. Check that it |
| 2477 | won't overflow min in either check, then perform the min-exponent |
| 2478 | check. */ |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 2479 | } else if (D.normalizedExponent - 1 < INT_MIN / 42039 || |
John McCall | b42cc68 | 2010-02-26 22:20:41 +0000 | [diff] [blame] | 2480 | (D.normalizedExponent + 1) * 28738 <= |
| 2481 | 8651 * (semantics->minExponent - (int) semantics->precision)) { |
Neil Booth | 9130551 | 2007-10-15 15:00:55 +0000 | [diff] [blame] | 2482 | /* Underflow to zero and round. */ |
Michael Gottesman | 30a90eb | 2013-07-27 21:49:21 +0000 | [diff] [blame] | 2483 | category = fcNormal; |
Neil Booth | 9130551 | 2007-10-15 15:00:55 +0000 | [diff] [blame] | 2484 | zeroSignificand(); |
| 2485 | fs = normalize(rounding_mode, lfLessThanHalf); |
John McCall | b42cc68 | 2010-02-26 22:20:41 +0000 | [diff] [blame] | 2486 | |
| 2487 | /* We can finally safely perform the max-exponent check. */ |
Neil Booth | 9130551 | 2007-10-15 15:00:55 +0000 | [diff] [blame] | 2488 | } else if ((D.normalizedExponent - 1) * 42039 |
| 2489 | >= 12655 * semantics->maxExponent) { |
| 2490 | /* Overflow and round. */ |
| 2491 | fs = handleOverflow(rounding_mode); |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2492 | } else { |
Dylan Noblesmith | 4e69e29 | 2014-08-26 02:03:33 +0000 | [diff] [blame] | 2493 | integerPart *decSignificand; |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 2494 | unsigned int partCount; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2495 | |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 2496 | /* A tight upper bound on number of bits required to hold an |
Neil Booth | 9130551 | 2007-10-15 15:00:55 +0000 | [diff] [blame] | 2497 | N-digit decimal integer is N * 196 / 59. Allocate enough space |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 2498 | to hold the full significand, and an extra part required by |
| 2499 | tcMultiplyPart. */ |
Evan Cheng | 82b9e96 | 2008-05-02 21:15:08 +0000 | [diff] [blame] | 2500 | partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1; |
Neil Booth | 9130551 | 2007-10-15 15:00:55 +0000 | [diff] [blame] | 2501 | partCount = partCountForBits(1 + 196 * partCount / 59); |
Dylan Noblesmith | 4e69e29 | 2014-08-26 02:03:33 +0000 | [diff] [blame] | 2502 | decSignificand = new integerPart[partCount + 1]; |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 2503 | partCount = 0; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2504 | |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 2505 | /* Convert to binary efficiently - we do almost all multiplication |
| 2506 | in an integerPart. When this would overflow do we do a single |
| 2507 | bignum multiplication, and then revert again to multiplication |
| 2508 | in an integerPart. */ |
| 2509 | do { |
| 2510 | integerPart decValue, val, multiplier; |
| 2511 | |
| 2512 | val = 0; |
| 2513 | multiplier = 1; |
| 2514 | |
| 2515 | do { |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 2516 | if (*p == '.') { |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 2517 | p++; |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 2518 | if (p == str.end()) { |
| 2519 | break; |
| 2520 | } |
| 2521 | } |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 2522 | decValue = decDigitValue(*p++); |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 2523 | assert(decValue < 10U && "Invalid character in significand"); |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 2524 | multiplier *= 10; |
| 2525 | val = val * 10 + decValue; |
| 2526 | /* The maximum number that can be multiplied by ten with any |
| 2527 | digit added without overflowing an integerPart. */ |
| 2528 | } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10); |
| 2529 | |
| 2530 | /* Multiply out the current part. */ |
| 2531 | APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val, |
| 2532 | partCount, partCount + 1, false); |
| 2533 | |
| 2534 | /* If we used another part (likely but not guaranteed), increase |
| 2535 | the count. */ |
| 2536 | if (decSignificand[partCount]) |
| 2537 | partCount++; |
| 2538 | } while (p <= D.lastSigDigit); |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2539 | |
Neil Booth | ae077d2 | 2007-11-01 22:51:07 +0000 | [diff] [blame] | 2540 | category = fcNormal; |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2541 | fs = roundSignificandWithExponent(decSignificand, partCount, |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 2542 | D.exponent, rounding_mode); |
Dylan Noblesmith | 4e69e29 | 2014-08-26 02:03:33 +0000 | [diff] [blame] | 2543 | |
| 2544 | delete [] decSignificand; |
Neil Booth | 4ed401b | 2007-10-14 10:16:12 +0000 | [diff] [blame] | 2545 | } |
Neil Booth | b93d90e | 2007-10-12 16:02:31 +0000 | [diff] [blame] | 2546 | |
| 2547 | return fs; |
| 2548 | } |
| 2549 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2550 | bool IEEEFloat::convertFromStringSpecials(StringRef str) { |
Michael Gottesman | 40e8a18 | 2013-06-24 09:58:05 +0000 | [diff] [blame] | 2551 | if (str.equals("inf") || str.equals("INFINITY")) { |
| 2552 | makeInf(false); |
| 2553 | return true; |
| 2554 | } |
| 2555 | |
| 2556 | if (str.equals("-inf") || str.equals("-INFINITY")) { |
| 2557 | makeInf(true); |
| 2558 | return true; |
| 2559 | } |
| 2560 | |
| 2561 | if (str.equals("nan") || str.equals("NaN")) { |
| 2562 | makeNaN(false, false); |
| 2563 | return true; |
| 2564 | } |
| 2565 | |
| 2566 | if (str.equals("-nan") || str.equals("-NaN")) { |
| 2567 | makeNaN(false, true); |
| 2568 | return true; |
| 2569 | } |
| 2570 | |
| 2571 | return false; |
| 2572 | } |
| 2573 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2574 | IEEEFloat::opStatus IEEEFloat::convertFromString(StringRef str, |
| 2575 | roundingMode rounding_mode) { |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 2576 | assert(!str.empty() && "Invalid string length"); |
Neil Booth | 06077e7 | 2007-10-14 10:29:28 +0000 | [diff] [blame] | 2577 | |
Michael Gottesman | 40e8a18 | 2013-06-24 09:58:05 +0000 | [diff] [blame] | 2578 | // Handle special cases. |
| 2579 | if (convertFromStringSpecials(str)) |
| 2580 | return opOK; |
| 2581 | |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2582 | /* Handle a leading minus sign. */ |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 2583 | StringRef::iterator p = str.begin(); |
| 2584 | size_t slen = str.size(); |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 2585 | sign = *p == '-' ? 1 : 0; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2586 | if (*p == '-' || *p == '+') { |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 2587 | p++; |
| 2588 | slen--; |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 2589 | assert(slen && "String has no digits"); |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 2590 | } |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2591 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 2592 | if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 2593 | assert(slen - 2 && "Invalid string"); |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 2594 | return convertFromHexadecimalString(StringRef(p + 2, slen - 2), |
Erick Tryzelaar | 19f63b2 | 2009-08-16 23:36:19 +0000 | [diff] [blame] | 2595 | rounding_mode); |
| 2596 | } |
Bill Wendling | c607540 | 2008-11-27 08:00:12 +0000 | [diff] [blame] | 2597 | |
Erick Tryzelaar | da666c8 | 2009-08-20 23:30:43 +0000 | [diff] [blame] | 2598 | return convertFromDecimalString(StringRef(p, slen), rounding_mode); |
Chris Lattner | fe02c1f | 2007-08-20 22:49:32 +0000 | [diff] [blame] | 2599 | } |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2600 | |
Neil Booth | 8f1946f | 2007-10-03 22:26:02 +0000 | [diff] [blame] | 2601 | /* Write out a hexadecimal representation of the floating point value |
| 2602 | to DST, which must be of sufficient size, in the C99 form |
| 2603 | [-]0xh.hhhhp[+-]d. Return the number of characters written, |
| 2604 | excluding the terminating NUL. |
| 2605 | |
| 2606 | If UPPERCASE, the output is in upper case, otherwise in lower case. |
| 2607 | |
| 2608 | HEXDIGITS digits appear altogether, rounding the value if |
| 2609 | necessary. If HEXDIGITS is 0, the minimal precision to display the |
| 2610 | number precisely is used instead. If nothing would appear after |
| 2611 | the decimal point it is suppressed. |
| 2612 | |
| 2613 | The decimal exponent is always printed and has at least one digit. |
| 2614 | Zero values display an exponent of zero. Infinities and NaNs |
| 2615 | appear as "infinity" or "nan" respectively. |
| 2616 | |
| 2617 | The above rules are as specified by C99. There is ambiguity about |
| 2618 | what the leading hexadecimal digit should be. This implementation |
| 2619 | uses whatever is necessary so that the exponent is displayed as |
| 2620 | stored. This implies the exponent will fall within the IEEE format |
| 2621 | range, and the leading hexadecimal digit will be 0 (for denormals), |
| 2622 | 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with |
| 2623 | any other digits zero). |
| 2624 | */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2625 | unsigned int IEEEFloat::convertToHexString(char *dst, unsigned int hexDigits, |
| 2626 | bool upperCase, |
| 2627 | roundingMode rounding_mode) const { |
Neil Booth | 8f1946f | 2007-10-03 22:26:02 +0000 | [diff] [blame] | 2628 | char *p; |
| 2629 | |
| 2630 | p = dst; |
| 2631 | if (sign) |
| 2632 | *dst++ = '-'; |
| 2633 | |
| 2634 | switch (category) { |
| 2635 | case fcInfinity: |
| 2636 | memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1); |
| 2637 | dst += sizeof infinityL - 1; |
| 2638 | break; |
| 2639 | |
| 2640 | case fcNaN: |
| 2641 | memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1); |
| 2642 | dst += sizeof NaNU - 1; |
| 2643 | break; |
| 2644 | |
| 2645 | case fcZero: |
| 2646 | *dst++ = '0'; |
| 2647 | *dst++ = upperCase ? 'X': 'x'; |
| 2648 | *dst++ = '0'; |
| 2649 | if (hexDigits > 1) { |
| 2650 | *dst++ = '.'; |
| 2651 | memset (dst, '0', hexDigits - 1); |
| 2652 | dst += hexDigits - 1; |
| 2653 | } |
| 2654 | *dst++ = upperCase ? 'P': 'p'; |
| 2655 | *dst++ = '0'; |
| 2656 | break; |
| 2657 | |
| 2658 | case fcNormal: |
| 2659 | dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode); |
| 2660 | break; |
| 2661 | } |
| 2662 | |
| 2663 | *dst = 0; |
| 2664 | |
Evan Cheng | 82b9e96 | 2008-05-02 21:15:08 +0000 | [diff] [blame] | 2665 | return static_cast<unsigned int>(dst - p); |
Neil Booth | 8f1946f | 2007-10-03 22:26:02 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | /* Does the hard work of outputting the correctly rounded hexadecimal |
| 2669 | form of a normal floating point number with the specified number of |
| 2670 | hexadecimal digits. If HEXDIGITS is zero the minimum number of |
| 2671 | digits necessary to print the value precisely is output. */ |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2672 | char *IEEEFloat::convertNormalToHexString(char *dst, unsigned int hexDigits, |
| 2673 | bool upperCase, |
| 2674 | roundingMode rounding_mode) const { |
Neil Booth | 8f1946f | 2007-10-03 22:26:02 +0000 | [diff] [blame] | 2675 | unsigned int count, valueBits, shift, partsCount, outputDigits; |
| 2676 | const char *hexDigitChars; |
| 2677 | const integerPart *significand; |
| 2678 | char *p; |
| 2679 | bool roundUp; |
| 2680 | |
| 2681 | *dst++ = '0'; |
| 2682 | *dst++ = upperCase ? 'X': 'x'; |
| 2683 | |
| 2684 | roundUp = false; |
| 2685 | hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower; |
| 2686 | |
| 2687 | significand = significandParts(); |
| 2688 | partsCount = partCount(); |
| 2689 | |
| 2690 | /* +3 because the first digit only uses the single integer bit, so |
| 2691 | we have 3 virtual zero most-significant-bits. */ |
| 2692 | valueBits = semantics->precision + 3; |
| 2693 | shift = integerPartWidth - valueBits % integerPartWidth; |
| 2694 | |
| 2695 | /* The natural number of digits required ignoring trailing |
| 2696 | insignificant zeroes. */ |
| 2697 | outputDigits = (valueBits - significandLSB () + 3) / 4; |
| 2698 | |
| 2699 | /* hexDigits of zero means use the required number for the |
| 2700 | precision. Otherwise, see if we are truncating. If we are, |
Neil Booth | 0ea72a9 | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 2701 | find out if we need to round away from zero. */ |
Neil Booth | 8f1946f | 2007-10-03 22:26:02 +0000 | [diff] [blame] | 2702 | if (hexDigits) { |
| 2703 | if (hexDigits < outputDigits) { |
| 2704 | /* We are dropping non-zero bits, so need to check how to round. |
| 2705 | "bits" is the number of dropped bits. */ |
| 2706 | unsigned int bits; |
| 2707 | lostFraction fraction; |
| 2708 | |
| 2709 | bits = valueBits - hexDigits * 4; |
| 2710 | fraction = lostFractionThroughTruncation (significand, partsCount, bits); |
| 2711 | roundUp = roundAwayFromZero(rounding_mode, fraction, bits); |
| 2712 | } |
| 2713 | outputDigits = hexDigits; |
| 2714 | } |
| 2715 | |
| 2716 | /* Write the digits consecutively, and start writing in the location |
| 2717 | of the hexadecimal point. We move the most significant digit |
| 2718 | left and add the hexadecimal point later. */ |
| 2719 | p = ++dst; |
| 2720 | |
| 2721 | count = (valueBits + integerPartWidth - 1) / integerPartWidth; |
| 2722 | |
| 2723 | while (outputDigits && count) { |
| 2724 | integerPart part; |
| 2725 | |
| 2726 | /* Put the most significant integerPartWidth bits in "part". */ |
| 2727 | if (--count == partsCount) |
| 2728 | part = 0; /* An imaginary higher zero part. */ |
| 2729 | else |
| 2730 | part = significand[count] << shift; |
| 2731 | |
| 2732 | if (count && shift) |
| 2733 | part |= significand[count - 1] >> (integerPartWidth - shift); |
| 2734 | |
| 2735 | /* Convert as much of "part" to hexdigits as we can. */ |
| 2736 | unsigned int curDigits = integerPartWidth / 4; |
| 2737 | |
| 2738 | if (curDigits > outputDigits) |
| 2739 | curDigits = outputDigits; |
| 2740 | dst += partAsHex (dst, part, curDigits, hexDigitChars); |
| 2741 | outputDigits -= curDigits; |
| 2742 | } |
| 2743 | |
| 2744 | if (roundUp) { |
| 2745 | char *q = dst; |
| 2746 | |
| 2747 | /* Note that hexDigitChars has a trailing '0'. */ |
| 2748 | do { |
| 2749 | q--; |
| 2750 | *q = hexDigitChars[hexDigitValue (*q) + 1]; |
Neil Booth | 0ea72a9 | 2007-10-06 00:24:48 +0000 | [diff] [blame] | 2751 | } while (*q == '0'); |
Evan Cheng | 67c9021 | 2009-10-27 21:35:42 +0000 | [diff] [blame] | 2752 | assert(q >= p); |
Neil Booth | 8f1946f | 2007-10-03 22:26:02 +0000 | [diff] [blame] | 2753 | } else { |
| 2754 | /* Add trailing zeroes. */ |
| 2755 | memset (dst, '0', outputDigits); |
| 2756 | dst += outputDigits; |
| 2757 | } |
| 2758 | |
| 2759 | /* Move the most significant digit to before the point, and if there |
| 2760 | is something after the decimal point add it. This must come |
| 2761 | after rounding above. */ |
| 2762 | p[-1] = p[0]; |
| 2763 | if (dst -1 == p) |
| 2764 | dst--; |
| 2765 | else |
| 2766 | p[0] = '.'; |
| 2767 | |
| 2768 | /* Finally output the exponent. */ |
| 2769 | *dst++ = upperCase ? 'P': 'p'; |
| 2770 | |
Neil Booth | 32897f5 | 2007-10-06 07:29:25 +0000 | [diff] [blame] | 2771 | return writeSignedDecimal (dst, exponent); |
Neil Booth | 8f1946f | 2007-10-03 22:26:02 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2774 | hash_code hash_value(const IEEEFloat &Arg) { |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 2775 | if (!Arg.isFiniteNonZero()) |
Chandler Carruth | 71bd7d1 | 2012-03-04 12:02:57 +0000 | [diff] [blame] | 2776 | return hash_combine((uint8_t)Arg.category, |
| 2777 | // NaN has no sign, fix it at zero. |
| 2778 | Arg.isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign, |
| 2779 | Arg.semantics->precision); |
| 2780 | |
| 2781 | // Normal floats need their exponent and significand hashed. |
| 2782 | return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign, |
| 2783 | Arg.semantics->precision, Arg.exponent, |
| 2784 | hash_combine_range( |
| 2785 | Arg.significandParts(), |
| 2786 | Arg.significandParts() + Arg.partCount())); |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | // Conversion from APFloat to/from host float/double. It may eventually be |
| 2790 | // possible to eliminate these and have everybody deal with APFloats, but that |
| 2791 | // will take a while. This approach will not easily extend to long double. |
Dale Johannesen | 146a0ea | 2007-09-20 23:47:58 +0000 | [diff] [blame] | 2792 | // Current implementation requires integerPartWidth==64, which is correct at |
| 2793 | // the moment but could be made more general. |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2794 | |
Dale Johannesen | 728687c | 2007-09-05 20:39:49 +0000 | [diff] [blame] | 2795 | // Denormals have exponent minExponent in APFloat, but minExponent-1 in |
Dale Johannesen | 146a0ea | 2007-09-20 23:47:58 +0000 | [diff] [blame] | 2796 | // the actual IEEE respresentations. We compensate for that here. |
Dale Johannesen | 728687c | 2007-09-05 20:39:49 +0000 | [diff] [blame] | 2797 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2798 | APInt IEEEFloat::convertF80LongDoubleAPFloatToAPInt() const { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2799 | assert(semantics == (const llvm::fltSemantics*)&semX87DoubleExtended); |
Evan Cheng | 67c9021 | 2009-10-27 21:35:42 +0000 | [diff] [blame] | 2800 | assert(partCount()==2); |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 2801 | |
| 2802 | uint64_t myexponent, mysignificand; |
| 2803 | |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 2804 | if (isFiniteNonZero()) { |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 2805 | myexponent = exponent+16383; //bias |
Dale Johannesen | 146a0ea | 2007-09-20 23:47:58 +0000 | [diff] [blame] | 2806 | mysignificand = significandParts()[0]; |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 2807 | if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL)) |
| 2808 | myexponent = 0; // denormal |
| 2809 | } else if (category==fcZero) { |
| 2810 | myexponent = 0; |
| 2811 | mysignificand = 0; |
| 2812 | } else if (category==fcInfinity) { |
| 2813 | myexponent = 0x7fff; |
| 2814 | mysignificand = 0x8000000000000000ULL; |
Chris Lattner | 2a9bcb9 | 2007-10-06 06:13:42 +0000 | [diff] [blame] | 2815 | } else { |
| 2816 | assert(category == fcNaN && "Unknown category"); |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 2817 | myexponent = 0x7fff; |
Dale Johannesen | 146a0ea | 2007-09-20 23:47:58 +0000 | [diff] [blame] | 2818 | mysignificand = significandParts()[0]; |
Chris Lattner | 2a9bcb9 | 2007-10-06 06:13:42 +0000 | [diff] [blame] | 2819 | } |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 2820 | |
| 2821 | uint64_t words[2]; |
Dale Johannesen | 93eefa0 | 2009-03-23 21:16:53 +0000 | [diff] [blame] | 2822 | words[0] = mysignificand; |
| 2823 | words[1] = ((uint64_t)(sign & 1) << 15) | |
| 2824 | (myexponent & 0x7fffLL); |
Jeffrey Yasskin | 7a16288 | 2011-07-18 21:45:40 +0000 | [diff] [blame] | 2825 | return APInt(80, words); |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2828 | APInt IEEEFloat::convertPPCDoubleDoubleAPFloatToAPInt() const { |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 2829 | assert(semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy); |
Evan Cheng | 67c9021 | 2009-10-27 21:35:42 +0000 | [diff] [blame] | 2830 | assert(partCount()==2); |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 2831 | |
Ulrich Weigand | d9f7e25 | 2012-10-29 18:09:01 +0000 | [diff] [blame] | 2832 | uint64_t words[2]; |
| 2833 | opStatus fs; |
| 2834 | bool losesInfo; |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 2835 | |
Ulrich Weigand | d9f7e25 | 2012-10-29 18:09:01 +0000 | [diff] [blame] | 2836 | // Convert number to double. To avoid spurious underflows, we re- |
| 2837 | // normalize against the "double" minExponent first, and only *then* |
| 2838 | // truncate the mantissa. The result of that second conversion |
| 2839 | // may be inexact, but should never underflow. |
Alexey Samsonov | 2b431d9 | 2012-11-30 22:27:54 +0000 | [diff] [blame] | 2840 | // Declare fltSemantics before APFloat that uses it (and |
| 2841 | // saves pointer to it) to ensure correct destruction order. |
Ulrich Weigand | d9f7e25 | 2012-10-29 18:09:01 +0000 | [diff] [blame] | 2842 | fltSemantics extendedSemantics = *semantics; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2843 | extendedSemantics.minExponent = semIEEEdouble.minExponent; |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2844 | IEEEFloat extended(*this); |
Ulrich Weigand | d9f7e25 | 2012-10-29 18:09:01 +0000 | [diff] [blame] | 2845 | fs = extended.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo); |
| 2846 | assert(fs == opOK && !losesInfo); |
| 2847 | (void)fs; |
| 2848 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2849 | IEEEFloat u(extended); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2850 | fs = u.convert(semIEEEdouble, rmNearestTiesToEven, &losesInfo); |
Ulrich Weigand | d9f7e25 | 2012-10-29 18:09:01 +0000 | [diff] [blame] | 2851 | assert(fs == opOK || fs == opInexact); |
| 2852 | (void)fs; |
| 2853 | words[0] = *u.convertDoubleAPFloatToAPInt().getRawData(); |
| 2854 | |
| 2855 | // If conversion was exact or resulted in a special case, we're done; |
| 2856 | // just set the second double to zero. Otherwise, re-convert back to |
| 2857 | // the extended format and compute the difference. This now should |
| 2858 | // convert exactly to double. |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 2859 | if (u.isFiniteNonZero() && losesInfo) { |
Ulrich Weigand | d9f7e25 | 2012-10-29 18:09:01 +0000 | [diff] [blame] | 2860 | fs = u.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo); |
| 2861 | assert(fs == opOK && !losesInfo); |
| 2862 | (void)fs; |
| 2863 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2864 | IEEEFloat v(extended); |
Ulrich Weigand | d9f7e25 | 2012-10-29 18:09:01 +0000 | [diff] [blame] | 2865 | v.subtract(u, rmNearestTiesToEven); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2866 | fs = v.convert(semIEEEdouble, rmNearestTiesToEven, &losesInfo); |
Ulrich Weigand | d9f7e25 | 2012-10-29 18:09:01 +0000 | [diff] [blame] | 2867 | assert(fs == opOK && !losesInfo); |
| 2868 | (void)fs; |
| 2869 | words[1] = *v.convertDoubleAPFloatToAPInt().getRawData(); |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 2870 | } else { |
Ulrich Weigand | d9f7e25 | 2012-10-29 18:09:01 +0000 | [diff] [blame] | 2871 | words[1] = 0; |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
Jeffrey Yasskin | 7a16288 | 2011-07-18 21:45:40 +0000 | [diff] [blame] | 2874 | return APInt(128, words); |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2877 | APInt IEEEFloat::convertQuadrupleAPFloatToAPInt() const { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2878 | assert(semantics == (const llvm::fltSemantics*)&semIEEEquad); |
Evan Cheng | 67c9021 | 2009-10-27 21:35:42 +0000 | [diff] [blame] | 2879 | assert(partCount()==2); |
Anton Korobeynikov | 13e8c7e | 2009-08-21 22:10:30 +0000 | [diff] [blame] | 2880 | |
| 2881 | uint64_t myexponent, mysignificand, mysignificand2; |
| 2882 | |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 2883 | if (isFiniteNonZero()) { |
Anton Korobeynikov | 13e8c7e | 2009-08-21 22:10:30 +0000 | [diff] [blame] | 2884 | myexponent = exponent+16383; //bias |
| 2885 | mysignificand = significandParts()[0]; |
| 2886 | mysignificand2 = significandParts()[1]; |
| 2887 | if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL)) |
| 2888 | myexponent = 0; // denormal |
| 2889 | } else if (category==fcZero) { |
| 2890 | myexponent = 0; |
| 2891 | mysignificand = mysignificand2 = 0; |
| 2892 | } else if (category==fcInfinity) { |
| 2893 | myexponent = 0x7fff; |
| 2894 | mysignificand = mysignificand2 = 0; |
| 2895 | } else { |
| 2896 | assert(category == fcNaN && "Unknown category!"); |
| 2897 | myexponent = 0x7fff; |
| 2898 | mysignificand = significandParts()[0]; |
| 2899 | mysignificand2 = significandParts()[1]; |
| 2900 | } |
| 2901 | |
| 2902 | uint64_t words[2]; |
| 2903 | words[0] = mysignificand; |
| 2904 | words[1] = ((uint64_t)(sign & 1) << 63) | |
| 2905 | ((myexponent & 0x7fff) << 48) | |
Anton Korobeynikov | 876955c | 2009-08-21 23:09:47 +0000 | [diff] [blame] | 2906 | (mysignificand2 & 0xffffffffffffLL); |
Anton Korobeynikov | 13e8c7e | 2009-08-21 22:10:30 +0000 | [diff] [blame] | 2907 | |
Jeffrey Yasskin | 7a16288 | 2011-07-18 21:45:40 +0000 | [diff] [blame] | 2908 | return APInt(128, words); |
Anton Korobeynikov | 13e8c7e | 2009-08-21 22:10:30 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2911 | APInt IEEEFloat::convertDoubleAPFloatToAPInt() const { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2912 | assert(semantics == (const llvm::fltSemantics*)&semIEEEdouble); |
Evan Cheng | 67c9021 | 2009-10-27 21:35:42 +0000 | [diff] [blame] | 2913 | assert(partCount()==1); |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2914 | |
Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 2915 | uint64_t myexponent, mysignificand; |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2916 | |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 2917 | if (isFiniteNonZero()) { |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2918 | myexponent = exponent+1023; //bias |
Dale Johannesen | 728687c | 2007-09-05 20:39:49 +0000 | [diff] [blame] | 2919 | mysignificand = *significandParts(); |
| 2920 | if (myexponent==1 && !(mysignificand & 0x10000000000000LL)) |
| 2921 | myexponent = 0; // denormal |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2922 | } else if (category==fcZero) { |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2923 | myexponent = 0; |
| 2924 | mysignificand = 0; |
| 2925 | } else if (category==fcInfinity) { |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2926 | myexponent = 0x7ff; |
| 2927 | mysignificand = 0; |
Chris Lattner | 2a9bcb9 | 2007-10-06 06:13:42 +0000 | [diff] [blame] | 2928 | } else { |
| 2929 | assert(category == fcNaN && "Unknown category!"); |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2930 | myexponent = 0x7ff; |
Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 2931 | mysignificand = *significandParts(); |
Chris Lattner | 2a9bcb9 | 2007-10-06 06:13:42 +0000 | [diff] [blame] | 2932 | } |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2933 | |
Evan Cheng | 82b9e96 | 2008-05-02 21:15:08 +0000 | [diff] [blame] | 2934 | return APInt(64, ((((uint64_t)(sign & 1) << 63) | |
Chris Lattner | 2a9bcb9 | 2007-10-06 06:13:42 +0000 | [diff] [blame] | 2935 | ((myexponent & 0x7ff) << 52) | |
| 2936 | (mysignificand & 0xfffffffffffffLL)))); |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2939 | APInt IEEEFloat::convertFloatAPFloatToAPInt() const { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2940 | assert(semantics == (const llvm::fltSemantics*)&semIEEEsingle); |
Evan Cheng | 67c9021 | 2009-10-27 21:35:42 +0000 | [diff] [blame] | 2941 | assert(partCount()==1); |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 2942 | |
Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 2943 | uint32_t myexponent, mysignificand; |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2944 | |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 2945 | if (isFiniteNonZero()) { |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2946 | myexponent = exponent+127; //bias |
Evan Cheng | 82b9e96 | 2008-05-02 21:15:08 +0000 | [diff] [blame] | 2947 | mysignificand = (uint32_t)*significandParts(); |
Dale Johannesen | 06a10df | 2007-11-17 01:02:27 +0000 | [diff] [blame] | 2948 | if (myexponent == 1 && !(mysignificand & 0x800000)) |
Dale Johannesen | 728687c | 2007-09-05 20:39:49 +0000 | [diff] [blame] | 2949 | myexponent = 0; // denormal |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2950 | } else if (category==fcZero) { |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2951 | myexponent = 0; |
| 2952 | mysignificand = 0; |
| 2953 | } else if (category==fcInfinity) { |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2954 | myexponent = 0xff; |
| 2955 | mysignificand = 0; |
Chris Lattner | 2a9bcb9 | 2007-10-06 06:13:42 +0000 | [diff] [blame] | 2956 | } else { |
| 2957 | assert(category == fcNaN && "Unknown category!"); |
Dale Johannesen | 728687c | 2007-09-05 20:39:49 +0000 | [diff] [blame] | 2958 | myexponent = 0xff; |
Evan Cheng | 82b9e96 | 2008-05-02 21:15:08 +0000 | [diff] [blame] | 2959 | mysignificand = (uint32_t)*significandParts(); |
Chris Lattner | 2a9bcb9 | 2007-10-06 06:13:42 +0000 | [diff] [blame] | 2960 | } |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2961 | |
Chris Lattner | 2a9bcb9 | 2007-10-06 06:13:42 +0000 | [diff] [blame] | 2962 | return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) | |
| 2963 | (mysignificand & 0x7fffff))); |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2966 | APInt IEEEFloat::convertHalfAPFloatToAPInt() const { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2967 | assert(semantics == (const llvm::fltSemantics*)&semIEEEhalf); |
Evan Cheng | 67c9021 | 2009-10-27 21:35:42 +0000 | [diff] [blame] | 2968 | assert(partCount()==1); |
Chris Lattner | 4794b2b | 2009-10-16 02:13:51 +0000 | [diff] [blame] | 2969 | |
| 2970 | uint32_t myexponent, mysignificand; |
| 2971 | |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 2972 | if (isFiniteNonZero()) { |
Chris Lattner | 4794b2b | 2009-10-16 02:13:51 +0000 | [diff] [blame] | 2973 | myexponent = exponent+15; //bias |
| 2974 | mysignificand = (uint32_t)*significandParts(); |
| 2975 | if (myexponent == 1 && !(mysignificand & 0x400)) |
| 2976 | myexponent = 0; // denormal |
| 2977 | } else if (category==fcZero) { |
| 2978 | myexponent = 0; |
| 2979 | mysignificand = 0; |
| 2980 | } else if (category==fcInfinity) { |
Dale Johannesen | 0d670b5 | 2009-10-23 04:02:51 +0000 | [diff] [blame] | 2981 | myexponent = 0x1f; |
Chris Lattner | 4794b2b | 2009-10-16 02:13:51 +0000 | [diff] [blame] | 2982 | mysignificand = 0; |
| 2983 | } else { |
| 2984 | assert(category == fcNaN && "Unknown category!"); |
Dale Johannesen | 0d670b5 | 2009-10-23 04:02:51 +0000 | [diff] [blame] | 2985 | myexponent = 0x1f; |
Chris Lattner | 4794b2b | 2009-10-16 02:13:51 +0000 | [diff] [blame] | 2986 | mysignificand = (uint32_t)*significandParts(); |
| 2987 | } |
| 2988 | |
| 2989 | return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) | |
| 2990 | (mysignificand & 0x3ff))); |
| 2991 | } |
| 2992 | |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 2993 | // This function creates an APInt that is just a bit map of the floating |
| 2994 | // point constant as it would appear in memory. It is not a conversion, |
| 2995 | // and treating the result as a normal integer is unlikely to be useful. |
| 2996 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 2997 | APInt IEEEFloat::bitcastToAPInt() const { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2998 | if (semantics == (const llvm::fltSemantics*)&semIEEEhalf) |
Chris Lattner | 4794b2b | 2009-10-16 02:13:51 +0000 | [diff] [blame] | 2999 | return convertHalfAPFloatToAPInt(); |
| 3000 | |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3001 | if (semantics == (const llvm::fltSemantics*)&semIEEEsingle) |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3002 | return convertFloatAPFloatToAPInt(); |
Anton Korobeynikov | 13e8c7e | 2009-08-21 22:10:30 +0000 | [diff] [blame] | 3003 | |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3004 | if (semantics == (const llvm::fltSemantics*)&semIEEEdouble) |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3005 | return convertDoubleAPFloatToAPInt(); |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 3006 | |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3007 | if (semantics == (const llvm::fltSemantics*)&semIEEEquad) |
Anton Korobeynikov | 13e8c7e | 2009-08-21 22:10:30 +0000 | [diff] [blame] | 3008 | return convertQuadrupleAPFloatToAPInt(); |
| 3009 | |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 3010 | if (semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy) |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 3011 | return convertPPCDoubleDoubleAPFloatToAPInt(); |
| 3012 | |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3013 | assert(semantics == (const llvm::fltSemantics*)&semX87DoubleExtended && |
Chris Lattner | 2a9bcb9 | 2007-10-06 06:13:42 +0000 | [diff] [blame] | 3014 | "unknown format!"); |
| 3015 | return convertF80LongDoubleAPFloatToAPInt(); |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3018 | float IEEEFloat::convertToFloat() const { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3019 | assert(semantics == (const llvm::fltSemantics*)&semIEEEsingle && |
Chris Lattner | 688f991 | 2009-09-24 21:44:20 +0000 | [diff] [blame] | 3020 | "Float semantics are not IEEEsingle"); |
Dale Johannesen | 54306fe | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 3021 | APInt api = bitcastToAPInt(); |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3022 | return api.bitsToFloat(); |
| 3023 | } |
| 3024 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3025 | double IEEEFloat::convertToDouble() const { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3026 | assert(semantics == (const llvm::fltSemantics*)&semIEEEdouble && |
Chris Lattner | 688f991 | 2009-09-24 21:44:20 +0000 | [diff] [blame] | 3027 | "Float semantics are not IEEEdouble"); |
Dale Johannesen | 54306fe | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 3028 | APInt api = bitcastToAPInt(); |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3029 | return api.bitsToDouble(); |
| 3030 | } |
| 3031 | |
Dale Johannesen | fff2995 | 2008-10-06 18:22:29 +0000 | [diff] [blame] | 3032 | /// Integer bit is explicit in this format. Intel hardware (387 and later) |
| 3033 | /// does not support these bit patterns: |
| 3034 | /// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity") |
| 3035 | /// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN") |
| 3036 | /// exponent = 0, integer bit 1 ("pseudodenormal") |
| 3037 | /// exponent!=0 nor all 1's, integer bit 0 ("unnormal") |
| 3038 | /// At the moment, the first two are treated as NaNs, the second two as Normal. |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3039 | void IEEEFloat::initFromF80LongDoubleAPInt(const APInt &api) { |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3040 | assert(api.getBitWidth()==80); |
| 3041 | uint64_t i1 = api.getRawData()[0]; |
| 3042 | uint64_t i2 = api.getRawData()[1]; |
Dale Johannesen | 93eefa0 | 2009-03-23 21:16:53 +0000 | [diff] [blame] | 3043 | uint64_t myexponent = (i2 & 0x7fff); |
| 3044 | uint64_t mysignificand = i1; |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3045 | |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3046 | initialize(&semX87DoubleExtended); |
Dale Johannesen | 146a0ea | 2007-09-20 23:47:58 +0000 | [diff] [blame] | 3047 | assert(partCount()==2); |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3048 | |
Dale Johannesen | 93eefa0 | 2009-03-23 21:16:53 +0000 | [diff] [blame] | 3049 | sign = static_cast<unsigned int>(i2>>15); |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3050 | if (myexponent==0 && mysignificand==0) { |
| 3051 | // exponent, significand meaningless |
| 3052 | category = fcZero; |
| 3053 | } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) { |
| 3054 | // exponent, significand meaningless |
| 3055 | category = fcInfinity; |
| 3056 | } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) { |
| 3057 | // exponent meaningless |
| 3058 | category = fcNaN; |
Dale Johannesen | 146a0ea | 2007-09-20 23:47:58 +0000 | [diff] [blame] | 3059 | significandParts()[0] = mysignificand; |
| 3060 | significandParts()[1] = 0; |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3061 | } else { |
| 3062 | category = fcNormal; |
| 3063 | exponent = myexponent - 16383; |
Dale Johannesen | 146a0ea | 2007-09-20 23:47:58 +0000 | [diff] [blame] | 3064 | significandParts()[0] = mysignificand; |
| 3065 | significandParts()[1] = 0; |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3066 | if (myexponent==0) // denormal |
| 3067 | exponent = -16382; |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 3068 | } |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3071 | void IEEEFloat::initFromPPCDoubleDoubleAPInt(const APInt &api) { |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 3072 | assert(api.getBitWidth()==128); |
| 3073 | uint64_t i1 = api.getRawData()[0]; |
| 3074 | uint64_t i2 = api.getRawData()[1]; |
Ulrich Weigand | d9f7e25 | 2012-10-29 18:09:01 +0000 | [diff] [blame] | 3075 | opStatus fs; |
| 3076 | bool losesInfo; |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 3077 | |
Ulrich Weigand | d9f7e25 | 2012-10-29 18:09:01 +0000 | [diff] [blame] | 3078 | // Get the first double and convert to our format. |
| 3079 | initFromDoubleAPInt(APInt(64, i1)); |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 3080 | fs = convert(semPPCDoubleDoubleLegacy, rmNearestTiesToEven, &losesInfo); |
Ulrich Weigand | d9f7e25 | 2012-10-29 18:09:01 +0000 | [diff] [blame] | 3081 | assert(fs == opOK && !losesInfo); |
| 3082 | (void)fs; |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 3083 | |
Ulrich Weigand | d9f7e25 | 2012-10-29 18:09:01 +0000 | [diff] [blame] | 3084 | // Unless we have a special case, add in second double. |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 3085 | if (isFiniteNonZero()) { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3086 | IEEEFloat v(semIEEEdouble, APInt(64, i2)); |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 3087 | fs = v.convert(semPPCDoubleDoubleLegacy, rmNearestTiesToEven, &losesInfo); |
Ulrich Weigand | d9f7e25 | 2012-10-29 18:09:01 +0000 | [diff] [blame] | 3088 | assert(fs == opOK && !losesInfo); |
| 3089 | (void)fs; |
| 3090 | |
| 3091 | add(v, rmNearestTiesToEven); |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 3092 | } |
| 3093 | } |
| 3094 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3095 | void IEEEFloat::initFromQuadrupleAPInt(const APInt &api) { |
Anton Korobeynikov | 13e8c7e | 2009-08-21 22:10:30 +0000 | [diff] [blame] | 3096 | assert(api.getBitWidth()==128); |
| 3097 | uint64_t i1 = api.getRawData()[0]; |
| 3098 | uint64_t i2 = api.getRawData()[1]; |
| 3099 | uint64_t myexponent = (i2 >> 48) & 0x7fff; |
| 3100 | uint64_t mysignificand = i1; |
| 3101 | uint64_t mysignificand2 = i2 & 0xffffffffffffLL; |
| 3102 | |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3103 | initialize(&semIEEEquad); |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 3104 | assert(partCount()==2); |
Anton Korobeynikov | 13e8c7e | 2009-08-21 22:10:30 +0000 | [diff] [blame] | 3105 | |
| 3106 | sign = static_cast<unsigned int>(i2>>63); |
| 3107 | if (myexponent==0 && |
| 3108 | (mysignificand==0 && mysignificand2==0)) { |
| 3109 | // exponent, significand meaningless |
| 3110 | category = fcZero; |
| 3111 | } else if (myexponent==0x7fff && |
| 3112 | (mysignificand==0 && mysignificand2==0)) { |
| 3113 | // exponent, significand meaningless |
| 3114 | category = fcInfinity; |
| 3115 | } else if (myexponent==0x7fff && |
| 3116 | (mysignificand!=0 || mysignificand2 !=0)) { |
| 3117 | // exponent meaningless |
| 3118 | category = fcNaN; |
| 3119 | significandParts()[0] = mysignificand; |
| 3120 | significandParts()[1] = mysignificand2; |
| 3121 | } else { |
| 3122 | category = fcNormal; |
| 3123 | exponent = myexponent - 16383; |
| 3124 | significandParts()[0] = mysignificand; |
| 3125 | significandParts()[1] = mysignificand2; |
| 3126 | if (myexponent==0) // denormal |
| 3127 | exponent = -16382; |
| 3128 | else |
| 3129 | significandParts()[1] |= 0x1000000000000LL; // integer bit |
| 3130 | } |
| 3131 | } |
| 3132 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3133 | void IEEEFloat::initFromDoubleAPInt(const APInt &api) { |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3134 | assert(api.getBitWidth()==64); |
| 3135 | uint64_t i = *api.getRawData(); |
Dale Johannesen | 918c33c | 2007-08-24 05:08:11 +0000 | [diff] [blame] | 3136 | uint64_t myexponent = (i >> 52) & 0x7ff; |
| 3137 | uint64_t mysignificand = i & 0xfffffffffffffLL; |
| 3138 | |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3139 | initialize(&semIEEEdouble); |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 3140 | assert(partCount()==1); |
| 3141 | |
Evan Cheng | 82b9e96 | 2008-05-02 21:15:08 +0000 | [diff] [blame] | 3142 | sign = static_cast<unsigned int>(i>>63); |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 3143 | if (myexponent==0 && mysignificand==0) { |
| 3144 | // exponent, significand meaningless |
| 3145 | category = fcZero; |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 3146 | } else if (myexponent==0x7ff && mysignificand==0) { |
| 3147 | // exponent, significand meaningless |
| 3148 | category = fcInfinity; |
Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 3149 | } else if (myexponent==0x7ff && mysignificand!=0) { |
| 3150 | // exponent meaningless |
| 3151 | category = fcNaN; |
| 3152 | *significandParts() = mysignificand; |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 3153 | } else { |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 3154 | category = fcNormal; |
| 3155 | exponent = myexponent - 1023; |
Dale Johannesen | 728687c | 2007-09-05 20:39:49 +0000 | [diff] [blame] | 3156 | *significandParts() = mysignificand; |
| 3157 | if (myexponent==0) // denormal |
| 3158 | exponent = -1022; |
| 3159 | else |
| 3160 | *significandParts() |= 0x10000000000000LL; // integer bit |
Neil Booth | 9acbf5a | 2007-09-26 21:33:42 +0000 | [diff] [blame] | 3161 | } |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 3162 | } |
| 3163 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3164 | void IEEEFloat::initFromFloatAPInt(const APInt &api) { |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3165 | assert(api.getBitWidth()==32); |
| 3166 | uint32_t i = (uint32_t)*api.getRawData(); |
Dale Johannesen | 918c33c | 2007-08-24 05:08:11 +0000 | [diff] [blame] | 3167 | uint32_t myexponent = (i >> 23) & 0xff; |
| 3168 | uint32_t mysignificand = i & 0x7fffff; |
| 3169 | |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3170 | initialize(&semIEEEsingle); |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 3171 | assert(partCount()==1); |
| 3172 | |
Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 3173 | sign = i >> 31; |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 3174 | if (myexponent==0 && mysignificand==0) { |
| 3175 | // exponent, significand meaningless |
| 3176 | category = fcZero; |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 3177 | } else if (myexponent==0xff && mysignificand==0) { |
| 3178 | // exponent, significand meaningless |
| 3179 | category = fcInfinity; |
Dale Johannesen | 4f55d9f | 2007-09-25 17:25:00 +0000 | [diff] [blame] | 3180 | } else if (myexponent==0xff && mysignificand!=0) { |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 3181 | // sign, exponent, significand meaningless |
Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 3182 | category = fcNaN; |
| 3183 | *significandParts() = mysignificand; |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 3184 | } else { |
| 3185 | category = fcNormal; |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 3186 | exponent = myexponent - 127; //bias |
Dale Johannesen | 728687c | 2007-09-05 20:39:49 +0000 | [diff] [blame] | 3187 | *significandParts() = mysignificand; |
| 3188 | if (myexponent==0) // denormal |
| 3189 | exponent = -126; |
| 3190 | else |
| 3191 | *significandParts() |= 0x800000; // integer bit |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 3192 | } |
| 3193 | } |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3194 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3195 | void IEEEFloat::initFromHalfAPInt(const APInt &api) { |
Chris Lattner | 4794b2b | 2009-10-16 02:13:51 +0000 | [diff] [blame] | 3196 | assert(api.getBitWidth()==16); |
| 3197 | uint32_t i = (uint32_t)*api.getRawData(); |
Dale Johannesen | 0d670b5 | 2009-10-23 04:02:51 +0000 | [diff] [blame] | 3198 | uint32_t myexponent = (i >> 10) & 0x1f; |
Chris Lattner | 4794b2b | 2009-10-16 02:13:51 +0000 | [diff] [blame] | 3199 | uint32_t mysignificand = i & 0x3ff; |
| 3200 | |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3201 | initialize(&semIEEEhalf); |
Chris Lattner | 4794b2b | 2009-10-16 02:13:51 +0000 | [diff] [blame] | 3202 | assert(partCount()==1); |
| 3203 | |
| 3204 | sign = i >> 15; |
| 3205 | if (myexponent==0 && mysignificand==0) { |
| 3206 | // exponent, significand meaningless |
| 3207 | category = fcZero; |
| 3208 | } else if (myexponent==0x1f && mysignificand==0) { |
| 3209 | // exponent, significand meaningless |
| 3210 | category = fcInfinity; |
| 3211 | } else if (myexponent==0x1f && mysignificand!=0) { |
| 3212 | // sign, exponent, significand meaningless |
| 3213 | category = fcNaN; |
| 3214 | *significandParts() = mysignificand; |
| 3215 | } else { |
| 3216 | category = fcNormal; |
| 3217 | exponent = myexponent - 15; //bias |
| 3218 | *significandParts() = mysignificand; |
| 3219 | if (myexponent==0) // denormal |
| 3220 | exponent = -14; |
| 3221 | else |
| 3222 | *significandParts() |= 0x400; // integer bit |
| 3223 | } |
| 3224 | } |
| 3225 | |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3226 | /// Treat api as containing the bits of a floating point number. Currently |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 3227 | /// we infer the floating point type from the size of the APInt. The |
| 3228 | /// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful |
| 3229 | /// when the size is anything else). |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3230 | void IEEEFloat::initFromAPInt(const fltSemantics *Sem, const APInt &api) { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3231 | if (Sem == &semIEEEhalf) |
Chris Lattner | 4794b2b | 2009-10-16 02:13:51 +0000 | [diff] [blame] | 3232 | return initFromHalfAPInt(api); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3233 | if (Sem == &semIEEEsingle) |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3234 | return initFromFloatAPInt(api); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3235 | if (Sem == &semIEEEdouble) |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3236 | return initFromDoubleAPInt(api); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3237 | if (Sem == &semX87DoubleExtended) |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3238 | return initFromF80LongDoubleAPInt(api); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3239 | if (Sem == &semIEEEquad) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 3240 | return initFromQuadrupleAPInt(api); |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 3241 | if (Sem == &semPPCDoubleDoubleLegacy) |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 3242 | return initFromPPCDoubleDoubleAPInt(api); |
| 3243 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3244 | llvm_unreachable(nullptr); |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 3247 | /// Make this number the largest magnitude normal number in the given |
| 3248 | /// semantics. |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3249 | void IEEEFloat::makeLargest(bool Negative) { |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3250 | // We want (in interchange format): |
| 3251 | // sign = {Negative} |
| 3252 | // exponent = 1..10 |
| 3253 | // significand = 1..1 |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 3254 | category = fcNormal; |
| 3255 | sign = Negative; |
| 3256 | exponent = semantics->maxExponent; |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3257 | |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 3258 | // Use memset to set all but the highest integerPart to all ones. |
| 3259 | integerPart *significand = significandParts(); |
| 3260 | unsigned PartCount = partCount(); |
| 3261 | memset(significand, 0xFF, sizeof(integerPart)*(PartCount - 1)); |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3262 | |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 3263 | // Set the high integerPart especially setting all unused top bits for |
| 3264 | // internal consistency. |
| 3265 | const unsigned NumUnusedHighBits = |
| 3266 | PartCount*integerPartWidth - semantics->precision; |
Alexey Samsonov | ba1ecbc | 2014-09-06 00:41:19 +0000 | [diff] [blame] | 3267 | significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth) |
| 3268 | ? (~integerPart(0) >> NumUnusedHighBits) |
| 3269 | : 0; |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 3272 | /// Make this number the smallest magnitude denormal number in the given |
| 3273 | /// semantics. |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3274 | void IEEEFloat::makeSmallest(bool Negative) { |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3275 | // We want (in interchange format): |
| 3276 | // sign = {Negative} |
| 3277 | // exponent = 0..0 |
| 3278 | // significand = 0..01 |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 3279 | category = fcNormal; |
| 3280 | sign = Negative; |
| 3281 | exponent = semantics->minExponent; |
| 3282 | APInt::tcSet(significandParts(), 1, partCount()); |
| 3283 | } |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3284 | |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 3285 | void IEEEFloat::makeSmallestNormalized(bool Negative) { |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3286 | // We want (in interchange format): |
| 3287 | // sign = {Negative} |
| 3288 | // exponent = 0..0 |
| 3289 | // significand = 10..0 |
| 3290 | |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 3291 | category = fcNormal; |
| 3292 | zeroSignificand(); |
| 3293 | sign = Negative; |
| 3294 | exponent = semantics->minExponent; |
| 3295 | significandParts()[partCountForBits(semantics->precision) - 1] |= |
| 3296 | (((integerPart)1) << ((semantics->precision - 1) % integerPartWidth)); |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3299 | IEEEFloat::IEEEFloat(const fltSemantics &Sem, const APInt &API) { |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 3300 | initFromAPInt(&Sem, API); |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3303 | IEEEFloat::IEEEFloat(float f) { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3304 | initFromAPInt(&semIEEEsingle, APInt::floatToBits(f)); |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3305 | } |
| 3306 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3307 | IEEEFloat::IEEEFloat(double d) { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3308 | initFromAPInt(&semIEEEdouble, APInt::doubleToBits(d)); |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 3309 | } |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3310 | |
| 3311 | namespace { |
David Blaikie | 70fdf72 | 2012-07-25 18:04:24 +0000 | [diff] [blame] | 3312 | void append(SmallVectorImpl<char> &Buffer, StringRef Str) { |
| 3313 | Buffer.append(Str.begin(), Str.end()); |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
John McCall | e6212ace | 2009-12-24 12:16:56 +0000 | [diff] [blame] | 3316 | /// Removes data from the given significand until it is no more |
| 3317 | /// precise than is required for the desired precision. |
| 3318 | void AdjustToPrecision(APInt &significand, |
| 3319 | int &exp, unsigned FormatPrecision) { |
| 3320 | unsigned bits = significand.getActiveBits(); |
| 3321 | |
| 3322 | // 196/59 is a very slight overestimate of lg_2(10). |
| 3323 | unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59; |
| 3324 | |
| 3325 | if (bits <= bitsRequired) return; |
| 3326 | |
| 3327 | unsigned tensRemovable = (bits - bitsRequired) * 59 / 196; |
| 3328 | if (!tensRemovable) return; |
| 3329 | |
| 3330 | exp += tensRemovable; |
| 3331 | |
| 3332 | APInt divisor(significand.getBitWidth(), 1); |
| 3333 | APInt powten(significand.getBitWidth(), 10); |
| 3334 | while (true) { |
| 3335 | if (tensRemovable & 1) |
| 3336 | divisor *= powten; |
| 3337 | tensRemovable >>= 1; |
| 3338 | if (!tensRemovable) break; |
| 3339 | powten *= powten; |
| 3340 | } |
| 3341 | |
| 3342 | significand = significand.udiv(divisor); |
| 3343 | |
Hao Liu | be99cc3 | 2013-03-20 01:46:36 +0000 | [diff] [blame] | 3344 | // Truncate the significand down to its active bit count. |
| 3345 | significand = significand.trunc(significand.getActiveBits()); |
John McCall | e6212ace | 2009-12-24 12:16:56 +0000 | [diff] [blame] | 3346 | } |
| 3347 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 3348 | |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3349 | void AdjustToPrecision(SmallVectorImpl<char> &buffer, |
| 3350 | int &exp, unsigned FormatPrecision) { |
| 3351 | unsigned N = buffer.size(); |
| 3352 | if (N <= FormatPrecision) return; |
| 3353 | |
| 3354 | // The most significant figures are the last ones in the buffer. |
| 3355 | unsigned FirstSignificant = N - FormatPrecision; |
| 3356 | |
| 3357 | // Round. |
| 3358 | // FIXME: this probably shouldn't use 'round half up'. |
| 3359 | |
| 3360 | // Rounding down is just a truncation, except we also want to drop |
| 3361 | // trailing zeros from the new result. |
| 3362 | if (buffer[FirstSignificant - 1] < '5') { |
NAKAMURA Takumi | 5adeb93 | 2012-02-19 03:18:29 +0000 | [diff] [blame] | 3363 | while (FirstSignificant < N && buffer[FirstSignificant] == '0') |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3364 | FirstSignificant++; |
| 3365 | |
| 3366 | exp += FirstSignificant; |
| 3367 | buffer.erase(&buffer[0], &buffer[FirstSignificant]); |
| 3368 | return; |
| 3369 | } |
| 3370 | |
| 3371 | // Rounding up requires a decimal add-with-carry. If we continue |
| 3372 | // the carry, the newly-introduced zeros will just be truncated. |
| 3373 | for (unsigned I = FirstSignificant; I != N; ++I) { |
| 3374 | if (buffer[I] == '9') { |
| 3375 | FirstSignificant++; |
| 3376 | } else { |
| 3377 | buffer[I]++; |
| 3378 | break; |
| 3379 | } |
| 3380 | } |
| 3381 | |
| 3382 | // If we carried through, we have exactly one digit of precision. |
| 3383 | if (FirstSignificant == N) { |
| 3384 | exp += FirstSignificant; |
| 3385 | buffer.clear(); |
| 3386 | buffer.push_back('1'); |
| 3387 | return; |
| 3388 | } |
| 3389 | |
| 3390 | exp += FirstSignificant; |
| 3391 | buffer.erase(&buffer[0], &buffer[FirstSignificant]); |
| 3392 | } |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 3393 | } |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3394 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3395 | void IEEEFloat::toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision, |
| 3396 | unsigned FormatMaxPadding) const { |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3397 | switch (category) { |
| 3398 | case fcInfinity: |
| 3399 | if (isNegative()) |
| 3400 | return append(Str, "-Inf"); |
| 3401 | else |
| 3402 | return append(Str, "+Inf"); |
| 3403 | |
| 3404 | case fcNaN: return append(Str, "NaN"); |
| 3405 | |
| 3406 | case fcZero: |
| 3407 | if (isNegative()) |
| 3408 | Str.push_back('-'); |
| 3409 | |
| 3410 | if (!FormatMaxPadding) |
| 3411 | append(Str, "0.0E+0"); |
| 3412 | else |
| 3413 | Str.push_back('0'); |
| 3414 | return; |
| 3415 | |
| 3416 | case fcNormal: |
| 3417 | break; |
| 3418 | } |
| 3419 | |
| 3420 | if (isNegative()) |
| 3421 | Str.push_back('-'); |
| 3422 | |
| 3423 | // Decompose the number into an APInt and an exponent. |
| 3424 | int exp = exponent - ((int) semantics->precision - 1); |
| 3425 | APInt significand(semantics->precision, |
Jeffrey Yasskin | 7a16288 | 2011-07-18 21:45:40 +0000 | [diff] [blame] | 3426 | makeArrayRef(significandParts(), |
| 3427 | partCountForBits(semantics->precision))); |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3428 | |
John McCall | dd5044a | 2009-12-24 23:18:09 +0000 | [diff] [blame] | 3429 | // Set FormatPrecision if zero. We want to do this before we |
| 3430 | // truncate trailing zeros, as those are part of the precision. |
| 3431 | if (!FormatPrecision) { |
Eli Friedman | e72f132 | 2013-08-29 23:44:34 +0000 | [diff] [blame] | 3432 | // We use enough digits so the number can be round-tripped back to an |
| 3433 | // APFloat. The formula comes from "How to Print Floating-Point Numbers |
| 3434 | // Accurately" by Steele and White. |
| 3435 | // FIXME: Using a formula based purely on the precision is conservative; |
| 3436 | // we can print fewer digits depending on the actual value being printed. |
John McCall | dd5044a | 2009-12-24 23:18:09 +0000 | [diff] [blame] | 3437 | |
Eli Friedman | e72f132 | 2013-08-29 23:44:34 +0000 | [diff] [blame] | 3438 | // FormatPrecision = 2 + floor(significandBits / lg_2(10)) |
| 3439 | FormatPrecision = 2 + semantics->precision * 59 / 196; |
John McCall | dd5044a | 2009-12-24 23:18:09 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3442 | // Ignore trailing binary zeros. |
| 3443 | int trailingZeros = significand.countTrailingZeros(); |
| 3444 | exp += trailingZeros; |
Craig Topper | fc947bc | 2017-04-18 17:14:21 +0000 | [diff] [blame^] | 3445 | significand.lshrInPlace(trailingZeros); |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3446 | |
| 3447 | // Change the exponent from 2^e to 10^e. |
| 3448 | if (exp == 0) { |
| 3449 | // Nothing to do. |
| 3450 | } else if (exp > 0) { |
| 3451 | // Just shift left. |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3452 | significand = significand.zext(semantics->precision + exp); |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3453 | significand <<= exp; |
| 3454 | exp = 0; |
| 3455 | } else { /* exp < 0 */ |
| 3456 | int texp = -exp; |
| 3457 | |
| 3458 | // We transform this using the identity: |
| 3459 | // (N)(2^-e) == (N)(5^e)(10^-e) |
| 3460 | // This means we have to multiply N (the significand) by 5^e. |
| 3461 | // To avoid overflow, we have to operate on numbers large |
| 3462 | // enough to store N * 5^e: |
| 3463 | // log2(N * 5^e) == log2(N) + e * log2(5) |
John McCall | dd5044a | 2009-12-24 23:18:09 +0000 | [diff] [blame] | 3464 | // <= semantics->precision + e * 137 / 59 |
| 3465 | // (log_2(5) ~ 2.321928 < 2.322034 ~ 137/59) |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 3466 | |
Eli Friedman | 1954641 | 2011-10-07 23:40:49 +0000 | [diff] [blame] | 3467 | unsigned precision = semantics->precision + (137 * texp + 136) / 59; |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3468 | |
| 3469 | // Multiply significand by 5^e. |
| 3470 | // N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8) |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3471 | significand = significand.zext(precision); |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3472 | APInt five_to_the_i(precision, 5); |
| 3473 | while (true) { |
| 3474 | if (texp & 1) significand *= five_to_the_i; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 3475 | |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3476 | texp >>= 1; |
| 3477 | if (!texp) break; |
| 3478 | five_to_the_i *= five_to_the_i; |
| 3479 | } |
| 3480 | } |
| 3481 | |
John McCall | e6212ace | 2009-12-24 12:16:56 +0000 | [diff] [blame] | 3482 | AdjustToPrecision(significand, exp, FormatPrecision); |
| 3483 | |
Dmitri Gribenko | 226fea5 | 2013-01-13 16:01:15 +0000 | [diff] [blame] | 3484 | SmallVector<char, 256> buffer; |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3485 | |
| 3486 | // Fill the buffer. |
| 3487 | unsigned precision = significand.getBitWidth(); |
| 3488 | APInt ten(precision, 10); |
| 3489 | APInt digit(precision, 0); |
| 3490 | |
| 3491 | bool inTrail = true; |
| 3492 | while (significand != 0) { |
| 3493 | // digit <- significand % 10 |
| 3494 | // significand <- significand / 10 |
| 3495 | APInt::udivrem(significand, ten, significand, digit); |
| 3496 | |
| 3497 | unsigned d = digit.getZExtValue(); |
| 3498 | |
| 3499 | // Drop trailing zeros. |
| 3500 | if (inTrail && !d) exp++; |
| 3501 | else { |
| 3502 | buffer.push_back((char) ('0' + d)); |
| 3503 | inTrail = false; |
| 3504 | } |
| 3505 | } |
| 3506 | |
| 3507 | assert(!buffer.empty() && "no characters in buffer!"); |
| 3508 | |
| 3509 | // Drop down to FormatPrecision. |
| 3510 | // TODO: don't do more precise calculations above than are required. |
| 3511 | AdjustToPrecision(buffer, exp, FormatPrecision); |
| 3512 | |
| 3513 | unsigned NDigits = buffer.size(); |
| 3514 | |
John McCall | dd5044a | 2009-12-24 23:18:09 +0000 | [diff] [blame] | 3515 | // Check whether we should use scientific notation. |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3516 | bool FormatScientific; |
| 3517 | if (!FormatMaxPadding) |
| 3518 | FormatScientific = true; |
| 3519 | else { |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3520 | if (exp >= 0) { |
John McCall | dd5044a | 2009-12-24 23:18:09 +0000 | [diff] [blame] | 3521 | // 765e3 --> 765000 |
| 3522 | // ^^^ |
| 3523 | // But we shouldn't make the number look more precise than it is. |
| 3524 | FormatScientific = ((unsigned) exp > FormatMaxPadding || |
| 3525 | NDigits + (unsigned) exp > FormatPrecision); |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3526 | } else { |
John McCall | dd5044a | 2009-12-24 23:18:09 +0000 | [diff] [blame] | 3527 | // Power of the most significant digit. |
| 3528 | int MSD = exp + (int) (NDigits - 1); |
| 3529 | if (MSD >= 0) { |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3530 | // 765e-2 == 7.65 |
John McCall | dd5044a | 2009-12-24 23:18:09 +0000 | [diff] [blame] | 3531 | FormatScientific = false; |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3532 | } else { |
| 3533 | // 765e-5 == 0.00765 |
| 3534 | // ^ ^^ |
John McCall | dd5044a | 2009-12-24 23:18:09 +0000 | [diff] [blame] | 3535 | FormatScientific = ((unsigned) -MSD) > FormatMaxPadding; |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3536 | } |
| 3537 | } |
John McCall | 29b5c28 | 2009-12-24 08:56:26 +0000 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | // Scientific formatting is pretty straightforward. |
| 3541 | if (FormatScientific) { |
| 3542 | exp += (NDigits - 1); |
| 3543 | |
| 3544 | Str.push_back(buffer[NDigits-1]); |
| 3545 | Str.push_back('.'); |
| 3546 | if (NDigits == 1) |
| 3547 | Str.push_back('0'); |
| 3548 | else |
| 3549 | for (unsigned I = 1; I != NDigits; ++I) |
| 3550 | Str.push_back(buffer[NDigits-1-I]); |
| 3551 | Str.push_back('E'); |
| 3552 | |
| 3553 | Str.push_back(exp >= 0 ? '+' : '-'); |
| 3554 | if (exp < 0) exp = -exp; |
| 3555 | SmallVector<char, 6> expbuf; |
| 3556 | do { |
| 3557 | expbuf.push_back((char) ('0' + (exp % 10))); |
| 3558 | exp /= 10; |
| 3559 | } while (exp); |
| 3560 | for (unsigned I = 0, E = expbuf.size(); I != E; ++I) |
| 3561 | Str.push_back(expbuf[E-1-I]); |
| 3562 | return; |
| 3563 | } |
| 3564 | |
| 3565 | // Non-scientific, positive exponents. |
| 3566 | if (exp >= 0) { |
| 3567 | for (unsigned I = 0; I != NDigits; ++I) |
| 3568 | Str.push_back(buffer[NDigits-1-I]); |
| 3569 | for (unsigned I = 0; I != (unsigned) exp; ++I) |
| 3570 | Str.push_back('0'); |
| 3571 | return; |
| 3572 | } |
| 3573 | |
| 3574 | // Non-scientific, negative exponents. |
| 3575 | |
| 3576 | // The number of digits to the left of the decimal point. |
| 3577 | int NWholeDigits = exp + (int) NDigits; |
| 3578 | |
| 3579 | unsigned I = 0; |
| 3580 | if (NWholeDigits > 0) { |
| 3581 | for (; I != (unsigned) NWholeDigits; ++I) |
| 3582 | Str.push_back(buffer[NDigits-I-1]); |
| 3583 | Str.push_back('.'); |
| 3584 | } else { |
| 3585 | unsigned NZeros = 1 + (unsigned) -NWholeDigits; |
| 3586 | |
| 3587 | Str.push_back('0'); |
| 3588 | Str.push_back('.'); |
| 3589 | for (unsigned Z = 1; Z != NZeros; ++Z) |
| 3590 | Str.push_back('0'); |
| 3591 | } |
| 3592 | |
| 3593 | for (; I != NDigits; ++I) |
| 3594 | Str.push_back(buffer[NDigits-I-1]); |
| 3595 | } |
Benjamin Kramer | 03fd672 | 2011-03-30 15:42:27 +0000 | [diff] [blame] | 3596 | |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 3597 | bool IEEEFloat::getExactInverse(APFloat *inv) const { |
Benjamin Kramer | 03fd672 | 2011-03-30 15:42:27 +0000 | [diff] [blame] | 3598 | // Special floats and denormals have no exact inverse. |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 3599 | if (!isFiniteNonZero()) |
Benjamin Kramer | 03fd672 | 2011-03-30 15:42:27 +0000 | [diff] [blame] | 3600 | return false; |
| 3601 | |
| 3602 | // Check that the number is a power of two by making sure that only the |
| 3603 | // integer bit is set in the significand. |
| 3604 | if (significandLSB() != semantics->precision - 1) |
| 3605 | return false; |
| 3606 | |
| 3607 | // Get the inverse. |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3608 | IEEEFloat reciprocal(*semantics, 1ULL); |
Benjamin Kramer | 03fd672 | 2011-03-30 15:42:27 +0000 | [diff] [blame] | 3609 | if (reciprocal.divide(*this, rmNearestTiesToEven) != opOK) |
| 3610 | return false; |
| 3611 | |
Benjamin Kramer | af0ed95 | 2011-03-30 17:02:54 +0000 | [diff] [blame] | 3612 | // Avoid multiplication with a denormal, it is not safe on all platforms and |
| 3613 | // may be slower than a normal division. |
Benjamin Kramer | 6bef24f | 2013-06-01 11:26:33 +0000 | [diff] [blame] | 3614 | if (reciprocal.isDenormal()) |
Benjamin Kramer | af0ed95 | 2011-03-30 17:02:54 +0000 | [diff] [blame] | 3615 | return false; |
| 3616 | |
Michael Gottesman | 8136c38 | 2013-06-26 23:17:28 +0000 | [diff] [blame] | 3617 | assert(reciprocal.isFiniteNonZero() && |
Benjamin Kramer | af0ed95 | 2011-03-30 17:02:54 +0000 | [diff] [blame] | 3618 | reciprocal.significandLSB() == reciprocal.semantics->precision - 1); |
| 3619 | |
Benjamin Kramer | 03fd672 | 2011-03-30 15:42:27 +0000 | [diff] [blame] | 3620 | if (inv) |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 3621 | *inv = APFloat(reciprocal, *semantics); |
Benjamin Kramer | 03fd672 | 2011-03-30 15:42:27 +0000 | [diff] [blame] | 3622 | |
| 3623 | return true; |
| 3624 | } |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 3625 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3626 | bool IEEEFloat::isSignaling() const { |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 3627 | if (!isNaN()) |
| 3628 | return false; |
| 3629 | |
| 3630 | // IEEE-754R 2008 6.2.1: A signaling NaN bit string should be encoded with the |
| 3631 | // first bit of the trailing significand being 0. |
| 3632 | return !APInt::tcExtractBit(significandParts(), semantics->precision - 2); |
| 3633 | } |
| 3634 | |
| 3635 | /// IEEE-754R 2008 5.3.1: nextUp/nextDown. |
| 3636 | /// |
| 3637 | /// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with |
| 3638 | /// appropriate sign switching before/after the computation. |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3639 | IEEEFloat::opStatus IEEEFloat::next(bool nextDown) { |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 3640 | // If we are performing nextDown, swap sign so we have -x. |
| 3641 | if (nextDown) |
| 3642 | changeSign(); |
| 3643 | |
| 3644 | // Compute nextUp(x) |
| 3645 | opStatus result = opOK; |
| 3646 | |
| 3647 | // Handle each float category separately. |
| 3648 | switch (category) { |
| 3649 | case fcInfinity: |
| 3650 | // nextUp(+inf) = +inf |
| 3651 | if (!isNegative()) |
| 3652 | break; |
| 3653 | // nextUp(-inf) = -getLargest() |
| 3654 | makeLargest(true); |
| 3655 | break; |
| 3656 | case fcNaN: |
| 3657 | // IEEE-754R 2008 6.2 Par 2: nextUp(sNaN) = qNaN. Set Invalid flag. |
| 3658 | // IEEE-754R 2008 6.2: nextUp(qNaN) = qNaN. Must be identity so we do not |
| 3659 | // change the payload. |
| 3660 | if (isSignaling()) { |
| 3661 | result = opInvalidOp; |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 3662 | // For consistency, propagate the sign of the sNaN to the qNaN. |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 3663 | makeNaN(false, isNegative(), nullptr); |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 3664 | } |
| 3665 | break; |
| 3666 | case fcZero: |
| 3667 | // nextUp(pm 0) = +getSmallest() |
| 3668 | makeSmallest(false); |
| 3669 | break; |
| 3670 | case fcNormal: |
| 3671 | // nextUp(-getSmallest()) = -0 |
| 3672 | if (isSmallest() && isNegative()) { |
| 3673 | APInt::tcSet(significandParts(), 0, partCount()); |
| 3674 | category = fcZero; |
| 3675 | exponent = 0; |
| 3676 | break; |
| 3677 | } |
| 3678 | |
| 3679 | // nextUp(getLargest()) == INFINITY |
| 3680 | if (isLargest() && !isNegative()) { |
| 3681 | APInt::tcSet(significandParts(), 0, partCount()); |
| 3682 | category = fcInfinity; |
| 3683 | exponent = semantics->maxExponent + 1; |
| 3684 | break; |
| 3685 | } |
| 3686 | |
| 3687 | // nextUp(normal) == normal + inc. |
| 3688 | if (isNegative()) { |
| 3689 | // If we are negative, we need to decrement the significand. |
| 3690 | |
| 3691 | // We only cross a binade boundary that requires adjusting the exponent |
| 3692 | // if: |
| 3693 | // 1. exponent != semantics->minExponent. This implies we are not in the |
| 3694 | // smallest binade or are dealing with denormals. |
| 3695 | // 2. Our significand excluding the integral bit is all zeros. |
| 3696 | bool WillCrossBinadeBoundary = |
| 3697 | exponent != semantics->minExponent && isSignificandAllZeros(); |
| 3698 | |
| 3699 | // Decrement the significand. |
| 3700 | // |
| 3701 | // We always do this since: |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 3702 | // 1. If we are dealing with a non-binade decrement, by definition we |
Michael Gottesman | 0c622ea | 2013-05-30 18:07:13 +0000 | [diff] [blame] | 3703 | // just decrement the significand. |
| 3704 | // 2. If we are dealing with a normal -> normal binade decrement, since |
| 3705 | // we have an explicit integral bit the fact that all bits but the |
| 3706 | // integral bit are zero implies that subtracting one will yield a |
| 3707 | // significand with 0 integral bit and 1 in all other spots. Thus we |
| 3708 | // must just adjust the exponent and set the integral bit to 1. |
| 3709 | // 3. If we are dealing with a normal -> denormal binade decrement, |
| 3710 | // since we set the integral bit to 0 when we represent denormals, we |
| 3711 | // just decrement the significand. |
| 3712 | integerPart *Parts = significandParts(); |
| 3713 | APInt::tcDecrement(Parts, partCount()); |
| 3714 | |
| 3715 | if (WillCrossBinadeBoundary) { |
| 3716 | // Our result is a normal number. Do the following: |
| 3717 | // 1. Set the integral bit to 1. |
| 3718 | // 2. Decrement the exponent. |
| 3719 | APInt::tcSetBit(Parts, semantics->precision - 1); |
| 3720 | exponent--; |
| 3721 | } |
| 3722 | } else { |
| 3723 | // If we are positive, we need to increment the significand. |
| 3724 | |
| 3725 | // We only cross a binade boundary that requires adjusting the exponent if |
| 3726 | // the input is not a denormal and all of said input's significand bits |
| 3727 | // are set. If all of said conditions are true: clear the significand, set |
| 3728 | // the integral bit to 1, and increment the exponent. If we have a |
| 3729 | // denormal always increment since moving denormals and the numbers in the |
| 3730 | // smallest normal binade have the same exponent in our representation. |
| 3731 | bool WillCrossBinadeBoundary = !isDenormal() && isSignificandAllOnes(); |
| 3732 | |
| 3733 | if (WillCrossBinadeBoundary) { |
| 3734 | integerPart *Parts = significandParts(); |
| 3735 | APInt::tcSet(Parts, 0, partCount()); |
| 3736 | APInt::tcSetBit(Parts, semantics->precision - 1); |
| 3737 | assert(exponent != semantics->maxExponent && |
| 3738 | "We can not increment an exponent beyond the maxExponent allowed" |
| 3739 | " by the given floating point semantics."); |
| 3740 | exponent++; |
| 3741 | } else { |
| 3742 | incrementSignificand(); |
| 3743 | } |
| 3744 | } |
| 3745 | break; |
| 3746 | } |
| 3747 | |
| 3748 | // If we are performing nextDown, swap sign so we have -nextUp(-x) |
| 3749 | if (nextDown) |
| 3750 | changeSign(); |
| 3751 | |
| 3752 | return result; |
| 3753 | } |
Michael Gottesman | c4facdf | 2013-06-24 09:58:02 +0000 | [diff] [blame] | 3754 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3755 | void IEEEFloat::makeInf(bool Negative) { |
Michael Gottesman | c4facdf | 2013-06-24 09:58:02 +0000 | [diff] [blame] | 3756 | category = fcInfinity; |
| 3757 | sign = Negative; |
| 3758 | exponent = semantics->maxExponent + 1; |
| 3759 | APInt::tcSet(significandParts(), 0, partCount()); |
| 3760 | } |
| 3761 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3762 | void IEEEFloat::makeZero(bool Negative) { |
Michael Gottesman | c4facdf | 2013-06-24 09:58:02 +0000 | [diff] [blame] | 3763 | category = fcZero; |
| 3764 | sign = Negative; |
| 3765 | exponent = semantics->minExponent-1; |
Matt Arsenault | c25a711 | 2016-03-21 16:49:16 +0000 | [diff] [blame] | 3766 | APInt::tcSet(significandParts(), 0, partCount()); |
| 3767 | } |
| 3768 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3769 | void IEEEFloat::makeQuiet() { |
Matt Arsenault | c25a711 | 2016-03-21 16:49:16 +0000 | [diff] [blame] | 3770 | assert(isNaN()); |
| 3771 | APInt::tcSetBit(significandParts(), semantics->precision - 2); |
Michael Gottesman | c4facdf | 2013-06-24 09:58:02 +0000 | [diff] [blame] | 3772 | } |
Chandler Carruth | d9edd1e | 2014-10-10 04:54:30 +0000 | [diff] [blame] | 3773 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3774 | int ilogb(const IEEEFloat &Arg) { |
Matt Arsenault | 69fdf9b | 2016-03-13 05:12:32 +0000 | [diff] [blame] | 3775 | if (Arg.isNaN()) |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3776 | return IEEEFloat::IEK_NaN; |
Matt Arsenault | 69fdf9b | 2016-03-13 05:12:32 +0000 | [diff] [blame] | 3777 | if (Arg.isZero()) |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3778 | return IEEEFloat::IEK_Zero; |
Matt Arsenault | 69fdf9b | 2016-03-13 05:12:32 +0000 | [diff] [blame] | 3779 | if (Arg.isInfinity()) |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3780 | return IEEEFloat::IEK_Inf; |
Matt Arsenault | 69fdf9b | 2016-03-13 05:12:32 +0000 | [diff] [blame] | 3781 | if (!Arg.isDenormal()) |
| 3782 | return Arg.exponent; |
| 3783 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3784 | IEEEFloat Normalized(Arg); |
Matt Arsenault | 69fdf9b | 2016-03-13 05:12:32 +0000 | [diff] [blame] | 3785 | int SignificandBits = Arg.getSemantics().precision - 1; |
| 3786 | |
| 3787 | Normalized.exponent += SignificandBits; |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3788 | Normalized.normalize(IEEEFloat::rmNearestTiesToEven, lfExactlyZero); |
Matt Arsenault | 69fdf9b | 2016-03-13 05:12:32 +0000 | [diff] [blame] | 3789 | return Normalized.exponent - SignificandBits; |
| 3790 | } |
| 3791 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3792 | IEEEFloat scalbn(IEEEFloat X, int Exp, IEEEFloat::roundingMode RoundingMode) { |
Chandler Carruth | d9edd1e | 2014-10-10 04:54:30 +0000 | [diff] [blame] | 3793 | auto MaxExp = X.getSemantics().maxExponent; |
| 3794 | auto MinExp = X.getSemantics().minExponent; |
Chandler Carruth | d9edd1e | 2014-10-10 04:54:30 +0000 | [diff] [blame] | 3795 | |
Matt Arsenault | afa31cf | 2016-03-13 05:11:51 +0000 | [diff] [blame] | 3796 | // If Exp is wildly out-of-scale, simply adding it to X.exponent will |
| 3797 | // overflow; clamp it to a safe range before adding, but ensure that the range |
| 3798 | // is large enough that the clamp does not change the result. The range we |
| 3799 | // need to support is the difference between the largest possible exponent and |
| 3800 | // the normalized exponent of half the smallest denormal. |
| 3801 | |
| 3802 | int SignificandBits = X.getSemantics().precision - 1; |
| 3803 | int MaxIncrement = MaxExp - (MinExp - SignificandBits) + 1; |
| 3804 | |
| 3805 | // Clamp to one past the range ends to let normalize handle overlflow. |
| 3806 | X.exponent += std::min(std::max(Exp, -MaxIncrement - 1), MaxIncrement); |
| 3807 | X.normalize(RoundingMode, lfExactlyZero); |
Matt Arsenault | ea00b49 | 2016-03-23 23:51:45 +0000 | [diff] [blame] | 3808 | if (X.isNaN()) |
| 3809 | X.makeQuiet(); |
Richard Trieu | 6ae3796 | 2015-04-30 23:07:00 +0000 | [diff] [blame] | 3810 | return X; |
Chandler Carruth | d9edd1e | 2014-10-10 04:54:30 +0000 | [diff] [blame] | 3811 | } |
Matt Arsenault | c25a711 | 2016-03-21 16:49:16 +0000 | [diff] [blame] | 3812 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3813 | IEEEFloat frexp(const IEEEFloat &Val, int &Exp, IEEEFloat::roundingMode RM) { |
Matt Arsenault | c25a711 | 2016-03-21 16:49:16 +0000 | [diff] [blame] | 3814 | Exp = ilogb(Val); |
| 3815 | |
| 3816 | // Quiet signalling nans. |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3817 | if (Exp == IEEEFloat::IEK_NaN) { |
| 3818 | IEEEFloat Quiet(Val); |
Matt Arsenault | c25a711 | 2016-03-21 16:49:16 +0000 | [diff] [blame] | 3819 | Quiet.makeQuiet(); |
| 3820 | return Quiet; |
| 3821 | } |
| 3822 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3823 | if (Exp == IEEEFloat::IEK_Inf) |
Matt Arsenault | c25a711 | 2016-03-21 16:49:16 +0000 | [diff] [blame] | 3824 | return Val; |
| 3825 | |
| 3826 | // 1 is added because frexp is defined to return a normalized fraction in |
| 3827 | // +/-[0.5, 1.0), rather than the usual +/-[1.0, 2.0). |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3828 | Exp = Exp == IEEEFloat::IEK_Zero ? 0 : Exp + 1; |
Matt Arsenault | c25a711 | 2016-03-21 16:49:16 +0000 | [diff] [blame] | 3829 | return scalbn(Val, -Exp, RM); |
| 3830 | } |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 3831 | |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 3832 | DoubleAPFloat::DoubleAPFloat(const fltSemantics &S) |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 3833 | : Semantics(&S), |
| 3834 | Floats(new APFloat[2]{APFloat(semIEEEdouble), APFloat(semIEEEdouble)}) { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3835 | assert(Semantics == &semPPCDoubleDouble); |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 3836 | } |
| 3837 | |
| 3838 | DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, uninitializedTag) |
| 3839 | : Semantics(&S), |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 3840 | Floats(new APFloat[2]{APFloat(semIEEEdouble, uninitialized), |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3841 | APFloat(semIEEEdouble, uninitialized)}) { |
| 3842 | assert(Semantics == &semPPCDoubleDouble); |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
| 3845 | DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, integerPart I) |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 3846 | : Semantics(&S), Floats(new APFloat[2]{APFloat(semIEEEdouble, I), |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3847 | APFloat(semIEEEdouble)}) { |
| 3848 | assert(Semantics == &semPPCDoubleDouble); |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, const APInt &I) |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 3852 | : Semantics(&S), |
| 3853 | Floats(new APFloat[2]{ |
| 3854 | APFloat(semIEEEdouble, APInt(64, I.getRawData()[0])), |
| 3855 | APFloat(semIEEEdouble, APInt(64, I.getRawData()[1]))}) { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3856 | assert(Semantics == &semPPCDoubleDouble); |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
| 3859 | DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, APFloat &&First, |
| 3860 | APFloat &&Second) |
| 3861 | : Semantics(&S), |
| 3862 | Floats(new APFloat[2]{std::move(First), std::move(Second)}) { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3863 | assert(Semantics == &semPPCDoubleDouble); |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 3864 | assert(&Floats[0].getSemantics() == &semIEEEdouble); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3865 | assert(&Floats[1].getSemantics() == &semIEEEdouble); |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 3866 | } |
| 3867 | |
| 3868 | DoubleAPFloat::DoubleAPFloat(const DoubleAPFloat &RHS) |
| 3869 | : Semantics(RHS.Semantics), |
Tim Shen | b4991548 | 2016-10-28 22:45:33 +0000 | [diff] [blame] | 3870 | Floats(RHS.Floats ? new APFloat[2]{APFloat(RHS.Floats[0]), |
| 3871 | APFloat(RHS.Floats[1])} |
| 3872 | : nullptr) { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3873 | assert(Semantics == &semPPCDoubleDouble); |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 3874 | } |
| 3875 | |
| 3876 | DoubleAPFloat::DoubleAPFloat(DoubleAPFloat &&RHS) |
| 3877 | : Semantics(RHS.Semantics), Floats(std::move(RHS.Floats)) { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3878 | RHS.Semantics = &semBogus; |
| 3879 | assert(Semantics == &semPPCDoubleDouble); |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 3880 | } |
| 3881 | |
| 3882 | DoubleAPFloat &DoubleAPFloat::operator=(const DoubleAPFloat &RHS) { |
Tim Shen | b4991548 | 2016-10-28 22:45:33 +0000 | [diff] [blame] | 3883 | if (Semantics == RHS.Semantics && RHS.Floats) { |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 3884 | Floats[0] = RHS.Floats[0]; |
| 3885 | Floats[1] = RHS.Floats[1]; |
| 3886 | } else if (this != &RHS) { |
| 3887 | this->~DoubleAPFloat(); |
| 3888 | new (this) DoubleAPFloat(RHS); |
| 3889 | } |
| 3890 | return *this; |
| 3891 | } |
| 3892 | |
Tim Shen | 7f12762 | 2017-01-24 00:19:45 +0000 | [diff] [blame] | 3893 | // Implement addition, subtraction, multiplication and division based on: |
Tim Shen | 44bde89 | 2016-12-12 21:59:30 +0000 | [diff] [blame] | 3894 | // "Software for Doubled-Precision Floating-Point Computations", |
| 3895 | // by Seppo Linnainmaa, ACM TOMS vol 7 no 3, September 1981, pages 272-283. |
| 3896 | APFloat::opStatus DoubleAPFloat::addImpl(const APFloat &a, const APFloat &aa, |
| 3897 | const APFloat &c, const APFloat &cc, |
| 3898 | roundingMode RM) { |
| 3899 | int Status = opOK; |
| 3900 | APFloat z = a; |
| 3901 | Status |= z.add(c, RM); |
| 3902 | if (!z.isFinite()) { |
| 3903 | if (!z.isInfinity()) { |
| 3904 | Floats[0] = std::move(z); |
Tim Shen | 7117e69 | 2017-01-26 00:11:07 +0000 | [diff] [blame] | 3905 | Floats[1].makeZero(/* Neg = */ false); |
Tim Shen | 44bde89 | 2016-12-12 21:59:30 +0000 | [diff] [blame] | 3906 | return (opStatus)Status; |
| 3907 | } |
| 3908 | Status = opOK; |
| 3909 | auto AComparedToC = a.compareAbsoluteValue(c); |
| 3910 | z = cc; |
| 3911 | Status |= z.add(aa, RM); |
| 3912 | if (AComparedToC == APFloat::cmpGreaterThan) { |
| 3913 | // z = cc + aa + c + a; |
| 3914 | Status |= z.add(c, RM); |
| 3915 | Status |= z.add(a, RM); |
| 3916 | } else { |
| 3917 | // z = cc + aa + a + c; |
| 3918 | Status |= z.add(a, RM); |
| 3919 | Status |= z.add(c, RM); |
| 3920 | } |
| 3921 | if (!z.isFinite()) { |
| 3922 | Floats[0] = std::move(z); |
Tim Shen | 7117e69 | 2017-01-26 00:11:07 +0000 | [diff] [blame] | 3923 | Floats[1].makeZero(/* Neg = */ false); |
Tim Shen | 44bde89 | 2016-12-12 21:59:30 +0000 | [diff] [blame] | 3924 | return (opStatus)Status; |
| 3925 | } |
| 3926 | Floats[0] = z; |
| 3927 | APFloat zz = aa; |
| 3928 | Status |= zz.add(cc, RM); |
| 3929 | if (AComparedToC == APFloat::cmpGreaterThan) { |
| 3930 | // Floats[1] = a - z + c + zz; |
| 3931 | Floats[1] = a; |
| 3932 | Status |= Floats[1].subtract(z, RM); |
| 3933 | Status |= Floats[1].add(c, RM); |
| 3934 | Status |= Floats[1].add(zz, RM); |
| 3935 | } else { |
| 3936 | // Floats[1] = c - z + a + zz; |
| 3937 | Floats[1] = c; |
| 3938 | Status |= Floats[1].subtract(z, RM); |
| 3939 | Status |= Floats[1].add(a, RM); |
| 3940 | Status |= Floats[1].add(zz, RM); |
| 3941 | } |
| 3942 | } else { |
| 3943 | // q = a - z; |
| 3944 | APFloat q = a; |
| 3945 | Status |= q.subtract(z, RM); |
| 3946 | |
| 3947 | // zz = q + c + (a - (q + z)) + aa + cc; |
| 3948 | // Compute a - (q + z) as -((q + z) - a) to avoid temporary copies. |
| 3949 | auto zz = q; |
| 3950 | Status |= zz.add(c, RM); |
| 3951 | Status |= q.add(z, RM); |
| 3952 | Status |= q.subtract(a, RM); |
| 3953 | q.changeSign(); |
| 3954 | Status |= zz.add(q, RM); |
| 3955 | Status |= zz.add(aa, RM); |
| 3956 | Status |= zz.add(cc, RM); |
| 3957 | if (zz.isZero() && !zz.isNegative()) { |
| 3958 | Floats[0] = std::move(z); |
Tim Shen | 7117e69 | 2017-01-26 00:11:07 +0000 | [diff] [blame] | 3959 | Floats[1].makeZero(/* Neg = */ false); |
Tim Shen | 44bde89 | 2016-12-12 21:59:30 +0000 | [diff] [blame] | 3960 | return opOK; |
| 3961 | } |
| 3962 | Floats[0] = z; |
| 3963 | Status |= Floats[0].add(zz, RM); |
| 3964 | if (!Floats[0].isFinite()) { |
Tim Shen | 7117e69 | 2017-01-26 00:11:07 +0000 | [diff] [blame] | 3965 | Floats[1].makeZero(/* Neg = */ false); |
Tim Shen | 44bde89 | 2016-12-12 21:59:30 +0000 | [diff] [blame] | 3966 | return (opStatus)Status; |
| 3967 | } |
| 3968 | Floats[1] = std::move(z); |
| 3969 | Status |= Floats[1].subtract(Floats[0], RM); |
| 3970 | Status |= Floats[1].add(zz, RM); |
| 3971 | } |
| 3972 | return (opStatus)Status; |
| 3973 | } |
| 3974 | |
| 3975 | APFloat::opStatus DoubleAPFloat::addWithSpecial(const DoubleAPFloat &LHS, |
| 3976 | const DoubleAPFloat &RHS, |
| 3977 | DoubleAPFloat &Out, |
| 3978 | roundingMode RM) { |
| 3979 | if (LHS.getCategory() == fcNaN) { |
| 3980 | Out = LHS; |
| 3981 | return opOK; |
| 3982 | } |
| 3983 | if (RHS.getCategory() == fcNaN) { |
| 3984 | Out = RHS; |
| 3985 | return opOK; |
| 3986 | } |
| 3987 | if (LHS.getCategory() == fcZero) { |
| 3988 | Out = RHS; |
| 3989 | return opOK; |
| 3990 | } |
| 3991 | if (RHS.getCategory() == fcZero) { |
| 3992 | Out = LHS; |
| 3993 | return opOK; |
| 3994 | } |
| 3995 | if (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcInfinity && |
| 3996 | LHS.isNegative() != RHS.isNegative()) { |
| 3997 | Out.makeNaN(false, Out.isNegative(), nullptr); |
| 3998 | return opInvalidOp; |
| 3999 | } |
| 4000 | if (LHS.getCategory() == fcInfinity) { |
| 4001 | Out = LHS; |
| 4002 | return opOK; |
| 4003 | } |
| 4004 | if (RHS.getCategory() == fcInfinity) { |
| 4005 | Out = RHS; |
| 4006 | return opOK; |
| 4007 | } |
| 4008 | assert(LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal); |
| 4009 | |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4010 | APFloat A(LHS.Floats[0]), AA(LHS.Floats[1]), C(RHS.Floats[0]), |
Tim Shen | 44bde89 | 2016-12-12 21:59:30 +0000 | [diff] [blame] | 4011 | CC(RHS.Floats[1]); |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4012 | assert(&A.getSemantics() == &semIEEEdouble); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 4013 | assert(&AA.getSemantics() == &semIEEEdouble); |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4014 | assert(&C.getSemantics() == &semIEEEdouble); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 4015 | assert(&CC.getSemantics() == &semIEEEdouble); |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4016 | assert(&Out.Floats[0].getSemantics() == &semIEEEdouble); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 4017 | assert(&Out.Floats[1].getSemantics() == &semIEEEdouble); |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4018 | return Out.addImpl(A, AA, C, CC, RM); |
Tim Shen | 44bde89 | 2016-12-12 21:59:30 +0000 | [diff] [blame] | 4019 | } |
| 4020 | |
| 4021 | APFloat::opStatus DoubleAPFloat::add(const DoubleAPFloat &RHS, |
| 4022 | roundingMode RM) { |
| 4023 | return addWithSpecial(*this, RHS, *this, RM); |
| 4024 | } |
| 4025 | |
| 4026 | APFloat::opStatus DoubleAPFloat::subtract(const DoubleAPFloat &RHS, |
| 4027 | roundingMode RM) { |
| 4028 | changeSign(); |
| 4029 | auto Ret = add(RHS, RM); |
| 4030 | changeSign(); |
| 4031 | return Ret; |
| 4032 | } |
| 4033 | |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4034 | APFloat::opStatus DoubleAPFloat::multiply(const DoubleAPFloat &RHS, |
| 4035 | APFloat::roundingMode RM) { |
Tim Shen | 7f12762 | 2017-01-24 00:19:45 +0000 | [diff] [blame] | 4036 | const auto &LHS = *this; |
| 4037 | auto &Out = *this; |
| 4038 | /* Interesting observation: For special categories, finding the lowest |
| 4039 | common ancestor of the following layered graph gives the correct |
| 4040 | return category: |
| 4041 | |
| 4042 | NaN |
| 4043 | / \ |
| 4044 | Zero Inf |
| 4045 | \ / |
| 4046 | Normal |
| 4047 | |
| 4048 | e.g. NaN * NaN = NaN |
| 4049 | Zero * Inf = NaN |
| 4050 | Normal * Zero = Zero |
| 4051 | Normal * Inf = Inf |
| 4052 | */ |
| 4053 | if (LHS.getCategory() == fcNaN) { |
| 4054 | Out = LHS; |
| 4055 | return opOK; |
| 4056 | } |
| 4057 | if (RHS.getCategory() == fcNaN) { |
| 4058 | Out = RHS; |
| 4059 | return opOK; |
| 4060 | } |
| 4061 | if ((LHS.getCategory() == fcZero && RHS.getCategory() == fcInfinity) || |
| 4062 | (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcZero)) { |
| 4063 | Out.makeNaN(false, false, nullptr); |
| 4064 | return opOK; |
| 4065 | } |
| 4066 | if (LHS.getCategory() == fcZero || LHS.getCategory() == fcInfinity) { |
| 4067 | Out = LHS; |
| 4068 | return opOK; |
| 4069 | } |
| 4070 | if (RHS.getCategory() == fcZero || RHS.getCategory() == fcInfinity) { |
| 4071 | Out = RHS; |
| 4072 | return opOK; |
| 4073 | } |
| 4074 | assert(LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal && |
| 4075 | "Special cases not handled exhaustively"); |
| 4076 | |
| 4077 | int Status = opOK; |
| 4078 | APFloat A = Floats[0], B = Floats[1], C = RHS.Floats[0], D = RHS.Floats[1]; |
| 4079 | // t = a * c |
| 4080 | APFloat T = A; |
| 4081 | Status |= T.multiply(C, RM); |
| 4082 | if (!T.isFiniteNonZero()) { |
| 4083 | Floats[0] = T; |
Tim Shen | 7117e69 | 2017-01-26 00:11:07 +0000 | [diff] [blame] | 4084 | Floats[1].makeZero(/* Neg = */ false); |
Tim Shen | 7f12762 | 2017-01-24 00:19:45 +0000 | [diff] [blame] | 4085 | return (opStatus)Status; |
| 4086 | } |
| 4087 | |
| 4088 | // tau = fmsub(a, c, t), that is -fmadd(-a, c, t). |
| 4089 | APFloat Tau = A; |
| 4090 | T.changeSign(); |
| 4091 | Status |= Tau.fusedMultiplyAdd(C, T, RM); |
| 4092 | T.changeSign(); |
| 4093 | { |
| 4094 | // v = a * d |
| 4095 | APFloat V = A; |
| 4096 | Status |= V.multiply(D, RM); |
| 4097 | // w = b * c |
| 4098 | APFloat W = B; |
| 4099 | Status |= W.multiply(C, RM); |
| 4100 | Status |= V.add(W, RM); |
| 4101 | // tau += v + w |
| 4102 | Status |= Tau.add(V, RM); |
| 4103 | } |
| 4104 | // u = t + tau |
| 4105 | APFloat U = T; |
| 4106 | Status |= U.add(Tau, RM); |
| 4107 | |
| 4108 | Floats[0] = U; |
| 4109 | if (!U.isFinite()) { |
Tim Shen | 7117e69 | 2017-01-26 00:11:07 +0000 | [diff] [blame] | 4110 | Floats[1].makeZero(/* Neg = */ false); |
Tim Shen | 7f12762 | 2017-01-24 00:19:45 +0000 | [diff] [blame] | 4111 | } else { |
| 4112 | // Floats[1] = (t - u) + tau |
| 4113 | Status |= T.subtract(U, RM); |
| 4114 | Status |= T.add(Tau, RM); |
| 4115 | Floats[1] = T; |
| 4116 | } |
| 4117 | return (opStatus)Status; |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | APFloat::opStatus DoubleAPFloat::divide(const DoubleAPFloat &RHS, |
| 4121 | APFloat::roundingMode RM) { |
| 4122 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4123 | APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); |
| 4124 | auto Ret = |
| 4125 | Tmp.divide(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()), RM); |
| 4126 | *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); |
| 4127 | return Ret; |
| 4128 | } |
| 4129 | |
| 4130 | APFloat::opStatus DoubleAPFloat::remainder(const DoubleAPFloat &RHS) { |
| 4131 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4132 | APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); |
| 4133 | auto Ret = |
| 4134 | Tmp.remainder(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt())); |
| 4135 | *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); |
| 4136 | return Ret; |
| 4137 | } |
| 4138 | |
| 4139 | APFloat::opStatus DoubleAPFloat::mod(const DoubleAPFloat &RHS) { |
| 4140 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4141 | APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); |
| 4142 | auto Ret = Tmp.mod(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt())); |
| 4143 | *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); |
| 4144 | return Ret; |
| 4145 | } |
| 4146 | |
| 4147 | APFloat::opStatus |
| 4148 | DoubleAPFloat::fusedMultiplyAdd(const DoubleAPFloat &Multiplicand, |
| 4149 | const DoubleAPFloat &Addend, |
| 4150 | APFloat::roundingMode RM) { |
| 4151 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4152 | APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); |
| 4153 | auto Ret = Tmp.fusedMultiplyAdd( |
| 4154 | APFloat(semPPCDoubleDoubleLegacy, Multiplicand.bitcastToAPInt()), |
| 4155 | APFloat(semPPCDoubleDoubleLegacy, Addend.bitcastToAPInt()), RM); |
| 4156 | *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); |
| 4157 | return Ret; |
| 4158 | } |
| 4159 | |
| 4160 | APFloat::opStatus DoubleAPFloat::roundToIntegral(APFloat::roundingMode RM) { |
| 4161 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4162 | APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); |
| 4163 | auto Ret = Tmp.roundToIntegral(RM); |
| 4164 | *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); |
| 4165 | return Ret; |
| 4166 | } |
| 4167 | |
Tim Shen | 44bde89 | 2016-12-12 21:59:30 +0000 | [diff] [blame] | 4168 | void DoubleAPFloat::changeSign() { |
| 4169 | Floats[0].changeSign(); |
| 4170 | Floats[1].changeSign(); |
| 4171 | } |
| 4172 | |
| 4173 | APFloat::cmpResult |
| 4174 | DoubleAPFloat::compareAbsoluteValue(const DoubleAPFloat &RHS) const { |
| 4175 | auto Result = Floats[0].compareAbsoluteValue(RHS.Floats[0]); |
| 4176 | if (Result != cmpEqual) |
| 4177 | return Result; |
| 4178 | Result = Floats[1].compareAbsoluteValue(RHS.Floats[1]); |
| 4179 | if (Result == cmpLessThan || Result == cmpGreaterThan) { |
| 4180 | auto Against = Floats[0].isNegative() ^ Floats[1].isNegative(); |
| 4181 | auto RHSAgainst = RHS.Floats[0].isNegative() ^ RHS.Floats[1].isNegative(); |
| 4182 | if (Against && !RHSAgainst) |
| 4183 | return cmpLessThan; |
| 4184 | if (!Against && RHSAgainst) |
| 4185 | return cmpGreaterThan; |
| 4186 | if (!Against && !RHSAgainst) |
| 4187 | return Result; |
| 4188 | if (Against && RHSAgainst) |
| 4189 | return (cmpResult)(cmpLessThan + cmpGreaterThan - Result); |
| 4190 | } |
| 4191 | return Result; |
| 4192 | } |
| 4193 | |
| 4194 | APFloat::fltCategory DoubleAPFloat::getCategory() const { |
| 4195 | return Floats[0].getCategory(); |
| 4196 | } |
| 4197 | |
| 4198 | bool DoubleAPFloat::isNegative() const { return Floats[0].isNegative(); } |
| 4199 | |
| 4200 | void DoubleAPFloat::makeInf(bool Neg) { |
| 4201 | Floats[0].makeInf(Neg); |
Tim Shen | 7117e69 | 2017-01-26 00:11:07 +0000 | [diff] [blame] | 4202 | Floats[1].makeZero(/* Neg = */ false); |
Tim Shen | 44bde89 | 2016-12-12 21:59:30 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4205 | void DoubleAPFloat::makeZero(bool Neg) { |
| 4206 | Floats[0].makeZero(Neg); |
Tim Shen | 7117e69 | 2017-01-26 00:11:07 +0000 | [diff] [blame] | 4207 | Floats[1].makeZero(/* Neg = */ false); |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | void DoubleAPFloat::makeLargest(bool Neg) { |
| 4211 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4212 | Floats[0] = APFloat(semIEEEdouble, APInt(64, 0x7fefffffffffffffull)); |
| 4213 | Floats[1] = APFloat(semIEEEdouble, APInt(64, 0x7c8ffffffffffffeull)); |
| 4214 | if (Neg) |
| 4215 | changeSign(); |
| 4216 | } |
| 4217 | |
| 4218 | void DoubleAPFloat::makeSmallest(bool Neg) { |
| 4219 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4220 | Floats[0].makeSmallest(Neg); |
Tim Shen | 7117e69 | 2017-01-26 00:11:07 +0000 | [diff] [blame] | 4221 | Floats[1].makeZero(/* Neg = */ false); |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4222 | } |
| 4223 | |
| 4224 | void DoubleAPFloat::makeSmallestNormalized(bool Neg) { |
| 4225 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4226 | Floats[0] = APFloat(semIEEEdouble, APInt(64, 0x0360000000000000ull)); |
| 4227 | if (Neg) |
| 4228 | Floats[0].changeSign(); |
Tim Shen | 7117e69 | 2017-01-26 00:11:07 +0000 | [diff] [blame] | 4229 | Floats[1].makeZero(/* Neg = */ false); |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4230 | } |
| 4231 | |
Tim Shen | 44bde89 | 2016-12-12 21:59:30 +0000 | [diff] [blame] | 4232 | void DoubleAPFloat::makeNaN(bool SNaN, bool Neg, const APInt *fill) { |
| 4233 | Floats[0].makeNaN(SNaN, Neg, fill); |
Tim Shen | 7117e69 | 2017-01-26 00:11:07 +0000 | [diff] [blame] | 4234 | Floats[1].makeZero(/* Neg = */ false); |
Tim Shen | 44bde89 | 2016-12-12 21:59:30 +0000 | [diff] [blame] | 4235 | } |
| 4236 | |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4237 | APFloat::cmpResult DoubleAPFloat::compare(const DoubleAPFloat &RHS) const { |
| 4238 | auto Result = Floats[0].compare(RHS.Floats[0]); |
Tim Shen | 7117e69 | 2017-01-26 00:11:07 +0000 | [diff] [blame] | 4239 | // |Float[0]| > |Float[1]| |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4240 | if (Result == APFloat::cmpEqual) |
| 4241 | return Floats[1].compare(RHS.Floats[1]); |
| 4242 | return Result; |
| 4243 | } |
| 4244 | |
| 4245 | bool DoubleAPFloat::bitwiseIsEqual(const DoubleAPFloat &RHS) const { |
| 4246 | return Floats[0].bitwiseIsEqual(RHS.Floats[0]) && |
| 4247 | Floats[1].bitwiseIsEqual(RHS.Floats[1]); |
| 4248 | } |
| 4249 | |
| 4250 | hash_code hash_value(const DoubleAPFloat &Arg) { |
| 4251 | if (Arg.Floats) |
| 4252 | return hash_combine(hash_value(Arg.Floats[0]), hash_value(Arg.Floats[1])); |
| 4253 | return hash_combine(Arg.Semantics); |
| 4254 | } |
| 4255 | |
| 4256 | APInt DoubleAPFloat::bitcastToAPInt() const { |
| 4257 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4258 | uint64_t Data[] = { |
| 4259 | Floats[0].bitcastToAPInt().getRawData()[0], |
| 4260 | Floats[1].bitcastToAPInt().getRawData()[0], |
| 4261 | }; |
| 4262 | return APInt(128, 2, Data); |
| 4263 | } |
| 4264 | |
| 4265 | APFloat::opStatus DoubleAPFloat::convertFromString(StringRef S, |
| 4266 | roundingMode RM) { |
| 4267 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4268 | APFloat Tmp(semPPCDoubleDoubleLegacy); |
| 4269 | auto Ret = Tmp.convertFromString(S, RM); |
| 4270 | *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); |
| 4271 | return Ret; |
| 4272 | } |
| 4273 | |
| 4274 | APFloat::opStatus DoubleAPFloat::next(bool nextDown) { |
| 4275 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4276 | APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); |
| 4277 | auto Ret = Tmp.next(nextDown); |
| 4278 | *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); |
| 4279 | return Ret; |
| 4280 | } |
| 4281 | |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 4282 | APFloat::opStatus |
| 4283 | DoubleAPFloat::convertToInteger(MutableArrayRef<integerPart> Input, |
| 4284 | unsigned int Width, bool IsSigned, |
| 4285 | roundingMode RM, bool *IsExact) const { |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4286 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4287 | return APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt()) |
| 4288 | .convertToInteger(Input, Width, IsSigned, RM, IsExact); |
| 4289 | } |
| 4290 | |
| 4291 | APFloat::opStatus DoubleAPFloat::convertFromAPInt(const APInt &Input, |
| 4292 | bool IsSigned, |
| 4293 | roundingMode RM) { |
| 4294 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4295 | APFloat Tmp(semPPCDoubleDoubleLegacy); |
| 4296 | auto Ret = Tmp.convertFromAPInt(Input, IsSigned, RM); |
| 4297 | *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); |
| 4298 | return Ret; |
| 4299 | } |
| 4300 | |
| 4301 | APFloat::opStatus |
| 4302 | DoubleAPFloat::convertFromSignExtendedInteger(const integerPart *Input, |
| 4303 | unsigned int InputSize, |
| 4304 | bool IsSigned, roundingMode RM) { |
| 4305 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4306 | APFloat Tmp(semPPCDoubleDoubleLegacy); |
| 4307 | auto Ret = Tmp.convertFromSignExtendedInteger(Input, InputSize, IsSigned, RM); |
| 4308 | *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); |
| 4309 | return Ret; |
| 4310 | } |
| 4311 | |
| 4312 | APFloat::opStatus |
| 4313 | DoubleAPFloat::convertFromZeroExtendedInteger(const integerPart *Input, |
| 4314 | unsigned int InputSize, |
| 4315 | bool IsSigned, roundingMode RM) { |
| 4316 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4317 | APFloat Tmp(semPPCDoubleDoubleLegacy); |
| 4318 | auto Ret = Tmp.convertFromZeroExtendedInteger(Input, InputSize, IsSigned, RM); |
| 4319 | *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); |
| 4320 | return Ret; |
| 4321 | } |
| 4322 | |
| 4323 | unsigned int DoubleAPFloat::convertToHexString(char *DST, |
| 4324 | unsigned int HexDigits, |
| 4325 | bool UpperCase, |
| 4326 | roundingMode RM) const { |
| 4327 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4328 | return APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt()) |
| 4329 | .convertToHexString(DST, HexDigits, UpperCase, RM); |
| 4330 | } |
| 4331 | |
| 4332 | bool DoubleAPFloat::isDenormal() const { |
| 4333 | return getCategory() == fcNormal && |
| 4334 | (Floats[0].isDenormal() || Floats[1].isDenormal() || |
Tim Shen | 7117e69 | 2017-01-26 00:11:07 +0000 | [diff] [blame] | 4335 | // (double)(Hi + Lo) == Hi defines a normal number. |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4336 | Floats[0].compare(Floats[0] + Floats[1]) != cmpEqual); |
| 4337 | } |
| 4338 | |
| 4339 | bool DoubleAPFloat::isSmallest() const { |
| 4340 | if (getCategory() != fcNormal) |
| 4341 | return false; |
| 4342 | DoubleAPFloat Tmp(*this); |
| 4343 | Tmp.makeSmallest(this->isNegative()); |
| 4344 | return Tmp.compare(*this) == cmpEqual; |
| 4345 | } |
| 4346 | |
| 4347 | bool DoubleAPFloat::isLargest() const { |
| 4348 | if (getCategory() != fcNormal) |
| 4349 | return false; |
| 4350 | DoubleAPFloat Tmp(*this); |
| 4351 | Tmp.makeLargest(this->isNegative()); |
| 4352 | return Tmp.compare(*this) == cmpEqual; |
| 4353 | } |
| 4354 | |
| 4355 | bool DoubleAPFloat::isInteger() const { |
| 4356 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4357 | APFloat Tmp(semPPCDoubleDoubleLegacy); |
| 4358 | (void)Tmp.add(Floats[0], rmNearestTiesToEven); |
| 4359 | (void)Tmp.add(Floats[1], rmNearestTiesToEven); |
| 4360 | return Tmp.isInteger(); |
| 4361 | } |
| 4362 | |
| 4363 | void DoubleAPFloat::toString(SmallVectorImpl<char> &Str, |
| 4364 | unsigned FormatPrecision, |
| 4365 | unsigned FormatMaxPadding) const { |
| 4366 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4367 | APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt()) |
| 4368 | .toString(Str, FormatPrecision, FormatMaxPadding); |
| 4369 | } |
| 4370 | |
| 4371 | bool DoubleAPFloat::getExactInverse(APFloat *inv) const { |
| 4372 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4373 | APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); |
| 4374 | if (!inv) |
| 4375 | return Tmp.getExactInverse(nullptr); |
| 4376 | APFloat Inv(semPPCDoubleDoubleLegacy); |
| 4377 | auto Ret = Tmp.getExactInverse(&Inv); |
| 4378 | *inv = APFloat(semPPCDoubleDouble, Inv.bitcastToAPInt()); |
| 4379 | return Ret; |
| 4380 | } |
| 4381 | |
| 4382 | DoubleAPFloat scalbn(DoubleAPFloat Arg, int Exp, APFloat::roundingMode RM) { |
| 4383 | assert(Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4384 | return DoubleAPFloat(semPPCDoubleDouble, scalbn(Arg.Floats[0], Exp, RM), |
| 4385 | scalbn(Arg.Floats[1], Exp, RM)); |
| 4386 | } |
| 4387 | |
| 4388 | DoubleAPFloat frexp(const DoubleAPFloat &Arg, int &Exp, |
| 4389 | APFloat::roundingMode RM) { |
| 4390 | assert(Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); |
| 4391 | APFloat First = frexp(Arg.Floats[0], Exp, RM); |
| 4392 | APFloat Second = Arg.Floats[1]; |
| 4393 | if (Arg.getCategory() == APFloat::fcNormal) |
| 4394 | Second = scalbn(Second, -Exp, RM); |
| 4395 | return DoubleAPFloat(semPPCDoubleDouble, std::move(First), std::move(Second)); |
| 4396 | } |
| 4397 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 4398 | } // End detail namespace |
| 4399 | |
Tim Shen | 398f90f | 2016-11-06 07:38:37 +0000 | [diff] [blame] | 4400 | APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) { |
| 4401 | if (usesLayout<IEEEFloat>(Semantics)) { |
| 4402 | new (&IEEE) IEEEFloat(std::move(F)); |
Tim Shen | 7b57ac4 | 2016-12-21 02:39:21 +0000 | [diff] [blame] | 4403 | return; |
| 4404 | } |
| 4405 | if (usesLayout<DoubleAPFloat>(Semantics)) { |
Tim Shen | 398f90f | 2016-11-06 07:38:37 +0000 | [diff] [blame] | 4406 | new (&Double) |
| 4407 | DoubleAPFloat(Semantics, APFloat(std::move(F), F.getSemantics()), |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 4408 | APFloat(semIEEEdouble)); |
Tim Shen | 7b57ac4 | 2016-12-21 02:39:21 +0000 | [diff] [blame] | 4409 | return; |
Tim Shen | 398f90f | 2016-11-06 07:38:37 +0000 | [diff] [blame] | 4410 | } |
Tim Shen | 7b57ac4 | 2016-12-21 02:39:21 +0000 | [diff] [blame] | 4411 | llvm_unreachable("Unexpected semantics"); |
Tim Shen | 398f90f | 2016-11-06 07:38:37 +0000 | [diff] [blame] | 4412 | } |
| 4413 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 4414 | APFloat::opStatus APFloat::convertFromString(StringRef Str, roundingMode RM) { |
Tim Shen | 601ba8c | 2017-01-27 02:11:07 +0000 | [diff] [blame] | 4415 | APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM)); |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 4416 | } |
| 4417 | |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4418 | hash_code hash_value(const APFloat &Arg) { |
| 4419 | if (APFloat::usesLayout<detail::IEEEFloat>(Arg.getSemantics())) |
| 4420 | return hash_value(Arg.U.IEEE); |
| 4421 | if (APFloat::usesLayout<detail::DoubleAPFloat>(Arg.getSemantics())) |
| 4422 | return hash_value(Arg.U.Double); |
| 4423 | llvm_unreachable("Unexpected semantics"); |
| 4424 | } |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 4425 | |
| 4426 | APFloat::APFloat(const fltSemantics &Semantics, StringRef S) |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 4427 | : APFloat(Semantics) { |
| 4428 | convertFromString(S, rmNearestTiesToEven); |
| 4429 | } |
| 4430 | |
| 4431 | APFloat::opStatus APFloat::convert(const fltSemantics &ToSemantics, |
| 4432 | roundingMode RM, bool *losesInfo) { |
| 4433 | if (&getSemantics() == &ToSemantics) |
| 4434 | return opOK; |
| 4435 | if (usesLayout<IEEEFloat>(getSemantics()) && |
Tim Shen | 7b57ac4 | 2016-12-21 02:39:21 +0000 | [diff] [blame] | 4436 | usesLayout<IEEEFloat>(ToSemantics)) |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 4437 | return U.IEEE.convert(ToSemantics, RM, losesInfo); |
Tim Shen | 7b57ac4 | 2016-12-21 02:39:21 +0000 | [diff] [blame] | 4438 | if (usesLayout<IEEEFloat>(getSemantics()) && |
| 4439 | usesLayout<DoubleAPFloat>(ToSemantics)) { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 4440 | assert(&ToSemantics == &semPPCDoubleDouble); |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4441 | auto Ret = U.IEEE.convert(semPPCDoubleDoubleLegacy, RM, losesInfo); |
| 4442 | *this = APFloat(ToSemantics, U.IEEE.bitcastToAPInt()); |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 4443 | return Ret; |
Tim Shen | 7b57ac4 | 2016-12-21 02:39:21 +0000 | [diff] [blame] | 4444 | } |
| 4445 | if (usesLayout<DoubleAPFloat>(getSemantics()) && |
| 4446 | usesLayout<IEEEFloat>(ToSemantics)) { |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 4447 | auto Ret = getIEEE().convert(ToSemantics, RM, losesInfo); |
Tim Shen | 398f90f | 2016-11-06 07:38:37 +0000 | [diff] [blame] | 4448 | *this = APFloat(std::move(getIEEE()), ToSemantics); |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 4449 | return Ret; |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 4450 | } |
Tim Shen | 7b57ac4 | 2016-12-21 02:39:21 +0000 | [diff] [blame] | 4451 | llvm_unreachable("Unexpected semantics"); |
Tim Shen | 139a58f | 2016-10-27 22:52:40 +0000 | [diff] [blame] | 4452 | } |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 4453 | |
Tim Shen | 398f90f | 2016-11-06 07:38:37 +0000 | [diff] [blame] | 4454 | APFloat APFloat::getAllOnesValue(unsigned BitWidth, bool isIEEE) { |
| 4455 | if (isIEEE) { |
| 4456 | switch (BitWidth) { |
| 4457 | case 16: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 4458 | return APFloat(semIEEEhalf, APInt::getAllOnesValue(BitWidth)); |
Tim Shen | 398f90f | 2016-11-06 07:38:37 +0000 | [diff] [blame] | 4459 | case 32: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 4460 | return APFloat(semIEEEsingle, APInt::getAllOnesValue(BitWidth)); |
Tim Shen | 398f90f | 2016-11-06 07:38:37 +0000 | [diff] [blame] | 4461 | case 64: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 4462 | return APFloat(semIEEEdouble, APInt::getAllOnesValue(BitWidth)); |
Tim Shen | 398f90f | 2016-11-06 07:38:37 +0000 | [diff] [blame] | 4463 | case 80: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 4464 | return APFloat(semX87DoubleExtended, APInt::getAllOnesValue(BitWidth)); |
Tim Shen | 398f90f | 2016-11-06 07:38:37 +0000 | [diff] [blame] | 4465 | case 128: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 4466 | return APFloat(semIEEEquad, APInt::getAllOnesValue(BitWidth)); |
Tim Shen | 398f90f | 2016-11-06 07:38:37 +0000 | [diff] [blame] | 4467 | default: |
| 4468 | llvm_unreachable("Unknown floating bit width"); |
| 4469 | } |
| 4470 | } else { |
| 4471 | assert(BitWidth == 128); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 4472 | return APFloat(semPPCDoubleDouble, APInt::getAllOnesValue(BitWidth)); |
Tim Shen | 398f90f | 2016-11-06 07:38:37 +0000 | [diff] [blame] | 4473 | } |
| 4474 | } |
| 4475 | |
Tim Shen | 44bde89 | 2016-12-12 21:59:30 +0000 | [diff] [blame] | 4476 | void APFloat::print(raw_ostream &OS) const { |
| 4477 | SmallVector<char, 16> Buffer; |
| 4478 | toString(Buffer); |
| 4479 | OS << Buffer << "\n"; |
| 4480 | } |
| 4481 | |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 4482 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 4483 | LLVM_DUMP_METHOD void APFloat::dump() const { print(dbgs()); } |
| 4484 | #endif |
Tim Shen | 44bde89 | 2016-12-12 21:59:30 +0000 | [diff] [blame] | 4485 | |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4486 | void APFloat::Profile(FoldingSetNodeID &NID) const { |
| 4487 | NID.Add(bitcastToAPInt()); |
| 4488 | } |
| 4489 | |
| 4490 | /* Same as convertToInteger(integerPart*, ...), except the result is returned in |
| 4491 | an APSInt, whose initial bit-width and signed-ness are used to determine the |
| 4492 | precision of the conversion. |
| 4493 | */ |
| 4494 | APFloat::opStatus APFloat::convertToInteger(APSInt &result, |
| 4495 | roundingMode rounding_mode, |
| 4496 | bool *isExact) const { |
| 4497 | unsigned bitWidth = result.getBitWidth(); |
| 4498 | SmallVector<uint64_t, 4> parts(result.getNumWords()); |
Simon Pilgrim | 00b3499 | 2017-03-20 14:40:12 +0000 | [diff] [blame] | 4499 | opStatus status = convertToInteger(parts, bitWidth, result.isSigned(), |
Tim Shen | fd1e5aa | 2017-01-23 22:39:35 +0000 | [diff] [blame] | 4500 | rounding_mode, isExact); |
| 4501 | // Keeps the original signed-ness. |
| 4502 | result = APInt(bitWidth, parts); |
| 4503 | return status; |
| 4504 | } |
| 4505 | |
Tim Shen | 85de51d | 2016-10-25 19:55:59 +0000 | [diff] [blame] | 4506 | } // End llvm namespace |
Tim Shen | 601ba8c | 2017-01-27 02:11:07 +0000 | [diff] [blame] | 4507 | |
| 4508 | #undef APFLOAT_DISPATCH_ON_SEMANTICS |