diff --git a/lib/insn.c b/lib/insn.c index f7c06565..23bbabfc 100644 --- a/lib/insn.c +++ b/lib/insn.c @@ -235,9 +235,6 @@ wasm_fmax(double a, double b) static uint32_t clz(uint32_t v) { - if (v == 0) { - return 32; - } #if __has_builtin(__builtin_clz) return __builtin_clz(v); #else @@ -245,7 +242,7 @@ clz(uint32_t v) uint32_t u = v; while ((u & 0x80000000) == 0) { cnt++; - u << 1; + u <<= 1; } return cnt; #endif @@ -254,9 +251,6 @@ clz(uint32_t v) static uint32_t ctz(uint32_t v) { - if (v == 0) { - return 32; - } #if __has_builtin(__builtin_ctz) return __builtin_ctz(v); #else @@ -270,6 +264,24 @@ ctz(uint32_t v) #endif } +static uint32_t +wasm_clz(uint32_t v) +{ + if (v == 0) { + return 32; + } + return clz(v); +} + +static uint32_t +wasm_ctz(uint32_t v) +{ + if (v == 0) { + return 32; + } + return ctz(v); +} + static uint32_t wasm_popcount(uint32_t v) { @@ -287,29 +299,29 @@ wasm_popcount(uint32_t v) } static uint64_t -clz64(uint64_t v) +wasm_clz64(uint64_t v) { if (v == 0) { return 64; } uint32_t high = v >> 32; if (high == 0) { - return 32 + __builtin_clz(v); + return 32 + clz(v); } - return __builtin_clz(high); + return clz(high); } static uint64_t -ctz64(uint64_t v) +wasm_ctz64(uint64_t v) { if (v == 0) { return 64; } uint32_t low = (uint32_t)v; if (low == 0) { - return 32 + __builtin_ctz((uint32_t)(v >> 32)); + return 32 + ctz((uint32_t)(v >> 32)); } - return __builtin_ctz(low); + return ctz(low); } static uint64_t diff --git a/lib/insn_impl_base.h b/lib/insn_impl_base.h index 6b310bba..cb6b0d47 100644 --- a/lib/insn_impl_base.h +++ b/lib/insn_impl_base.h @@ -383,8 +383,8 @@ CMPOP(i64_le_u, 64, uint64_t, <=) CMPOP(i64_ge_s, 64, int64_t, >=) CMPOP(i64_ge_u, 64, uint64_t, >=) -BITCOUNTOP(i32_clz, i32, clz) -BITCOUNTOP(i32_ctz, i32, ctz) +BITCOUNTOP(i32_clz, i32, wasm_clz) +BITCOUNTOP(i32_ctz, i32, wasm_ctz) BITCOUNTOP(i32_popcnt, i32, wasm_popcount) BINOP(i32_add, i, 32, ADD) @@ -406,8 +406,8 @@ BINOP(i32_shr_u, i, 32, SHR_U) BINOP(i32_rotl, i, 32, ROTL) BINOP(i32_rotr, i, 32, ROTR) -BITCOUNTOP(i64_clz, i64, clz64) -BITCOUNTOP(i64_ctz, i64, ctz64) +BITCOUNTOP(i64_clz, i64, wasm_clz64) +BITCOUNTOP(i64_ctz, i64, wasm_ctz64) BITCOUNTOP(i64_popcnt, i64, wasm_popcount64) BINOP(i64_add, i, 64, ADD)