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