summaryrefslogtreecommitdiff
path: root/llvm/lib/Support/APFloat.cpp
AgeCommit message (Collapse)Author
2025-05-21[NFC][ADT/Support] Add {} for else when if body has {} (#140758)Rahul Joshi
2025-05-11[llvm] Use StringRef::consume_front (NFC) (#139458)Kazu Hirata
2025-04-14[InstCombine] Fold fneg/fabs patterns with ppc_f128 (#130557)Yingwei Zheng
This patch is needed by https://github.com/llvm/llvm-project/pull/130496.
2025-03-10[APFloat] Fix `IEEEFloat::addOrSubtractSignificand` and ↵beetrees
`IEEEFloat::normalize` (#98721) Fixes #63895 Fixes #104984 Before this PR, `addOrSubtractSignificand` presumed that the loss came from the side being subtracted, and didn't handle the case where lhs == rhs and there was loss. This can occur during FMA. This PR fixes the situation by correctly determining where the loss came from and handling it appropriately. Additionally, `normalize` failed to adjust the exponent when the significand is zero but `lost_fraction != lfExactlyZero`. This meant that the test case from #63895 was rounded incorrectly as the loss wasn't adjusted to account for the exponent being below the minimum exponent. This PR fixes this by only skipping the exponent adjustment if the significand is zero and there was no lost fraction. (Note to reviewer: I don't have commit access)
2025-03-05ADT: Switch to a raw pointer for DoubleAPFloat::Floats.Peter Collingbourne
In order for the union APFloat::Storage to permit access to the semantics field when another union member is stored there, all members of Storage must be standard layout. This is not necessarily the case for DoubleAPFloat which may be non-standard layout because there is no requirement that its std::unique_ptr member is standard layout. Fix this by converting Floats to a raw pointer. Reviewers: arsenm Reviewed By: arsenm Pull Request: https://github.com/llvm/llvm-project/pull/129981
2025-02-25[X86][DAGCombiner] Skip x87 fp80 values in `combineFMulOrFDivWithIntPow2` ↵Yingwei Zheng
(#128618) f80 is not a valid IEEE floating-point type. Closes https://github.com/llvm/llvm-project/issues/128528.
2025-01-14[APFloat][NFC]extract `fltSemantics::isRepresentableBy` to header (#122636)Congcong Cai
isRepresentableBy is useful to check float point type compatibility
2024-12-04[llvm][NFC] `APFloat`: Add missing semantics to enum (#117291)Matthias Springer
* Add missing semantics to the `Semantics` enum. * Move all documentation of the semantics to the header file. * Also rename some functions for consistency.
2024-11-16[llvm] `APFloat`: Add helpers to query NaN/inf semantics (#116315)Matthias Springer
`APFloat` changes extracted from #116176 as per reviewer comments.
2024-11-15[llvm] `APFloat`: Query `hasNanOrInf` from semantics (#116158)Matthias Springer
Whether a floating point type supports NaN or infinity can be queried from its semantics. No need to hard-code a list of types.
2024-11-12[llvm] Remove redundant control flow statements (NFC) (#115831)Kazu Hirata
Identified with readability-redundant-control-flow.
2024-10-22Fix bitcasting E8M0 APFloat to APInt (#113298)Sergey Kozub
Fixes a bug in APFloat handling of E8M0 type (zero mantissa). Related PRs: - https://github.com/llvm/llvm-project/pull/107127 - https://github.com/llvm/llvm-project/pull/111028
2024-10-09[APFloat] add predicates to fltSemantics for hasZero and hasSignedRepr (#111451)Ariel-Burton
We add static methods to APFloatBase to allow the hasZero and hasSignedRepr properties of fltSemantics to be obtained.
2024-10-09[ADT][APFloat] Make sure EBO is performed on APFloat (#111641)Yingwei Zheng
Since both APFloat and (Double)IEEEFloat inherit from APFloatBase, empty base optimization is not performed by GCC/Clang (Minimal reproducer: https://godbolt.org/z/dY8cM3Wre). This patch removes inheritance relation between (Double)IEEEFloat and APFloatBase to make sure EBO is performed on APFloat. After this patch, the size of `ConstantFPRange` will be reduced from 72 to 56. Address comment https://github.com/llvm/llvm-project/pull/111544#discussion_r1792398427.
2024-10-02[APFloat] Add APFloat support for E8M0 type (#107127)Durgadoss R
This patch adds an APFloat type for unsigned E8M0 format. This format is used for representing the "scale-format" in the MX specification: (section 5.4) https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf This format does not support {Inf, denorms, zeroes}. Like FP32, this format's exponents are 8-bits (all bits here) and the bias value is 127. However, it differs from IEEE-FP32 in that the minExponent is -127 (instead of -126). There are updates done in the APFloat utility functions to handle these constraints for this format. * The bias calculation is different and convertIEEE* APIs are updated to handle this. * Since there are no significand bits, the isSignificandAll{Zeroes/Ones} methods are updated accordingly. * Although the format does not have any precision, the precision bit in the fltSemantics is set to 1 for consistency with APFloat's internal representation. * Many utility functions are updated to handle the fact that this format does not support Zero. * Provide a separate initFromAPInt() implementation to handle the quirks of the format. * Add specific tests to verify the range of values for this format. Signed-off-by: Durgadoss R <durgadossr@nvidia.com>
2024-09-25[LLVM][IR] Add constant range support for floating-point types (#86483)Yingwei Zheng
This patch adds basic constant range support for floating-point types to enable range-based optimizations.
2024-08-25Revert "Enable logf128 constant folding for hosts with 128bit long double ↵NAKAMURA Takumi
(#104929)" ConstantFolding behaves differently depending on host's `HAS_IEE754_FLOAT128`. LLVM should not change the behavior depending on host configurations. This reverts commit 14c7e4a1844904f3db9b2dc93b722925a8c66b27. (llvmorg-20-init-3262-g14c7e4a18449 and llvmorg-20-init-3498-g001e423ac626)
2024-08-22Enable logf128 constant folding for hosts with 128bit long double (#104929)Matthew Devereau
This is a reland of (#96287). This patch attempts to reduce the reverted patch's clang compile time by removing #includes of float128.h and inlining convertToQuad functions instead.
2024-08-14Revert "Reland logf128 constant folding (#103217)"Nikita Popov
This reverts commit 3cab7c555ad6451f2b1b4dc918a4b4f4e4a3e45d. The modified test fails on ppc64le buildbots.
2024-08-14Reland logf128 constant folding (#103217)Matthew Devereau
This is a reland of #96287. This change makes tests in logf128.ll ignore the sign of NaNs for negative value tests and moves an #include <cmath> to be blocked behind #ifndef _GLIBCXX_MATH_H.
2024-08-09Revert "Enable logf128 constant folding for hosts with 128bit floats (#96287)"Nikita Popov
This reverts commit ccb2b011e577e861254f61df9c59494e9e122b38. Causes buildbot failures, e.g. on ppc64le builders.
2024-08-09Enable logf128 constant folding for hosts with 128bit floats (#96287)Matthew Devereau
Hosts which support a float size of 128 bits can benefit from constant fp128 folding.
2024-07-30[APFloat] Add support for f8E3M4 IEEE 754 type (#99698)Alexander Pivovarov
This PR adds `f8E4M3` type to APFloat. `f8E3M4` type follows IEEE 754 convention ```c f8E3M4 (IEEE 754) - Exponent bias: 3 - Maximum stored exponent value: 6 (binary 110) - Maximum unbiased exponent value: 6 - 3 = 3 - Minimum stored exponent value: 1 (binary 001) - Minimum unbiased exponent value: 1 − 3 = −2 - Precision specifies the total number of bits used for the significand (mantissa), including implicit leading integer bit = 4 + 1 = 5 - Follows IEEE 754 conventions for representation of special values - Has Positive and Negative zero - Has Positive and Negative infinity - Has NaNs Additional details: - Max exp (unbiased): 3 - Min exp (unbiased): -2 - Infinities (+/-): S.111.0000 - Zeros (+/-): S.000.0000 - NaNs: S.111.{0,1}⁴ except S.111.0000 - Max normal number: S.110.1111 = +/-2^(6-3) x (1 + 15/16) = +/-2^3 x 31 x 2^(-4) = +/-15.5 - Min normal number: S.001.0000 = +/-2^(1-3) x (1 + 0) = +/-2^(-2) - Max subnormal number: S.000.1111 = +/-2^(-2) x 15/16 = +/-2^(-2) x 15 x 2^(-4) = +/-15 x 2^(-6) - Min subnormal number: S.000.0001 = +/-2^(-2) x 1/16 = +/-2^(-2) x 2^(-4) = +/-2^(-6) ``` Related PRs: - [PR-97179](https://github.com/llvm/llvm-project/pull/97179) [APFloat] Add support for f8E4M3 IEEE 754 type
2024-07-17[APFloat] Add support for f8E4M3 IEEE 754 type (#97179)Alexander Pivovarov
This PR adds `f8E4M3` type to APFloat. `f8E4M3` type follows IEEE 754 convention ```c f8E4M3 (IEEE 754) - Exponent bias: 7 - Maximum stored exponent value: 14 (binary 1110) - Maximum unbiased exponent value: 14 - 7 = 7 - Minimum stored exponent value: 1 (binary 0001) - Minimum unbiased exponent value: 1 − 7 = −6 - Precision specifies the total number of bits used for the significand (mantisa), including implicit leading integer bit = 3 + 1 = 4 - Follows IEEE 754 conventions for representation of special values - Has Positive and Negative zero - Has Positive and Negative infinity - Has NaNs Additional details: - Max exp (unbiased): 7 - Min exp (unbiased): -6 - Infinities (+/-): S.1111.000 - Zeros (+/-): S.0000.000 - NaNs: S.1111.{001, 010, 011, 100, 101, 110, 111} - Max normal number: S.1110.111 = +/-2^(7) x (1 + 0.875) = +/-240 - Min normal number: S.0001.000 = +/-2^(-6) - Max subnormal number: S.0000.111 = +/-2^(-6) x 0.875 = +/-2^(-9) x 7 - Min subnormal number: S.0000.001 = +/-2^(-6) x 0.125 = +/-2^(-9) ``` Related PRs: - [PR-97118](https://github.com/llvm/llvm-project/pull/97118) Add f8E4M3 IEEE 754 type to mlir
2024-07-04[NFC] [APFloat] Refactor IEEEFloat::toString (#97117)Ariel-Burton
This PR lifts the body of IEEEFloat::toString out to a standalone function. We do this to facilitate code sharing with other floating point types, e.g., the forthcoming support for HexFloat. There is no change in functionality.
2024-06-29Rename f8E4M3 to f8E4M3FN in mlir.extras.types py package (#97102)Alexander Pivovarov
Currently `f8E4M3` is mapped to `Float8E4M3FNType`. This PR renames `f8E4M3` to `f8E4M3FN` to accurately reflect the actual type. This PR is needed to avoid names conflict in upcoming PR which will add IEEE 754 `Float8E4M3Type`. https://github.com/llvm/llvm-project/pull/97118 Add f8E4M3 IEEE 754 type Maksim, can you review this PR? @makslevental ?
2024-06-14[APFloat] Add APFloat support for FP4 data type (#95392)Durgadoss R
This patch adds APFloat type support for the E2M1 FP4 datatype. The definitions for this format are detailed in section 5.3.3 of the OCP specification, which can be accessed here: https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf Signed-off-by: Durgadoss R <durgadossr@nvidia.com>
2024-06-11[APFloat] Add APFloat support for FP6 data types (#94735)Durgadoss R
This patch adds APFloat type support for two FP6 data types, E2M3 and E3M2. The definitions for the two formats are detailed in section 5.3.2 of the OCP specification, which can be accessed here: https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf Signed-off-by: Durgadoss R <durgadossr@nvidia.com>
2024-05-29Constant Fold logf128 calls (#90611)Matthew Devereau
This is a second attempt to land #84501 which failed on several targets. This patch adds the HAS_IEE754_FLOAT128 define which makes the check for typedef'ing float128 more precise by checking whether __uint128_t is available and checking if the host does not use __ibm128 which is prevalent on power pc targets and replaces IEEE754 float128s.
2024-05-14[APFloat] Replace partsCount array with single variable (NFC) (#91910)Nikita Popov
We only ever use the last element of this array, so there shouldn't be a need to store the preceding elements as well.
2024-05-04[Support] Use StringRef::operator== instead of StringRef::equals (NFC) (#91042)Kazu Hirata
I'm planning to remove StringRef::equals in favor of StringRef::operator==. - StringRef::operator== outnumbers StringRef::equals by a factor of 25 under llvm/ in terms of their usage. - The elimination of StringRef::equals brings StringRef closer to std::string_view, which has operator== but not equals. - S == "foo" is more readable than S.equals("foo"), especially for !Long.Expression.equals("str") vs Long.Expression != "str".
2024-05-01Revert "Constant Fold logf128 calls"Matt Devereau
This reverts commit 088aa81a545421933254f19cd3c8914a0373b493.
2024-05-01Constant Fold logf128 callsMatt Devereau
This is a second attempt to land #84501 which failed on several targets. This patch adds the HAS_IEE754_FLOAT128 define which makes the check for typedef'ing float128 more precise by checking whether __uint128_t is available and checking if the host does not use __ibm128 which is prevalent on power pc targets and replaces IEEE754 float128s.
2024-04-18Revert "Constant Fold Logf128 calls (#84501)"Matt Devereau
This reverts commit e90bc9cfd4d22c89dd993f62ede700ae25df49c5.
2024-04-18Constant Fold Logf128 calls (#84501)Matthew Devereau
This patch enables constant folding for 128 bit floating-point logf calls. This is achieved by querying if the host system has the logf128() symbol available with a CMake test. If so, replace the runtime call with the compile time value returned from logf128.
2023-11-03[Support] Use StringRef::starts_with/ends_with instead of ↵Simon Pilgrim
startswith/endswith. NFC. startswith/endswith wrap starts_with/ends_with and will eventually go away (to more closely match string_view)
2023-08-23APFloat: Add getExactLog2AbsMatt Arsenault
Like the recently added getExactLog2 except ignore the sign bit. https://reviews.llvm.org/D158102
2023-08-07APFloat: Add getExactLog2Matt Arsenault
https://reviews.llvm.org/D157108
2023-06-23[APFloat] Add APFloat semantic support for TF32Jeremy Furtek
This diff adds APFloat support for a semantic that matches the TF32 data type used by some accelerators (most notably GPUs from both NVIDIA and AMD). For more information on the TF32 data type, see https://blogs.nvidia.com/blog/2020/05/14/tensorfloat-32-precision-format/. Some intrinsics that support the TF32 data type were added in https://reviews.llvm.org/D122044. For some discussion on supporting common semantics in `APFloat`, see similar efforts for 8-bit formats at https://reviews.llvm.org/D146441, as well as https://discourse.llvm.org/t/rfc-adding-the-amd-graphcore-maybe-others-float8-formats-to-apfloat/67969. A subsequent diff will extend MLIR to use this data type. (Those changes are not part of this diff to simplify the review process.) Reviewed By: mehdi_amini Differential Revision: https://reviews.llvm.org/D151923
2023-04-21ValueTracking: Implement computeKnownFPClass for fpextMatt Arsenault
2023-04-04[APFloat] Refactor common code for APFloat<->APInt conversionDavid Majnemer
All the IEEE formats are quite similar, we can merge their code effectively by writing it parametrically via the fltSemantics object. We can metaprogram the implementation such that this parametricity is zero-cost.
2023-03-24[APFloat] Add E4M3B11FNUZDavid Majnemer
X. Sun et al. (https://dl.acm.org/doi/10.5555/3454287.3454728) published a paper showing that an FP format with 4 bits of exponent, 3 bits of significand and an exponent bias of 11 would work quite well for ML applications. Google hardware supports a variant of this format where 0x80 is used to represent NaN, as in the Float8E4M3FNUZ format. Just like the Float8E4M3FNUZ format, this format does not support -0 and values which would map to it will become +0. This format is proposed for inclusion in OpenXLA's StableHLO dialect: https://github.com/openxla/stablehlo/pull/1308 As part of inclusion in that dialect, APFloat needs to know how to handle this format. Differential Revision: https://reviews.llvm.org/D146441
2023-03-03APFloat: Add classifyMatt Arsenault
2023-02-19Use APInt::count{l,r}_{zero,one} (NFC)Kazu Hirata
2023-02-14[NFC] Replace -1U{LL} expressions with appropriate *_MAX macros in Support ↵Pavel Kopyl
library. This makes a code a bit more clear and also gets rid of C4146 warning on MSVC compiler: 'unary minus operator applied to unsigned type, result still unsigned'. In case uint64_t variable is initialized or compared against -1U expression, which corresponds to 32-bit constant, UINT_MAX macro is used to preserve NFC semantics; -1ULL is replaced with UINT64_MAX. Reviewed By: dblaikie, craig.topper Differential Revision: https://reviews.llvm.org/D143942
2023-02-09[llvm][APFloat] Add NaN-in-negative-zero formats by AMD and GraphCoreKrzysztof Drewniak
AMD, GraphCore, and Qualcom have published a standard for 8-bit floats that differs from the 8-bit floats defined by Nvidia, Intel, and ARM. This commit adds support for these alternate 8-bit floats to APFloat in order to enable their usage in MLIR. These formats are presented in the paper at https://arxiv.org/abs/2206.02915 and are implemented in GRaphCore hardware whose ISA is available at https://docs.graphcore.ai/projects/isa-mk2-with-fp8/en/latest/_static/TileVertexISA-IPU21-1.3.1.pdf . In these formats, like the existing Float8E4M3FN, there are no infinity values and there is only one NaN. Unlike in that format, however, the NaN values is 0x80, which would be negative 0 in IEEE formats. This means that these formats also make 0 unsigned. To allow for these new variant semantics, this commit adds fltNanEncoding, which can be IEEE (the default), AllOnes (used by Fleat8E4M3FN), or NegativeZero (used by the new formats, Float8E5M2FNUZ and Float8E4M3FNUZ). Normalization, arithmetic, and other such routines have been updated to account for the potential variant semantics. The two new formats are Float8E5M2FNUZ (5 bits exponent, 2 bits mantissa, finite, unsigned zero) and Float8E4M3FNUZ (4 bits exponent, 3 bits mantissa, finite, unsigned zero). Reviewed By: jakeh-gc, reedwm, lattner Differential Revision: https://reviews.llvm.org/D141863
2023-01-30[DAGCombine] fp_to_sint isSaturatingMinMaxSamuel Parker
Recommitting after fixing scalable vector crash. Check for single smax pattern against zero when converting from a small enough float. Differential Revision: https://reviews.llvm.org/D142481
2023-01-29[Support] Use std::clamp (NFC)Kazu Hirata
2023-01-27Revert "[DAGCombine] fp_to_sint isSaturatingMinMax"Samuel Parker
This reverts commit 85395af27241ab9c8d5763b8afcaa07f1bab26d5. This is causing trouble with scalable vectors.
2023-01-26[DAGCombine] fp_to_sint isSaturatingMinMaxSamuel Parker
Check for single smax pattern against zero when converting from a small enough float. Differential Revision: https://reviews.llvm.org/D142481