Skip to content

Commit 3245c24

Browse files
committed
add Log2 and Pow2 support methods
1 parent 41173e4 commit 3245c24

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/compiler-support.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#elif defined(_MSC_VER)
3333
# define WASM_UNREACHABLE() __assume(false)
3434
#else
35+
# include <stdlib.h>
3536
# define WASM_UNREACHABLE() abort()
3637
#endif
3738

src/support/bits.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#define wasm_support_bits_definitions
18+
#include "../compiler-support.h"
1819
#include "support/bits.h"
1920

2021
namespace wasm {
@@ -99,4 +100,28 @@ int CountLeadingZeroes<uint64_t>(uint64_t v) {
99100
: 32 + CountLeadingZeroes((uint32_t)v);
100101
}
101102

103+
uint32_t Log2(uint32_t v) {
104+
switch (v) {
105+
default: WASM_UNREACHABLE();
106+
case 1: return 0;
107+
case 2: return 1;
108+
case 4: return 2;
109+
case 8: return 3;
110+
case 16: return 4;
111+
case 32: return 5;
112+
}
113+
}
114+
115+
uint32_t Pow2(uint32_t v) {
116+
switch (v) {
117+
default: WASM_UNREACHABLE();
118+
case 0: return 1;
119+
case 1: return 2;
120+
case 2: return 4;
121+
case 3: return 8;
122+
case 4: return 16;
123+
case 5: return 32;
124+
}
125+
}
126+
102127
} // namespace wasm

src/support/bits.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ inline static T RotateRight(T val, T count) {
7979
return (val >> count) | (val << (-count & mask));
8080
}
8181

82+
extern uint32_t Log2(uint32_t v);
83+
extern uint32_t Pow2(uint32_t v);
8284

8385
} // namespace wasm
8486

0 commit comments

Comments
 (0)