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