aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/ADT/APFloat.h
diff options
context:
space:
mode:
authorRichard-Qin-X <richard.qin.001@gmail.com>2026-08-13 06:52:15 +0800
committerRichard-Qin-X <richard.qin.001@gmail.com>2026-08-13 06:52:15 +0800
commitaa7c85ca71859aab15bdf1e6dc1ea3a5aa5d1e28 (patch)
tree20d94c4debfe4900e362c6c7f8443bcfbd58ec28 /backend/include/rcc/ADT/APFloat.h
parentd62f022c92fcc3ebe2269273628943a0089683fd (diff)
downloadrcc-project-aa7c85ca71859aab15bdf1e6dc1ea3a5aa5d1e28.tar.gz
rcc-project-aa7c85ca71859aab15bdf1e6dc1ea3a5aa5d1e28.tar.xz
[Support] Split APInt and APFloat ownership
Diffstat (limited to 'backend/include/rcc/ADT/APFloat.h')
-rw-r--r--backend/include/rcc/ADT/APFloat.h78
1 files changed, 78 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