Skip to content

Commit

Permalink
refactor: 为 common 和 graph_topo 添加所有 noexcept
Browse files Browse the repository at this point in the history
Signed-off-by: YdrMaster <ydrml@hotmail.com>
  • Loading branch information
YdrMaster committed Oct 6, 2023
1 parent 92fc620 commit fac19f1
Show file tree
Hide file tree
Showing 22 changed files with 438 additions and 413 deletions.
18 changes: 9 additions & 9 deletions src/00common/include/common/bf16_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,38 @@ namespace refactor::common {
constexpr bf16_t(bf16_t const &) noexcept = default;
constexpr bf16_t(bf16_t &&) noexcept = default;

constexpr uint16_t as_code() const {
constexpr uint16_t as_code() const noexcept {
return code;
}

constexpr float to_f32() const {
constexpr float to_f32() const noexcept {
converter c{};
c.u16[1] = code;
c.u16[0] = 0;
return c.f32;
}

constexpr bool is_inf() const {
constexpr bool is_inf() const noexcept {
return std::isinf(to_f32());
}

constexpr bool is_nan() const {
constexpr bool is_nan() const noexcept {
return std::isnan(to_f32());
}

constexpr bf16_t operator-() const {
return (uint16_t) (code ^ (code | MASK_SIGN16));
constexpr bf16_t operator-() const noexcept {
return static_cast<decltype(code)>(code ^ (code | MASK_SIGN16));
}

constexpr bool operator==(bf16_t const &others) const {
constexpr bool operator==(bf16_t const &others) const noexcept {
return code == others.code && !is_nan();
}

constexpr bool operator!=(bf16_t const &others) const {
constexpr bool operator!=(bf16_t const &others) const noexcept {
return !operator==(others);
}

std::string to_string() const {
std::string to_string() const noexcept {
return std::to_string(to_f32());
}
};
Expand Down
33 changes: 17 additions & 16 deletions src/00common/include/common/data_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,26 @@ namespace refactor::common {
BF16 = 16, // bf16_t
} internal;

constexpr DataType(decltype(internal) i) : internal(i) {}
constexpr DataType(decltype(internal) i) noexcept
: internal(i) {}

static std::optional<DataType> parse(uint8_t);
static std::optional<DataType> parse(uint8_t) noexcept;

bool operator==(DataType const &) const;
bool operator!=(DataType const &) const;
bool operator<(DataType const &) const;
bool operator>(DataType const &) const;
bool operator<=(DataType const &) const;
bool operator>=(DataType const &) const;
bool operator==(DataType const &) const noexcept;
bool operator!=(DataType const &) const noexcept;
bool operator<(DataType const &) const noexcept;
bool operator>(DataType const &) const noexcept;
bool operator<=(DataType const &) const noexcept;
bool operator>=(DataType const &) const noexcept;

std::string_view name() const;
bool isIeee754() const;
bool isFloat() const;
bool isSignedLarge() const;
bool isSigned() const;
bool isNumberic() const;
bool isBool() const;
size_t size() const;
std::string_view name() const noexcept;
bool isIeee754() const noexcept;
bool isFloat() const noexcept;
bool isSignedLarge() const noexcept;
bool isSigned() const noexcept;
bool isNumberic() const noexcept;
bool isBool() const noexcept;
size_t size() const noexcept;
};

template<class T>
Expand Down
54 changes: 29 additions & 25 deletions src/00common/include/common/error_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,35 @@
#include <fmt/format.h>
#include <stdexcept>

inline std::string buildMsg(std::string msg, const char *file, int line) {
msg += " Source ";
msg += file;
msg += ':';
msg += std::to_string(line);
return msg;
}

struct UnimplementError : public std::logic_error {
explicit UnimplementError(std::string msg)
: std::logic_error(std::move(msg)) {}
};

struct UnreachableError : public std::logic_error {
explicit UnreachableError(std::string msg)
: std::logic_error(std::move(msg)) {}
};

#define RUNTIME_ERROR(msg) throw std::runtime_error(buildMsg(msg, __FILE__, __LINE__))
#define OUT_OF_RANGE(msg, a, b) throw std::out_of_range(buildMsg((std::to_string(a) + '/' + std::to_string(b) + ' ' + msg), __FILE__, __LINE__))
#define TODO(msg) throw UnimplementError(buildMsg(msg, __FILE__, __LINE__))

#define UNREACHABLEX(T, F, ...) \
[&]() -> T { \
throw UnreachableError(buildMsg(fmt::format("Unreachable: " #F, ##__VA_ARGS__), __FILE__, __LINE__)); \
namespace refactor::common::error {
inline std::string buildMsg(std::string msg, const char *file, int line) noexcept {
msg += " Source ";
msg += file;
msg += ':';
msg += std::to_string(line);
return msg;
}

struct UnimplementError : public std::logic_error {
explicit UnimplementError(std::string msg)
: std::logic_error(std::move(msg)) {}
};

struct UnreachableError : public std::logic_error {
explicit UnreachableError(std::string msg)
: std::logic_error(std::move(msg)) {}
};

}// namespace refactor::common::error

#define ERROR_MSG(msg) refactor::common::error::buildMsg(msg, __FILE__, __LINE__)
#define RUNTIME_ERROR(msg) throw std::runtime_error(ERROR_MSG(msg))
#define OUT_OF_RANGE(msg, a, b) throw std::out_of_range(ERROR_MSG((std::to_string(a) + '/' + std::to_string(b) + ' ' + msg)))
#define TODO(msg) throw refactor::common::error::UnimplementError(ERROR_MSG(msg))

#define UNREACHABLEX(T, F, ...) \
[&]() -> T { \
throw refactor::common::error::UnreachableError(ERROR_MSG(fmt::format("Unreachable: " #F, ##__VA_ARGS__))); \
}()
#define UNREACHABLE() UNREACHABLEX(void, "no message")

Expand Down
22 changes: 11 additions & 11 deletions src/00common/include/common/fp16_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ namespace refactor::common {
const static uint16_t MASK_EXP_16 = 0b0'11111'0000000000;
const static uint16_t MASK_TAIL16 = 0b0'00000'1111111111;

constexpr static uint16_t mask_low(int bits) {
constexpr static uint16_t mask_low(int bits) noexcept {
return (1 << bits) - 1;
}

constexpr static uint16_t from_f32(float val) {
constexpr static uint16_t from_f32(float val) noexcept {
union {
float f32;
uint32_t u32;
Expand All @@ -37,11 +37,11 @@ namespace refactor::common {
constexpr fp16_t(fp16_t const &) noexcept = default;
constexpr fp16_t(fp16_t &&) noexcept = default;

constexpr uint16_t as_code() const {
constexpr uint16_t as_code() const noexcept {
return code;
}

constexpr float to_f32() const {
constexpr float to_f32() const noexcept {
union {
uint32_t u32;
float f32;
Expand All @@ -52,27 +52,27 @@ namespace refactor::common {
return ans.f32;
}

constexpr bool is_inf() const {
constexpr bool is_inf() const noexcept {
return MASK_EXP_16 == (code & MASK_EXP_16) && 0 == (code & MASK_TAIL16);
}

constexpr bool is_nan() const {
constexpr bool is_nan() const noexcept {
return MASK_EXP_16 == (code & MASK_EXP_16) && 0 != (code & MASK_TAIL16);
}

constexpr fp16_t operator-() const {
constexpr fp16_t operator-() const noexcept {
return (uint16_t) (code ^ (code | MASK_SIGN16));
}

constexpr bool operator==(fp16_t const &others) const {
constexpr bool operator==(fp16_t const &others) const noexcept {
return code == others.code && !is_nan();
}

constexpr bool operator!=(fp16_t const &others) const {
constexpr bool operator!=(fp16_t const &others) const noexcept {
return !operator==(others);
}

constexpr std::array<char, 38> format() const {
constexpr std::array<char, 38> format() const noexcept {
// 将 fp16 格式化字符串保存到栈上的内存块上。
std::array<char, 38> ans{"0'00000'0000000000\n+ 2^-15x1. "};
ans[0] += (code >> 15);
Expand All @@ -94,7 +94,7 @@ namespace refactor::common {
return ans;
}

std::string to_string() const {
std::string to_string() const noexcept {
return std::string(format().data());
}
};
Expand Down
40 changes: 20 additions & 20 deletions src/00common/include/common/natural.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ namespace refactor::common {
size_t _i;

public:
natural_t(t val) : _i(val) {}
bool operator==(natural_t const &rhs) const { return _i == rhs._i; }
bool operator!=(natural_t const &rhs) const { return _i != rhs._i; }
bool operator<(natural_t const &rhs) const { return _i < rhs._i; }
bool operator>(natural_t const &rhs) const { return _i > rhs._i; }
bool operator<=(natural_t const &rhs) const { return _i <= rhs._i; }
bool operator>=(natural_t const &rhs) const { return _i >= rhs._i; }
natural_t &operator++() {
natural_t(t val) noexcept : _i(val) {}
bool operator==(natural_t const &rhs) const noexcept { return _i == rhs._i; }
bool operator!=(natural_t const &rhs) const noexcept { return _i != rhs._i; }
bool operator<(natural_t const &rhs) const noexcept { return _i < rhs._i; }
bool operator>(natural_t const &rhs) const noexcept { return _i > rhs._i; }
bool operator<=(natural_t const &rhs) const noexcept { return _i <= rhs._i; }
bool operator>=(natural_t const &rhs) const noexcept { return _i >= rhs._i; }
natural_t &operator++() noexcept {
++_i;
return *this;
}
natural_t operator++(int) {
natural_t operator++(int) noexcept {
auto ans = *this;
operator++();
return ans;
}
t operator*() const {
t operator*() const noexcept {
return _i;
}
};
Expand All @@ -36,23 +36,23 @@ namespace refactor::common {
size_t _i;

public:
rev_natural_t(t val) : _i(val - 1) {}
bool operator==(rev_natural_t const &rhs) const { return _i == rhs._i; }
bool operator!=(rev_natural_t const &rhs) const { return _i != rhs._i; }
bool operator<(rev_natural_t const &rhs) const { return _i > rhs._i; }
bool operator>(rev_natural_t const &rhs) const { return _i < rhs._i; }
bool operator<=(rev_natural_t const &rhs) const { return _i >= rhs._i; }
bool operator>=(rev_natural_t const &rhs) const { return _i <= rhs._i; }
rev_natural_t &operator++() {
rev_natural_t(t val) noexcept : _i(val - 1) {}
bool operator==(rev_natural_t const &rhs) const noexcept { return _i == rhs._i; }
bool operator!=(rev_natural_t const &rhs) const noexcept { return _i != rhs._i; }
bool operator<(rev_natural_t const &rhs) const noexcept { return _i > rhs._i; }
bool operator>(rev_natural_t const &rhs) const noexcept { return _i < rhs._i; }
bool operator<=(rev_natural_t const &rhs) const noexcept { return _i >= rhs._i; }
bool operator>=(rev_natural_t const &rhs) const noexcept { return _i <= rhs._i; }
rev_natural_t &operator++() noexcept {
--_i;
return *this;
}
rev_natural_t operator++(int) {
rev_natural_t operator++(int) noexcept {
auto ans = *this;
operator++();
return ans;
}
t operator*() const {
t operator*() const noexcept {
return _i;
}
};
Expand Down
24 changes: 12 additions & 12 deletions src/00common/include/common/range.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ namespace refactor::common {

using Iterator = common::natural_t<t>;

bool empty() const { return end_ == begin_; }
size_t size() const { return end_ - begin_; }
t at(size_t i) const {
bool empty() const noexcept { return end_ == begin_; }
size_t size() const noexcept { return end_ - begin_; }
t at(size_t i) const noexcept {
ASSERT(i < size(), "Index out of range");
return operator[](i);
}
t operator[](size_t i) const { return begin_ + i; }
Iterator begin() const { return begin_; }
Iterator end() const { return end_; }
rev_range_t<t> rev() const { return {end_, begin_}; }
t operator[](size_t i) const noexcept { return begin_ + i; }
Iterator begin() const noexcept { return begin_; }
Iterator end() const noexcept { return end_; }
rev_range_t<t> rev() const noexcept { return {end_, begin_}; }
};

template<class t = size_t>
Expand All @@ -32,15 +32,15 @@ namespace refactor::common {

using Iterator = common::rev_natural_t<t>;

bool empty() const { return end_ == begin_; }
size_t size() const { return end_ - begin_; }
bool empty() const noexcept { return end_ == begin_; }
size_t size() const noexcept { return end_ - begin_; }
t at(size_t i) const {
ASSERT(i < size(), "Index out of range");
return operator[](i);
}
t operator[](size_t i) const { return begin_ + i; }
Iterator begin() const { return begin_; }
Iterator end() const { return end_; }
t operator[](size_t i) const noexcept { return begin_ + i; }
Iterator begin() const noexcept { return begin_; }
Iterator end() const noexcept { return end_; }
};

template<class t = size_t> range_t<t> range0_(t end) {
Expand Down
16 changes: 8 additions & 8 deletions src/00common/include/common/slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ namespace refactor::common {

using Iterator = t const *;

bool empty() const { return end_ == begin_; }
size_t size() const { return end_ - begin_; }
bool empty() const noexcept { return end_ == begin_; }
size_t size() const noexcept { return end_ - begin_; }
t const &at(size_t i) const {
ASSERT(i < size(), "Index out of range");
return operator[](i);
}
t const &operator[](int i) const { return begin_[i]; }
Iterator begin() const { return begin_; }
Iterator end() const { return end_; }
t const &operator[](int i) const noexcept { return begin_[i]; }
Iterator begin() const noexcept { return begin_; }
Iterator end() const noexcept { return end_; }
};

template<class t> slice_t<t> slice(t const *begin, t const *end) { return {begin, end}; }
template<class t> slice_t<t> slice(t const *begin, int64_t size) { return {begin, begin + size}; }
template<class t> slice_t<t> slice(t const *begin, size_t size) { return {begin, begin + size}; }
template<class t> slice_t<t> slice(t const *begin, t const *end) noexcept { return {begin, end}; }
template<class t> slice_t<t> slice(t const *begin, int64_t size) noexcept { return {begin, begin + size}; }
template<class t> slice_t<t> slice(t const *begin, size_t size) noexcept { return {begin, begin + size}; }
}// namespace refactor::common

#endif// SLICE_H
Loading

0 comments on commit fac19f1

Please sign in to comment.