diff options
author | Zhigang Gong <zhigang.gong@intel.com> | 2014-07-22 15:56:08 +0800 |
---|---|---|
committer | Zhigang Gong <zhigang.gong@intel.com> | 2014-07-31 10:52:49 +0800 |
commit | 5a1f1e95f41f811b0a0bc7003b9e12d4b57593ec (patch) | |
tree | 5d9c2b626aa0a2b2160ab93402e042c3c0239ac0 /backend/src/ir/immediate.hpp | |
parent | 435f63e9fde93c38331bf0231df5ee8625f88a62 (diff) |
GBE: refactor the immediate class to support vector data type.
Signed-off-by: Zhigang Gong <zhigang.gong@intel.com>
Reviewed-by: "Song, Ruiling" <ruiling.song@intel.com>
Diffstat (limited to 'backend/src/ir/immediate.hpp')
-rw-r--r-- | backend/src/ir/immediate.hpp | 162 |
1 files changed, 143 insertions, 19 deletions
diff --git a/backend/src/ir/immediate.hpp b/backend/src/ir/immediate.hpp index 67dd03fb..1902fdeb 100644 --- a/backend/src/ir/immediate.hpp +++ b/backend/src/ir/immediate.hpp @@ -25,6 +25,7 @@ #ifndef __GBE_IR_IMMEDIATE_HPP__ #define __GBE_IR_IMMEDIATE_HPP__ +#include <string.h> #include "ir/type.hpp" #include "sys/platform.hpp" @@ -36,12 +37,38 @@ namespace ir { { public: INLINE Immediate(void) {} -#define DECL_CONSTRUCTOR(TYPE, FIELD, IR_TYPE) \ - Immediate(TYPE FIELD) { \ - this->type = IR_TYPE; \ - this->data.u64 = 0llu; \ - this->data.FIELD = FIELD; \ + + Type getType(void) const { + return type; + } + + uint32_t getTypeSize(void) const { + switch(type) { + default: + GBE_ASSERT(0 && "Invalid immeidate type.\n"); + case TYPE_BOOL: + case TYPE_S8: + case TYPE_U8: return 1; + case TYPE_S16: + case TYPE_U16: return 2; + case TYPE_FLOAT: + case TYPE_S32: + case TYPE_U32: return 4; + case TYPE_DOUBLE: + case TYPE_S64: + case TYPE_U64: return 8; + } } + +#define DECL_CONSTRUCTOR(TYPE, FIELD, IR_TYPE) \ + Immediate(TYPE FIELD) { \ + this->type = IR_TYPE; \ + this->elemNum = 1; \ + this->data.p = &defaultData; \ + defaultData = 0ull; \ + *this->data.FIELD = FIELD; \ + } + DECL_CONSTRUCTOR(bool, b, TYPE_BOOL) DECL_CONSTRUCTOR(int8_t, s8, TYPE_S8) DECL_CONSTRUCTOR(uint8_t, u8, TYPE_U8) @@ -54,28 +81,125 @@ namespace ir { DECL_CONSTRUCTOR(float, f32, TYPE_FLOAT) DECL_CONSTRUCTOR(double, f64, TYPE_DOUBLE) #undef DECL_CONSTRUCTOR + +#define DECL_CONSTRUCTOR(TYPE, FIELD, IR_TYPE, ELEMNUM) \ + Immediate(TYPE *FIELD, uint32_t ELEMNUM) { \ + this->type = IR_TYPE; \ + this->elemNum = ELEMNUM; \ + if (elemNum * ELEMNUM > 8) \ + this->data.p = malloc(ELEMNUM * getTypeSize()); \ + else \ + this->data.p = &defaultData; \ + defaultData = 0ull; \ + memcpy(this->data.FIELD, FIELD, ELEMNUM * getTypeSize()); \ + } + + DECL_CONSTRUCTOR(bool, b, TYPE_BOOL, elemNum) + DECL_CONSTRUCTOR(int8_t, s8, TYPE_S8, elemNum) + DECL_CONSTRUCTOR(uint8_t, u8, TYPE_U8, elemNum) + DECL_CONSTRUCTOR(int16_t, s16, TYPE_S16, elemNum) + DECL_CONSTRUCTOR(uint16_t, u16, TYPE_S16, elemNum) + DECL_CONSTRUCTOR(int32_t, s32, TYPE_S32, elemNum) + DECL_CONSTRUCTOR(uint32_t, u32, TYPE_S32, elemNum) + DECL_CONSTRUCTOR(int64_t, s64, TYPE_S64, elemNum) + DECL_CONSTRUCTOR(uint64_t, u64, TYPE_S64, elemNum) + DECL_CONSTRUCTOR(float, f32, TYPE_FLOAT, elemNum) + DECL_CONSTRUCTOR(double, f64, TYPE_DOUBLE, elemNum) +#undef DECL_CONSTRUCTOR + + int64_t getIntegerValue(void) const { + switch (type) { + default: + GBE_ASSERT(0 && "Invalid immediate type.\n"); + case TYPE_BOOL: return *data.b; + case TYPE_S8: return *data.s8; + case TYPE_U8: return *data.u8; + case TYPE_S16: return *data.s16; + case TYPE_U16: return *data.u16; + case TYPE_S32: return *data.s32; + case TYPE_U32: return *data.u32; + case TYPE_S64: return *data.s64; + case TYPE_U64: return *data.u64; + } + } + + float getFloatValue(void) const { + GBE_ASSERT(type == TYPE_FLOAT); + return *data.f32; + } + + float asFloatValue(void) const { + GBE_ASSERT(type == TYPE_FLOAT || type == TYPE_U32 || type == TYPE_S32); + return *data.f32; + } + + int64_t asIntegerValue(void) const { + GBE_ASSERT(elemNum == 1); + return *data.s64; + } + + double getDoubleValue(void) const { + GBE_ASSERT(type == TYPE_DOUBLE); + return *data.f64; + } + + Immediate(const Immediate & other) { + if (this != &other) { + this->type = other.type; + this->elemNum = other.elemNum; + if (other.data.p != &other.defaultData) { + this->data.p = malloc(other.elemNum * other.getTypeSize()); + memcpy(this->data.p, other.data.p, other.elemNum * other.getTypeSize()); + } + else { + this->defaultData = other.defaultData; + this->data.p = &this->defaultData; + } + } + } + + Immediate & operator= (const Immediate & other) { + *this = Immediate(other); + return *this; + } + + ~Immediate() { + if (data.p != &defaultData) { + free(data.p); + data.p = NULL; + } + } + + private: union { - bool b; - int8_t s8; - uint8_t u8; - int16_t s16; - uint16_t u16; - int32_t s32; - uint32_t u32; - int64_t s64; - uint64_t u64; - float f32; - double f64; + bool *b; + int8_t *s8; + uint8_t *u8; + int16_t *s16; + uint16_t *u16; + int32_t *s32; + uint32_t *u32; + int64_t *s64; + uint64_t *u64; + float *f32; + double *f64; + void *p; } data; //!< Value to store Type type; //!< Type of the value + uint32_t elemNum; //!< vector imm data type + uint64_t defaultData; + GBE_CLASS(Immediate); }; /*! Compare two immediates */ INLINE bool operator< (const Immediate &imm0, const Immediate &imm1) { - if (imm0.type != imm1.type) - return uint32_t(imm0.type) < uint32_t(imm1.type); - return imm0.data.u64 < imm1.data.u64; + if (imm0.getType() != imm1.getType()) + return uint32_t(imm0.getType()) < uint32_t(imm1.getType()); + else if (imm0.getType() == TYPE_FLOAT || imm0.getType() == TYPE_DOUBLE) + return imm0.asIntegerValue() < imm1.asIntegerValue(); + else + return imm0.getIntegerValue() < imm1.getIntegerValue(); } /*! A value is stored in a per-function vector. This is the index to it */ |