diff options
author | JF Bastien <github@jfbastien.com> | 2016-05-04 14:10:37 -0700 |
---|---|---|
committer | JF Bastien <github@jfbastien.com> | 2016-05-04 14:10:37 -0700 |
commit | 1397997bc91663f19c387c69c7e47930efe57539 (patch) | |
tree | ae1afc446c8336110ab822caadfcb4f36ac44676 /src | |
parent | c7d7083920ddd9ff7d1a44577287b8ecc048a221 (diff) | |
download | binaryen-1397997bc91663f19c387c69c7e47930efe57539.tar.gz binaryen-1397997bc91663f19c387c69c7e47930efe57539.tar.bz2 binaryen-1397997bc91663f19c387c69c7e47930efe57539.zip |
Nicer shift masks (#431)
* Nicer shift masks
* Yet nicer shift mask.
* Fix typo.
Diffstat (limited to 'src')
-rw-r--r-- | src/wasm.h | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/src/wasm.h b/src/wasm.h index 843b4bc6c..6b171f8bd 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -165,7 +165,13 @@ private: int64_t i64; }; -public: + // The RHS of shl/shru/shrs must be masked by bitwidth. + template <typename T> + static T shiftMask(T val) { + return val & (sizeof(T) * 8 - 1); + } + + public: Literal() : type(WasmType::none), i64(0) {} explicit Literal(WasmType type) : type(type), i64(0) {} explicit Literal(int32_t init) : type(WasmType::i32), i32(init) {} @@ -530,22 +536,22 @@ public: } Literal shl(const Literal& other) const { switch (type) { - case WasmType::i32: return Literal(uint32_t(i32) << (other.i32 & 0x1f)); - case WasmType::i64: return Literal(uint64_t(i64) << (other.i64 & 0x3f)); + case WasmType::i32: return Literal(uint32_t(i32) << shiftMask(other.i32)); + case WasmType::i64: return Literal(uint64_t(i64) << shiftMask(other.i64)); default: WASM_UNREACHABLE(); } } Literal shrS(const Literal& other) const { switch (type) { - case WasmType::i32: return Literal(i32 >> (other.i32 & 0x1f)); - case WasmType::i64: return Literal(i64 >> (other.i64 & 0x3f)); + case WasmType::i32: return Literal(i32 >> shiftMask(other.i32)); + case WasmType::i64: return Literal(i64 >> shiftMask(other.i64)); default: WASM_UNREACHABLE(); } } Literal shrU(const Literal& other) const { switch (type) { - case WasmType::i32: return Literal(uint32_t(i32) >> uint32_t(other.i32 & 0x1f)); - case WasmType::i64: return Literal(uint64_t(i64) >> uint64_t(other.i64 & 0x3f)); + case WasmType::i32: return Literal(uint32_t(i32) >> shiftMask(other.i32)); + case WasmType::i64: return Literal(uint64_t(i64) >> shiftMask(other.i64)); default: WASM_UNREACHABLE(); } } |