1 //===- llvm/ADT/APFloat.h - Arbitrary Precision Floating Point ---*- C++ -*-==//
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 /// \file
10 /// This file declares a class to represent arbitrary precision floating point
11 /// values and provide a variety of arithmetic operations on them.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ADT_APFLOAT_H
16 #define LLVM_ADT_APFLOAT_H
17
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/FloatingPointMode.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include <memory>
23
24 #define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \
25 do { \
26 if (usesLayout<IEEEFloat>(getSemantics())) \
27 return U.IEEE.METHOD_CALL; \
28 if (usesLayout<DoubleAPFloat>(getSemantics())) \
29 return U.Double.METHOD_CALL; \
30 llvm_unreachable("Unexpected semantics"); \
31 } while (false)
32
33 namespace llvm {
34
35 struct fltSemantics;
36 class APSInt;
37 class StringRef;
38 class APFloat;
39 class raw_ostream;
40
41 template <typename T> class Expected;
42 template <typename T> class SmallVectorImpl;
43
44 /// Enum that represents what fraction of the LSB truncated bits of an fp number
45 /// represent.
46 ///
47 /// This essentially combines the roles of guard and sticky bits.
48 enum lostFraction { // Example of truncated bits:
49 lfExactlyZero, // 000000
50 lfLessThanHalf, // 0xxxxx x's not all zero
51 lfExactlyHalf, // 100000
52 lfMoreThanHalf // 1xxxxx x's not all zero
53 };
54
55 /// A self-contained host- and target-independent arbitrary-precision
56 /// floating-point software implementation.
57 ///
58 /// APFloat uses bignum integer arithmetic as provided by static functions in
59 /// the APInt class. The library will work with bignum integers whose parts are
60 /// any unsigned type at least 16 bits wide, but 64 bits is recommended.
61 ///
62 /// Written for clarity rather than speed, in particular with a view to use in
63 /// the front-end of a cross compiler so that target arithmetic can be correctly
64 /// performed on the host. Performance should nonetheless be reasonable,
65 /// particularly for its intended use. It may be useful as a base
66 /// implementation for a run-time library during development of a faster
67 /// target-specific one.
68 ///
69 /// All 5 rounding modes in the IEEE-754R draft are handled correctly for all
70 /// implemented operations. Currently implemented operations are add, subtract,
71 /// multiply, divide, fused-multiply-add, conversion-to-float,
72 /// conversion-to-integer and conversion-from-integer. New rounding modes
73 /// (e.g. away from zero) can be added with three or four lines of code.
74 ///
75 /// Four formats are built-in: IEEE single precision, double precision,
76 /// quadruple precision, and x87 80-bit extended double (when operating with
77 /// full extended precision). Adding a new format that obeys IEEE semantics
78 /// only requires adding two lines of code: a declaration and definition of the
79 /// format.
80 ///
81 /// All operations return the status of that operation as an exception bit-mask,
82 /// so multiple operations can be done consecutively with their results or-ed
83 /// together. The returned status can be useful for compiler diagnostics; e.g.,
84 /// inexact, underflow and overflow can be easily diagnosed on constant folding,
85 /// and compiler optimizers can determine what exceptions would be raised by
86 /// folding operations and optimize, or perhaps not optimize, accordingly.
87 ///
88 /// At present, underflow tininess is detected after rounding; it should be
89 /// straight forward to add support for the before-rounding case too.
90 ///
91 /// The library reads hexadecimal floating point numbers as per C99, and
92 /// correctly rounds if necessary according to the specified rounding mode.
93 /// Syntax is required to have been validated by the caller. It also converts
94 /// floating point numbers to hexadecimal text as per the C99 %a and %A
95 /// conversions. The output precision (or alternatively the natural minimal
96 /// precision) can be specified; if the requested precision is less than the
97 /// natural precision the output is correctly rounded for the specified rounding
98 /// mode.
99 ///
100 /// It also reads decimal floating point numbers and correctly rounds according
101 /// to the specified rounding mode.
102 ///
103 /// Conversion to decimal text is not currently implemented.
104 ///
105 /// Non-zero finite numbers are represented internally as a sign bit, a 16-bit
106 /// signed exponent, and the significand as an array of integer parts. After
107 /// normalization of a number of precision P the exponent is within the range of
108 /// the format, and if the number is not denormal the P-th bit of the
109 /// significand is set as an explicit integer bit. For denormals the most
110 /// significant bit is shifted right so that the exponent is maintained at the
111 /// format's minimum, so that the smallest denormal has just the least
112 /// significant bit of the significand set. The sign of zeroes and infinities
113 /// is significant; the exponent and significand of such numbers is not stored,
114 /// but has a known implicit (deterministic) value: 0 for the significands, 0
115 /// for zero exponent, all 1 bits for infinity exponent. For NaNs the sign and
116 /// significand are deterministic, although not really meaningful, and preserved
117 /// in non-conversion operations. The exponent is implicitly all 1 bits.
118 ///
119 /// APFloat does not provide any exception handling beyond default exception
120 /// handling. We represent Signaling NaNs via IEEE-754R 2008 6.2.1 should clause
121 /// by encoding Signaling NaNs with the first bit of its trailing significand as
122 /// 0.
123 ///
124 /// TODO
125 /// ====
126 ///
127 /// Some features that may or may not be worth adding:
128 ///
129 /// Binary to decimal conversion (hard).
130 ///
131 /// Optional ability to detect underflow tininess before rounding.
132 ///
133 /// New formats: x87 in single and double precision mode (IEEE apart from
134 /// extended exponent range) (hard).
135 ///
136 /// New operations: sqrt, IEEE remainder, C90 fmod, nexttoward.
137 ///
138
139 // This is the common type definitions shared by APFloat and its internal
140 // implementation classes. This struct should not define any non-static data
141 // members.
142 struct APFloatBase {
143 typedef APInt::WordType integerPart;
144 static constexpr unsigned integerPartWidth = APInt::APINT_BITS_PER_WORD;
145
146 /// A signed type to represent a floating point numbers unbiased exponent.
147 typedef int32_t ExponentType;
148
149 /// \name Floating Point Semantics.
150 /// @{
151 enum Semantics {
152 S_IEEEhalf,
153 S_BFloat,
154 S_IEEEsingle,
155 S_IEEEdouble,
156 S_IEEEquad,
157 S_PPCDoubleDouble,
158 // 8-bit floating point number following IEEE-754 conventions with bit
159 // layout S1E5M2 as described in https://arxiv.org/abs/2209.05433.
160 S_Float8E5M2,
161 // 8-bit floating point number mostly following IEEE-754 conventions
162 // and bit layout S1E5M2 described in https://arxiv.org/abs/2206.02915,
163 // with expanded range and with no infinity or signed zero.
164 // NaN is represented as negative zero. (FN -> Finite, UZ -> unsigned zero).
165 // This format's exponent bias is 16, instead of the 15 (2 ** (5 - 1) - 1)
166 // that IEEE precedent would imply.
167 S_Float8E5M2FNUZ,
168 // 8-bit floating point number mostly following IEEE-754 conventions with
169 // bit layout S1E4M3 as described in https://arxiv.org/abs/2209.05433.
170 // Unlike IEEE-754 types, there are no infinity values, and NaN is
171 // represented with the exponent and mantissa bits set to all 1s.
172 S_Float8E4M3FN,
173 // 8-bit floating point number mostly following IEEE-754 conventions
174 // and bit layout S1E4M3 described in https://arxiv.org/abs/2206.02915,
175 // with expanded range and with no infinity or signed zero.
176 // NaN is represented as negative zero. (FN -> Finite, UZ -> unsigned zero).
177 // This format's exponent bias is 8, instead of the 7 (2 ** (4 - 1) - 1)
178 // that IEEE precedent would imply.
179 S_Float8E4M3FNUZ,
180 // 8-bit floating point number mostly following IEEE-754 conventions
181 // and bit layout S1E4M3 with expanded range and with no infinity or signed
182 // zero.
183 // NaN is represented as negative zero. (FN -> Finite, UZ -> unsigned zero).
184 // This format's exponent bias is 11, instead of the 7 (2 ** (4 - 1) - 1)
185 // that IEEE precedent would imply.
186 S_Float8E4M3B11FNUZ,
187 // Floating point number that occupies 32 bits or less of storage, providing
188 // improved range compared to half (16-bit) formats, at (potentially)
189 // greater throughput than single precision (32-bit) formats.
190 S_FloatTF32,
191
192 S_x87DoubleExtended,
193 S_MaxSemantics = S_x87DoubleExtended,
194 };
195
196 static const llvm::fltSemantics &EnumToSemantics(Semantics S);
197 static Semantics SemanticsToEnum(const llvm::fltSemantics &Sem);
198
199 static const fltSemantics &IEEEhalf() LLVM_READNONE;
200 static const fltSemantics &BFloat() LLVM_READNONE;
201 static const fltSemantics &IEEEsingle() LLVM_READNONE;
202 static const fltSemantics &IEEEdouble() LLVM_READNONE;
203 static const fltSemantics &IEEEquad() LLVM_READNONE;
204 static const fltSemantics &PPCDoubleDouble() LLVM_READNONE;
205 static const fltSemantics &Float8E5M2() LLVM_READNONE;
206 static const fltSemantics &Float8E5M2FNUZ() LLVM_READNONE;
207 static const fltSemantics &Float8E4M3FN() LLVM_READNONE;
208 static const fltSemantics &Float8E4M3FNUZ() LLVM_READNONE;
209 static const fltSemantics &Float8E4M3B11FNUZ() LLVM_READNONE;
210 static const fltSemantics &FloatTF32() LLVM_READNONE;
211 static const fltSemantics &x87DoubleExtended() LLVM_READNONE;
212
213 /// A Pseudo fltsemantic used to construct APFloats that cannot conflict with
214 /// anything real.
215 static const fltSemantics &Bogus() LLVM_READNONE;
216
217 /// @}
218
219 /// IEEE-754R 5.11: Floating Point Comparison Relations.
220 enum cmpResult {
221 cmpLessThan,
222 cmpEqual,
223 cmpGreaterThan,
224 cmpUnordered
225 };
226
227 /// IEEE-754R 4.3: Rounding-direction attributes.
228 using roundingMode = llvm::RoundingMode;
229
230 static constexpr roundingMode rmNearestTiesToEven =
231 RoundingMode::NearestTiesToEven;
232 static constexpr roundingMode rmTowardPositive = RoundingMode::TowardPositive;
233 static constexpr roundingMode rmTowardNegative = RoundingMode::TowardNegative;
234 static constexpr roundingMode rmTowardZero = RoundingMode::TowardZero;
235 static constexpr roundingMode rmNearestTiesToAway =
236 RoundingMode::NearestTiesToAway;
237
238 /// IEEE-754R 7: Default exception handling.
239 ///
240 /// opUnderflow or opOverflow are always returned or-ed with opInexact.
241 ///
242 /// APFloat models this behavior specified by IEEE-754:
243 /// "For operations producing results in floating-point format, the default
244 /// result of an operation that signals the invalid operation exception
245 /// shall be a quiet NaN."
246 enum opStatus {
247 opOK = 0x00,
248 opInvalidOp = 0x01,
249 opDivByZero = 0x02,
250 opOverflow = 0x04,
251 opUnderflow = 0x08,
252 opInexact = 0x10
253 };
254
255 /// Category of internally-represented number.
256 enum fltCategory {
257 fcInfinity,
258 fcNaN,
259 fcNormal,
260 fcZero
261 };
262
263 /// Convenience enum used to construct an uninitialized APFloat.
264 enum uninitializedTag {
265 uninitialized
266 };
267
268 /// Enumeration of \c ilogb error results.
269 enum IlogbErrorKinds {
270 IEK_Zero = INT_MIN + 1,
271 IEK_NaN = INT_MIN,
272 IEK_Inf = INT_MAX
273 };
274
275 static unsigned int semanticsPrecision(const fltSemantics &);
276 static ExponentType semanticsMinExponent(const fltSemantics &);
277 static ExponentType semanticsMaxExponent(const fltSemantics &);
278 static unsigned int semanticsSizeInBits(const fltSemantics &);
279 static unsigned int semanticsIntSizeInBits(const fltSemantics&, bool);
280
281 // Returns true if any number described by \p Src can be precisely represented
282 // by a normal (not subnormal) value in \p Dst.
283 static bool isRepresentableAsNormalIn(const fltSemantics &Src,
284 const fltSemantics &Dst);
285
286 /// Returns the size of the floating point number (in bits) in the given
287 /// semantics.
288 static unsigned getSizeInBits(const fltSemantics &Sem);
289 };
290
291 namespace detail {
292
293 class IEEEFloat final : public APFloatBase {
294 public:
295 /// \name Constructors
296 /// @{
297
298 IEEEFloat(const fltSemantics &); // Default construct to +0.0
299 IEEEFloat(const fltSemantics &, integerPart);
300 IEEEFloat(const fltSemantics &, uninitializedTag);
301 IEEEFloat(const fltSemantics &, const APInt &);
302 explicit IEEEFloat(double d);
303 explicit IEEEFloat(float f);
304 IEEEFloat(const IEEEFloat &);
305 IEEEFloat(IEEEFloat &&);
306 ~IEEEFloat();
307
308 /// @}
309
310 /// Returns whether this instance allocated memory.
needsCleanup()311 bool needsCleanup() const { return partCount() > 1; }
312
313 /// \name Convenience "constructors"
314 /// @{
315
316 /// @}
317
318 /// \name Arithmetic
319 /// @{
320
321 opStatus add(const IEEEFloat &, roundingMode);
322 opStatus subtract(const IEEEFloat &, roundingMode);
323 opStatus multiply(const IEEEFloat &, roundingMode);
324 opStatus divide(const IEEEFloat &, roundingMode);
325 /// IEEE remainder.
326 opStatus remainder(const IEEEFloat &);
327 /// C fmod, or llvm frem.
328 opStatus mod(const IEEEFloat &);
329 opStatus fusedMultiplyAdd(const IEEEFloat &, const IEEEFloat &, roundingMode);
330 opStatus roundToIntegral(roundingMode);
331 /// IEEE-754R 5.3.1: nextUp/nextDown.
332 opStatus next(bool nextDown);
333
334 /// @}
335
336 /// \name Sign operations.
337 /// @{
338
339 void changeSign();
340
341 /// @}
342
343 /// \name Conversions
344 /// @{
345
346 opStatus convert(const fltSemantics &, roundingMode, bool *);
347 opStatus convertToInteger(MutableArrayRef<integerPart>, unsigned int, bool,
348 roundingMode, bool *) const;
349 opStatus convertFromAPInt(const APInt &, bool, roundingMode);
350 opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int,
351 bool, roundingMode);
352 opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int,
353 bool, roundingMode);
354 Expected<opStatus> convertFromString(StringRef, roundingMode);
355 APInt bitcastToAPInt() const;
356 double convertToDouble() const;
357 float convertToFloat() const;
358
359 /// @}
360
361 /// The definition of equality is not straightforward for floating point, so
362 /// we won't use operator==. Use one of the following, or write whatever it
363 /// is you really mean.
364 bool operator==(const IEEEFloat &) const = delete;
365
366 /// IEEE comparison with another floating point number (NaNs compare
367 /// unordered, 0==-0).
368 cmpResult compare(const IEEEFloat &) const;
369
370 /// Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
371 bool bitwiseIsEqual(const IEEEFloat &) const;
372
373 /// Write out a hexadecimal representation of the floating point value to DST,
374 /// which must be of sufficient size, in the C99 form [-]0xh.hhhhp[+-]d.
375 /// Return the number of characters written, excluding the terminating NUL.
376 unsigned int convertToHexString(char *dst, unsigned int hexDigits,
377 bool upperCase, roundingMode) const;
378
379 /// \name IEEE-754R 5.7.2 General operations.
380 /// @{
381
382 /// IEEE-754R isSignMinus: Returns true if and only if the current value is
383 /// negative.
384 ///
385 /// This applies to zeros and NaNs as well.
isNegative()386 bool isNegative() const { return sign; }
387
388 /// IEEE-754R isNormal: Returns true if and only if the current value is normal.
389 ///
390 /// This implies that the current value of the float is not zero, subnormal,
391 /// infinite, or NaN following the definition of normality from IEEE-754R.
isNormal()392 bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
393
394 /// Returns true if and only if the current value is zero, subnormal, or
395 /// normal.
396 ///
397 /// This means that the value is not infinite or NaN.
isFinite()398 bool isFinite() const { return !isNaN() && !isInfinity(); }
399
400 /// Returns true if and only if the float is plus or minus zero.
isZero()401 bool isZero() const { return category == fcZero; }
402
403 /// IEEE-754R isSubnormal(): Returns true if and only if the float is a
404 /// denormal.
405 bool isDenormal() const;
406
407 /// IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
isInfinity()408 bool isInfinity() const { return category == fcInfinity; }
409
410 /// Returns true if and only if the float is a quiet or signaling NaN.
isNaN()411 bool isNaN() const { return category == fcNaN; }
412
413 /// Returns true if and only if the float is a signaling NaN.
414 bool isSignaling() const;
415
416 /// @}
417
418 /// \name Simple Queries
419 /// @{
420
getCategory()421 fltCategory getCategory() const { return category; }
getSemantics()422 const fltSemantics &getSemantics() const { return *semantics; }
isNonZero()423 bool isNonZero() const { return category != fcZero; }
isFiniteNonZero()424 bool isFiniteNonZero() const { return isFinite() && !isZero(); }
isPosZero()425 bool isPosZero() const { return isZero() && !isNegative(); }
isNegZero()426 bool isNegZero() const { return isZero() && isNegative(); }
427
428 /// Returns true if and only if the number has the smallest possible non-zero
429 /// magnitude in the current semantics.
430 bool isSmallest() const;
431
432 /// Returns true if this is the smallest (by magnitude) normalized finite
433 /// number in the given semantics.
434 bool isSmallestNormalized() const;
435
436 /// Returns true if and only if the number has the largest possible finite
437 /// magnitude in the current semantics.
438 bool isLargest() const;
439
440 /// Returns true if and only if the number is an exact integer.
441 bool isInteger() const;
442
443 /// @}
444
445 IEEEFloat &operator=(const IEEEFloat &);
446 IEEEFloat &operator=(IEEEFloat &&);
447
448 /// Overload to compute a hash code for an APFloat value.
449 ///
450 /// Note that the use of hash codes for floating point values is in general
451 /// frought with peril. Equality is hard to define for these values. For
452 /// example, should negative and positive zero hash to different codes? Are
453 /// they equal or not? This hash value implementation specifically
454 /// emphasizes producing different codes for different inputs in order to
455 /// be used in canonicalization and memoization. As such, equality is
456 /// bitwiseIsEqual, and 0 != -0.
457 friend hash_code hash_value(const IEEEFloat &Arg);
458
459 /// Converts this value into a decimal string.
460 ///
461 /// \param FormatPrecision The maximum number of digits of
462 /// precision to output. If there are fewer digits available,
463 /// zero padding will not be used unless the value is
464 /// integral and small enough to be expressed in
465 /// FormatPrecision digits. 0 means to use the natural
466 /// precision of the number.
467 /// \param FormatMaxPadding The maximum number of zeros to
468 /// consider inserting before falling back to scientific
469 /// notation. 0 means to always use scientific notation.
470 ///
471 /// \param TruncateZero Indicate whether to remove the trailing zero in
472 /// fraction part or not. Also setting this parameter to false forcing
473 /// producing of output more similar to default printf behavior.
474 /// Specifically the lower e is used as exponent delimiter and exponent
475 /// always contains no less than two digits.
476 ///
477 /// Number Precision MaxPadding Result
478 /// ------ --------- ---------- ------
479 /// 1.01E+4 5 2 10100
480 /// 1.01E+4 4 2 1.01E+4
481 /// 1.01E+4 5 1 1.01E+4
482 /// 1.01E-2 5 2 0.0101
483 /// 1.01E-2 4 2 0.0101
484 /// 1.01E-2 4 1 1.01E-2
485 void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0,
486 unsigned FormatMaxPadding = 3, bool TruncateZero = true) const;
487
488 /// If this value has an exact multiplicative inverse, store it in inv and
489 /// return true.
490 bool getExactInverse(APFloat *inv) const;
491
492 // If this is an exact power of two, return the exponent while ignoring the
493 // sign bit. If it's not an exact power of 2, return INT_MIN
494 LLVM_READONLY
495 int getExactLog2Abs() const;
496
497 // If this is an exact power of two, return the exponent. If it's not an exact
498 // power of 2, return INT_MIN
499 LLVM_READONLY
getExactLog2()500 int getExactLog2() const {
501 return isNegative() ? INT_MIN : getExactLog2Abs();
502 }
503
504 /// Returns the exponent of the internal representation of the APFloat.
505 ///
506 /// Because the radix of APFloat is 2, this is equivalent to floor(log2(x)).
507 /// For special APFloat values, this returns special error codes:
508 ///
509 /// NaN -> \c IEK_NaN
510 /// 0 -> \c IEK_Zero
511 /// Inf -> \c IEK_Inf
512 ///
513 friend int ilogb(const IEEEFloat &Arg);
514
515 /// Returns: X * 2^Exp for integral exponents.
516 friend IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode);
517
518 friend IEEEFloat frexp(const IEEEFloat &X, int &Exp, roundingMode);
519
520 /// \name Special value setters.
521 /// @{
522
523 void makeLargest(bool Neg = false);
524 void makeSmallest(bool Neg = false);
525 void makeNaN(bool SNaN = false, bool Neg = false,
526 const APInt *fill = nullptr);
527 void makeInf(bool Neg = false);
528 void makeZero(bool Neg = false);
529 void makeQuiet();
530
531 /// Returns the smallest (by magnitude) normalized finite number in the given
532 /// semantics.
533 ///
534 /// \param Negative - True iff the number should be negative
535 void makeSmallestNormalized(bool Negative = false);
536
537 /// @}
538
539 cmpResult compareAbsoluteValue(const IEEEFloat &) const;
540
541 private:
542 /// \name Simple Queries
543 /// @{
544
545 integerPart *significandParts();
546 const integerPart *significandParts() const;
547 unsigned int partCount() const;
548
549 /// @}
550
551 /// \name Significand operations.
552 /// @{
553
554 integerPart addSignificand(const IEEEFloat &);
555 integerPart subtractSignificand(const IEEEFloat &, integerPart);
556 lostFraction addOrSubtractSignificand(const IEEEFloat &, bool subtract);
557 lostFraction multiplySignificand(const IEEEFloat &, IEEEFloat);
558 lostFraction multiplySignificand(const IEEEFloat&);
559 lostFraction divideSignificand(const IEEEFloat &);
560 void incrementSignificand();
561 void initialize(const fltSemantics *);
562 void shiftSignificandLeft(unsigned int);
563 lostFraction shiftSignificandRight(unsigned int);
564 unsigned int significandLSB() const;
565 unsigned int significandMSB() const;
566 void zeroSignificand();
567 /// Return true if the significand excluding the integral bit is all ones.
568 bool isSignificandAllOnes() const;
569 bool isSignificandAllOnesExceptLSB() const;
570 /// Return true if the significand excluding the integral bit is all zeros.
571 bool isSignificandAllZeros() const;
572 bool isSignificandAllZerosExceptMSB() const;
573
574 /// @}
575
576 /// \name Arithmetic on special values.
577 /// @{
578
579 opStatus addOrSubtractSpecials(const IEEEFloat &, bool subtract);
580 opStatus divideSpecials(const IEEEFloat &);
581 opStatus multiplySpecials(const IEEEFloat &);
582 opStatus modSpecials(const IEEEFloat &);
583 opStatus remainderSpecials(const IEEEFloat&);
584
585 /// @}
586
587 /// \name Miscellany
588 /// @{
589
590 bool convertFromStringSpecials(StringRef str);
591 opStatus normalize(roundingMode, lostFraction);
592 opStatus addOrSubtract(const IEEEFloat &, roundingMode, bool subtract);
593 opStatus handleOverflow(roundingMode);
594 bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
595 opStatus convertToSignExtendedInteger(MutableArrayRef<integerPart>,
596 unsigned int, bool, roundingMode,
597 bool *) const;
598 opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
599 roundingMode);
600 Expected<opStatus> convertFromHexadecimalString(StringRef, roundingMode);
601 Expected<opStatus> convertFromDecimalString(StringRef, roundingMode);
602 char *convertNormalToHexString(char *, unsigned int, bool,
603 roundingMode) const;
604 opStatus roundSignificandWithExponent(const integerPart *, unsigned int, int,
605 roundingMode);
606 ExponentType exponentNaN() const;
607 ExponentType exponentInf() const;
608 ExponentType exponentZero() const;
609
610 /// @}
611
612 template <const fltSemantics &S> APInt convertIEEEFloatToAPInt() const;
613 APInt convertHalfAPFloatToAPInt() const;
614 APInt convertBFloatAPFloatToAPInt() const;
615 APInt convertFloatAPFloatToAPInt() const;
616 APInt convertDoubleAPFloatToAPInt() const;
617 APInt convertQuadrupleAPFloatToAPInt() const;
618 APInt convertF80LongDoubleAPFloatToAPInt() const;
619 APInt convertPPCDoubleDoubleAPFloatToAPInt() const;
620 APInt convertFloat8E5M2APFloatToAPInt() const;
621 APInt convertFloat8E5M2FNUZAPFloatToAPInt() const;
622 APInt convertFloat8E4M3FNAPFloatToAPInt() const;
623 APInt convertFloat8E4M3FNUZAPFloatToAPInt() const;
624 APInt convertFloat8E4M3B11FNUZAPFloatToAPInt() const;
625 APInt convertFloatTF32APFloatToAPInt() const;
626 void initFromAPInt(const fltSemantics *Sem, const APInt &api);
627 template <const fltSemantics &S> void initFromIEEEAPInt(const APInt &api);
628 void initFromHalfAPInt(const APInt &api);
629 void initFromBFloatAPInt(const APInt &api);
630 void initFromFloatAPInt(const APInt &api);
631 void initFromDoubleAPInt(const APInt &api);
632 void initFromQuadrupleAPInt(const APInt &api);
633 void initFromF80LongDoubleAPInt(const APInt &api);
634 void initFromPPCDoubleDoubleAPInt(const APInt &api);
635 void initFromFloat8E5M2APInt(const APInt &api);
636 void initFromFloat8E5M2FNUZAPInt(const APInt &api);
637 void initFromFloat8E4M3FNAPInt(const APInt &api);
638 void initFromFloat8E4M3FNUZAPInt(const APInt &api);
639 void initFromFloat8E4M3B11FNUZAPInt(const APInt &api);
640 void initFromFloatTF32APInt(const APInt &api);
641
642 void assign(const IEEEFloat &);
643 void copySignificand(const IEEEFloat &);
644 void freeSignificand();
645
646 /// Note: this must be the first data member.
647 /// The semantics that this value obeys.
648 const fltSemantics *semantics;
649
650 /// A binary fraction with an explicit integer bit.
651 ///
652 /// The significand must be at least one bit wider than the target precision.
653 union Significand {
654 integerPart part;
655 integerPart *parts;
656 } significand;
657
658 /// The signed unbiased exponent of the value.
659 ExponentType exponent;
660
661 /// What kind of floating point number this is.
662 ///
663 /// Only 2 bits are required, but VisualStudio incorrectly sign extends it.
664 /// Using the extra bit keeps it from failing under VisualStudio.
665 fltCategory category : 3;
666
667 /// Sign bit of the number.
668 unsigned int sign : 1;
669 };
670
671 hash_code hash_value(const IEEEFloat &Arg);
672 int ilogb(const IEEEFloat &Arg);
673 IEEEFloat scalbn(IEEEFloat X, int Exp, IEEEFloat::roundingMode);
674 IEEEFloat frexp(const IEEEFloat &Val, int &Exp, IEEEFloat::roundingMode RM);
675
676 // This mode implements more precise float in terms of two APFloats.
677 // The interface and layout is designed for arbitrary underlying semantics,
678 // though currently only PPCDoubleDouble semantics are supported, whose
679 // corresponding underlying semantics are IEEEdouble.
680 class DoubleAPFloat final : public APFloatBase {
681 // Note: this must be the first data member.
682 const fltSemantics *Semantics;
683 std::unique_ptr<APFloat[]> Floats;
684
685 opStatus addImpl(const APFloat &a, const APFloat &aa, const APFloat &c,
686 const APFloat &cc, roundingMode RM);
687
688 opStatus addWithSpecial(const DoubleAPFloat &LHS, const DoubleAPFloat &RHS,
689 DoubleAPFloat &Out, roundingMode RM);
690
691 public:
692 DoubleAPFloat(const fltSemantics &S);
693 DoubleAPFloat(const fltSemantics &S, uninitializedTag);
694 DoubleAPFloat(const fltSemantics &S, integerPart);
695 DoubleAPFloat(const fltSemantics &S, const APInt &I);
696 DoubleAPFloat(const fltSemantics &S, APFloat &&First, APFloat &&Second);
697 DoubleAPFloat(const DoubleAPFloat &RHS);
698 DoubleAPFloat(DoubleAPFloat &&RHS);
699
700 DoubleAPFloat &operator=(const DoubleAPFloat &RHS);
701 inline DoubleAPFloat &operator=(DoubleAPFloat &&RHS);
702
needsCleanup()703 bool needsCleanup() const { return Floats != nullptr; }
704
705 inline APFloat &getFirst();
706 inline const APFloat &getFirst() const;
707 inline APFloat &getSecond();
708 inline const APFloat &getSecond() const;
709
710 opStatus add(const DoubleAPFloat &RHS, roundingMode RM);
711 opStatus subtract(const DoubleAPFloat &RHS, roundingMode RM);
712 opStatus multiply(const DoubleAPFloat &RHS, roundingMode RM);
713 opStatus divide(const DoubleAPFloat &RHS, roundingMode RM);
714 opStatus remainder(const DoubleAPFloat &RHS);
715 opStatus mod(const DoubleAPFloat &RHS);
716 opStatus fusedMultiplyAdd(const DoubleAPFloat &Multiplicand,
717 const DoubleAPFloat &Addend, roundingMode RM);
718 opStatus roundToIntegral(roundingMode RM);
719 void changeSign();
720 cmpResult compareAbsoluteValue(const DoubleAPFloat &RHS) const;
721
722 fltCategory getCategory() const;
723 bool isNegative() const;
724
725 void makeInf(bool Neg);
726 void makeZero(bool Neg);
727 void makeLargest(bool Neg);
728 void makeSmallest(bool Neg);
729 void makeSmallestNormalized(bool Neg);
730 void makeNaN(bool SNaN, bool Neg, const APInt *fill);
731
732 cmpResult compare(const DoubleAPFloat &RHS) const;
733 bool bitwiseIsEqual(const DoubleAPFloat &RHS) const;
734 APInt bitcastToAPInt() const;
735 Expected<opStatus> convertFromString(StringRef, roundingMode);
736 opStatus next(bool nextDown);
737
738 opStatus convertToInteger(MutableArrayRef<integerPart> Input,
739 unsigned int Width, bool IsSigned, roundingMode RM,
740 bool *IsExact) const;
741 opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM);
742 opStatus convertFromSignExtendedInteger(const integerPart *Input,
743 unsigned int InputSize, bool IsSigned,
744 roundingMode RM);
745 opStatus convertFromZeroExtendedInteger(const integerPart *Input,
746 unsigned int InputSize, bool IsSigned,
747 roundingMode RM);
748 unsigned int convertToHexString(char *DST, unsigned int HexDigits,
749 bool UpperCase, roundingMode RM) const;
750
751 bool isDenormal() const;
752 bool isSmallest() const;
753 bool isSmallestNormalized() const;
754 bool isLargest() const;
755 bool isInteger() const;
756
757 void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision,
758 unsigned FormatMaxPadding, bool TruncateZero = true) const;
759
760 bool getExactInverse(APFloat *inv) const;
761
762 LLVM_READONLY
763 int getExactLog2() const;
764 LLVM_READONLY
765 int getExactLog2Abs() const;
766
767 friend DoubleAPFloat scalbn(const DoubleAPFloat &X, int Exp, roundingMode);
768 friend DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp, roundingMode);
769 friend hash_code hash_value(const DoubleAPFloat &Arg);
770 };
771
772 hash_code hash_value(const DoubleAPFloat &Arg);
773 DoubleAPFloat scalbn(const DoubleAPFloat &Arg, int Exp, IEEEFloat::roundingMode RM);
774 DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp, IEEEFloat::roundingMode);
775
776 } // End detail namespace
777
778 // This is a interface class that is currently forwarding functionalities from
779 // detail::IEEEFloat.
780 class APFloat : public APFloatBase {
781 typedef detail::IEEEFloat IEEEFloat;
782 typedef detail::DoubleAPFloat DoubleAPFloat;
783
784 static_assert(std::is_standard_layout<IEEEFloat>::value);
785
786 union Storage {
787 const fltSemantics *semantics;
788 IEEEFloat IEEE;
789 DoubleAPFloat Double;
790
791 explicit Storage(IEEEFloat F, const fltSemantics &S);
Storage(DoubleAPFloat F,const fltSemantics & S)792 explicit Storage(DoubleAPFloat F, const fltSemantics &S)
793 : Double(std::move(F)) {
794 assert(&S == &PPCDoubleDouble());
795 }
796
797 template <typename... ArgTypes>
Storage(const fltSemantics & Semantics,ArgTypes &&...Args)798 Storage(const fltSemantics &Semantics, ArgTypes &&... Args) {
799 if (usesLayout<IEEEFloat>(Semantics)) {
800 new (&IEEE) IEEEFloat(Semantics, std::forward<ArgTypes>(Args)...);
801 return;
802 }
803 if (usesLayout<DoubleAPFloat>(Semantics)) {
804 new (&Double) DoubleAPFloat(Semantics, std::forward<ArgTypes>(Args)...);
805 return;
806 }
807 llvm_unreachable("Unexpected semantics");
808 }
809
~Storage()810 ~Storage() {
811 if (usesLayout<IEEEFloat>(*semantics)) {
812 IEEE.~IEEEFloat();
813 return;
814 }
815 if (usesLayout<DoubleAPFloat>(*semantics)) {
816 Double.~DoubleAPFloat();
817 return;
818 }
819 llvm_unreachable("Unexpected semantics");
820 }
821
Storage(const Storage & RHS)822 Storage(const Storage &RHS) {
823 if (usesLayout<IEEEFloat>(*RHS.semantics)) {
824 new (this) IEEEFloat(RHS.IEEE);
825 return;
826 }
827 if (usesLayout<DoubleAPFloat>(*RHS.semantics)) {
828 new (this) DoubleAPFloat(RHS.Double);
829 return;
830 }
831 llvm_unreachable("Unexpected semantics");
832 }
833
Storage(Storage && RHS)834 Storage(Storage &&RHS) {
835 if (usesLayout<IEEEFloat>(*RHS.semantics)) {
836 new (this) IEEEFloat(std::move(RHS.IEEE));
837 return;
838 }
839 if (usesLayout<DoubleAPFloat>(*RHS.semantics)) {
840 new (this) DoubleAPFloat(std::move(RHS.Double));
841 return;
842 }
843 llvm_unreachable("Unexpected semantics");
844 }
845
846 Storage &operator=(const Storage &RHS) {
847 if (usesLayout<IEEEFloat>(*semantics) &&
848 usesLayout<IEEEFloat>(*RHS.semantics)) {
849 IEEE = RHS.IEEE;
850 } else if (usesLayout<DoubleAPFloat>(*semantics) &&
851 usesLayout<DoubleAPFloat>(*RHS.semantics)) {
852 Double = RHS.Double;
853 } else if (this != &RHS) {
854 this->~Storage();
855 new (this) Storage(RHS);
856 }
857 return *this;
858 }
859
860 Storage &operator=(Storage &&RHS) {
861 if (usesLayout<IEEEFloat>(*semantics) &&
862 usesLayout<IEEEFloat>(*RHS.semantics)) {
863 IEEE = std::move(RHS.IEEE);
864 } else if (usesLayout<DoubleAPFloat>(*semantics) &&
865 usesLayout<DoubleAPFloat>(*RHS.semantics)) {
866 Double = std::move(RHS.Double);
867 } else if (this != &RHS) {
868 this->~Storage();
869 new (this) Storage(std::move(RHS));
870 }
871 return *this;
872 }
873 } U;
874
usesLayout(const fltSemantics & Semantics)875 template <typename T> static bool usesLayout(const fltSemantics &Semantics) {
876 static_assert(std::is_same<T, IEEEFloat>::value ||
877 std::is_same<T, DoubleAPFloat>::value);
878 if (std::is_same<T, DoubleAPFloat>::value) {
879 return &Semantics == &PPCDoubleDouble();
880 }
881 return &Semantics != &PPCDoubleDouble();
882 }
883
getIEEE()884 IEEEFloat &getIEEE() {
885 if (usesLayout<IEEEFloat>(*U.semantics))
886 return U.IEEE;
887 if (usesLayout<DoubleAPFloat>(*U.semantics))
888 return U.Double.getFirst().U.IEEE;
889 llvm_unreachable("Unexpected semantics");
890 }
891
getIEEE()892 const IEEEFloat &getIEEE() const {
893 if (usesLayout<IEEEFloat>(*U.semantics))
894 return U.IEEE;
895 if (usesLayout<DoubleAPFloat>(*U.semantics))
896 return U.Double.getFirst().U.IEEE;
897 llvm_unreachable("Unexpected semantics");
898 }
899
makeZero(bool Neg)900 void makeZero(bool Neg) { APFLOAT_DISPATCH_ON_SEMANTICS(makeZero(Neg)); }
901
makeInf(bool Neg)902 void makeInf(bool Neg) { APFLOAT_DISPATCH_ON_SEMANTICS(makeInf(Neg)); }
903
makeNaN(bool SNaN,bool Neg,const APInt * fill)904 void makeNaN(bool SNaN, bool Neg, const APInt *fill) {
905 APFLOAT_DISPATCH_ON_SEMANTICS(makeNaN(SNaN, Neg, fill));
906 }
907
makeLargest(bool Neg)908 void makeLargest(bool Neg) {
909 APFLOAT_DISPATCH_ON_SEMANTICS(makeLargest(Neg));
910 }
911
makeSmallest(bool Neg)912 void makeSmallest(bool Neg) {
913 APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallest(Neg));
914 }
915
makeSmallestNormalized(bool Neg)916 void makeSmallestNormalized(bool Neg) {
917 APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallestNormalized(Neg));
918 }
919
APFloat(IEEEFloat F,const fltSemantics & S)920 explicit APFloat(IEEEFloat F, const fltSemantics &S) : U(std::move(F), S) {}
APFloat(DoubleAPFloat F,const fltSemantics & S)921 explicit APFloat(DoubleAPFloat F, const fltSemantics &S)
922 : U(std::move(F), S) {}
923
compareAbsoluteValue(const APFloat & RHS)924 cmpResult compareAbsoluteValue(const APFloat &RHS) const {
925 assert(&getSemantics() == &RHS.getSemantics() &&
926 "Should only compare APFloats with the same semantics");
927 if (usesLayout<IEEEFloat>(getSemantics()))
928 return U.IEEE.compareAbsoluteValue(RHS.U.IEEE);
929 if (usesLayout<DoubleAPFloat>(getSemantics()))
930 return U.Double.compareAbsoluteValue(RHS.U.Double);
931 llvm_unreachable("Unexpected semantics");
932 }
933
934 public:
APFloat(const fltSemantics & Semantics)935 APFloat(const fltSemantics &Semantics) : U(Semantics) {}
936 APFloat(const fltSemantics &Semantics, StringRef S);
APFloat(const fltSemantics & Semantics,integerPart I)937 APFloat(const fltSemantics &Semantics, integerPart I) : U(Semantics, I) {}
938 template <typename T,
939 typename = std::enable_if_t<std::is_floating_point<T>::value>>
940 APFloat(const fltSemantics &Semantics, T V) = delete;
941 // TODO: Remove this constructor. This isn't faster than the first one.
APFloat(const fltSemantics & Semantics,uninitializedTag)942 APFloat(const fltSemantics &Semantics, uninitializedTag)
943 : U(Semantics, uninitialized) {}
APFloat(const fltSemantics & Semantics,const APInt & I)944 APFloat(const fltSemantics &Semantics, const APInt &I) : U(Semantics, I) {}
APFloat(double d)945 explicit APFloat(double d) : U(IEEEFloat(d), IEEEdouble()) {}
APFloat(float f)946 explicit APFloat(float f) : U(IEEEFloat(f), IEEEsingle()) {}
947 APFloat(const APFloat &RHS) = default;
948 APFloat(APFloat &&RHS) = default;
949
950 ~APFloat() = default;
951
needsCleanup()952 bool needsCleanup() const { APFLOAT_DISPATCH_ON_SEMANTICS(needsCleanup()); }
953
954 /// Factory for Positive and Negative Zero.
955 ///
956 /// \param Negative True iff the number should be negative.
957 static APFloat getZero(const fltSemantics &Sem, bool Negative = false) {
958 APFloat Val(Sem, uninitialized);
959 Val.makeZero(Negative);
960 return Val;
961 }
962
963 /// Factory for Positive and Negative Infinity.
964 ///
965 /// \param Negative True iff the number should be negative.
966 static APFloat getInf(const fltSemantics &Sem, bool Negative = false) {
967 APFloat Val(Sem, uninitialized);
968 Val.makeInf(Negative);
969 return Val;
970 }
971
972 /// Factory for NaN values.
973 ///
974 /// \param Negative - True iff the NaN generated should be negative.
975 /// \param payload - The unspecified fill bits for creating the NaN, 0 by
976 /// default. The value is truncated as necessary.
977 static APFloat getNaN(const fltSemantics &Sem, bool Negative = false,
978 uint64_t payload = 0) {
979 if (payload) {
980 APInt intPayload(64, payload);
981 return getQNaN(Sem, Negative, &intPayload);
982 } else {
983 return getQNaN(Sem, Negative, nullptr);
984 }
985 }
986
987 /// Factory for QNaN values.
988 static APFloat getQNaN(const fltSemantics &Sem, bool Negative = false,
989 const APInt *payload = nullptr) {
990 APFloat Val(Sem, uninitialized);
991 Val.makeNaN(false, Negative, payload);
992 return Val;
993 }
994
995 /// Factory for SNaN values.
996 static APFloat getSNaN(const fltSemantics &Sem, bool Negative = false,
997 const APInt *payload = nullptr) {
998 APFloat Val(Sem, uninitialized);
999 Val.makeNaN(true, Negative, payload);
1000 return Val;
1001 }
1002
1003 /// Returns the largest finite number in the given semantics.
1004 ///
1005 /// \param Negative - True iff the number should be negative
1006 static APFloat getLargest(const fltSemantics &Sem, bool Negative = false) {
1007 APFloat Val(Sem, uninitialized);
1008 Val.makeLargest(Negative);
1009 return Val;
1010 }
1011
1012 /// Returns the smallest (by magnitude) finite number in the given semantics.
1013 /// Might be denormalized, which implies a relative loss of precision.
1014 ///
1015 /// \param Negative - True iff the number should be negative
1016 static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false) {
1017 APFloat Val(Sem, uninitialized);
1018 Val.makeSmallest(Negative);
1019 return Val;
1020 }
1021
1022 /// Returns the smallest (by magnitude) normalized finite number in the given
1023 /// semantics.
1024 ///
1025 /// \param Negative - True iff the number should be negative
1026 static APFloat getSmallestNormalized(const fltSemantics &Sem,
1027 bool Negative = false) {
1028 APFloat Val(Sem, uninitialized);
1029 Val.makeSmallestNormalized(Negative);
1030 return Val;
1031 }
1032
1033 /// Returns a float which is bitcasted from an all one value int.
1034 ///
1035 /// \param Semantics - type float semantics
1036 static APFloat getAllOnesValue(const fltSemantics &Semantics);
1037
1038 /// Used to insert APFloat objects, or objects that contain APFloat objects,
1039 /// into FoldingSets.
1040 void Profile(FoldingSetNodeID &NID) const;
1041
add(const APFloat & RHS,roundingMode RM)1042 opStatus add(const APFloat &RHS, roundingMode RM) {
1043 assert(&getSemantics() == &RHS.getSemantics() &&
1044 "Should only call on two APFloats with the same semantics");
1045 if (usesLayout<IEEEFloat>(getSemantics()))
1046 return U.IEEE.add(RHS.U.IEEE, RM);
1047 if (usesLayout<DoubleAPFloat>(getSemantics()))
1048 return U.Double.add(RHS.U.Double, RM);
1049 llvm_unreachable("Unexpected semantics");
1050 }
subtract(const APFloat & RHS,roundingMode RM)1051 opStatus subtract(const APFloat &RHS, roundingMode RM) {
1052 assert(&getSemantics() == &RHS.getSemantics() &&
1053 "Should only call on two APFloats with the same semantics");
1054 if (usesLayout<IEEEFloat>(getSemantics()))
1055 return U.IEEE.subtract(RHS.U.IEEE, RM);
1056 if (usesLayout<DoubleAPFloat>(getSemantics()))
1057 return U.Double.subtract(RHS.U.Double, RM);
1058 llvm_unreachable("Unexpected semantics");
1059 }
multiply(const APFloat & RHS,roundingMode RM)1060 opStatus multiply(const APFloat &RHS, roundingMode RM) {
1061 assert(&getSemantics() == &RHS.getSemantics() &&
1062 "Should only call on two APFloats with the same semantics");
1063 if (usesLayout<IEEEFloat>(getSemantics()))
1064 return U.IEEE.multiply(RHS.U.IEEE, RM);
1065 if (usesLayout<DoubleAPFloat>(getSemantics()))
1066 return U.Double.multiply(RHS.U.Double, RM);
1067 llvm_unreachable("Unexpected semantics");
1068 }
divide(const APFloat & RHS,roundingMode RM)1069 opStatus divide(const APFloat &RHS, roundingMode RM) {
1070 assert(&getSemantics() == &RHS.getSemantics() &&
1071 "Should only call on two APFloats with the same semantics");
1072 if (usesLayout<IEEEFloat>(getSemantics()))
1073 return U.IEEE.divide(RHS.U.IEEE, RM);
1074 if (usesLayout<DoubleAPFloat>(getSemantics()))
1075 return U.Double.divide(RHS.U.Double, RM);
1076 llvm_unreachable("Unexpected semantics");
1077 }
remainder(const APFloat & RHS)1078 opStatus remainder(const APFloat &RHS) {
1079 assert(&getSemantics() == &RHS.getSemantics() &&
1080 "Should only call on two APFloats with the same semantics");
1081 if (usesLayout<IEEEFloat>(getSemantics()))
1082 return U.IEEE.remainder(RHS.U.IEEE);
1083 if (usesLayout<DoubleAPFloat>(getSemantics()))
1084 return U.Double.remainder(RHS.U.Double);
1085 llvm_unreachable("Unexpected semantics");
1086 }
mod(const APFloat & RHS)1087 opStatus mod(const APFloat &RHS) {
1088 assert(&getSemantics() == &RHS.getSemantics() &&
1089 "Should only call on two APFloats with the same semantics");
1090 if (usesLayout<IEEEFloat>(getSemantics()))
1091 return U.IEEE.mod(RHS.U.IEEE);
1092 if (usesLayout<DoubleAPFloat>(getSemantics()))
1093 return U.Double.mod(RHS.U.Double);
1094 llvm_unreachable("Unexpected semantics");
1095 }
fusedMultiplyAdd(const APFloat & Multiplicand,const APFloat & Addend,roundingMode RM)1096 opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend,
1097 roundingMode RM) {
1098 assert(&getSemantics() == &Multiplicand.getSemantics() &&
1099 "Should only call on APFloats with the same semantics");
1100 assert(&getSemantics() == &Addend.getSemantics() &&
1101 "Should only call on APFloats with the same semantics");
1102 if (usesLayout<IEEEFloat>(getSemantics()))
1103 return U.IEEE.fusedMultiplyAdd(Multiplicand.U.IEEE, Addend.U.IEEE, RM);
1104 if (usesLayout<DoubleAPFloat>(getSemantics()))
1105 return U.Double.fusedMultiplyAdd(Multiplicand.U.Double, Addend.U.Double,
1106 RM);
1107 llvm_unreachable("Unexpected semantics");
1108 }
roundToIntegral(roundingMode RM)1109 opStatus roundToIntegral(roundingMode RM) {
1110 APFLOAT_DISPATCH_ON_SEMANTICS(roundToIntegral(RM));
1111 }
1112
1113 // TODO: bool parameters are not readable and a source of bugs.
1114 // Do something.
next(bool nextDown)1115 opStatus next(bool nextDown) {
1116 APFLOAT_DISPATCH_ON_SEMANTICS(next(nextDown));
1117 }
1118
1119 /// Negate an APFloat.
1120 APFloat operator-() const {
1121 APFloat Result(*this);
1122 Result.changeSign();
1123 return Result;
1124 }
1125
1126 /// Add two APFloats, rounding ties to the nearest even.
1127 /// No error checking.
1128 APFloat operator+(const APFloat &RHS) const {
1129 APFloat Result(*this);
1130 (void)Result.add(RHS, rmNearestTiesToEven);
1131 return Result;
1132 }
1133
1134 /// Subtract two APFloats, rounding ties to the nearest even.
1135 /// No error checking.
1136 APFloat operator-(const APFloat &RHS) const {
1137 APFloat Result(*this);
1138 (void)Result.subtract(RHS, rmNearestTiesToEven);
1139 return Result;
1140 }
1141
1142 /// Multiply two APFloats, rounding ties to the nearest even.
1143 /// No error checking.
1144 APFloat operator*(const APFloat &RHS) const {
1145 APFloat Result(*this);
1146 (void)Result.multiply(RHS, rmNearestTiesToEven);
1147 return Result;
1148 }
1149
1150 /// Divide the first APFloat by the second, rounding ties to the nearest even.
1151 /// No error checking.
1152 APFloat operator/(const APFloat &RHS) const {
1153 APFloat Result(*this);
1154 (void)Result.divide(RHS, rmNearestTiesToEven);
1155 return Result;
1156 }
1157
changeSign()1158 void changeSign() { APFLOAT_DISPATCH_ON_SEMANTICS(changeSign()); }
clearSign()1159 void clearSign() {
1160 if (isNegative())
1161 changeSign();
1162 }
copySign(const APFloat & RHS)1163 void copySign(const APFloat &RHS) {
1164 if (isNegative() != RHS.isNegative())
1165 changeSign();
1166 }
1167
1168 /// A static helper to produce a copy of an APFloat value with its sign
1169 /// copied from some other APFloat.
copySign(APFloat Value,const APFloat & Sign)1170 static APFloat copySign(APFloat Value, const APFloat &Sign) {
1171 Value.copySign(Sign);
1172 return Value;
1173 }
1174
1175 /// Assuming this is an IEEE-754 NaN value, quiet its signaling bit.
1176 /// This preserves the sign and payload bits.
makeQuiet()1177 APFloat makeQuiet() const {
1178 APFloat Result(*this);
1179 Result.getIEEE().makeQuiet();
1180 return Result;
1181 }
1182
1183 opStatus convert(const fltSemantics &ToSemantics, roundingMode RM,
1184 bool *losesInfo);
convertToInteger(MutableArrayRef<integerPart> Input,unsigned int Width,bool IsSigned,roundingMode RM,bool * IsExact)1185 opStatus convertToInteger(MutableArrayRef<integerPart> Input,
1186 unsigned int Width, bool IsSigned, roundingMode RM,
1187 bool *IsExact) const {
1188 APFLOAT_DISPATCH_ON_SEMANTICS(
1189 convertToInteger(Input, Width, IsSigned, RM, IsExact));
1190 }
1191 opStatus convertToInteger(APSInt &Result, roundingMode RM,
1192 bool *IsExact) const;
convertFromAPInt(const APInt & Input,bool IsSigned,roundingMode RM)1193 opStatus convertFromAPInt(const APInt &Input, bool IsSigned,
1194 roundingMode RM) {
1195 APFLOAT_DISPATCH_ON_SEMANTICS(convertFromAPInt(Input, IsSigned, RM));
1196 }
convertFromSignExtendedInteger(const integerPart * Input,unsigned int InputSize,bool IsSigned,roundingMode RM)1197 opStatus convertFromSignExtendedInteger(const integerPart *Input,
1198 unsigned int InputSize, bool IsSigned,
1199 roundingMode RM) {
1200 APFLOAT_DISPATCH_ON_SEMANTICS(
1201 convertFromSignExtendedInteger(Input, InputSize, IsSigned, RM));
1202 }
convertFromZeroExtendedInteger(const integerPart * Input,unsigned int InputSize,bool IsSigned,roundingMode RM)1203 opStatus convertFromZeroExtendedInteger(const integerPart *Input,
1204 unsigned int InputSize, bool IsSigned,
1205 roundingMode RM) {
1206 APFLOAT_DISPATCH_ON_SEMANTICS(
1207 convertFromZeroExtendedInteger(Input, InputSize, IsSigned, RM));
1208 }
1209 Expected<opStatus> convertFromString(StringRef, roundingMode);
bitcastToAPInt()1210 APInt bitcastToAPInt() const {
1211 APFLOAT_DISPATCH_ON_SEMANTICS(bitcastToAPInt());
1212 }
1213
1214 /// Converts this APFloat to host double value.
1215 ///
1216 /// \pre The APFloat must be built using semantics, that can be represented by
1217 /// the host double type without loss of precision. It can be IEEEdouble and
1218 /// shorter semantics, like IEEEsingle and others.
1219 double convertToDouble() const;
1220
1221 /// Converts this APFloat to host float value.
1222 ///
1223 /// \pre The APFloat must be built using semantics, that can be represented by
1224 /// the host float type without loss of precision. It can be IEEEsingle and
1225 /// shorter semantics, like IEEEhalf.
1226 float convertToFloat() const;
1227
1228 bool operator==(const APFloat &RHS) const { return compare(RHS) == cmpEqual; }
1229
1230 bool operator!=(const APFloat &RHS) const { return compare(RHS) != cmpEqual; }
1231
1232 bool operator<(const APFloat &RHS) const {
1233 return compare(RHS) == cmpLessThan;
1234 }
1235
1236 bool operator>(const APFloat &RHS) const {
1237 return compare(RHS) == cmpGreaterThan;
1238 }
1239
1240 bool operator<=(const APFloat &RHS) const {
1241 cmpResult Res = compare(RHS);
1242 return Res == cmpLessThan || Res == cmpEqual;
1243 }
1244
1245 bool operator>=(const APFloat &RHS) const {
1246 cmpResult Res = compare(RHS);
1247 return Res == cmpGreaterThan || Res == cmpEqual;
1248 }
1249
compare(const APFloat & RHS)1250 cmpResult compare(const APFloat &RHS) const {
1251 assert(&getSemantics() == &RHS.getSemantics() &&
1252 "Should only compare APFloats with the same semantics");
1253 if (usesLayout<IEEEFloat>(getSemantics()))
1254 return U.IEEE.compare(RHS.U.IEEE);
1255 if (usesLayout<DoubleAPFloat>(getSemantics()))
1256 return U.Double.compare(RHS.U.Double);
1257 llvm_unreachable("Unexpected semantics");
1258 }
1259
bitwiseIsEqual(const APFloat & RHS)1260 bool bitwiseIsEqual(const APFloat &RHS) const {
1261 if (&getSemantics() != &RHS.getSemantics())
1262 return false;
1263 if (usesLayout<IEEEFloat>(getSemantics()))
1264 return U.IEEE.bitwiseIsEqual(RHS.U.IEEE);
1265 if (usesLayout<DoubleAPFloat>(getSemantics()))
1266 return U.Double.bitwiseIsEqual(RHS.U.Double);
1267 llvm_unreachable("Unexpected semantics");
1268 }
1269
1270 /// We don't rely on operator== working on double values, as
1271 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1272 /// As such, this method can be used to do an exact bit-for-bit comparison of
1273 /// two floating point values.
1274 ///
1275 /// We leave the version with the double argument here because it's just so
1276 /// convenient to write "2.0" and the like. Without this function we'd
1277 /// have to duplicate its logic everywhere it's called.
isExactlyValue(double V)1278 bool isExactlyValue(double V) const {
1279 bool ignored;
1280 APFloat Tmp(V);
1281 Tmp.convert(getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
1282 return bitwiseIsEqual(Tmp);
1283 }
1284
convertToHexString(char * DST,unsigned int HexDigits,bool UpperCase,roundingMode RM)1285 unsigned int convertToHexString(char *DST, unsigned int HexDigits,
1286 bool UpperCase, roundingMode RM) const {
1287 APFLOAT_DISPATCH_ON_SEMANTICS(
1288 convertToHexString(DST, HexDigits, UpperCase, RM));
1289 }
1290
isZero()1291 bool isZero() const { return getCategory() == fcZero; }
isInfinity()1292 bool isInfinity() const { return getCategory() == fcInfinity; }
isNaN()1293 bool isNaN() const { return getCategory() == fcNaN; }
1294
isNegative()1295 bool isNegative() const { return getIEEE().isNegative(); }
isDenormal()1296 bool isDenormal() const { APFLOAT_DISPATCH_ON_SEMANTICS(isDenormal()); }
isSignaling()1297 bool isSignaling() const { return getIEEE().isSignaling(); }
1298
isNormal()1299 bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
isFinite()1300 bool isFinite() const { return !isNaN() && !isInfinity(); }
1301
getCategory()1302 fltCategory getCategory() const { return getIEEE().getCategory(); }
getSemantics()1303 const fltSemantics &getSemantics() const { return *U.semantics; }
isNonZero()1304 bool isNonZero() const { return !isZero(); }
isFiniteNonZero()1305 bool isFiniteNonZero() const { return isFinite() && !isZero(); }
isPosZero()1306 bool isPosZero() const { return isZero() && !isNegative(); }
isNegZero()1307 bool isNegZero() const { return isZero() && isNegative(); }
isPosInfinity()1308 bool isPosInfinity() const { return isInfinity() && !isNegative(); }
isNegInfinity()1309 bool isNegInfinity() const { return isInfinity() && isNegative(); }
isSmallest()1310 bool isSmallest() const { APFLOAT_DISPATCH_ON_SEMANTICS(isSmallest()); }
isLargest()1311 bool isLargest() const { APFLOAT_DISPATCH_ON_SEMANTICS(isLargest()); }
isInteger()1312 bool isInteger() const { APFLOAT_DISPATCH_ON_SEMANTICS(isInteger()); }
isIEEE()1313 bool isIEEE() const { return usesLayout<IEEEFloat>(getSemantics()); }
1314
isSmallestNormalized()1315 bool isSmallestNormalized() const {
1316 APFLOAT_DISPATCH_ON_SEMANTICS(isSmallestNormalized());
1317 }
1318
1319 /// Return the FPClassTest which will return true for the value.
1320 FPClassTest classify() const;
1321
1322 APFloat &operator=(const APFloat &RHS) = default;
1323 APFloat &operator=(APFloat &&RHS) = default;
1324
1325 void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0,
1326 unsigned FormatMaxPadding = 3, bool TruncateZero = true) const {
1327 APFLOAT_DISPATCH_ON_SEMANTICS(
1328 toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero));
1329 }
1330
1331 void print(raw_ostream &) const;
1332 void dump() const;
1333
getExactInverse(APFloat * inv)1334 bool getExactInverse(APFloat *inv) const {
1335 APFLOAT_DISPATCH_ON_SEMANTICS(getExactInverse(inv));
1336 }
1337
1338 LLVM_READONLY
getExactLog2Abs()1339 int getExactLog2Abs() const {
1340 APFLOAT_DISPATCH_ON_SEMANTICS(getExactLog2Abs());
1341 }
1342
1343 LLVM_READONLY
getExactLog2()1344 int getExactLog2() const {
1345 APFLOAT_DISPATCH_ON_SEMANTICS(getExactLog2());
1346 }
1347
1348 friend hash_code hash_value(const APFloat &Arg);
ilogb(const APFloat & Arg)1349 friend int ilogb(const APFloat &Arg) { return ilogb(Arg.getIEEE()); }
1350 friend APFloat scalbn(APFloat X, int Exp, roundingMode RM);
1351 friend APFloat frexp(const APFloat &X, int &Exp, roundingMode RM);
1352 friend IEEEFloat;
1353 friend DoubleAPFloat;
1354 };
1355
1356 /// See friend declarations above.
1357 ///
1358 /// These additional declarations are required in order to compile LLVM with IBM
1359 /// xlC compiler.
1360 hash_code hash_value(const APFloat &Arg);
scalbn(APFloat X,int Exp,APFloat::roundingMode RM)1361 inline APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM) {
1362 if (APFloat::usesLayout<detail::IEEEFloat>(X.getSemantics()))
1363 return APFloat(scalbn(X.U.IEEE, Exp, RM), X.getSemantics());
1364 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.getSemantics()))
1365 return APFloat(scalbn(X.U.Double, Exp, RM), X.getSemantics());
1366 llvm_unreachable("Unexpected semantics");
1367 }
1368
1369 /// Equivalent of C standard library function.
1370 ///
1371 /// While the C standard says Exp is an unspecified value for infinity and nan,
1372 /// this returns INT_MAX for infinities, and INT_MIN for NaNs.
frexp(const APFloat & X,int & Exp,APFloat::roundingMode RM)1373 inline APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM) {
1374 if (APFloat::usesLayout<detail::IEEEFloat>(X.getSemantics()))
1375 return APFloat(frexp(X.U.IEEE, Exp, RM), X.getSemantics());
1376 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.getSemantics()))
1377 return APFloat(frexp(X.U.Double, Exp, RM), X.getSemantics());
1378 llvm_unreachable("Unexpected semantics");
1379 }
1380 /// Returns the absolute value of the argument.
abs(APFloat X)1381 inline APFloat abs(APFloat X) {
1382 X.clearSign();
1383 return X;
1384 }
1385
1386 /// Returns the negated value of the argument.
neg(APFloat X)1387 inline APFloat neg(APFloat X) {
1388 X.changeSign();
1389 return X;
1390 }
1391
1392 /// Implements IEEE-754 2019 minimumNumber semantics. Returns the smaller of the
1393 /// 2 arguments if both are not NaN. If either argument is a NaN, returns the
1394 /// other argument. -0 is treated as ordered less than +0.
1395 LLVM_READONLY
minnum(const APFloat & A,const APFloat & B)1396 inline APFloat minnum(const APFloat &A, const APFloat &B) {
1397 if (A.isNaN())
1398 return B;
1399 if (B.isNaN())
1400 return A;
1401 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1402 return A.isNegative() ? A : B;
1403 return B < A ? B : A;
1404 }
1405
1406 /// Implements IEEE-754 2019 maximumNumber semantics. Returns the larger of the
1407 /// 2 arguments if both are not NaN. If either argument is a NaN, returns the
1408 /// other argument. +0 is treated as ordered greater than -0.
1409 LLVM_READONLY
maxnum(const APFloat & A,const APFloat & B)1410 inline APFloat maxnum(const APFloat &A, const APFloat &B) {
1411 if (A.isNaN())
1412 return B;
1413 if (B.isNaN())
1414 return A;
1415 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1416 return A.isNegative() ? B : A;
1417 return A < B ? B : A;
1418 }
1419
1420 /// Implements IEEE 754-2019 minimum semantics. Returns the smaller of 2
1421 /// arguments, propagating NaNs and treating -0 as less than +0.
1422 LLVM_READONLY
minimum(const APFloat & A,const APFloat & B)1423 inline APFloat minimum(const APFloat &A, const APFloat &B) {
1424 if (A.isNaN())
1425 return A;
1426 if (B.isNaN())
1427 return B;
1428 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1429 return A.isNegative() ? A : B;
1430 return B < A ? B : A;
1431 }
1432
1433 /// Implements IEEE 754-2019 maximum semantics. Returns the larger of 2
1434 /// arguments, propagating NaNs and treating -0 as less than +0.
1435 LLVM_READONLY
maximum(const APFloat & A,const APFloat & B)1436 inline APFloat maximum(const APFloat &A, const APFloat &B) {
1437 if (A.isNaN())
1438 return A;
1439 if (B.isNaN())
1440 return B;
1441 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1442 return A.isNegative() ? B : A;
1443 return A < B ? B : A;
1444 }
1445
1446 // We want the following functions to be available in the header for inlining.
1447 // We cannot define them inline in the class definition of `DoubleAPFloat`
1448 // because doing so would instantiate `std::unique_ptr<APFloat[]>` before
1449 // `APFloat` is defined, and that would be undefined behavior.
1450 namespace detail {
1451
1452 DoubleAPFloat &DoubleAPFloat::operator=(DoubleAPFloat &&RHS) {
1453 if (this != &RHS) {
1454 this->~DoubleAPFloat();
1455 new (this) DoubleAPFloat(std::move(RHS));
1456 }
1457 return *this;
1458 }
1459
getFirst()1460 APFloat &DoubleAPFloat::getFirst() { return Floats[0]; }
getFirst()1461 const APFloat &DoubleAPFloat::getFirst() const { return Floats[0]; }
getSecond()1462 APFloat &DoubleAPFloat::getSecond() { return Floats[1]; }
getSecond()1463 const APFloat &DoubleAPFloat::getSecond() const { return Floats[1]; }
1464
1465 } // namespace detail
1466
1467 } // namespace llvm
1468
1469 #undef APFLOAT_DISPATCH_ON_SEMANTICS
1470 #endif // LLVM_ADT_APFLOAT_H
1471