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