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 +++++++++++++++++++++++++++++++++++++++ backend/include/rcc/ADT/APInt.h | 74 +++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 backend/include/rcc/ADT/APFloat.h create mode 100644 backend/include/rcc/ADT/APInt.h (limited to 'backend/include/rcc/ADT') 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 diff --git a/backend/include/rcc/ADT/APInt.h b/backend/include/rcc/ADT/APInt.h new file mode 100644 index 0000000..abd10f9 --- /dev/null +++ b/backend/include/rcc/ADT/APInt.h @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_ADT_APINT_H +#define RCC_ADT_APINT_H + +#include "rcc/Support/Error.h" + +#include +#include +#include +#include +#include +#include +#include + +namespace rcc { + +class APInt final { +public: + static constexpr std::uint32_t MaxBitWidth = std::uint32_t{1} << 23U; + + APInt() = default; + explicit APInt(std::uint64_t Value); + + [[nodiscard]] static Expected create(std::uint32_t BitWidth, + std::span Words); + [[nodiscard]] static Expected fromUnsigned(std::uint32_t BitWidth, std::uint64_t Value); + [[nodiscard]] static Expected fromSigned(std::uint32_t BitWidth, std::int64_t Value); + [[nodiscard]] static Expected parseDecimal(std::uint32_t BitWidth, std::string_view Text); + [[nodiscard]] static Expected parseLLVMHex(std::uint32_t BitWidth, std::string_view Text); + + [[nodiscard]] std::uint32_t bitWidth() const noexcept { + return BitWidth; + } + + [[nodiscard]] std::span words() const noexcept { + return Words; + } + + [[nodiscard]] bool isZero() const noexcept; + [[nodiscard]] bool isNegative() const noexcept; + [[nodiscard]] std::size_t activeBitWidth() const noexcept; + [[nodiscard]] std::size_t countrZero() const noexcept; + [[nodiscard]] std::uint64_t word(std::size_t Index) const noexcept; + [[nodiscard]] int compare(const APInt &Other) const noexcept; + [[nodiscard]] int compareToShifted(const APInt &Other, std::size_t OtherShift) const noexcept; + + void multiplySmall(std::uint32_t Multiplier); + void addSmall(std::uint32_t Value); + void setBit(std::size_t Bit); + void clearBit(std::size_t Bit) noexcept; + void shiftRight(std::size_t Amount) noexcept; + [[nodiscard]] APInt shiftedLeft(std::size_t Amount) const; + void subtract(const APInt &Other) noexcept; + + [[nodiscard]] std::string toUnsignedDecimal() const; + [[nodiscard]] std::string toSignedDecimal() const; + [[nodiscard]] Expected verify() const; + + friend bool operator==(const APInt &Left, const APInt &Right) = default; + +private: + APInt(std::uint32_t BitWidth, std::vector Words) + : BitWidth(BitWidth), Words(std::move(Words)) {} + + void normalizeMagnitude() noexcept; + + std::uint32_t BitWidth = 0; + std::vector Words; +}; + +} // namespace rcc + +#endif // RCC_ADT_APINT_H -- cgit v1.2.3