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