From 1397997bc91663f19c387c69c7e47930efe57539 Mon Sep 17 00:00:00 2001 From: JF Bastien Date: Wed, 4 May 2016 14:10:37 -0700 Subject: Nicer shift masks (#431) * Nicer shift masks * Yet nicer shift mask. * Fix typo. --- src/wasm.h | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'src') 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 + 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(); } } -- cgit v1.2.3