diff options
| author | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-13 06:52:15 +0800 |
|---|---|---|
| committer | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-13 06:52:15 +0800 |
| commit | aa7c85ca71859aab15bdf1e6dc1ea3a5aa5d1e28 (patch) | |
| tree | 20d94c4debfe4900e362c6c7f8443bcfbd58ec28 /backend/include/rcc/ADT | |
| parent | d62f022c92fcc3ebe2269273628943a0089683fd (diff) | |
| download | rcc-project-aa7c85ca71859aab15bdf1e6dc1ea3a5aa5d1e28.tar.gz rcc-project-aa7c85ca71859aab15bdf1e6dc1ea3a5aa5d1e28.tar.xz | |
[Support] Split APInt and APFloat ownership
Diffstat (limited to 'backend/include/rcc/ADT')
| -rw-r--r-- | backend/include/rcc/ADT/APFloat.h | 78 | ||||
| -rw-r--r-- | backend/include/rcc/ADT/APInt.h | 74 |
2 files changed, 152 insertions, 0 deletions
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 <array> +#include <cstdint> +#include <optional> +#include <span> +#include <string> +#include <string_view> + +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<std::uint32_t> +getFloatSemanticsBitWidth(FloatSemantics Semantics) noexcept; +[[nodiscard]] std::string_view getFloatSemanticsSpelling(FloatSemantics Semantics) noexcept; + +class APFloat final { +public: + [[nodiscard]] static Expected<APFloat> create(FloatSemantics Semantics, + std::span<const std::uint64_t> Words); + [[nodiscard]] static Expected<APFloat> 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<const std::uint64_t> 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<std::uint64_t, 2> Words, + std::uint8_t WordCount) noexcept + : Semantics(Semantics), Words(Words), WordCount(WordCount) {} + + FloatSemantics Semantics; + std::array<std::uint64_t, 2> 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 <cstddef> +#include <cstdint> +#include <span> +#include <string> +#include <string_view> +#include <utility> +#include <vector> + +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<APInt> create(std::uint32_t BitWidth, + std::span<const std::uint64_t> Words); + [[nodiscard]] static Expected<APInt> fromUnsigned(std::uint32_t BitWidth, std::uint64_t Value); + [[nodiscard]] static Expected<APInt> fromSigned(std::uint32_t BitWidth, std::int64_t Value); + [[nodiscard]] static Expected<APInt> parseDecimal(std::uint32_t BitWidth, std::string_view Text); + [[nodiscard]] static Expected<APInt> parseLLVMHex(std::uint32_t BitWidth, std::string_view Text); + + [[nodiscard]] std::uint32_t bitWidth() const noexcept { + return BitWidth; + } + + [[nodiscard]] std::span<const std::uint64_t> 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<void> verify() const; + + friend bool operator==(const APInt &Left, const APInt &Right) = default; + +private: + APInt(std::uint32_t BitWidth, std::vector<std::uint64_t> Words) + : BitWidth(BitWidth), Words(std::move(Words)) {} + + void normalizeMagnitude() noexcept; + + std::uint32_t BitWidth = 0; + std::vector<std::uint64_t> Words; +}; + +} // namespace rcc + +#endif // RCC_ADT_APINT_H |
