LLVM 24.0.0git
APFloat.h
Go to the documentation of this file.
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"
24#include <optional>
25
26#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \
27 do { \
28 if (usesLayout<IEEEFloat>(getSemantics())) \
29 return U.IEEE.METHOD_CALL; \
30 if (usesLayout<DoubleAPFloat>(getSemantics())) \
31 return U.Double.METHOD_CALL; \
32 llvm_unreachable("Unexpected semantics"); \
33 } while (false)
34
35namespace llvm {
36
37struct fltSemantics;
38class APSInt;
39class StringRef;
40class APFloat;
41class FloatRepr;
42class raw_ostream;
43
44template <typename T> class Expected;
45template <typename T> class SmallVectorImpl;
46
47/// Enum that represents what fraction of the LSB truncated bits of an fp number
48/// represent.
49///
50/// This essentially combines the roles of guard and sticky bits.
51enum lostFraction { // Example of truncated bits:
52 lfExactlyZero, // 000000
53 lfLessThanHalf, // 0xxxxx x's not all zero
54 lfExactlyHalf, // 100000
55 lfMoreThanHalf // 1xxxxx x's not all zero
56};
57
58/// A self-contained host- and target-independent arbitrary-precision
59/// floating-point software implementation.
60///
61/// APFloat uses bignum integer arithmetic as provided by static functions in
62/// the APInt class. The library will work with bignum integers whose parts are
63/// any unsigned type at least 16 bits wide, but 64 bits is recommended.
64///
65/// Written for clarity rather than speed, in particular with a view to use in
66/// the front-end of a cross compiler so that target arithmetic can be correctly
67/// performed on the host. Performance should nonetheless be reasonable,
68/// particularly for its intended use. It may be useful as a base
69/// implementation for a run-time library during development of a faster
70/// target-specific one.
71///
72/// All 5 rounding modes in the IEEE-754R draft are handled correctly for all
73/// implemented operations. Currently implemented operations are add, subtract,
74/// multiply, divide, fused-multiply-add, conversion-to-float,
75/// conversion-to-integer and conversion-from-integer. New rounding modes
76/// (e.g. away from zero) can be added with three or four lines of code.
77///
78/// Four formats are built-in: IEEE single precision, double precision,
79/// quadruple precision, and x87 80-bit extended double (when operating with
80/// full extended precision). Adding a new format that obeys IEEE semantics
81/// only requires adding two lines of code: a declaration and definition of the
82/// format.
83///
84/// All operations return the status of that operation as an exception bit-mask,
85/// so multiple operations can be done consecutively with their results or-ed
86/// together. The returned status can be useful for compiler diagnostics; e.g.,
87/// inexact, underflow and overflow can be easily diagnosed on constant folding,
88/// and compiler optimizers can determine what exceptions would be raised by
89/// folding operations and optimize, or perhaps not optimize, accordingly.
90///
91/// At present, underflow tininess is detected after rounding; it should be
92/// straight forward to add support for the before-rounding case too.
93///
94/// The library reads hexadecimal floating point numbers as per C99, and
95/// correctly rounds if necessary according to the specified rounding mode.
96/// Syntax is required to have been validated by the caller. It also converts
97/// floating point numbers to hexadecimal text as per the C99 %a and %A
98/// conversions. The output precision (or alternatively the natural minimal
99/// precision) can be specified; if the requested precision is less than the
100/// natural precision the output is correctly rounded for the specified rounding
101/// mode.
102///
103/// It also reads decimal floating point numbers and correctly rounds according
104/// to the specified rounding mode.
105///
106/// Conversion to decimal text is not currently implemented.
107///
108/// Non-zero finite numbers are represented internally as a sign bit, a 16-bit
109/// signed exponent, and the significand as an array of integer parts. After
110/// normalization of a number of precision P the exponent is within the range of
111/// the format, and if the number is not denormal the P-th bit of the
112/// significand is set as an explicit integer bit. For denormals the most
113/// significant bit is shifted right so that the exponent is maintained at the
114/// format's minimum, so that the smallest denormal has just the least
115/// significant bit of the significand set. The sign of zeroes and infinities
116/// is significant; the exponent and significand of such numbers is not stored,
117/// but has a known implicit (deterministic) value: 0 for the significands, 0
118/// for zero exponent, all 1 bits for infinity exponent. For NaNs the sign and
119/// significand are deterministic, although not really meaningful, and preserved
120/// in non-conversion operations. The exponent is implicitly all 1 bits.
121///
122/// APFloat does not provide any exception handling beyond default exception
123/// handling. We represent Signaling NaNs via IEEE-754R 2008 6.2.1 should clause
124/// by encoding Signaling NaNs with the first bit of its trailing significand as
125/// 0.
126///
127/// TODO
128/// ====
129///
130/// Some features that may or may not be worth adding:
131///
132/// Binary to decimal conversion (hard).
133///
134/// Optional ability to detect underflow tininess before rounding.
135///
136/// New formats: x87 in single and double precision mode (IEEE apart from
137/// extended exponent range) (hard).
138///
139/// New operations: sqrt, IEEE remainder, C90 fmod, nexttoward.
140///
141
142namespace detail {
143class IEEEFloat;
144class DoubleAPFloat;
145} // namespace detail
146
147// This is the common type definitions shared by APFloat and its internal
148// implementation classes. This struct should not define any non-static data
149// members.
151public:
153 static constexpr unsigned integerPartWidth = APInt::APINT_BITS_PER_WORD;
154
155 /// A signed type to represent a floating point numbers unbiased exponent.
156 using ExponentType = int32_t;
157
158 /// \name Floating Point Semantics.
159 /// @{
166 // TODO: Documentation is missing.
168 // The IBM double-double semantics. Such a number consists of a pair of
169 // IEEE 64-bit doubles (Hi, Lo), where |Hi| > |Lo|, and if normal,
170 // (double)(Hi + Lo) == Hi. The numeric value it's modeling is Hi + Lo.
171 // Therefore it has two 53-bit mantissa parts that aren't necessarily
172 // adjacent to each other, and two 11-bit exponents.
173 //
174 // Note: we need to make the value different from semBogus as otherwise
175 // an unsafe optimization may collapse both values to a single address,
176 // and we heavily rely on them having distinct addresses.
178 // These are legacy semantics for the fallback, inaccurate implementation
179 // of IBM double-double, if the accurate semPPCDoubleDouble doesn't handle
180 // the operation. It's equivalent to having an IEEE number with consecutive
181 // 106 bits of mantissa and 11 bits of exponent.
182 //
183 // It's not equivalent to IBM double-double. For example, a legit IBM
184 // double-double, 1 + epsilon:
185 //
186 // 1 + epsilon = 1 + (1 >> 1076)
187 //
188 // is not representable by a consecutive 106 bits of mantissa.
189 //
190 // Currently, these semantics are used in the following way:
191 //
192 // semPPCDoubleDouble -> (IEEEdouble, IEEEdouble) ->
193 // (64-bit APInt, 64-bit APInt) -> (128-bit APInt) ->
194 // semPPCDoubleDoubleLegacy -> IEEE operations
195 //
196 // We use bitcastToAPInt() to get the bit representation (in APInt) of the
197 // underlying IEEEdouble, then use the APInt constructor to construct the
198 // legacy IEEE float.
199 //
200 // TODO: Implement all operations in semPPCDoubleDouble, and delete these
201 // semantics.
203 // 8-bit floating point number following IEEE-754 conventions with bit
204 // layout S1E5M2 as described in https://arxiv.org/abs/2209.05433.
206 // 8-bit floating point number mostly following IEEE-754 conventions
207 // and bit layout S1E5M2 described in https://arxiv.org/abs/2206.02915,
208 // with expanded range and with no infinity or signed zero.
209 // NaN is represented as negative zero. (FN -> Finite, UZ -> unsigned zero).
210 // This format's exponent bias is 16, instead of the 15 (2 ** (5 - 1) - 1)
211 // that IEEE precedent would imply.
213 // 8-bit floating point number following IEEE-754 conventions with bit
214 // layout S1E4M3.
216 // 8-bit floating point number mostly following IEEE-754 conventions with
217 // bit layout S1E4M3 as described in https://arxiv.org/abs/2209.05433.
218 // Unlike IEEE-754 types, there are no infinity values, and NaN is
219 // represented with the exponent and mantissa bits set to all 1s.
221 // 8-bit floating point number mostly following IEEE-754 conventions
222 // and bit layout S1E4M3 described in https://arxiv.org/abs/2206.02915,
223 // with expanded range and with no infinity or signed zero.
224 // NaN is represented as negative zero. (FN -> Finite, UZ -> unsigned zero).
225 // This format's exponent bias is 8, instead of the 7 (2 ** (4 - 1) - 1)
226 // that IEEE precedent would imply.
228 // 8-bit floating point number mostly following IEEE-754 conventions
229 // and bit layout S1E4M3 with expanded range and with no infinity or signed
230 // zero.
231 // NaN is represented as negative zero. (FN -> Finite, UZ -> unsigned zero).
232 // This format's exponent bias is 11, instead of the 7 (2 ** (4 - 1) - 1)
233 // that IEEE precedent would imply.
235 // 8-bit floating point number following IEEE-754 conventions with bit
236 // layout S1E3M4.
238 // Floating point number that occupies 32 bits or less of storage, providing
239 // improved range compared to half (16-bit) formats, at (potentially)
240 // greater throughput than single precision (32-bit) formats.
242 // 8-bit floating point number with (all the) 8 bits for the exponent
243 // like in FP32. There are no zeroes, no infinities, and no denormal values.
244 // This format has unsigned representation only. (U -> Unsigned only).
245 // NaN is represented with all bits set to 1. Bias is 127.
246 // This format represents the scale data type in the MX specification from:
247 // https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
249 // 6-bit floating point number with bit layout S1E3M2. Unlike IEEE-754
250 // types, there are no infinity or NaN values. The format is detailed in
251 // https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
253 // 6-bit floating point number with bit layout S1E2M3. Unlike IEEE-754
254 // types, there are no infinity or NaN values. The format is detailed in
255 // https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
257 // 4-bit floating point number with bit layout S1E2M1. Unlike IEEE-754
258 // types, there are no infinity or NaN values. The format is detailed in
259 // https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
261 // 8-bit floating point number mostly following IEEE-754 conventions with
262 // bit layout S0E5M3 as described in PTX ISA page.
263 // https://docs.nvidia.com/cuda/developer-preview/13.4/parallel-thread-execution/index.html#alternate-floating-point-data-formats
264 // Unlike IEEE-754 types, there are no infinity values, and NaN is
265 // represented with the exponent and mantissa bits set to all 1s.
268 };
269
272
273private:
274 LLVM_ABI static const fltSemantics semIEEEhalf;
275 LLVM_ABI static const fltSemantics semBFloat;
276 LLVM_ABI static const fltSemantics semIEEEsingle;
277 LLVM_ABI static const fltSemantics semIEEEdouble;
278 LLVM_ABI static const fltSemantics semIEEEquad;
279 LLVM_ABI static const fltSemantics semX87DoubleExtended;
280 LLVM_ABI static const fltSemantics semPPCDoubleDouble;
281 LLVM_ABI static const fltSemantics semPPCDoubleDoubleLegacy;
282 LLVM_ABI static const fltSemantics semFloat8E5M2;
283 LLVM_ABI static const fltSemantics semFloat8E5M2FNUZ;
284 LLVM_ABI static const fltSemantics semFloat8E4M3;
285 LLVM_ABI static const fltSemantics semFloat8E4M3FN;
286 LLVM_ABI static const fltSemantics semFloat8E4M3FNUZ;
287 LLVM_ABI static const fltSemantics semFloat8E4M3B11FNUZ;
288 LLVM_ABI static const fltSemantics semFloat8E3M4;
289 LLVM_ABI static const fltSemantics semFloatTF32;
290 LLVM_ABI static const fltSemantics semFloat8E8M0FNU;
291 LLVM_ABI static const fltSemantics semFloat8E5M3FNU;
292 LLVM_ABI static const fltSemantics semFloat6E3M2FN;
293 LLVM_ABI static const fltSemantics semFloat6E2M3FN;
294 LLVM_ABI static const fltSemantics semFloat4E2M1FN;
295 LLVM_ABI static const fltSemantics semBogus;
296
297 friend class detail::IEEEFloat;
299 friend class APFloat;
300
301public:
302 static constexpr const fltSemantics &IEEEhalf() { return semIEEEhalf; }
303 static constexpr const fltSemantics &BFloat() { return semBFloat; }
304 static constexpr const fltSemantics &IEEEsingle() { return semIEEEsingle; }
305 static constexpr const fltSemantics &IEEEdouble() { return semIEEEdouble; }
306 static constexpr const fltSemantics &IEEEquad() { return semIEEEquad; }
307 static constexpr const fltSemantics &x87DoubleExtended() {
308 return semX87DoubleExtended;
309 }
310 static constexpr const fltSemantics &PPCDoubleDouble() {
311 return semPPCDoubleDouble;
312 }
313 static constexpr const fltSemantics &PPCDoubleDoubleLegacy() {
314 return semPPCDoubleDoubleLegacy;
315 }
316 static constexpr const fltSemantics &Float8E5M2() { return semFloat8E5M2; }
317 static constexpr const fltSemantics &Float8E5M2FNUZ() {
318 return semFloat8E5M2FNUZ;
319 }
320 static constexpr const fltSemantics &Float8E4M3() { return semFloat8E4M3; }
321 static constexpr const fltSemantics &Float8E4M3FN() {
322 return semFloat8E4M3FN;
323 }
324 static constexpr const fltSemantics &Float8E4M3FNUZ() {
325 return semFloat8E4M3FNUZ;
326 }
327 static constexpr const fltSemantics &Float8E4M3B11FNUZ() {
328 return semFloat8E4M3B11FNUZ;
329 }
330 static constexpr const fltSemantics &Float8E3M4() { return semFloat8E3M4; }
331 static constexpr const fltSemantics &FloatTF32() { return semFloatTF32; }
332 static constexpr const fltSemantics &Float8E8M0FNU() {
333 return semFloat8E8M0FNU;
334 }
335 static constexpr const fltSemantics &Float8E5M3FNU() {
336 return semFloat8E5M3FNU;
337 }
338 static constexpr const fltSemantics &Float6E3M2FN() {
339 return semFloat6E3M2FN;
340 }
341 static constexpr const fltSemantics &Float6E2M3FN() {
342 return semFloat6E2M3FN;
343 }
344 static constexpr const fltSemantics &Float4E2M1FN() {
345 return semFloat4E2M1FN;
346 }
347
348 /// A Pseudo fltsemantic used to construct APFloats that cannot conflict with
349 /// anything real.
350 static constexpr const fltSemantics &Bogus() { return semBogus; }
351
352 // Returns true if any number described by this semantics can be precisely
353 // represented by the specified semantics. Does not take into account
354 // the value of fltNonfiniteBehavior, hasZero, hasSignedRepr.
355 LLVM_ABI static bool isRepresentableBy(const fltSemantics &A,
356 const fltSemantics &B);
357
358 /// Returns whether converting a value from \p From to \p To is known to
359 /// preserve all information. If \p IgnoreNaNs is true, differences between
360 /// NaN representations are ignored, but NaNs must remain NaNs and infinities
361 /// must remain infinities.
362 LLVM_ABI static bool isLosslesslyConvertibleTo(const fltSemantics &From,
363 const fltSemantics &To,
364 bool IgnoreNaNs = false);
365
366 /// @}
367
368 /// IEEE-754R 5.11: Floating Point Comparison Relations.
375
376 /// IEEE-754R 4.3: Rounding-direction attributes.
378
386
387 /// IEEE-754R 7: Default exception handling.
388 ///
389 /// opUnderflow or opOverflow are always returned or-ed with opInexact.
390 ///
391 /// APFloat models this behavior specified by IEEE-754:
392 /// "For operations producing results in floating-point format, the default
393 /// result of an operation that signals the invalid operation exception
394 /// shall be a quiet NaN."
395 enum opStatus {
396 opOK = 0x00,
402 };
403
404 /// Category of internally-represented number.
411
412 /// Convenience enum used to construct an uninitialized APFloat.
416
417 /// Enumeration of \c ilogb error results.
419 IEK_Zero = INT_MIN + 1,
420 IEK_NaN = INT_MIN,
421 IEK_Inf = INT_MAX
422 };
423
424 LLVM_ABI static unsigned int semanticsPrecision(const fltSemantics &);
427 LLVM_ABI static unsigned int semanticsSizeInBits(const fltSemantics &);
428 LLVM_ABI static unsigned int semanticsIntSizeInBits(const fltSemantics &,
429 bool);
430 LLVM_ABI static bool semanticsHasZero(const fltSemantics &);
431 LLVM_ABI static bool semanticsHasSignedRepr(const fltSemantics &);
432 LLVM_ABI static bool semanticsHasInf(const fltSemantics &);
433 LLVM_ABI static bool semanticsHasNaN(const fltSemantics &);
434 LLVM_ABI static bool isIEEELikeFP(const fltSemantics &);
435 LLVM_ABI static bool hasSignBitInMSB(const fltSemantics &);
436
437 // Returns true if any number described by \p Src can be precisely represented
438 // by a normal (not subnormal) value in \p Dst.
439 LLVM_ABI static bool isRepresentableAsNormalIn(const fltSemantics &Src,
440 const fltSemantics &Dst);
441
442 /// Returns the size of the floating point number (in bits) in the given
443 /// semantics.
444 LLVM_ABI static unsigned getSizeInBits(const fltSemantics &Sem);
445
446 /// Returns true if the given string is a valid arbitrary floating-point
447 /// format interpretation for llvm.convert.to.arbitrary.fp and
448 /// llvm.convert.from.arbitrary.fp intrinsics.
450
451 /// Returns the size in bits of a valid arbitrary floating-point format
452 /// string, or 0 if the string is not a valid format. Covers every format
453 /// accepted by isValidArbitraryFPFormat, not only those
454 /// getArbitraryFPSemantics can currently lower.
456
457 /// Returns the fltSemantics for a given arbitrary FP format string,
458 /// or nullptr if invalid.
460};
461
462namespace detail {
463
484static constexpr opStatus opOK = APFloatBase::opOK;
494
495class IEEEFloat final {
496public:
497 /// \name Constructors
498 /// @{
499
500 LLVM_ABI IEEEFloat(const fltSemantics &); // Default construct to +0.0
503 LLVM_ABI IEEEFloat(const fltSemantics &, const APInt &);
504 LLVM_ABI explicit IEEEFloat(double d);
505 LLVM_ABI explicit IEEEFloat(float f);
509
510 /// @}
511
512 /// Returns whether this instance allocated memory.
513 bool needsCleanup() const { return partCount() > 1; }
514
515 /// \name Convenience "constructors"
516 /// @{
517
518 /// @}
519
520 /// \name Arithmetic
521 /// @{
522
527 /// IEEE remainder.
529 /// C fmod, or llvm frem.
534 /// IEEE-754R 5.3.1: nextUp/nextDown.
535 LLVM_ABI opStatus next(bool nextDown);
536
537 /// @}
538
539 /// \name Sign operations.
540 /// @{
541
542 LLVM_ABI void changeSign();
543
544 /// @}
545
546 /// \name Conversions
547 /// @{
548
551 bool, roundingMode, bool *) const;
555 LLVM_ABI double convertToDouble() const;
556#ifdef HAS_IEE754_FLOAT128
557 LLVM_ABI float128 convertToQuad() const;
558#endif
559 LLVM_ABI float convertToFloat() const;
560
561 /// @}
562
563 /// The definition of equality is not straightforward for floating point, so
564 /// we won't use operator==. Use one of the following, or write whatever it
565 /// is you really mean.
566 bool operator==(const IEEEFloat &) const = delete;
567
568 /// IEEE comparison with another floating point number (NaNs compare
569 /// unordered, 0==-0).
570 LLVM_ABI cmpResult compare(const IEEEFloat &) const;
571
572 /// Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
573 LLVM_ABI bool bitwiseIsEqual(const IEEEFloat &) const;
574
575 /// Write out a hexadecimal representation of the floating point value to DST,
576 /// which must be of sufficient size, in the C99 form [-]0xh.hhhhp[+-]d.
577 /// Return the number of characters written, excluding the terminating NUL.
578 LLVM_ABI unsigned int convertToHexString(char *dst, unsigned int hexDigits,
579 bool upperCase, roundingMode) const;
580
581 /// \name IEEE-754R 5.7.2 General operations.
582 /// @{
583
584 /// IEEE-754R isSignMinus: Returns true if and only if the current value is
585 /// negative.
586 ///
587 /// This applies to zeros and NaNs as well.
588 bool isNegative() const { return sign; }
589
590 /// IEEE-754R isNormal: Returns true if and only if the current value is normal.
591 ///
592 /// This implies that the current value of the float is not zero, subnormal,
593 /// infinite, or NaN following the definition of normality from IEEE-754R.
594 bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
595
596 /// Returns true if and only if the current value is zero, subnormal, or
597 /// normal.
598 ///
599 /// This means that the value is not infinite or NaN.
600 bool isFinite() const { return !isNaN() && !isInfinity(); }
601
602 /// Returns true if and only if the float is plus or minus zero.
603 bool isZero() const { return category == fltCategory::fcZero; }
604
605 /// IEEE-754R isSubnormal(): Returns true if and only if the float is a
606 /// denormal.
607 LLVM_ABI bool isDenormal() const;
608
609 /// IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
610 bool isInfinity() const { return category == fcInfinity; }
611
612 /// Returns true if and only if the float is a quiet or signaling NaN.
613 bool isNaN() const { return category == fcNaN; }
614
615 /// Returns true if and only if the float is a signaling NaN.
616 LLVM_ABI bool isSignaling() const;
617
618 /// @}
619
620 /// \name Simple Queries
621 /// @{
622
623 fltCategory getCategory() const { return category; }
624 const fltSemantics &getSemantics() const { return *semantics; }
625 bool isNonZero() const { return category != fltCategory::fcZero; }
626 bool isFiniteNonZero() const { return isFinite() && !isZero(); }
627 bool isPosZero() const { return isZero() && !isNegative(); }
628 bool isNegZero() const { return isZero() && isNegative(); }
629
630 /// Returns true if and only if the number has the smallest possible non-zero
631 /// magnitude in the current semantics.
632 LLVM_ABI bool isSmallest() const;
633
634 /// Returns true if this is the smallest (by magnitude) normalized finite
635 /// number in the given semantics.
636 LLVM_ABI bool isSmallestNormalized() const;
637
638 /// Returns true if and only if the number has the largest possible finite
639 /// magnitude in the current semantics.
640 LLVM_ABI bool isLargest() const;
641
642 /// Returns true if and only if the number is an exact integer.
643 LLVM_ABI bool isInteger() const;
644
645 /// @}
646
649
650 /// Overload to compute a hash code for an APFloat value.
651 ///
652 /// Note that the use of hash codes for floating point values is in general
653 /// frought with peril. Equality is hard to define for these values. For
654 /// example, should negative and positive zero hash to different codes? Are
655 /// they equal or not? This hash value implementation specifically
656 /// emphasizes producing different codes for different inputs in order to
657 /// be used in canonicalization and memoization. As such, equality is
658 /// bitwiseIsEqual, and 0 != -0.
659 LLVM_ABI friend hash_code hash_value(const IEEEFloat &Arg);
660
661 /// Converts this value into a decimal string.
662 ///
663 /// \param FormatPrecision The maximum number of digits of
664 /// precision to output. If there are fewer digits available,
665 /// zero padding will not be used unless the value is
666 /// integral and small enough to be expressed in
667 /// FormatPrecision digits. 0 means to use the natural
668 /// precision of the number.
669 /// \param FormatMaxPadding The maximum number of zeros to
670 /// consider inserting before falling back to scientific
671 /// notation. 0 means to always use scientific notation.
672 ///
673 /// \param TruncateZero Indicate whether to remove the trailing zero in
674 /// fraction part or not. Also setting this parameter to false forcing
675 /// producing of output more similar to default printf behavior.
676 /// Specifically the lower e is used as exponent delimiter and exponent
677 /// always contains no less than two digits.
678 ///
679 /// Number Precision MaxPadding Result
680 /// ------ --------- ---------- ------
681 /// 1.01E+4 5 2 10100
682 /// 1.01E+4 4 2 1.01E+4
683 /// 1.01E+4 5 1 1.01E+4
684 /// 1.01E-2 5 2 0.0101
685 /// 1.01E-2 4 2 0.0101
686 /// 1.01E-2 4 1 1.01E-2
688 unsigned FormatPrecision = 0,
689 unsigned FormatMaxPadding = 3,
690 bool TruncateZero = true) const;
691
693
694 LLVM_ABI friend int ilogb(const IEEEFloat &Arg);
695
697
698 LLVM_ABI friend IEEEFloat frexp(const IEEEFloat &X, int &Exp, roundingMode);
699
700 /// \name Special value setters.
701 /// @{
702
703 LLVM_ABI void makeLargest(bool Neg = false);
704 LLVM_ABI void makeSmallest(bool Neg = false);
705 LLVM_ABI void makeNaN(bool SNaN = false, bool Neg = false,
706 const APInt *fill = nullptr);
707 LLVM_ABI void makeInf(bool Neg = false);
708 LLVM_ABI void makeZero(bool Neg = false);
709 LLVM_ABI void makeQuiet();
710
711 /// Returns the smallest (by magnitude) normalized finite number in the given
712 /// semantics.
713 ///
714 /// \param Negative - True iff the number should be negative
715 LLVM_ABI void makeSmallestNormalized(bool Negative = false);
716
717 /// @}
718
720
722
723private:
724 /// \name Simple Queries
725 /// @{
726
727 integerPart *significandParts();
728 const integerPart *significandParts() const;
729 LLVM_ABI unsigned int partCount() const;
730 static unsigned int partCountForSemantic(APFloatBase::Semantics E);
731
732 /// @}
733
734 /// \name Significand operations.
735 /// @{
736
737 integerPart addSignificand(const IEEEFloat &);
738 integerPart subtractSignificand(const IEEEFloat &, integerPart);
739 // Exported for IEEEFloatUnitTestHelper.
740 LLVM_ABI lostFraction addOrSubtractSignificand(const IEEEFloat &,
741 bool subtract);
742 lostFraction multiplySignificand(const IEEEFloat &, IEEEFloat,
743 bool ignoreAddend = false);
744 lostFraction multiplySignificand(const IEEEFloat&);
745 lostFraction divideSignificand(const IEEEFloat &);
746 void incrementSignificand();
747 void initialize(const fltSemantics *);
748 void shiftSignificandLeft(unsigned int);
749 lostFraction shiftSignificandRight(unsigned int);
750 unsigned int significandLSB() const;
751 unsigned int significandMSB() const;
752 void zeroSignificand();
753 unsigned int getNumHighBits() const;
754 /// Return true if the significand excluding the integral bit is all ones.
755 bool isSignificandAllOnes() const;
756 bool isSignificandAllOnesExceptLSB() const;
757 /// Return true if the significand excluding the integral bit is all zeros.
758 bool isSignificandAllZeros() const;
759 bool isSignificandAllZerosExceptMSB() const;
760
761 /// @}
762
763 /// \name Arithmetic on special values.
764 /// @{
765
766 opStatus addOrSubtractSpecials(const IEEEFloat &, bool subtract);
767 opStatus divideSpecials(const IEEEFloat &);
768 opStatus multiplySpecials(const IEEEFloat &);
769 opStatus modSpecials(const IEEEFloat &);
770 opStatus remainderSpecials(const IEEEFloat&);
771
772 /// @}
773
774 /// \name Miscellany
775 /// @{
776
777 bool convertFromStringSpecials(StringRef str);
779 opStatus addOrSubtract(const IEEEFloat &, roundingMode, bool subtract);
780 opStatus handleOverflow(roundingMode);
781 bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
782 opStatus convertToSignExtendedInteger(MutableArrayRef<integerPart>,
783 unsigned int, bool, roundingMode,
784 bool *) const;
785 opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
787 Expected<opStatus> convertFromHexadecimalString(StringRef, roundingMode);
788 Expected<opStatus> convertFromDecimalString(StringRef, roundingMode);
789 char *convertNormalToHexString(char *, unsigned int, bool,
790 roundingMode) const;
791 opStatus roundSignificandWithExponent(const integerPart *, unsigned int, int,
796
797 /// @}
798
799 static IEEEFloat &fromRepr(FloatRepr R);
800
801 template <APFloatBase::Semantics S>
803
804 template <APFloatBase::Semantics E> APInt convertIEEEFloatToAPInt() const;
805
806 template <APFloatBase::Semantics S>
807 friend void initFromAPIntForSemantics(FloatRepr, const APInt &);
808
809 void initFromAPInt(const fltSemantics *Sem, const APInt &api);
810 template <APFloatBase::Semantics E> void initFromIEEEAPInt(const APInt &api);
811
812 void assign(const IEEEFloat &);
813 void copySignificand(const IEEEFloat &);
814 void freeSignificand();
815
816 /// Note: this must be the first data member.
817 /// The semantics that this value obeys.
818 const fltSemantics *semantics;
819
820 /// A binary fraction with an explicit integer bit.
821 ///
822 /// The significand must be at least one bit wider than the target precision.
823 union Significand {
824 integerPart part;
825 integerPart *parts;
826 } significand;
827
828 /// The signed unbiased exponent of the value.
829 ExponentType exponent;
830
831 /// What kind of floating point number this is.
832 ///
833 /// Only 2 bits are required, but VisualStudio incorrectly sign extends it.
834 /// Using the extra bit keeps it from failing under VisualStudio.
835 fltCategory category : 3;
836
837 /// Sign bit of the number.
838 unsigned int sign : 1;
839
841 friend class llvm::FloatRepr;
842};
843
845LLVM_ABI int ilogb(const IEEEFloat &Arg);
847LLVM_ABI IEEEFloat frexp(const IEEEFloat &Val, int &Exp, roundingMode RM);
848
849// This mode implements more precise float in terms of two APFloats.
850// The interface and layout is designed for arbitrary underlying semantics,
851// though currently only PPCDoubleDouble semantics are supported, whose
852// corresponding underlying semantics are IEEEdouble.
853class DoubleAPFloat final {
854 // Note: this must be the first data member.
855 const fltSemantics *Semantics;
856 APFloat *Floats;
857
858 opStatus addImpl(const APFloat &a, const APFloat &aa, const APFloat &c,
859 const APFloat &cc, roundingMode RM);
860
861 opStatus addWithSpecial(const DoubleAPFloat &LHS, const DoubleAPFloat &RHS,
862 DoubleAPFloat &Out, roundingMode RM);
863 opStatus convertToSignExtendedInteger(MutableArrayRef<integerPart> Input,
864 unsigned int Width, bool IsSigned,
865 roundingMode RM, bool *IsExact) const;
866
867 // Convert an unsigned integer Src to a floating point number,
868 // rounding according to RM. The sign of the floating point number is not
869 // modified.
870 opStatus convertFromUnsignedParts(const integerPart *Src,
871 unsigned int SrcCount, roundingMode RM);
872
873 // Handle overflow. Sign is preserved. We either become infinity or
874 // the largest finite number.
875 opStatus handleOverflow(roundingMode RM);
876
877public:
881 LLVM_ABI DoubleAPFloat(const fltSemantics &S, const APInt &I);
883 APFloat &&Second);
887
890
891 bool needsCleanup() const { return Floats != nullptr; }
892
893 inline APFloat &getFirst();
894 inline const APFloat &getFirst() const;
895 inline APFloat &getSecond();
896 inline const APFloat &getSecond() const;
897
905 const DoubleAPFloat &Addend,
906 roundingMode RM);
908 LLVM_ABI void changeSign();
910
912 LLVM_ABI bool isNegative() const;
913
914 LLVM_ABI void makeInf(bool Neg);
915 LLVM_ABI void makeZero(bool Neg);
916 LLVM_ABI void makeLargest(bool Neg);
917 LLVM_ABI void makeSmallest(bool Neg);
918 LLVM_ABI void makeSmallestNormalized(bool Neg);
919 LLVM_ABI void makeNaN(bool SNaN, bool Neg, const APInt *fill);
920
922 LLVM_ABI bool bitwiseIsEqual(const DoubleAPFloat &RHS) const;
925 LLVM_ABI opStatus next(bool nextDown);
926
928 unsigned int Width, bool IsSigned,
929 roundingMode RM, bool *IsExact) const;
930 LLVM_ABI opStatus convertFromAPInt(const APInt &Input, bool IsSigned,
931 roundingMode RM);
932 LLVM_ABI unsigned int convertToHexString(char *DST, unsigned int HexDigits,
933 bool UpperCase,
934 roundingMode RM) const;
935
936 LLVM_ABI bool isDenormal() const;
937 LLVM_ABI bool isSmallest() const;
938 LLVM_ABI bool isSmallestNormalized() const;
939 LLVM_ABI bool isLargest() const;
940 LLVM_ABI bool isInteger() const;
941
943
944 LLVM_ABI void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision,
945 unsigned FormatMaxPadding,
946 bool TruncateZero = true) const;
947
949
950 LLVM_ABI friend int ilogb(const DoubleAPFloat &X);
953 LLVM_ABI friend DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp,
955 LLVM_ABI friend hash_code hash_value(const DoubleAPFloat &Arg);
956};
957
959LLVM_ABI DoubleAPFloat scalbn(const DoubleAPFloat &Arg, int Exp,
960 roundingMode RM);
962
963} // End detail namespace
964
965// How the nonfinite values Inf and NaN are represented.
967 // Represents standard IEEE 754 behavior. A value is nonfinite if the
968 // exponent field is all 1s. In such cases, a value is Inf if the
969 // significand bits are all zero, and NaN otherwise
971
972 // This behavior is present in the Float8ExMyFN* types (Float8E4M3FN,
973 // Float8E5M2FNUZ, Float8E4M3FNUZ, and Float8E4M3B11FNUZ). There is no
974 // representation for Inf, and operations that would ordinarily produce Inf
975 // produce NaN instead.
976 // The details of the NaN representation(s) in this form are determined by the
977 // `fltNanEncoding` enum. We treat all NaNs as quiet, as the available
978 // encodings do not distinguish between signalling and quiet NaN.
980
981 // This behavior is present in Float6E3M2FN, Float6E2M3FN, and
982 // Float4E2M1FN types, which do not support Inf or NaN values.
984};
985
986// How NaN values are represented. This is curently only used in combination
987// with fltNonfiniteBehavior::NanOnly, and using a variant other than IEEE
988// while having IEEE non-finite behavior is liable to lead to unexpected
989// results.
990enum class fltNanEncoding {
991 // Represents the standard IEEE behavior where a value is NaN if its
992 // exponent is all 1s and the significand is non-zero.
994
995 // Represents the behavior in the Float8E4M3FN floating point type where NaN
996 // is represented by having the exponent and mantissa set to all 1s.
997 // This behavior matches the FP8 E4M3 type described in
998 // https://arxiv.org/abs/2209.05433. We treat both signed and unsigned NaNs
999 // as non-signalling, although the paper does not state whether the NaN
1000 // values are signalling or not.
1002
1003 // Represents the behavior in Float8E{5,4}E{2,3}FNUZ floating point types
1004 // where NaN is represented by a sign bit of 1 and all 0s in the exponent
1005 // and mantissa (i.e. the negative zero encoding in a IEEE float). Since
1006 // there is only one NaN value, it is treated as quiet NaN. This matches the
1007 // behavior described in https://arxiv.org/abs/2206.02915 .
1009};
1010
1011/// Interface for fltSemantics callbacks to access the internal data of
1012/// APFloat/IEEEFloat.
1013///
1014/// Used to let user provided callback functions in fltSemantics access the
1015/// internals of APFloat without making them public and to hide the concrete
1016/// class used in APFloat
1017class FloatRepr {
1018 detail::IEEEFloat *Repr;
1019
1020 friend class detail::IEEEFloat;
1021 explicit FloatRepr(detail::IEEEFloat &F) : Repr(&F) {}
1022
1023public:
1024 /// Set the semantics and allocate storage for the significand. Must be
1025 /// called exactly once before any setter when initializing from bits.
1026 LLVM_ABI void initialize(const fltSemantics &Sem);
1027
1028 LLVM_ABI const fltSemantics &getSemantics() const;
1029
1031 /// Exponent and significand are only meaningful for fcNormal and fcNaN.
1033
1034 LLVM_ABI bool isNegative() const;
1035 LLVM_ABI void setNegative(bool Negative);
1036
1038 /// - The exponent is unbiased. Denormals use the semantics' minExponent and
1039 /// have the integer bit cleared.
1041
1043 /// The significand is precision bits wide and includes the integer bit.
1044 LLVM_ABI void setSignificand(const APInt &Significand);
1045};
1046
1047/* Represents floating point arithmetic semantics. */
1049 /* The largest E such that 2^E is representable; this matches the
1050 definition of IEEE 754. */
1052
1053 /* The smallest E such that 2^E is a normalized number; this
1054 matches the definition of IEEE 754. */
1056
1057 /* Number of bits in the significand. This includes the integer
1058 bit. */
1059 unsigned int precision;
1060
1061 /* Number of bits actually used in the semantics. */
1062 unsigned int sizeInBits;
1063
1065
1067
1068 /* Whether this semantics has an encoding for Zero */
1069 bool hasZero = true;
1070
1071 /* Whether this semantics can represent signed values */
1072 bool hasSignedRepr = true;
1073
1074 /* Whether the sign bit of this semantics is the most significant bit */
1075 bool hasSignBitInMSB = true;
1076
1077 /* Whether the format supports IEEE754 denormal representation.
1078 If both hasDenormals and hasZero are false exponent 0 is assumed to be a
1079 regular exponent instead of being reserved. This changes the bias by +1. */
1080 bool hasDenormals = true;
1081
1082 /* Whether the integer bit is explicitly represented between significant and
1083 exponent, for example as specified by the x86 double extended precision
1084 format.
1085
1086 For bit patterns designated as undefined under the standard the following
1087 conversions will happen when converting from bits. These follow x87
1088 behaviour:
1089 - exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
1090 - exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN")
1091 - exponent!=0 nor all 1's, integer bit 0 ("unnormal")
1092 - exponent = 0, integer bit 1 ("pseudodenormal")
1093 The first three are treated as NaNs, the last one as Normal */
1095
1096 /* Decode the sizeInBits wide bit pattern into the given FloatRepr. Must call
1097 FloatRepr::initialize with this semantics before using any setter. */
1098 void (*initFromAPInt)(FloatRepr, const APInt &) = nullptr;
1099
1100 /* Encode the value of the given FloatRepr as a sizeInBits wide bit pattern
1101 as it would appear in memory. */
1103};
1104
1105// This is a interface class that is currently forwarding functionalities from
1106// detail::IEEEFloat.
1107class APFloat : public APFloatBase {
1108 using IEEEFloat = detail::IEEEFloat;
1109 using DoubleAPFloat = detail::DoubleAPFloat;
1110
1111 static_assert(std::is_standard_layout<IEEEFloat>::value);
1112
1113 union Storage {
1114 const fltSemantics *semantics;
1115 IEEEFloat IEEE;
1116 DoubleAPFloat Double;
1117
1118 LLVM_ABI explicit Storage(IEEEFloat F, const fltSemantics &S);
1119 explicit Storage(DoubleAPFloat F, const fltSemantics &S)
1120 : Double(std::move(F)) {
1121 assert(&S == &PPCDoubleDouble());
1122 }
1123
1124 template <typename... ArgTypes>
1125 Storage(const fltSemantics &Semantics, ArgTypes &&... Args) {
1126 if (usesLayout<IEEEFloat>(Semantics)) {
1127 new (&IEEE) IEEEFloat(Semantics, std::forward<ArgTypes>(Args)...);
1128 return;
1129 }
1130 if (usesLayout<DoubleAPFloat>(Semantics)) {
1131 new (&Double) DoubleAPFloat(Semantics, std::forward<ArgTypes>(Args)...);
1132 return;
1133 }
1134 llvm_unreachable("Unexpected semantics");
1135 }
1136
1137 LLVM_ABI ~Storage();
1138 LLVM_ABI Storage(const Storage &RHS);
1139 LLVM_ABI Storage(Storage &&RHS);
1140 LLVM_ABI Storage &operator=(const Storage &RHS);
1141 LLVM_ABI Storage &operator=(Storage &&RHS);
1142 } U;
1143
1144 template <typename T> static bool usesLayout(const fltSemantics &Semantics) {
1145 static_assert(std::is_same<T, IEEEFloat>::value ||
1146 std::is_same<T, DoubleAPFloat>::value);
1147 if (std::is_same<T, DoubleAPFloat>::value) {
1148 return &Semantics == &PPCDoubleDouble();
1149 }
1150 return &Semantics != &PPCDoubleDouble();
1151 }
1152
1153 IEEEFloat &getIEEE() {
1154 if (usesLayout<IEEEFloat>(*U.semantics))
1155 return U.IEEE;
1156 if (usesLayout<DoubleAPFloat>(*U.semantics))
1157 return U.Double.getFirst().U.IEEE;
1158 llvm_unreachable("Unexpected semantics");
1159 }
1160
1161 const IEEEFloat &getIEEE() const {
1162 if (usesLayout<IEEEFloat>(*U.semantics))
1163 return U.IEEE;
1164 if (usesLayout<DoubleAPFloat>(*U.semantics))
1165 return U.Double.getFirst().U.IEEE;
1166 llvm_unreachable("Unexpected semantics");
1167 }
1168
1169 void makeZero(bool Neg) { APFLOAT_DISPATCH_ON_SEMANTICS(makeZero(Neg)); }
1170
1171 void makeInf(bool Neg) { APFLOAT_DISPATCH_ON_SEMANTICS(makeInf(Neg)); }
1172
1173 void makeNaN(bool SNaN, bool Neg, const APInt *fill) {
1174 APFLOAT_DISPATCH_ON_SEMANTICS(makeNaN(SNaN, Neg, fill));
1175 }
1176
1177 void makeLargest(bool Neg) {
1178 APFLOAT_DISPATCH_ON_SEMANTICS(makeLargest(Neg));
1179 }
1180
1181 void makeSmallest(bool Neg) {
1182 APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallest(Neg));
1183 }
1184
1185 void makeSmallestNormalized(bool Neg) {
1186 APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallestNormalized(Neg));
1187 }
1188
1189 explicit APFloat(IEEEFloat F, const fltSemantics &S) : U(std::move(F), S) {}
1190 explicit APFloat(DoubleAPFloat F, const fltSemantics &S)
1191 : U(std::move(F), S) {}
1192
1193public:
1197 template <typename T,
1198 typename = std::enable_if_t<std::is_floating_point<T>::value>>
1199 APFloat(const fltSemantics &Semantics, T V) = delete;
1200 // TODO: Remove this constructor. This isn't faster than the first one.
1204 explicit APFloat(double d) : U(IEEEFloat(d), IEEEdouble()) {}
1205 explicit APFloat(float f) : U(IEEEFloat(f), IEEEsingle()) {}
1206 APFloat(const APFloat &RHS) = default;
1207 APFloat(APFloat &&RHS) = default;
1208
1209 ~APFloat() = default;
1210
1212
1213 /// Factory for Positive and Negative Zero.
1214 ///
1215 /// \param Negative True iff the number should be negative.
1216 static APFloat getZero(const fltSemantics &Sem, bool Negative = false) {
1217 APFloat Val(Sem, uninitialized);
1218 Val.makeZero(Negative);
1219 return Val;
1220 }
1221
1222 /// Factory for Positive and Negative One.
1223 ///
1224 /// \param Negative True iff the number should be negative.
1225 static APFloat getOne(const fltSemantics &Sem, bool Negative = false) {
1226 APFloat Val(Sem, 1U);
1227 if (Negative)
1228 Val.changeSign();
1229 return Val;
1230 }
1231
1232 /// Factory for Positive and Negative Infinity.
1233 ///
1234 /// \param Negative True iff the number should be negative.
1235 static APFloat getInf(const fltSemantics &Sem, bool Negative = false) {
1236 APFloat Val(Sem, uninitialized);
1237 Val.makeInf(Negative);
1238 return Val;
1239 }
1240
1241 /// Factory for NaN values.
1242 ///
1243 /// \param Negative - True iff the NaN generated should be negative.
1244 /// \param payload - The unspecified fill bits for creating the NaN, 0 by
1245 /// default. The value is truncated as necessary.
1246 static APFloat getNaN(const fltSemantics &Sem, bool Negative = false,
1247 uint64_t payload = 0) {
1248 if (payload) {
1249 APInt intPayload(64, payload);
1250 return getQNaN(Sem, Negative, &intPayload);
1251 } else {
1252 return getQNaN(Sem, Negative, nullptr);
1253 }
1254 }
1255
1256 /// Factory for QNaN values.
1257 static APFloat getQNaN(const fltSemantics &Sem, bool Negative = false,
1258 const APInt *payload = nullptr) {
1259 APFloat Val(Sem, uninitialized);
1260 Val.makeNaN(false, Negative, payload);
1261 return Val;
1262 }
1263
1264 /// Factory for SNaN values.
1265 static APFloat getSNaN(const fltSemantics &Sem, bool Negative = false,
1266 const APInt *payload = nullptr) {
1267 APFloat Val(Sem, uninitialized);
1268 Val.makeNaN(true, Negative, payload);
1269 return Val;
1270 }
1271
1272 /// Returns the largest finite number in the given semantics.
1273 ///
1274 /// \param Negative - True iff the number should be negative
1275 static APFloat getLargest(const fltSemantics &Sem, bool Negative = false) {
1276 APFloat Val(Sem, uninitialized);
1277 Val.makeLargest(Negative);
1278 return Val;
1279 }
1280
1281 /// Returns the smallest (by magnitude) finite number in the given semantics.
1282 /// Might be denormalized, which implies a relative loss of precision.
1283 ///
1284 /// \param Negative - True iff the number should be negative
1285 static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false) {
1286 APFloat Val(Sem, uninitialized);
1287 Val.makeSmallest(Negative);
1288 return Val;
1289 }
1290
1291 /// Returns the smallest (by magnitude) normalized finite number in the given
1292 /// semantics.
1293 ///
1294 /// \param Negative - True iff the number should be negative
1295 static APFloat getSmallestNormalized(const fltSemantics &Sem,
1296 bool Negative = false) {
1297 APFloat Val(Sem, uninitialized);
1298 Val.makeSmallestNormalized(Negative);
1299 return Val;
1300 }
1301
1302 /// Returns a float which is bitcasted from an all one value int.
1303 ///
1304 /// \param Semantics - type float semantics
1306
1307 /// Returns true if the given semantics has actual significand.
1308 ///
1309 /// \param Sem - type float semantics
1310 static bool hasSignificand(const fltSemantics &Sem) {
1311 return &Sem != &Float8E8M0FNU();
1312 }
1313
1314 /// Used to insert APFloat objects, or objects that contain APFloat objects,
1315 /// into FoldingSets.
1316 LLVM_ABI void Profile(FoldingSetNodeID &NID) const;
1317
1318 opStatus add(const APFloat &RHS, roundingMode RM) {
1319 assert(&getSemantics() == &RHS.getSemantics() &&
1320 "Should only call on two APFloats with the same semantics");
1321 if (usesLayout<IEEEFloat>(getSemantics()))
1322 return U.IEEE.add(RHS.U.IEEE, RM);
1323 if (usesLayout<DoubleAPFloat>(getSemantics()))
1324 return U.Double.add(RHS.U.Double, RM);
1325 llvm_unreachable("Unexpected semantics");
1326 }
1327 opStatus subtract(const APFloat &RHS, roundingMode RM) {
1328 assert(&getSemantics() == &RHS.getSemantics() &&
1329 "Should only call on two APFloats with the same semantics");
1330 if (usesLayout<IEEEFloat>(getSemantics()))
1331 return U.IEEE.subtract(RHS.U.IEEE, RM);
1332 if (usesLayout<DoubleAPFloat>(getSemantics()))
1333 return U.Double.subtract(RHS.U.Double, RM);
1334 llvm_unreachable("Unexpected semantics");
1335 }
1336 opStatus multiply(const APFloat &RHS, roundingMode RM) {
1337 assert(&getSemantics() == &RHS.getSemantics() &&
1338 "Should only call on two APFloats with the same semantics");
1339 if (usesLayout<IEEEFloat>(getSemantics()))
1340 return U.IEEE.multiply(RHS.U.IEEE, RM);
1341 if (usesLayout<DoubleAPFloat>(getSemantics()))
1342 return U.Double.multiply(RHS.U.Double, RM);
1343 llvm_unreachable("Unexpected semantics");
1344 }
1345 opStatus divide(const APFloat &RHS, roundingMode RM) {
1346 assert(&getSemantics() == &RHS.getSemantics() &&
1347 "Should only call on two APFloats with the same semantics");
1348 if (usesLayout<IEEEFloat>(getSemantics()))
1349 return U.IEEE.divide(RHS.U.IEEE, RM);
1350 if (usesLayout<DoubleAPFloat>(getSemantics()))
1351 return U.Double.divide(RHS.U.Double, RM);
1352 llvm_unreachable("Unexpected semantics");
1353 }
1354 opStatus remainder(const APFloat &RHS) {
1355 assert(&getSemantics() == &RHS.getSemantics() &&
1356 "Should only call on two APFloats with the same semantics");
1357 if (usesLayout<IEEEFloat>(getSemantics()))
1358 return U.IEEE.remainder(RHS.U.IEEE);
1359 if (usesLayout<DoubleAPFloat>(getSemantics()))
1360 return U.Double.remainder(RHS.U.Double);
1361 llvm_unreachable("Unexpected semantics");
1362 }
1363 opStatus mod(const APFloat &RHS) {
1364 assert(&getSemantics() == &RHS.getSemantics() &&
1365 "Should only call on two APFloats with the same semantics");
1366 if (usesLayout<IEEEFloat>(getSemantics()))
1367 return U.IEEE.mod(RHS.U.IEEE);
1368 if (usesLayout<DoubleAPFloat>(getSemantics()))
1369 return U.Double.mod(RHS.U.Double);
1370 llvm_unreachable("Unexpected semantics");
1371 }
1372 opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend,
1373 roundingMode RM) {
1374 assert(&getSemantics() == &Multiplicand.getSemantics() &&
1375 "Should only call on APFloats with the same semantics");
1376 assert(&getSemantics() == &Addend.getSemantics() &&
1377 "Should only call on APFloats with the same semantics");
1378 if (usesLayout<IEEEFloat>(getSemantics()))
1379 return U.IEEE.fusedMultiplyAdd(Multiplicand.U.IEEE, Addend.U.IEEE, RM);
1380 if (usesLayout<DoubleAPFloat>(getSemantics()))
1381 return U.Double.fusedMultiplyAdd(Multiplicand.U.Double, Addend.U.Double,
1382 RM);
1383 llvm_unreachable("Unexpected semantics");
1384 }
1388
1389 // TODO: bool parameters are not readable and a source of bugs.
1390 // Do something.
1391 opStatus next(bool nextDown) {
1393 }
1394
1395 /// Negate an APFloat.
1396 APFloat operator-() const {
1397 APFloat Result(*this);
1398 Result.changeSign();
1399 return Result;
1400 }
1401
1402 /// Add two APFloats, rounding ties to the nearest even.
1403 /// No error checking.
1404 APFloat operator+(const APFloat &RHS) const {
1405 APFloat Result(*this);
1406 (void)Result.add(RHS, rmNearestTiesToEven);
1407 return Result;
1408 }
1409
1410 /// Subtract two APFloats, rounding ties to the nearest even.
1411 /// No error checking.
1412 APFloat operator-(const APFloat &RHS) const {
1413 APFloat Result(*this);
1414 (void)Result.subtract(RHS, rmNearestTiesToEven);
1415 return Result;
1416 }
1417
1418 /// Multiply two APFloats, rounding ties to the nearest even.
1419 /// No error checking.
1420 APFloat operator*(const APFloat &RHS) const {
1421 APFloat Result(*this);
1422 (void)Result.multiply(RHS, rmNearestTiesToEven);
1423 return Result;
1424 }
1425
1426 /// Divide the first APFloat by the second, rounding ties to the nearest even.
1427 /// No error checking.
1428 APFloat operator/(const APFloat &RHS) const {
1429 APFloat Result(*this);
1430 (void)Result.divide(RHS, rmNearestTiesToEven);
1431 return Result;
1432 }
1433
1435 void clearSign() {
1436 if (isNegative())
1437 changeSign();
1438 }
1439 void copySign(const APFloat &RHS) {
1440 if (isNegative() != RHS.isNegative())
1441 changeSign();
1442 }
1443
1444 /// A static helper to produce a copy of an APFloat value with its sign
1445 /// copied from some other APFloat.
1446 static APFloat copySign(APFloat Value, const APFloat &Sign) {
1447 Value.copySign(Sign);
1448 return Value;
1449 }
1450
1451 /// Assuming this is an IEEE-754 NaN value, quiet its signaling bit.
1452 /// This preserves the sign and payload bits.
1453 [[nodiscard]] APFloat makeQuiet() const {
1454 APFloat Result(*this);
1455 Result.getIEEE().makeQuiet();
1456 return Result;
1457 }
1458
1459 LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM,
1460 bool *losesInfo);
1461 // Convert a floating point number to an integer according to the
1462 // rounding mode. We provide deterministic values in case of an invalid
1463 // operation exception, namely zero for NaNs and the minimal or maximal value
1464 // respectively for underflow or overflow.
1465 // The *IsExact output tells whether the result is exact, in the sense that
1466 // converting it back to the original floating point type produces the
1467 // original value. This is almost equivalent to result==opOK, except for
1468 // negative zeroes.
1470 unsigned int Width, bool IsSigned, roundingMode RM,
1471 bool *IsExact) const {
1473 convertToInteger(Input, Width, IsSigned, RM, IsExact));
1474 }
1475 // Same as convertToInteger(integerPart*, ...), except the result is returned
1476 // in an APSInt, whose initial bit-width and signed-ness are used to determine
1477 // the precision of the conversion.
1478 LLVM_ABI opStatus convertToInteger(APSInt &Result, roundingMode RM,
1479 bool *IsExact) const;
1480
1481 // Convert a two's complement integer Input to a floating point number,
1482 // rounding according to RM. IsSigned is true if the integer is signed,
1483 // in which case it must be sign-extended.
1484 opStatus convertFromAPInt(const APInt &Input, bool IsSigned,
1485 roundingMode RM) {
1487 }
1488
1489 /// Fill this APFloat with the result of a string conversion.
1490 ///
1491 /// The following strings are accepted for conversion purposes:
1492 /// * Decimal floating-point literals (e.g., `0.1e-5`)
1493 /// * Hexadecimal floating-point literals (e.g., `0x1.0p-5`)
1494 /// * Positive infinity via "inf", "INFINITY", "Inf", "+Inf", or "+inf".
1495 /// * Negative infinity via "-inf", "-INFINITY", or "-Inf".
1496 /// * Quiet NaNs via "nan", "NaN", "nan(...)", or "NaN(...)", where the
1497 /// "..." is either a decimal or hexadecimal integer representing the
1498 /// payload. A negative sign may be optionally provided.
1499 /// * Signaling NaNs via "snan", "sNaN", "snan(...)", or "sNaN(...)", where
1500 /// the "..." is either a decimal or hexadecimal integer representing the
1501 /// payload. A negative sign may be optionally provided.
1502 ///
1503 /// If the input string is none of these forms, then an error is returned.
1504 ///
1505 /// If a floating-point exception occurs during conversion, then no error is
1506 /// returned, and the exception is indicated via opStatus.
1511
1512 /// Converts this APFloat to host double value.
1513 ///
1514 /// \pre The APFloat must be built using semantics, that can be represented by
1515 /// the host double type without loss of precision. It can be IEEEdouble and
1516 /// shorter semantics, like IEEEsingle and others.
1517 LLVM_ABI double convertToDouble() const;
1518
1519 /// Converts this APFloat to host float value.
1520 ///
1521 /// \pre The APFloat must be built using semantics, that can be represented by
1522 /// the host float type without loss of precision. It can be IEEEquad and
1523 /// shorter semantics, like IEEEdouble and others.
1524#ifdef HAS_IEE754_FLOAT128
1525 LLVM_ABI float128 convertToQuad() const;
1526#endif
1527
1528 /// Converts this APFloat to host float value.
1529 ///
1530 /// \pre The APFloat must be built using semantics, that can be represented by
1531 /// the host float type without loss of precision. It can be IEEEsingle and
1532 /// shorter semantics, like IEEEhalf.
1533 LLVM_ABI float convertToFloat() const;
1534
1535 bool operator==(const APFloat &RHS) const { return compare(RHS) == cmpEqual; }
1536
1537 bool operator!=(const APFloat &RHS) const { return compare(RHS) != cmpEqual; }
1538
1539 bool operator<(const APFloat &RHS) const {
1540 return compare(RHS) == cmpLessThan;
1541 }
1542
1543 bool operator>(const APFloat &RHS) const {
1544 return compare(RHS) == cmpGreaterThan;
1545 }
1546
1547 bool operator<=(const APFloat &RHS) const {
1548 cmpResult Res = compare(RHS);
1549 return Res == cmpLessThan || Res == cmpEqual;
1550 }
1551
1552 bool operator>=(const APFloat &RHS) const {
1553 cmpResult Res = compare(RHS);
1554 return Res == cmpGreaterThan || Res == cmpEqual;
1555 }
1556
1557 // IEEE comparison with another floating point number (NaNs compare unordered,
1558 // 0==-0).
1559 cmpResult compare(const APFloat &RHS) const {
1560 assert(&getSemantics() == &RHS.getSemantics() &&
1561 "Should only compare APFloats with the same semantics");
1562 if (usesLayout<IEEEFloat>(getSemantics()))
1563 return U.IEEE.compare(RHS.U.IEEE);
1564 if (usesLayout<DoubleAPFloat>(getSemantics()))
1565 return U.Double.compare(RHS.U.Double);
1566 llvm_unreachable("Unexpected semantics");
1567 }
1568
1569 // Compares the absolute value of this APFloat with another. Both operands
1570 // must be finite non-zero.
1571 cmpResult compareAbsoluteValue(const APFloat &RHS) const {
1572 assert(&getSemantics() == &RHS.getSemantics() &&
1573 "Should only compare APFloats with the same semantics");
1574 if (usesLayout<IEEEFloat>(getSemantics()))
1575 return U.IEEE.compareAbsoluteValue(RHS.U.IEEE);
1576 if (usesLayout<DoubleAPFloat>(getSemantics()))
1577 return U.Double.compareAbsoluteValue(RHS.U.Double);
1578 llvm_unreachable("Unexpected semantics");
1579 }
1580
1581 bool bitwiseIsEqual(const APFloat &RHS) const {
1582 if (&getSemantics() != &RHS.getSemantics())
1583 return false;
1584 if (usesLayout<IEEEFloat>(getSemantics()))
1585 return U.IEEE.bitwiseIsEqual(RHS.U.IEEE);
1586 if (usesLayout<DoubleAPFloat>(getSemantics()))
1587 return U.Double.bitwiseIsEqual(RHS.U.Double);
1588 llvm_unreachable("Unexpected semantics");
1589 }
1590
1591 /// We don't rely on operator== working on double values, as
1592 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1593 /// As such, this method can be used to do an exact bit-for-bit comparison of
1594 /// two floating point values.
1595 ///
1596 /// We leave the version with the double argument here because it's just so
1597 /// convenient to write "2.0" and the like. Without this function we'd
1598 /// have to duplicate its logic everywhere it's called.
1599 bool isExactlyValue(double V) const {
1600 bool ignored;
1601 APFloat Tmp(V);
1603 return bitwiseIsEqual(Tmp);
1604 }
1605
1606 unsigned int convertToHexString(char *DST, unsigned int HexDigits,
1607 bool UpperCase, roundingMode RM) const {
1609 convertToHexString(DST, HexDigits, UpperCase, RM));
1610 }
1611
1612 bool isZero() const { return getCategory() == fcZero; }
1613 bool isInfinity() const { return getCategory() == fcInfinity; }
1614 bool isNaN() const { return getCategory() == fcNaN; }
1615
1616 bool isNegative() const { return getIEEE().isNegative(); }
1618 bool isSignaling() const { return getIEEE().isSignaling(); }
1619
1620 bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
1621 bool isFinite() const { return !isNaN() && !isInfinity(); }
1622
1623 fltCategory getCategory() const { return getIEEE().getCategory(); }
1624 const fltSemantics &getSemantics() const { return *U.semantics; }
1625 bool isNonZero() const { return !isZero(); }
1626 bool isFiniteNonZero() const { return isFinite() && !isZero(); }
1627 bool isPosZero() const { return isZero() && !isNegative(); }
1628 bool isNegZero() const { return isZero() && isNegative(); }
1629 bool isPosInfinity() const { return isInfinity() && !isNegative(); }
1630 bool isNegInfinity() const { return isInfinity() && isNegative(); }
1634
1638
1639 /// If the value is a NaN value, return an integer containing the payload of
1640 /// this value. This payload will include the quiet bit as part of the
1641 /// returned integer.
1643 assert(isNaN() && "Can only call this on a NaN value");
1645 }
1646
1647 /// Return the FPClassTest which will return true for the value.
1649
1650 APFloat &operator=(const APFloat &RHS) = default;
1651 APFloat &operator=(APFloat &&RHS) = default;
1652
1653 void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0,
1654 unsigned FormatMaxPadding = 3, bool TruncateZero = true) const {
1656 toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero));
1657 }
1658
1659 /// Format the value as toString does and return true if the string parses
1660 /// back bitwise equal. Non-finite spellings ("+Inf", "NaN") parse back
1661 /// here but may not fit a caller's grammar.
1663 unsigned FormatPrecision = 0,
1664 unsigned FormatMaxPadding = 3,
1665 bool TruncateZero = true) const;
1666
1667 /// Like toString, but picks the smallest FormatPrecision whose output
1668 /// parses back bitwise equal: "3.14", not "3.1400000000000001". An integer
1669 /// in fixed notation keeps its digits: "100", not "1.0E+2".
1671 unsigned FormatMaxPadding = 3,
1672 bool TruncateZero = true) const;
1673
1674 LLVM_ABI void print(raw_ostream &) const;
1675
1676#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1677 LLVM_DUMP_METHOD void dump() const;
1678#endif
1679
1680 /// If this value is normal and has an exact, normal, multiplicative inverse,
1681 /// store it in inv and return true.
1682 LLVM_ABI bool getExactInverse(APFloat *Inv) const;
1683
1684 // If this is an exact power of two, return the exponent while ignoring the
1685 // sign bit. If it's not an exact power of 2, return INT_MIN
1690
1691 // If this is an exact power of two, return the exponent. If it's not an exact
1692 // power of 2, return INT_MIN
1694 int getExactLog2() const {
1695 return isNegative() ? INT_MIN : getExactLog2Abs();
1696 }
1697
1698 // Returns true if this value is exactly 2^N.
1700 bool isPowerOf2(int N) const { return N != INT_MIN && getExactLog2() == N; }
1701
1702 // Returns true if this value is exactly -(2^N).
1704 bool isNegPowerOf2(int N) const {
1705 return N != INT_MIN && isNegative() && getExactLog2Abs() == N;
1706 }
1707
1708 // Returns true if this value is exactly +1.0.
1709 LLVM_READONLY bool isOne() const { return isPowerOf2(0); }
1710
1711 // Returns true if this value is exactly -1.0.
1712 LLVM_READONLY bool isMinusOne() const { return isNegPowerOf2(0); }
1713
1714 LLVM_ABI friend hash_code hash_value(const APFloat &Arg);
1715 friend int ilogb(const APFloat &Arg);
1716 friend APFloat scalbn(APFloat X, int Exp, roundingMode RM);
1717 friend APFloat frexp(const APFloat &X, int &Exp, roundingMode RM);
1718 friend IEEEFloat;
1719 friend DoubleAPFloat;
1720};
1721
1722static_assert(sizeof(APFloat) == sizeof(detail::IEEEFloat),
1723 "Empty base class optimization is not performed.");
1724
1725/// See friend declarations above.
1726///
1727/// These additional declarations are required in order to compile LLVM with IBM
1728/// xlC compiler.
1730
1731/// Returns the exponent of the internal representation of the APFloat.
1732///
1733/// Because the radix of APFloat is 2, this is equivalent to floor(log2(x)).
1734/// For special APFloat values, this returns special error codes:
1735///
1736/// NaN -> \c IEK_NaN
1737/// 0 -> \c IEK_Zero
1738/// Inf -> \c IEK_Inf
1739///
1740inline int ilogb(const APFloat &Arg) {
1741 if (APFloat::usesLayout<detail::IEEEFloat>(Arg.getSemantics()))
1742 return ilogb(Arg.U.IEEE);
1743 if (APFloat::usesLayout<detail::DoubleAPFloat>(Arg.getSemantics()))
1744 return ilogb(Arg.U.Double);
1745 llvm_unreachable("Unexpected semantics");
1746}
1747
1748/// Returns: X * 2^Exp for integral exponents.
1750 if (APFloat::usesLayout<detail::IEEEFloat>(X.getSemantics()))
1751 return APFloat(scalbn(X.U.IEEE, Exp, RM), X.getSemantics());
1752 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.getSemantics()))
1753 return APFloat(scalbn(X.U.Double, Exp, RM), X.getSemantics());
1754 llvm_unreachable("Unexpected semantics");
1755}
1756
1757/// Equivalent of C standard library function.
1758///
1759/// While the C standard says Exp is an unspecified value for infinity and nan,
1760/// this returns INT_MAX for infinities, and INT_MIN for NaNs.
1761inline APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM) {
1762 if (APFloat::usesLayout<detail::IEEEFloat>(X.getSemantics()))
1763 return APFloat(frexp(X.U.IEEE, Exp, RM), X.getSemantics());
1764 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.getSemantics()))
1765 return APFloat(frexp(X.U.Double, Exp, RM), X.getSemantics());
1766 llvm_unreachable("Unexpected semantics");
1767}
1768/// Returns the absolute value of the argument.
1770 X.clearSign();
1771 return X;
1772}
1773
1774/// Returns the negated value of the argument.
1776 X.changeSign();
1777 return X;
1778}
1779
1780/// Implements IEEE-754 2008 minNum semantics. Returns the smaller of the
1781/// 2 arguments if both are not NaN. If either argument is a qNaN, returns the
1782/// other argument. If either argument is sNaN, return a qNaN.
1783/// -0 is treated as ordered less than +0.
1785inline APFloat minnum(const APFloat &A, const APFloat &B) {
1786 if (A.isSignaling())
1787 return A.makeQuiet();
1788 if (B.isSignaling())
1789 return B.makeQuiet();
1790 if (A.isNaN())
1791 return B;
1792 if (B.isNaN())
1793 return A;
1794 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1795 return A.isNegative() ? A : B;
1796 return B < A ? B : A;
1797}
1798
1799/// Implements IEEE-754 2008 maxNum semantics. Returns the larger of the
1800/// 2 arguments if both are not NaN. If either argument is a qNaN, returns the
1801/// other argument. If either argument is sNaN, return a qNaN.
1802/// +0 is treated as ordered greater than -0.
1804inline APFloat maxnum(const APFloat &A, const APFloat &B) {
1805 if (A.isSignaling())
1806 return A.makeQuiet();
1807 if (B.isSignaling())
1808 return B.makeQuiet();
1809 if (A.isNaN())
1810 return B;
1811 if (B.isNaN())
1812 return A;
1813 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1814 return A.isNegative() ? B : A;
1815 return A < B ? B : A;
1816}
1817
1818/// Implements IEEE 754-2019 minimum semantics. Returns the smaller of 2
1819/// arguments, returning a quiet NaN if an argument is a NaN and treating -0
1820/// as less than +0.
1822inline APFloat minimum(const APFloat &A, const APFloat &B) {
1823 if (A.isNaN())
1824 return A.makeQuiet();
1825 if (B.isNaN())
1826 return B.makeQuiet();
1827 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1828 return A.isNegative() ? A : B;
1829 return B < A ? B : A;
1830}
1831
1832/// Implements IEEE 754-2019 minimumNumber semantics. Returns the smaller
1833/// of 2 arguments, not propagating NaNs and treating -0 as less than +0.
1835inline APFloat minimumnum(const APFloat &A, const APFloat &B) {
1836 if (A.isNaN())
1837 return B.isNaN() ? B.makeQuiet() : B;
1838 if (B.isNaN())
1839 return A;
1840 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1841 return A.isNegative() ? A : B;
1842 return B < A ? B : A;
1843}
1844
1845/// Implements IEEE 754-2019 maximum semantics. Returns the larger of 2
1846/// arguments, returning a quiet NaN if an argument is a NaN and treating -0
1847/// as less than +0.
1849inline APFloat maximum(const APFloat &A, const APFloat &B) {
1850 if (A.isNaN())
1851 return A.makeQuiet();
1852 if (B.isNaN())
1853 return B.makeQuiet();
1854 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1855 return A.isNegative() ? B : A;
1856 return A < B ? B : A;
1857}
1858
1859/// Implements IEEE 754-2019 maximumNumber semantics. Returns the larger
1860/// of 2 arguments, not propagating NaNs and treating -0 as less than +0.
1862inline APFloat maximumnum(const APFloat &A, const APFloat &B) {
1863 if (A.isNaN())
1864 return B.isNaN() ? B.makeQuiet() : B;
1865 if (B.isNaN())
1866 return A;
1867 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1868 return A.isNegative() ? B : A;
1869 return A < B ? B : A;
1870}
1871
1872/// Implement IEEE 754-2019 exp functions
1874LLVM_ABI std::optional<APFloat>
1875exp(const APFloat &X, RoundingMode RM = APFloat::rmNearestTiesToEven,
1876 APFloat::opStatus *Status = nullptr);
1877
1879 V.print(OS);
1880 return OS;
1881}
1882
1883// We want the following functions to be available in the header for inlining.
1884// We cannot define them inline in the class definition of `DoubleAPFloat`
1885// because they subscript `Floats`, which requires `APFloat` to be a complete
1886// type.
1887namespace detail {
1888
1890 if (this != &RHS) {
1891 this->~DoubleAPFloat();
1892 new (this) DoubleAPFloat(std::move(RHS));
1893 }
1894 return *this;
1895}
1896
1897APFloat &DoubleAPFloat::getFirst() { return Floats[0]; }
1898const APFloat &DoubleAPFloat::getFirst() const { return Floats[0]; }
1899APFloat &DoubleAPFloat::getSecond() { return Floats[1]; }
1900const APFloat &DoubleAPFloat::getSecond() const { return Floats[1]; }
1901
1902inline DoubleAPFloat::~DoubleAPFloat() { delete[] Floats; }
1903
1904} // namespace detail
1905
1906} // namespace llvm
1907
1908#undef APFLOAT_DISPATCH_ON_SEMANTICS
1909#endif // LLVM_ADT_APFLOAT_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL)
Definition APFloat.h:26
This file implements a class to represent arbitrary precision integral constant values and operations...
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:702
#define LLVM_READONLY
Definition Compiler.h:334
Utilities for dealing with flags related to floating point properties and mode controls.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Load MIR Sample Profile
#define T
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, const llvm::StringTable &StandardNames, VectorLibrary VecLib)
Initialize the set of available library functions based on the specified target triple.
Value * RHS
Value * LHS
The Input class is used to parse a yaml document into in-memory structs and vectors.
static constexpr const fltSemantics & Float8E4M3()
Definition APFloat.h:320
static LLVM_ABI const llvm::fltSemantics & EnumToSemantics(Semantics S)
Definition APFloat.cpp:451
static LLVM_ABI bool semanticsHasInf(const fltSemantics &)
Definition APFloat.cpp:642
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition APFloat.h:369
static constexpr const fltSemantics & Float8E5M2FNUZ()
Definition APFloat.h:317
static constexpr roundingMode rmTowardZero
Definition APFloat.h:383
static LLVM_ABI ExponentType semanticsMinExponent(const fltSemantics &)
Definition APFloat.cpp:617
llvm::RoundingMode roundingMode
IEEE-754R 4.3: Rounding-direction attributes.
Definition APFloat.h:377
static constexpr const fltSemantics & Float8E3M4()
Definition APFloat.h:330
static constexpr const fltSemantics & PPCDoubleDouble()
Definition APFloat.h:310
static LLVM_ABI unsigned int semanticsSizeInBits(const fltSemantics &)
Definition APFloat.cpp:620
static LLVM_ABI bool semanticsHasSignedRepr(const fltSemantics &)
Definition APFloat.cpp:638
static LLVM_ABI unsigned getSizeInBits(const fltSemantics &Sem)
Returns the size of the floating point number (in bits) in the given semantics.
Definition APFloat.cpp:673
static constexpr const fltSemantics & Float8E8M0FNU()
Definition APFloat.h:332
static constexpr const fltSemantics & Float8E4M3FNUZ()
Definition APFloat.h:324
static constexpr roundingMode rmTowardNegative
Definition APFloat.h:382
uninitializedTag
Convenience enum used to construct an uninitialized APFloat.
Definition APFloat.h:413
static constexpr const fltSemantics & FloatTF32()
Definition APFloat.h:331
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:379
static LLVM_ABI bool isValidArbitraryFPFormat(StringRef Format)
Returns true if the given string is a valid arbitrary floating-point format interpretation for llvm....
Definition APFloat.cpp:6394
static LLVM_ABI bool hasSignBitInMSB(const fltSemantics &)
Definition APFloat.cpp:655
static constexpr const fltSemantics & IEEEhalf()
Definition APFloat.h:302
static LLVM_ABI ExponentType semanticsMaxExponent(const fltSemantics &)
Definition APFloat.cpp:613
friend class APFloat
Definition APFloat.h:299
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:609
static constexpr const fltSemantics & Bogus()
A Pseudo fltsemantic used to construct APFloats that cannot conflict with anything real.
Definition APFloat.h:350
static LLVM_ABI bool semanticsHasNaN(const fltSemantics &)
Definition APFloat.cpp:646
static constexpr const fltSemantics & IEEEsingle()
Definition APFloat.h:304
static LLVM_ABI Semantics SemanticsToEnum(const llvm::fltSemantics &Sem)
Definition APFloat.cpp:459
int32_t ExponentType
A signed type to represent a floating point numbers unbiased exponent.
Definition APFloat.h:156
static constexpr const fltSemantics & Float6E2M3FN()
Definition APFloat.h:341
static constexpr unsigned integerPartWidth
Definition APFloat.h:153
static LLVM_ABI bool isLosslesslyConvertibleTo(const fltSemantics &From, const fltSemantics &To, bool IgnoreNaNs=false)
Returns whether converting a value from From to To is known to preserve all information.
Definition APFloat.cpp:512
APInt::WordType integerPart
Definition APFloat.h:152
static LLVM_ABI bool semanticsHasZero(const fltSemantics &)
Definition APFloat.cpp:634
static LLVM_ABI bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition APFloat.cpp:659
static constexpr roundingMode rmTowardPositive
Definition APFloat.h:381
static constexpr const fltSemantics & PPCDoubleDoubleLegacy()
Definition APFloat.h:313
static constexpr const fltSemantics & BFloat()
Definition APFloat.h:303
static constexpr const fltSemantics & Float6E3M2FN()
Definition APFloat.h:338
IlogbErrorKinds
Enumeration of ilogb error results.
Definition APFloat.h:418
static constexpr const fltSemantics & IEEEdouble()
Definition APFloat.h:305
static LLVM_ABI bool isRepresentableBy(const fltSemantics &A, const fltSemantics &B)
Definition APFloat.cpp:506
static constexpr const fltSemantics & Float8E5M2()
Definition APFloat.h:316
static LLVM_ABI bool isIEEELikeFP(const fltSemantics &)
Definition APFloat.cpp:650
fltCategory
Category of internally-represented number.
Definition APFloat.h:405
static constexpr const fltSemantics & Float8E5M3FNU()
Definition APFloat.h:335
static constexpr roundingMode rmNearestTiesToAway
Definition APFloat.h:384
static constexpr const fltSemantics & IEEEquad()
Definition APFloat.h:306
static constexpr const fltSemantics & Float4E2M1FN()
Definition APFloat.h:344
opStatus
IEEE-754R 7: Default exception handling.
Definition APFloat.h:395
static constexpr const fltSemantics & Float8E4M3B11FNUZ()
Definition APFloat.h:327
static constexpr const fltSemantics & Float8E4M3FN()
Definition APFloat.h:321
static LLVM_ABI unsigned getArbitraryFPFormatSizeInBits(StringRef Format)
Returns the size in bits of a valid arbitrary floating-point format string, or 0 if the string is not...
Definition APFloat.cpp:6377
static LLVM_ABI const fltSemantics * getArbitraryFPSemantics(StringRef Format)
Returns the fltSemantics for a given arbitrary FP format string, or nullptr if invalid.
Definition APFloat.cpp:6398
static constexpr const fltSemantics & x87DoubleExtended()
Definition APFloat.h:307
static LLVM_ABI unsigned int semanticsIntSizeInBits(const fltSemantics &, bool)
Definition APFloat.cpp:623
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition APFloat.h:1257
static APFloat getSNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for SNaN values.
Definition APFloat.h:1265
LLVM_READONLY bool isNegPowerOf2(int N) const
Definition APFloat.h:1704
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1345
APFloat & operator=(APFloat &&RHS)=default
bool isFiniteNonZero() const
Definition APFloat.h:1626
APFloat(const APFloat &RHS)=default
void copySign(const APFloat &RHS)
Definition APFloat.h:1439
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:6103
LLVM_READONLY int getExactLog2Abs() const
Definition APFloat.h:1687
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1327
bool bitwiseIsEqual(const APFloat &RHS) const
Definition APFloat.h:1581
bool isNegative() const
Definition APFloat.h:1616
~APFloat()=default
LLVM_ABI bool getExactInverse(APFloat *Inv) const
If this value is normal and has an exact, normal, multiplicative inverse, store it in inv and return ...
Definition APFloat.cpp:6045
cmpResult compareAbsoluteValue(const APFloat &RHS) const
Definition APFloat.h:1571
APFloat operator+(const APFloat &RHS) const
Add two APFloats, rounding ties to the nearest even.
Definition APFloat.h:1404
friend DoubleAPFloat
Definition APFloat.h:1719
LLVM_ABI double convertToDouble() const
Converts this APFloat to host double value.
Definition APFloat.cpp:6336
bool isPosInfinity() const
Definition APFloat.h:1629
APFloat(APFloat &&RHS)=default
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
Definition APFloat.h:1653
bool isNormal() const
Definition APFloat.h:1620
bool isDenormal() const
Definition APFloat.h:1617
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
Definition APFloat.h:1599
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1318
LLVM_READONLY int getExactLog2() const
Definition APFloat.h:1694
APFloat(double d)
Definition APFloat.h:1204
APFloat & operator=(const APFloat &RHS)=default
LLVM_READONLY bool isPowerOf2(int N) const
Definition APFloat.h:1700
static LLVM_ABI APFloat getAllOnesValue(const fltSemantics &Semantics)
Returns a float which is bitcasted from an all one value int.
Definition APFloat.cpp:6129
LLVM_ABI friend hash_code hash_value(const APFloat &Arg)
See friend declarations above.
Definition APFloat.cpp:6017
APFloat(const fltSemantics &Semantics, integerPart I)
Definition APFloat.h:1196
bool operator!=(const APFloat &RHS) const
Definition APFloat.h:1537
APFloat(const fltSemantics &Semantics, T V)=delete
const fltSemantics & getSemantics() const
Definition APFloat.h:1624
APFloat operator-(const APFloat &RHS) const
Subtract two APFloats, rounding ties to the nearest even.
Definition APFloat.h:1412
APFloat operator*(const APFloat &RHS) const
Multiply two APFloats, rounding ties to the nearest even.
Definition APFloat.h:1420
APFloat(const fltSemantics &Semantics)
Definition APFloat.h:1194
bool isNonZero() const
Definition APFloat.h:1625
void clearSign()
Definition APFloat.h:1435
bool operator<(const APFloat &RHS) const
Definition APFloat.h:1539
bool isFinite() const
Definition APFloat.h:1621
APFloat makeQuiet() const
Assuming this is an IEEE-754 NaN value, quiet its signaling bit.
Definition APFloat.h:1453
bool isNaN() const
Definition APFloat.h:1614
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.h:1484
static APFloat getOne(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative One.
Definition APFloat.h:1225
unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
Definition APFloat.h:1606
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1336
LLVM_ABI float convertToFloat() const
Converts this APFloat to host float value.
Definition APFloat.cpp:6364
bool isSignaling() const
Definition APFloat.h:1618
bool operator>(const APFloat &RHS) const
Definition APFloat.h:1543
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition APFloat.h:1372
APFloat operator/(const APFloat &RHS) const
Divide the first APFloat by the second, rounding ties to the nearest even.
Definition APFloat.h:1428
opStatus remainder(const APFloat &RHS)
Definition APFloat.h:1354
APFloat operator-() const
Negate an APFloat.
Definition APFloat.h:1396
bool isZero() const
Definition APFloat.h:1612
LLVM_READONLY bool isOne() const
Definition APFloat.h:1709
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition APFloat.h:1295
APInt bitcastToAPInt() const
Definition APFloat.h:1508
bool isLargest() const
Definition APFloat.h:1632
friend APFloat frexp(const APFloat &X, int &Exp, roundingMode RM)
bool isSmallest() const
Definition APFloat.h:1631
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1275
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1469
opStatus next(bool nextDown)
Definition APFloat.h:1391
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1235
friend APFloat scalbn(APFloat X, int Exp, roundingMode RM)
bool operator>=(const APFloat &RHS) const
Definition APFloat.h:1552
bool needsCleanup() const
Definition APFloat.h:1211
static APFloat getSmallest(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) finite number in the given semantics.
Definition APFloat.h:1285
LLVM_ABI FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition APFloat.cpp:6032
bool operator==(const APFloat &RHS) const
Definition APFloat.h:1535
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1363
LLVM_ABI void toStringShortest(SmallVectorImpl< char > &Str, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
Like toString, but picks the smallest FormatPrecision whose output parses back bitwise equal: "3....
Definition APFloat.cpp:6272
bool isPosZero() const
Definition APFloat.h:1627
APInt getNaNPayload() const
If the value is a NaN value, return an integer containing the payload of this value.
Definition APFloat.h:1642
friend int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1740
LLVM_ABI Expected< opStatus > convertFromString(StringRef, roundingMode)
Fill this APFloat with the result of a string conversion.
Definition APFloat.cpp:6012
fltCategory getCategory() const
Definition APFloat.h:1623
APFloat(const fltSemantics &Semantics, uninitializedTag)
Definition APFloat.h:1201
bool isInteger() const
Definition APFloat.h:1633
bool isNegInfinity() const
Definition APFloat.h:1630
friend IEEEFloat
Definition APFloat.h:1718
LLVM_DUMP_METHOD void dump() const
Definition APFloat.cpp:6314
bool isNegZero() const
Definition APFloat.h:1628
LLVM_ABI bool toStringRoundTrip(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
Format the value as toString does and return true if the string parses back bitwise equal.
Definition APFloat.cpp:6133
LLVM_ABI void print(raw_ostream &) const
Definition APFloat.cpp:6307
static APFloat copySign(APFloat Value, const APFloat &Sign)
A static helper to produce a copy of an APFloat value with its sign copied from some other APFloat.
Definition APFloat.h:1446
LLVM_READONLY bool isMinusOne() const
Definition APFloat.h:1712
APFloat(float f)
Definition APFloat.h:1205
opStatus roundToIntegral(roundingMode RM)
Definition APFloat.h:1385
void changeSign()
Definition APFloat.h:1434
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition APFloat.h:1246
static bool hasSignificand(const fltSemantics &Sem)
Returns true if the given semantics has actual significand.
Definition APFloat.h:1310
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition APFloat.h:1216
cmpResult compare(const APFloat &RHS) const
Definition APFloat.h:1559
bool isSmallestNormalized() const
Definition APFloat.h:1635
APFloat(const fltSemantics &Semantics, const APInt &I)
Definition APFloat.h:1203
bool isInfinity() const
Definition APFloat.h:1613
bool operator<=(const APFloat &RHS) const
Definition APFloat.h:1547
Class for arbitrary precision integers.
Definition APInt.h:78
uint64_t WordType
Definition APInt.h:80
static constexpr unsigned APINT_BITS_PER_WORD
Bits in a word.
Definition APInt.h:86
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
Tagged union holding either a T or a Error.
Definition Error.h:485
Interface for fltSemantics callbacks to access the internal data of APFloat/IEEEFloat.
Definition APFloat.h:1017
LLVM_ABI void setNegative(bool Negative)
Definition APFloat.cpp:5978
LLVM_ABI bool isNegative() const
Definition APFloat.cpp:5963
LLVM_ABI void initialize(const fltSemantics &Sem)
Set the semantics and allocate storage for the significand.
Definition APFloat.cpp:5955
LLVM_ABI APFloatBase::fltCategory getCategory() const
Definition APFloat.cpp:5959
LLVM_ABI void setExponent(APFloatBase::ExponentType Exponent)
Definition APFloat.cpp:5984
LLVM_ABI APInt getSignificand() const
Definition APFloat.cpp:5969
LLVM_ABI const fltSemantics & getSemantics() const
Definition APFloat.cpp:5957
LLVM_ABI void setCategory(APFloatBase::fltCategory Category)
Exponent and significand are only meaningful for fcNormal and fcNaN.
Definition APFloat.cpp:5974
LLVM_ABI void setSignificand(const APInt &Significand)
The significand is precision bits wide and includes the integer bit.
Definition APFloat.cpp:5988
LLVM_ABI APFloatBase::ExponentType getExponent() const
Definition APFloat.cpp:5965
This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:162
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI void makeSmallestNormalized(bool Neg)
Definition APFloat.cpp:5309
LLVM_ABI DoubleAPFloat & operator=(const DoubleAPFloat &RHS)
Definition APFloat.cpp:4839
LLVM_ABI void changeSign()
Definition APFloat.cpp:5216
LLVM_ABI bool isLargest() const
Definition APFloat.cpp:5783
LLVM_ABI opStatus remainder(const DoubleAPFloat &RHS)
Definition APFloat.cpp:5103
LLVM_ABI opStatus multiply(const DoubleAPFloat &RHS, roundingMode RM)
Definition APFloat.cpp:5006
LLVM_ABI fltCategory getCategory() const
Definition APFloat.cpp:5275
LLVM_ABI bool bitwiseIsEqual(const DoubleAPFloat &RHS) const
Definition APFloat.cpp:5332
LLVM_ABI LLVM_READONLY int getExactLog2Abs() const
Definition APFloat.cpp:5807
LLVM_ABI opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.cpp:5734
LLVM_ABI APInt bitcastToAPInt() const
Definition APFloat.cpp:5343
LLVM_ABI Expected< opStatus > convertFromString(StringRef, roundingMode)
Definition APFloat.cpp:5353
LLVM_ABI bool isSmallest() const
Definition APFloat.cpp:5766
LLVM_ABI opStatus subtract(const DoubleAPFloat &RHS, roundingMode RM)
Definition APFloat.cpp:4998
LLVM_ABI friend hash_code hash_value(const DoubleAPFloat &Arg)
Definition APFloat.cpp:5337
LLVM_ABI cmpResult compareAbsoluteValue(const DoubleAPFloat &RHS) const
Definition APFloat.cpp:5222
LLVM_ABI bool isDenormal() const
Definition APFloat.cpp:5759
LLVM_ABI opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.cpp:5570
LLVM_ABI void makeSmallest(bool Neg)
Definition APFloat.cpp:5302
LLVM_ABI friend int ilogb(const DoubleAPFloat &X)
Definition APFloat.cpp:5816
LLVM_ABI opStatus next(bool nextDown)
Definition APFloat.cpp:5369
LLVM_ABI void makeInf(bool Neg)
Definition APFloat.cpp:5281
LLVM_ABI bool isInteger() const
Definition APFloat.cpp:5791
LLVM_ABI void makeZero(bool Neg)
Definition APFloat.cpp:5286
LLVM_ABI opStatus divide(const DoubleAPFloat &RHS, roundingMode RM)
Definition APFloat.cpp:5092
LLVM_ABI bool isSmallestNormalized() const
Definition APFloat.cpp:5774
LLVM_ABI opStatus mod(const DoubleAPFloat &RHS)
Definition APFloat.cpp:5113
LLVM_ABI DoubleAPFloat(const fltSemantics &S)
Definition APFloat.cpp:4786
LLVM_ABI void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision, unsigned FormatMaxPadding, bool TruncateZero=true) const
Definition APFloat.cpp:5797
LLVM_ABI void makeLargest(bool Neg)
Definition APFloat.cpp:5291
LLVM_ABI cmpResult compare(const DoubleAPFloat &RHS) const
Definition APFloat.cpp:5324
LLVM_ABI friend DoubleAPFloat scalbn(const DoubleAPFloat &X, int Exp, roundingMode)
LLVM_ABI opStatus roundToIntegral(roundingMode RM)
Definition APFloat.cpp:5139
LLVM_ABI opStatus fusedMultiplyAdd(const DoubleAPFloat &Multiplicand, const DoubleAPFloat &Addend, roundingMode RM)
Definition APFloat.cpp:5124
LLVM_ABI APInt getNaNPayload() const
Definition APFloat.cpp:5950
LLVM_ABI unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
Definition APFloat.cpp:5749
bool needsCleanup() const
Definition APFloat.h:891
LLVM_ABI bool isNegative() const
Definition APFloat.cpp:5279
LLVM_ABI opStatus add(const DoubleAPFloat &RHS, roundingMode RM)
Definition APFloat.cpp:4993
LLVM_ABI friend DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp, roundingMode)
LLVM_ABI void makeNaN(bool SNaN, bool Neg, const APInt *fill)
Definition APFloat.cpp:5319
LLVM_ABI unsigned int convertToHexString(char *dst, unsigned int hexDigits, bool upperCase, roundingMode) const
Write out a hexadecimal representation of the floating point value to DST, which must be of sufficien...
Definition APFloat.cpp:3599
LLVM_ABI cmpResult compareAbsoluteValue(const IEEEFloat &) const
Definition APFloat.cpp:1828
LLVM_ABI opStatus mod(const IEEEFloat &)
C fmod, or llvm frem.
Definition APFloat.cpp:2584
fltCategory getCategory() const
Definition APFloat.h:623
LLVM_ABI opStatus convertFromAPInt(const APInt &, bool, roundingMode)
Definition APFloat.cpp:3156
LLVM_ABI APInt getNaNPayload() const
Definition APFloat.cpp:4674
bool isNonZero() const
Definition APFloat.h:625
bool isFiniteNonZero() const
Definition APFloat.h:626
bool needsCleanup() const
Returns whether this instance allocated memory.
Definition APFloat.h:513
LLVM_ABI void makeLargest(bool Neg=false)
Make this number the largest magnitude normal number in the given semantics.
Definition APFloat.cpp:4101
LLVM_ABI LLVM_READONLY int getExactLog2Abs() const
Definition APFloat.cpp:4496
LLVM_ABI APInt bitcastToAPInt() const
Definition APFloat.cpp:3892
LLVM_ABI friend IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode)
Definition APFloat.cpp:4746
LLVM_ABI cmpResult compare(const IEEEFloat &) const
IEEE comparison with another floating point number (NaNs compare unordered, 0==-0).
Definition APFloat.cpp:2752
bool isNegative() const
IEEE-754R isSignMinus: Returns true if and only if the current value is negative.
Definition APFloat.h:588
friend APInt bitcastForSemantics(FloatRepr R)
Definition APFloat.cpp:92
LLVM_ABI opStatus divide(const IEEEFloat &, roundingMode)
Definition APFloat.cpp:2458
LLVM_ABI friend hash_code hash_value(const IEEEFloat &Arg)
Overload to compute a hash code for an APFloat value.
Definition APFloat.cpp:3739
bool isNaN() const
Returns true if and only if the float is a quiet or signaling NaN.
Definition APFloat.h:613
LLVM_ABI opStatus remainder(const IEEEFloat &)
IEEE remainder.
Definition APFloat.cpp:2476
LLVM_ABI double convertToDouble() const
Definition APFloat.cpp:3906
LLVM_ABI float convertToFloat() const
Definition APFloat.cpp:3899
LLVM_ABI opStatus subtract(const IEEEFloat &, roundingMode)
Definition APFloat.cpp:2434
LLVM_ABI void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
Converts this value into a decimal string.
Definition APFloat.cpp:4452
LLVM_ABI void makeSmallest(bool Neg=false)
Make this number the smallest magnitude denormal number in the given semantics.
Definition APFloat.cpp:4133
LLVM_ABI void makeInf(bool Neg=false)
Definition APFloat.cpp:4693
bool isNormal() const
IEEE-754R isNormal: Returns true if and only if the current value is normal.
Definition APFloat.h:594
LLVM_ABI bool isSmallestNormalized() const
Returns true if this is the smallest (by magnitude) normalized finite number in the given semantics.
Definition APFloat.cpp:1349
friend class IEEEFloatUnitTestHelper
Definition APFloat.h:840
LLVM_ABI void makeQuiet()
Definition APFloat.cpp:4722
LLVM_ABI bool isLargest() const
Returns true if and only if the number has the largest possible finite magnitude in the current seman...
Definition APFloat.cpp:1451
LLVM_ABI opStatus add(const IEEEFloat &, roundingMode)
Definition APFloat.cpp:2428
bool isFinite() const
Returns true if and only if the current value is zero, subnormal, or normal.
Definition APFloat.h:600
LLVM_ABI Expected< opStatus > convertFromString(StringRef, roundingMode)
Definition APFloat.cpp:3542
LLVM_ABI void makeNaN(bool SNaN=false, bool Neg=false, const APInt *fill=nullptr)
Definition APFloat.cpp:1237
LLVM_ABI opStatus multiply(const IEEEFloat &, roundingMode)
Definition APFloat.cpp:2440
LLVM_ABI opStatus roundToIntegral(roundingMode)
Definition APFloat.cpp:2667
LLVM_ABI IEEEFloat & operator=(const IEEEFloat &)
Definition APFloat.cpp:1309
LLVM_ABI bool bitwiseIsEqual(const IEEEFloat &) const
Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
Definition APFloat.cpp:1476
LLVM_ABI void makeSmallestNormalized(bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition APFloat.cpp:4147
LLVM_ABI bool isInteger() const
Returns true if and only if the number is an exact integer.
Definition APFloat.cpp:1468
bool isPosZero() const
Definition APFloat.h:627
LLVM_ABI IEEEFloat(const fltSemantics &)
Definition APFloat.cpp:1503
LLVM_ABI opStatus fusedMultiplyAdd(const IEEEFloat &, const IEEEFloat &, roundingMode)
Definition APFloat.cpp:2621
LLVM_ABI friend int ilogb(const IEEEFloat &Arg)
Definition APFloat.cpp:4728
LLVM_ABI opStatus next(bool nextDown)
IEEE-754R 5.3.1: nextUp/nextDown.
Definition APFloat.cpp:4541
bool isInfinity() const
IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
Definition APFloat.h:610
const fltSemantics & getSemantics() const
Definition APFloat.h:624
bool isZero() const
Returns true if and only if the float is plus or minus zero.
Definition APFloat.h:603
LLVM_ABI bool isSignaling() const
Returns true if and only if the float is a signaling NaN.
Definition APFloat.cpp:4525
bool operator==(const IEEEFloat &) const =delete
The definition of equality is not straightforward for floating point, so we won't use operator==.
LLVM_ABI void makeZero(bool Neg=false)
Definition APFloat.cpp:4708
friend void initFromAPIntForSemantics(FloatRepr, const APInt &)
Definition APFloat.cpp:99
LLVM_ABI opStatus convert(const fltSemantics &, roundingMode, bool *)
IEEEFloat::convert - convert a value of one floating point type to another.
Definition APFloat.cpp:2828
LLVM_ABI void changeSign()
Definition APFloat.cpp:2386
LLVM_ABI bool isDenormal() const
IEEE-754R isSubnormal(): Returns true if and only if the float is a denormal.
Definition APFloat.cpp:1334
LLVM_ABI opStatus convertToInteger(MutableArrayRef< integerPart >, unsigned int, bool, roundingMode, bool *) const
Definition APFloat.cpp:3101
LLVM_ABI friend IEEEFloat frexp(const IEEEFloat &X, int &Exp, roundingMode)
Definition APFloat.cpp:4767
LLVM_ABI bool isSmallest() const
Returns true if and only if the number has the smallest possible non-zero magnitude in the current se...
Definition APFloat.cpp:1341
bool isNegZero() const
Definition APFloat.h:628
An opaque object representing a hash code.
Definition Hashing.h:77
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static constexpr opStatus opInexact
Definition APFloat.h:489
static constexpr fltCategory fcNaN
Definition APFloat.h:491
static constexpr opStatus opDivByZero
Definition APFloat.h:486
static constexpr opStatus opOverflow
Definition APFloat.h:487
static constexpr cmpResult cmpLessThan
Definition APFloat.h:481
static constexpr roundingMode rmTowardPositive
Definition APFloat.h:477
static constexpr uninitializedTag uninitialized
Definition APFloat.h:471
static constexpr fltCategory fcZero
Definition APFloat.h:493
static constexpr opStatus opOK
Definition APFloat.h:484
static constexpr cmpResult cmpGreaterThan
Definition APFloat.h:482
static constexpr unsigned integerPartWidth
Definition APFloat.h:479
LLVM_ABI hash_code hash_value(const IEEEFloat &Arg)
Definition APFloat.cpp:3739
APFloatBase::ExponentType ExponentType
Definition APFloat.h:470
APFloatBase::fltCategory fltCategory
Definition APFloat.h:469
static constexpr fltCategory fcNormal
Definition APFloat.h:492
static constexpr opStatus opInvalidOp
Definition APFloat.h:485
APFloatBase::opStatus opStatus
Definition APFloat.h:467
LLVM_ABI IEEEFloat frexp(const IEEEFloat &Val, int &Exp, roundingMode RM)
Definition APFloat.cpp:4767
APFloatBase::uninitializedTag uninitializedTag
Definition APFloat.h:465
static constexpr cmpResult cmpUnordered
Definition APFloat.h:483
static constexpr roundingMode rmTowardNegative
Definition APFloat.h:476
APFloatBase::roundingMode roundingMode
Definition APFloat.h:466
APFloatBase::cmpResult cmpResult
Definition APFloat.h:468
static constexpr fltCategory fcInfinity
Definition APFloat.h:490
static constexpr roundingMode rmNearestTiesToAway
Definition APFloat.h:474
static constexpr roundingMode rmTowardZero
Definition APFloat.h:478
static constexpr opStatus opUnderflow
Definition APFloat.h:488
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:472
LLVM_ABI int ilogb(const IEEEFloat &Arg)
Definition APFloat.cpp:4728
static constexpr cmpResult cmpEqual
Definition APFloat.h:480
LLVM_ABI IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode)
Definition APFloat.cpp:4746
APFloatBase::integerPart integerPart
Definition APFloat.h:464
This is an optimization pass for GlobalISel generic memory operations.
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1775
hash_code hash_value(const FixedPointSemantics &Val)
static constexpr APFloatBase::ExponentType exponentZero(const fltSemantics &semantics)
Definition APFloat.cpp:678
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition APFloat.h:1769
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition APFloat.h:1849
static void assign(DXContainerYAML::SourceInfo::SectionHeader &Dst, const dxbc::SourceInfo::SectionHeader &Src)
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1740
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition APFloat.h:1761
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 maxNum semantics.
Definition APFloat.h:1804
lostFraction
Enum that represents what fraction of the LSB truncated bits of an fp number represent.
Definition APFloat.h:51
@ lfMoreThanHalf
Definition APFloat.h:55
@ lfLessThanHalf
Definition APFloat.h:53
@ lfExactlyHalf
Definition APFloat.h:54
@ lfExactlyZero
Definition APFloat.h:52
LLVM_READONLY LLVM_ABI std::optional< APFloat > exp(const APFloat &X, RoundingMode RM=APFloat::rmNearestTiesToEven, APFloat::opStatus *Status=nullptr)
Implement IEEE 754-2019 exp functions.
Definition APFloat.cpp:6496
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1835
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM)
Returns: X * 2^Exp for integral exponents.
Definition APFloat.h:1749
static constexpr APFloatBase::ExponentType exponentNaN(const fltSemantics &semantics)
Definition APFloat.cpp:688
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 minNum semantics.
Definition APFloat.h:1785
fltNonfiniteBehavior
Definition APFloat.h:966
RoundingMode
Rounding mode.
@ TowardZero
roundTowardZero.
@ NearestTiesToEven
roundTiesToEven.
@ TowardPositive
roundTowardPositive.
@ NearestTiesToAway
roundTiesToAway.
@ TowardNegative
roundTowardNegative.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
static constexpr APFloatBase::ExponentType exponentInf(const fltSemantics &semantics)
Definition APFloat.cpp:683
APFloat neg(APFloat X)
Returns the negated value of the argument.
Definition APFloat.h:1775
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition APFloat.h:1822
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1862
fltNanEncoding
Definition APFloat.h:990
#define N
void(* initFromAPInt)(FloatRepr, const APInt &)
Definition APFloat.h:1098
APFloatBase::ExponentType maxExponent
Definition APFloat.h:1051
fltNonfiniteBehavior nonFiniteBehavior
Definition APFloat.h:1064
APFloatBase::ExponentType minExponent
Definition APFloat.h:1055
unsigned int sizeInBits
Definition APFloat.h:1062
unsigned int precision
Definition APFloat.h:1059
fltNanEncoding nanEncoding
Definition APFloat.h:1066
bool hasExplicitIntegerBit
Definition APFloat.h:1094
APInt(* bitcastToAPInt)(FloatRepr)
Definition APFloat.h:1102