Skip to content

Commit fac19f1

Browse files
committed
refactor: 为 common 和 graph_topo 添加所有 noexcept
Signed-off-by: YdrMaster <ydrml@hotmail.com>
1 parent 92fc620 commit fac19f1

File tree

22 files changed

+438
-413
lines changed

22 files changed

+438
-413
lines changed

src/00common/include/common/bf16_t.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,38 @@ namespace refactor::common {
2424
constexpr bf16_t(bf16_t const &) noexcept = default;
2525
constexpr bf16_t(bf16_t &&) noexcept = default;
2626

27-
constexpr uint16_t as_code() const {
27+
constexpr uint16_t as_code() const noexcept {
2828
return code;
2929
}
3030

31-
constexpr float to_f32() const {
31+
constexpr float to_f32() const noexcept {
3232
converter c{};
3333
c.u16[1] = code;
3434
c.u16[0] = 0;
3535
return c.f32;
3636
}
3737

38-
constexpr bool is_inf() const {
38+
constexpr bool is_inf() const noexcept {
3939
return std::isinf(to_f32());
4040
}
4141

42-
constexpr bool is_nan() const {
42+
constexpr bool is_nan() const noexcept {
4343
return std::isnan(to_f32());
4444
}
4545

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

50-
constexpr bool operator==(bf16_t const &others) const {
50+
constexpr bool operator==(bf16_t const &others) const noexcept {
5151
return code == others.code && !is_nan();
5252
}
5353

54-
constexpr bool operator!=(bf16_t const &others) const {
54+
constexpr bool operator!=(bf16_t const &others) const noexcept {
5555
return !operator==(others);
5656
}
5757

58-
std::string to_string() const {
58+
std::string to_string() const noexcept {
5959
return std::to_string(to_f32());
6060
}
6161
};

src/00common/include/common/data_type.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,26 @@ namespace refactor::common {
3030
BF16 = 16, // bf16_t
3131
} internal;
3232

33-
constexpr DataType(decltype(internal) i) : internal(i) {}
33+
constexpr DataType(decltype(internal) i) noexcept
34+
: internal(i) {}
3435

35-
static std::optional<DataType> parse(uint8_t);
36+
static std::optional<DataType> parse(uint8_t) noexcept;
3637

37-
bool operator==(DataType const &) const;
38-
bool operator!=(DataType const &) const;
39-
bool operator<(DataType const &) const;
40-
bool operator>(DataType const &) const;
41-
bool operator<=(DataType const &) const;
42-
bool operator>=(DataType const &) const;
38+
bool operator==(DataType const &) const noexcept;
39+
bool operator!=(DataType const &) const noexcept;
40+
bool operator<(DataType const &) const noexcept;
41+
bool operator>(DataType const &) const noexcept;
42+
bool operator<=(DataType const &) const noexcept;
43+
bool operator>=(DataType const &) const noexcept;
4344

44-
std::string_view name() const;
45-
bool isIeee754() const;
46-
bool isFloat() const;
47-
bool isSignedLarge() const;
48-
bool isSigned() const;
49-
bool isNumberic() const;
50-
bool isBool() const;
51-
size_t size() const;
45+
std::string_view name() const noexcept;
46+
bool isIeee754() const noexcept;
47+
bool isFloat() const noexcept;
48+
bool isSignedLarge() const noexcept;
49+
bool isSigned() const noexcept;
50+
bool isNumberic() const noexcept;
51+
bool isBool() const noexcept;
52+
size_t size() const noexcept;
5253
};
5354

5455
template<class T>

src/00common/include/common/error_handler.h

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,35 @@
44
#include <fmt/format.h>
55
#include <stdexcept>
66

7-
inline std::string buildMsg(std::string msg, const char *file, int line) {
8-
msg += " Source ";
9-
msg += file;
10-
msg += ':';
11-
msg += std::to_string(line);
12-
return msg;
13-
}
14-
15-
struct UnimplementError : public std::logic_error {
16-
explicit UnimplementError(std::string msg)
17-
: std::logic_error(std::move(msg)) {}
18-
};
19-
20-
struct UnreachableError : public std::logic_error {
21-
explicit UnreachableError(std::string msg)
22-
: std::logic_error(std::move(msg)) {}
23-
};
24-
25-
#define RUNTIME_ERROR(msg) throw std::runtime_error(buildMsg(msg, __FILE__, __LINE__))
26-
#define OUT_OF_RANGE(msg, a, b) throw std::out_of_range(buildMsg((std::to_string(a) + '/' + std::to_string(b) + ' ' + msg), __FILE__, __LINE__))
27-
#define TODO(msg) throw UnimplementError(buildMsg(msg, __FILE__, __LINE__))
28-
29-
#define UNREACHABLEX(T, F, ...) \
30-
[&]() -> T { \
31-
throw UnreachableError(buildMsg(fmt::format("Unreachable: " #F, ##__VA_ARGS__), __FILE__, __LINE__)); \
7+
namespace refactor::common::error {
8+
inline std::string buildMsg(std::string msg, const char *file, int line) noexcept {
9+
msg += " Source ";
10+
msg += file;
11+
msg += ':';
12+
msg += std::to_string(line);
13+
return msg;
14+
}
15+
16+
struct UnimplementError : public std::logic_error {
17+
explicit UnimplementError(std::string msg)
18+
: std::logic_error(std::move(msg)) {}
19+
};
20+
21+
struct UnreachableError : public std::logic_error {
22+
explicit UnreachableError(std::string msg)
23+
: std::logic_error(std::move(msg)) {}
24+
};
25+
26+
}// namespace refactor::common::error
27+
28+
#define ERROR_MSG(msg) refactor::common::error::buildMsg(msg, __FILE__, __LINE__)
29+
#define RUNTIME_ERROR(msg) throw std::runtime_error(ERROR_MSG(msg))
30+
#define OUT_OF_RANGE(msg, a, b) throw std::out_of_range(ERROR_MSG((std::to_string(a) + '/' + std::to_string(b) + ' ' + msg)))
31+
#define TODO(msg) throw refactor::common::error::UnimplementError(ERROR_MSG(msg))
32+
33+
#define UNREACHABLEX(T, F, ...) \
34+
[&]() -> T { \
35+
throw refactor::common::error::UnreachableError(ERROR_MSG(fmt::format("Unreachable: " #F, ##__VA_ARGS__))); \
3236
}()
3337
#define UNREACHABLE() UNREACHABLEX(void, "no message")
3438

src/00common/include/common/fp16_t.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ namespace refactor::common {
1414
const static uint16_t MASK_EXP_16 = 0b0'11111'0000000000;
1515
const static uint16_t MASK_TAIL16 = 0b0'00000'1111111111;
1616

17-
constexpr static uint16_t mask_low(int bits) {
17+
constexpr static uint16_t mask_low(int bits) noexcept {
1818
return (1 << bits) - 1;
1919
}
2020

21-
constexpr static uint16_t from_f32(float val) {
21+
constexpr static uint16_t from_f32(float val) noexcept {
2222
union {
2323
float f32;
2424
uint32_t u32;
@@ -37,11 +37,11 @@ namespace refactor::common {
3737
constexpr fp16_t(fp16_t const &) noexcept = default;
3838
constexpr fp16_t(fp16_t &&) noexcept = default;
3939

40-
constexpr uint16_t as_code() const {
40+
constexpr uint16_t as_code() const noexcept {
4141
return code;
4242
}
4343

44-
constexpr float to_f32() const {
44+
constexpr float to_f32() const noexcept {
4545
union {
4646
uint32_t u32;
4747
float f32;
@@ -52,27 +52,27 @@ namespace refactor::common {
5252
return ans.f32;
5353
}
5454

55-
constexpr bool is_inf() const {
55+
constexpr bool is_inf() const noexcept {
5656
return MASK_EXP_16 == (code & MASK_EXP_16) && 0 == (code & MASK_TAIL16);
5757
}
5858

59-
constexpr bool is_nan() const {
59+
constexpr bool is_nan() const noexcept {
6060
return MASK_EXP_16 == (code & MASK_EXP_16) && 0 != (code & MASK_TAIL16);
6161
}
6262

63-
constexpr fp16_t operator-() const {
63+
constexpr fp16_t operator-() const noexcept {
6464
return (uint16_t) (code ^ (code | MASK_SIGN16));
6565
}
6666

67-
constexpr bool operator==(fp16_t const &others) const {
67+
constexpr bool operator==(fp16_t const &others) const noexcept {
6868
return code == others.code && !is_nan();
6969
}
7070

71-
constexpr bool operator!=(fp16_t const &others) const {
71+
constexpr bool operator!=(fp16_t const &others) const noexcept {
7272
return !operator==(others);
7373
}
7474

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

97-
std::string to_string() const {
97+
std::string to_string() const noexcept {
9898
return std::string(format().data());
9999
}
100100
};

src/00common/include/common/natural.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ namespace refactor::common {
1010
size_t _i;
1111

1212
public:
13-
natural_t(t val) : _i(val) {}
14-
bool operator==(natural_t const &rhs) const { return _i == rhs._i; }
15-
bool operator!=(natural_t const &rhs) const { return _i != rhs._i; }
16-
bool operator<(natural_t const &rhs) const { return _i < rhs._i; }
17-
bool operator>(natural_t const &rhs) const { return _i > rhs._i; }
18-
bool operator<=(natural_t const &rhs) const { return _i <= rhs._i; }
19-
bool operator>=(natural_t const &rhs) const { return _i >= rhs._i; }
20-
natural_t &operator++() {
13+
natural_t(t val) noexcept : _i(val) {}
14+
bool operator==(natural_t const &rhs) const noexcept { return _i == rhs._i; }
15+
bool operator!=(natural_t const &rhs) const noexcept { return _i != rhs._i; }
16+
bool operator<(natural_t const &rhs) const noexcept { return _i < rhs._i; }
17+
bool operator>(natural_t const &rhs) const noexcept { return _i > rhs._i; }
18+
bool operator<=(natural_t const &rhs) const noexcept { return _i <= rhs._i; }
19+
bool operator>=(natural_t const &rhs) const noexcept { return _i >= rhs._i; }
20+
natural_t &operator++() noexcept {
2121
++_i;
2222
return *this;
2323
}
24-
natural_t operator++(int) {
24+
natural_t operator++(int) noexcept {
2525
auto ans = *this;
2626
operator++();
2727
return ans;
2828
}
29-
t operator*() const {
29+
t operator*() const noexcept {
3030
return _i;
3131
}
3232
};
@@ -36,23 +36,23 @@ namespace refactor::common {
3636
size_t _i;
3737

3838
public:
39-
rev_natural_t(t val) : _i(val - 1) {}
40-
bool operator==(rev_natural_t const &rhs) const { return _i == rhs._i; }
41-
bool operator!=(rev_natural_t const &rhs) const { return _i != rhs._i; }
42-
bool operator<(rev_natural_t const &rhs) const { return _i > rhs._i; }
43-
bool operator>(rev_natural_t const &rhs) const { return _i < rhs._i; }
44-
bool operator<=(rev_natural_t const &rhs) const { return _i >= rhs._i; }
45-
bool operator>=(rev_natural_t const &rhs) const { return _i <= rhs._i; }
46-
rev_natural_t &operator++() {
39+
rev_natural_t(t val) noexcept : _i(val - 1) {}
40+
bool operator==(rev_natural_t const &rhs) const noexcept { return _i == rhs._i; }
41+
bool operator!=(rev_natural_t const &rhs) const noexcept { return _i != rhs._i; }
42+
bool operator<(rev_natural_t const &rhs) const noexcept { return _i > rhs._i; }
43+
bool operator>(rev_natural_t const &rhs) const noexcept { return _i < rhs._i; }
44+
bool operator<=(rev_natural_t const &rhs) const noexcept { return _i >= rhs._i; }
45+
bool operator>=(rev_natural_t const &rhs) const noexcept { return _i <= rhs._i; }
46+
rev_natural_t &operator++() noexcept {
4747
--_i;
4848
return *this;
4949
}
50-
rev_natural_t operator++(int) {
50+
rev_natural_t operator++(int) noexcept {
5151
auto ans = *this;
5252
operator++();
5353
return ans;
5454
}
55-
t operator*() const {
55+
t operator*() const noexcept {
5656
return _i;
5757
}
5858
};

src/00common/include/common/range.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ namespace refactor::common {
1414

1515
using Iterator = common::natural_t<t>;
1616

17-
bool empty() const { return end_ == begin_; }
18-
size_t size() const { return end_ - begin_; }
19-
t at(size_t i) const {
17+
bool empty() const noexcept { return end_ == begin_; }
18+
size_t size() const noexcept { return end_ - begin_; }
19+
t at(size_t i) const noexcept {
2020
ASSERT(i < size(), "Index out of range");
2121
return operator[](i);
2222
}
23-
t operator[](size_t i) const { return begin_ + i; }
24-
Iterator begin() const { return begin_; }
25-
Iterator end() const { return end_; }
26-
rev_range_t<t> rev() const { return {end_, begin_}; }
23+
t operator[](size_t i) const noexcept { return begin_ + i; }
24+
Iterator begin() const noexcept { return begin_; }
25+
Iterator end() const noexcept { return end_; }
26+
rev_range_t<t> rev() const noexcept { return {end_, begin_}; }
2727
};
2828

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

3333
using Iterator = common::rev_natural_t<t>;
3434

35-
bool empty() const { return end_ == begin_; }
36-
size_t size() const { return end_ - begin_; }
35+
bool empty() const noexcept { return end_ == begin_; }
36+
size_t size() const noexcept { return end_ - begin_; }
3737
t at(size_t i) const {
3838
ASSERT(i < size(), "Index out of range");
3939
return operator[](i);
4040
}
41-
t operator[](size_t i) const { return begin_ + i; }
42-
Iterator begin() const { return begin_; }
43-
Iterator end() const { return end_; }
41+
t operator[](size_t i) const noexcept { return begin_ + i; }
42+
Iterator begin() const noexcept { return begin_; }
43+
Iterator end() const noexcept { return end_; }
4444
};
4545

4646
template<class t = size_t> range_t<t> range0_(t end) {

src/00common/include/common/slice.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ namespace refactor::common {
88

99
using Iterator = t const *;
1010

11-
bool empty() const { return end_ == begin_; }
12-
size_t size() const { return end_ - begin_; }
11+
bool empty() const noexcept { return end_ == begin_; }
12+
size_t size() const noexcept { return end_ - begin_; }
1313
t const &at(size_t i) const {
1414
ASSERT(i < size(), "Index out of range");
1515
return operator[](i);
1616
}
17-
t const &operator[](int i) const { return begin_[i]; }
18-
Iterator begin() const { return begin_; }
19-
Iterator end() const { return end_; }
17+
t const &operator[](int i) const noexcept { return begin_[i]; }
18+
Iterator begin() const noexcept { return begin_; }
19+
Iterator end() const noexcept { return end_; }
2020
};
2121

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

2727
#endif// SLICE_H

0 commit comments

Comments
 (0)