aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/ADT/APFloat.h
blob: 9b56a19e438b7bd3456b32e77ff45afe18be5743 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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