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