-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[refactor] Replace std::map with switch case statements in lang_util.cpp #1755
Merged
Merged
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
04042fc
Fixed typo "has no common" -> "has nothing in common"
aryansoman fe38ef5
Typo and grammatical fixes
aryansoman 807ab65
clean
archibate 874421d
Merge remote-tracking branch 'upstream/master'
aryansoman 87d1a1b
Merge branch 'master' of https://github.com/aryansoman/taichi
aryansoman 0c4ba22
Merge remote-tracking branch 'upstream/master'
aryansoman 00bccef
Merge remote-tracking branch 'upstream/master'
aryansoman f082427
First two std::map to switch-case changes
aryansoman bc63d2b
Updated rest of functions with TI_NOT_IMPLEMENTED
aryansoman caa1f6b
Fixed sizeof for certain special datatypes
aryansoman c9ec024
Added # in front of return
aryansoman dd2ae92
Pushed backup by mistake, corrected
aryansoman b6b3e6c
Fixed merge conflict in binary_op_type_symbol
aryansoman 4449368
Fixed typos (missed bracket, etc)
aryansoman 607e5a1
Removed return statements from TI_NOT_IMPLEMENTED
aryansoman 1d3ec81
Use switch case in binary_op_type_symbol
aryansoman e0c28c0
Merge remote-tracking branch 'upstream/master' into dev
aryansoman 7d1bb3f
Reverted promoted type because it has pair
aryansoman 27834e0
[skip ci] enforce code format
taichi-gardener 8a0c212
Merge remote-tracking branch 'upstream/master' into HEAD
aryansoman 67ae562
Removed duplicate line
aryansoman eb8e1c2
Replaced shift operations
aryansoman 0f2023c
Merge remote-tracking branch 'upstream/master' into HEAD
aryansoman 61e0cf6
merge commit
aryansoman d011397
[skip ci] enforce code format
taichi-gardener d29bc6e
Merge remote-tracking branch 'upstream/master' into HEAD
yuanming-hu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,9 +65,11 @@ real measure_cpe(std::function<void()> target, | |
} | ||
|
||
std::string data_type_name(DataType t) { | ||
static std::map<DataType, std::string> type_names; | ||
if (type_names.empty()) { | ||
#define REGISTER_DATA_TYPE(i, j) type_names[DataType::i] = #j; | ||
switch (t) { | ||
#define REGISTER_DATA_TYPE(i, j) \ | ||
case DataType::i: \ | ||
return #j; | ||
|
||
REGISTER_DATA_TYPE(f16, float16); | ||
REGISTER_DATA_TYPE(f32, float32); | ||
REGISTER_DATA_TYPE(f64, float64); | ||
|
@@ -82,9 +84,11 @@ std::string data_type_name(DataType t) { | |
REGISTER_DATA_TYPE(u64, uint64); | ||
REGISTER_DATA_TYPE(gen, generic); | ||
REGISTER_DATA_TYPE(unknown, unknown); | ||
|
||
#undef REGISTER_DATA_TYPE | ||
default: | ||
TI_NOT_IMPLEMENTED | ||
} | ||
return type_names[t]; | ||
} | ||
|
||
std::string data_type_format(DataType dt) { | ||
|
@@ -106,77 +110,102 @@ std::string data_type_format(DataType dt) { | |
} | ||
|
||
int data_type_size(DataType t) { | ||
static std::map<DataType, int> type_sizes; | ||
if (type_sizes.empty()) { | ||
#define REGISTER_DATA_TYPE(i, j) type_sizes[DataType::i] = sizeof(j); | ||
type_sizes[DataType::f16] = 2; | ||
REGISTER_DATA_TYPE(f32, float32); | ||
REGISTER_DATA_TYPE(f64, float64); | ||
REGISTER_DATA_TYPE(i8, bool); | ||
REGISTER_DATA_TYPE(i8, int8); | ||
REGISTER_DATA_TYPE(i16, int16); | ||
REGISTER_DATA_TYPE(i32, int32); | ||
REGISTER_DATA_TYPE(i64, int64); | ||
REGISTER_DATA_TYPE(u8, uint8); | ||
REGISTER_DATA_TYPE(u16, uint16); | ||
REGISTER_DATA_TYPE(u32, uint32); | ||
REGISTER_DATA_TYPE(u64, uint64); | ||
type_sizes[DataType::gen] = 0; | ||
type_sizes[DataType::unknown] = -1; | ||
switch (t) { | ||
case DataType::f16: | ||
return 2; | ||
case DataType::gen: | ||
return 0; | ||
case DataType::unknown: | ||
return -1; | ||
|
||
#define REGISTER_DATA_TYPE(i, j) \ | ||
case DataType::i: \ | ||
return sizeof(j); | ||
|
||
REGISTER_DATA_TYPE(f32, float32); | ||
REGISTER_DATA_TYPE(f64, float64); | ||
// REGISTER_DATA_TYPE(i8, bool); // duplicate value | ||
REGISTER_DATA_TYPE(i8, int8); | ||
REGISTER_DATA_TYPE(i16, int16); | ||
REGISTER_DATA_TYPE(i32, int32); | ||
REGISTER_DATA_TYPE(i64, int64); | ||
REGISTER_DATA_TYPE(u8, uint8); | ||
REGISTER_DATA_TYPE(u16, uint16); | ||
REGISTER_DATA_TYPE(u32, uint32); | ||
REGISTER_DATA_TYPE(u64, uint64); | ||
|
||
#undef REGISTER_DATA_TYPE | ||
default: | ||
TI_NOT_IMPLEMENTED | ||
} | ||
return type_sizes[t]; | ||
} | ||
|
||
std::string data_type_short_name(DataType t) { | ||
static std::map<DataType, std::string> type_names; | ||
if (type_names.empty()) { | ||
#define PER_TYPE(i) type_names[DataType::i] = #i; | ||
switch (t) { | ||
#define PER_TYPE(i) \ | ||
case DataType::i: \ | ||
return #i; | ||
|
||
#include "taichi/inc/data_type.inc.h" | ||
|
||
#undef PER_TYPE | ||
default: | ||
TI_NOT_IMPLEMENTED | ||
} | ||
return type_names[t]; | ||
} | ||
|
||
std::string snode_type_name(SNodeType t) { | ||
static std::map<SNodeType, std::string> type_names; | ||
if (type_names.empty()) { | ||
#define PER_SNODE(i) type_names[SNodeType::i] = #i; | ||
switch (t) { | ||
#define PER_SNODE(i) \ | ||
case SNodeType::i: \ | ||
return #i; | ||
|
||
#include "inc/snodes.inc.h" | ||
|
||
#undef PER_SNODE | ||
default: | ||
TI_NOT_IMPLEMENTED | ||
} | ||
return type_names[t]; | ||
} | ||
|
||
bool is_gc_able(SNodeType t) { | ||
return (t == SNodeType::pointer || t == SNodeType::dynamic); | ||
} | ||
|
||
std::string unary_op_type_name(UnaryOpType type) { | ||
static std::map<UnaryOpType, std::string> type_names; | ||
if (type_names.empty()) { | ||
#define PER_UNARY_OP(i) type_names[UnaryOpType::i] = #i; | ||
switch (type) { | ||
#define PER_UNARY_OP(i) \ | ||
case UnaryOpType::i: \ | ||
return #i; | ||
|
||
#include "taichi/inc/unary_op.inc.h" | ||
|
||
#undef PER_UNARY_OP | ||
default: | ||
TI_NOT_IMPLEMENTED | ||
} | ||
return type_names[type]; | ||
} | ||
|
||
std::string binary_op_type_name(BinaryOpType type) { | ||
static std::map<BinaryOpType, std::string> type_names; | ||
if (type_names.empty()) { | ||
#define PER_BINARY_OP(x) type_names[BinaryOpType::x] = #x; | ||
switch (type) { | ||
#define PER_BINARY_OP(x) \ | ||
case BinaryOpType::x: \ | ||
return #x; | ||
|
||
#include "inc/binary_op.inc.h" | ||
|
||
#undef PER_BINARY_OP | ||
default: | ||
TI_NOT_IMPLEMENTED | ||
} | ||
return type_names[type]; | ||
} | ||
|
||
std::string binary_op_type_symbol(BinaryOpType type) { | ||
static std::map<BinaryOpType, std::string> type_names; | ||
if (type_names.empty()) { | ||
#define REGISTER_TYPE(i, s) type_names[BinaryOpType::i] = #s; | ||
switch (type) { | ||
#define REGISTER_TYPE(i, s) \ | ||
case BinaryOpType::i: \ | ||
return #s; | ||
|
||
REGISTER_TYPE(mul, *); | ||
REGISTER_TYPE(add, +); | ||
REGISTER_TYPE(sub, -); | ||
|
@@ -196,53 +225,65 @@ std::string binary_op_type_symbol(BinaryOpType type) { | |
REGISTER_TYPE(bit_and, &); | ||
REGISTER_TYPE(bit_or, |); | ||
REGISTER_TYPE(bit_xor, ^); | ||
REGISTER_TYPE(bit_shl, <<); | ||
REGISTER_TYPE(bit_sar, >>); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we still need these lines? |
||
REGISTER_TYPE(pow, pow); | ||
|
||
#undef REGISTER_TYPE | ||
default: | ||
TI_NOT_IMPLEMENTED | ||
} | ||
return type_names[type]; | ||
} | ||
|
||
std::string ternary_type_name(TernaryOpType type) { | ||
static std::map<TernaryOpType, std::string> type_names; | ||
if (type_names.empty()) { | ||
#define REGISTER_TYPE(i) type_names[TernaryOpType::i] = #i; | ||
switch (type) { | ||
#define REGISTER_TYPE(i) \ | ||
case TernaryOpType::i: \ | ||
return #i; | ||
|
||
REGISTER_TYPE(select); | ||
|
||
#undef REGISTER_TYPE | ||
default: | ||
TI_NOT_IMPLEMENTED | ||
} | ||
return type_names[type]; | ||
} | ||
|
||
std::string atomic_op_type_name(AtomicOpType type) { | ||
static std::map<AtomicOpType, std::string> type_names; | ||
if (type_names.empty()) { | ||
#define REGISTER_TYPE(i) type_names[AtomicOpType::i] = #i; | ||
switch (type) { | ||
#define REGISTER_TYPE(i) \ | ||
case AtomicOpType::i: \ | ||
return #i; | ||
|
||
REGISTER_TYPE(add); | ||
REGISTER_TYPE(max); | ||
REGISTER_TYPE(min); | ||
REGISTER_TYPE(bit_and); | ||
REGISTER_TYPE(bit_or); | ||
REGISTER_TYPE(bit_xor); | ||
|
||
#undef REGISTER_TYPE | ||
default: | ||
TI_NOT_IMPLEMENTED | ||
} | ||
return type_names[type]; | ||
} | ||
|
||
std::string snode_op_type_name(SNodeOpType type) { | ||
static std::map<SNodeOpType, std::string> type_names; | ||
if (type_names.empty()) { | ||
#define REGISTER_TYPE(i) type_names[SNodeOpType::i] = #i; | ||
switch (type) { | ||
#define REGISTER_TYPE(i) \ | ||
case SNodeOpType::i: \ | ||
return #i; | ||
|
||
REGISTER_TYPE(is_active); | ||
REGISTER_TYPE(length); | ||
REGISTER_TYPE(activate); | ||
REGISTER_TYPE(deactivate); | ||
REGISTER_TYPE(append); | ||
REGISTER_TYPE(clear); | ||
REGISTER_TYPE(undefined); | ||
|
||
#undef REGISTER_TYPE | ||
default: | ||
TI_NOT_IMPLEMENTED | ||
} | ||
return type_names[type]; | ||
} | ||
|
||
bool command_exist(const std::string &command) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch! Let's remove this line then :-)