From aa7c85ca71859aab15bdf1e6dc1ea3a5aa5d1e28 Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Thu, 13 Aug 2026 06:52:15 +0800 Subject: [Support] Split APInt and APFloat ownership --- backend/include/rcc/ADT/APFloat.h | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 backend/include/rcc/ADT/APFloat.h (limited to 'backend/include/rcc/ADT/APFloat.h') diff --git a/backend/include/rcc/ADT/APFloat.h b/backend/include/rcc/ADT/APFloat.h new file mode 100644 index 0000000..9b56a19 --- /dev/null +++ b/backend/include/rcc/ADT/APFloat.h @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_ADT_APFLOAT_H +#define RCC_ADT_APFLOAT_H + +#include "rcc/Support/Error.h" + +#include +#include +#include +#include +#include +#include + +namespace rcc { + +enum class FloatSemantics : std::uint8_t { + IEEEHalf, + BFloat, + IEEESingle, + IEEEDouble, + IEEEQuad, + PPCDoubleDouble, + Float8E5M2, + Float8E5M2FNUZ, + Float8E4M3, + Float8E4M3FN, + Float8E4M3FNUZ, + Float8E4M3B11FNUZ, + Float8E3M4, + FloatTF32, + Float8E8M0FNU, + Float6E3M2FN, + Float6E2M3FN, + Float4E2M1FN, + X87DoubleExtended, + Count, +}; + +[[nodiscard]] std::optional +getFloatSemanticsBitWidth(FloatSemantics Semantics) noexcept; +[[nodiscard]] std::string_view getFloatSemanticsSpelling(FloatSemantics Semantics) noexcept; + +class APFloat final { +public: + [[nodiscard]] static Expected create(FloatSemantics Semantics, + std::span Words); + [[nodiscard]] static Expected parse(FloatSemantics Semantics, std::string_view Text); + + [[nodiscard]] FloatSemantics semantics() const noexcept { + return Semantics; + } + + [[nodiscard]] std::uint32_t bitWidth() const noexcept { + return getFloatSemanticsBitWidth(Semantics).value_or(0); + } + + [[nodiscard]] std::span words() const noexcept { + return std::span(Words).first(WordCount); + } + + [[nodiscard]] std::string toString() const; + + friend bool operator==(const APFloat &Left, const APFloat &Right) = default; + +private: + APFloat(FloatSemantics Semantics, std::array Words, + std::uint8_t WordCount) noexcept + : Semantics(Semantics), Words(Words), WordCount(WordCount) {} + + FloatSemantics Semantics; + std::array Words; + std::uint8_t WordCount; +}; + +} // namespace rcc + +#endif // RCC_ADT_APFLOAT_H -- cgit v1.2.3