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