Skip to content
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 26 commits into from
Aug 29, 2020
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 Aug 13, 2020
fe38ef5
Typo and grammatical fixes
aryansoman Aug 13, 2020
807ab65
clean
archibate Aug 14, 2020
874421d
Merge remote-tracking branch 'upstream/master'
aryansoman Aug 15, 2020
87d1a1b
Merge branch 'master' of https://github.com/aryansoman/taichi
aryansoman Aug 15, 2020
0c4ba22
Merge remote-tracking branch 'upstream/master'
aryansoman Aug 19, 2020
00bccef
Merge remote-tracking branch 'upstream/master'
aryansoman Aug 23, 2020
f082427
First two std::map to switch-case changes
aryansoman Aug 23, 2020
bc63d2b
Updated rest of functions with TI_NOT_IMPLEMENTED
aryansoman Aug 23, 2020
caa1f6b
Fixed sizeof for certain special datatypes
aryansoman Aug 23, 2020
c9ec024
Added # in front of return
aryansoman Aug 24, 2020
dd2ae92
Pushed backup by mistake, corrected
aryansoman Aug 24, 2020
b6b3e6c
Fixed merge conflict in binary_op_type_symbol
aryansoman Aug 24, 2020
4449368
Fixed typos (missed bracket, etc)
aryansoman Aug 25, 2020
607e5a1
Removed return statements from TI_NOT_IMPLEMENTED
aryansoman Aug 27, 2020
1d3ec81
Use switch case in binary_op_type_symbol
aryansoman Aug 27, 2020
e0c28c0
Merge remote-tracking branch 'upstream/master' into dev
aryansoman Aug 29, 2020
7d1bb3f
Reverted promoted type because it has pair
aryansoman Aug 29, 2020
27834e0
[skip ci] enforce code format
taichi-gardener Aug 29, 2020
8a0c212
Merge remote-tracking branch 'upstream/master' into HEAD
aryansoman Aug 29, 2020
67ae562
Removed duplicate line
aryansoman Aug 29, 2020
eb8e1c2
Replaced shift operations
aryansoman Aug 29, 2020
0f2023c
Merge remote-tracking branch 'upstream/master' into HEAD
aryansoman Aug 29, 2020
61e0cf6
merge commit
aryansoman Aug 29, 2020
d011397
[skip ci] enforce code format
taichi-gardener Aug 29, 2020
d29bc6e
Merge remote-tracking branch 'upstream/master' into HEAD
yuanming-hu Aug 29, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 97 additions & 56 deletions taichi/lang_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// REGISTER_DATA_TYPE(i8, bool); // duplicate value

Nice catch! Let's remove this line then :-)

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, -);
Expand All @@ -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, >>);
Copy link
Member

Choose a reason for hiding this comment

The 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) {
Expand Down