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