summaryrefslogtreecommitdiff
path: root/src/support/bits.h
diff options
context:
space:
mode:
authorJF Bastien <jfb@chromium.org>2016-02-02 11:12:06 -0800
committerJF Bastien <jfb@chromium.org>2016-02-03 01:42:22 -0800
commit66da1ee9cc70e3848c45745c21a244e54512fa9c (patch)
treeacbe2ca565971d4d1036703210f15c51c3a9809a /src/support/bits.h
parent1eb620220ba91257d080721f194874960f4fb5fe (diff)
downloadbinaryen-66da1ee9cc70e3848c45745c21a244e54512fa9c.tar.gz
binaryen-66da1ee9cc70e3848c45745c21a244e54512fa9c.tar.bz2
binaryen-66da1ee9cc70e3848c45745c21a244e54512fa9c.zip
Move bits.h to support/
Faster compiles.
Diffstat (limited to 'src/support/bits.h')
-rw-r--r--src/support/bits.h70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/support/bits.h b/src/support/bits.h
new file mode 100644
index 000000000..3049a2cf1
--- /dev/null
+++ b/src/support/bits.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2015 WebAssembly Community Group participants
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef wasm_support_bits_h
+#define wasm_support_bits_h
+
+#include <cstdint>
+#include <type_traits>
+
+/*
+ * Portable bit functions.
+ *
+ * Not all platforms offer fast intrinsics for these functions, and some
+ * compilers require checking CPUID at runtime before using the intrinsic.
+ *
+ * We instead use portable and reasonably-fast implementations, while
+ * avoiding implementations with large lookup tables.
+ */
+
+namespace wasm {
+
+template<typename T> int PopCount(T);
+template<typename T> uint32_t BitReverse(T);
+template<typename T> int CountTrailingZeroes(T);
+template<typename T> int CountLeadingZeroes(T);
+
+#ifndef wasm_support_bits_definitions
+// The template specializations are provided elsewhere.
+extern template int PopCount(uint8_t);
+extern template int PopCount(uint16_t);
+extern template int PopCount(uint32_t);
+extern template int PopCount(uint64_t);
+extern template uint32_t BitReverse(uint32_t);
+extern template int CountTrailingZeroes(uint32_t);
+extern template int CountTrailingZeroes(uint64_t);
+extern template int CountLeadingZeroes(uint32_t);
+extern template int CountLeadingZeroes(uint64_t);
+#endif
+
+// Convenience signed -> unsigned. It usually doesn't make much sense to use bit
+// functions on signed types.
+template <typename T>
+inline int PopCount(T v) {
+ return PopCount(typename std::make_unsigned<T>::type(v));
+}
+template <typename T>
+inline int CountTrailingZeroes(T v) {
+ return CountTrailingZeroes(typename std::make_unsigned<T>::type(v));
+}
+template <typename T>
+inline int CountLeadingZeroes(T v) {
+ return CountLeadingZeroes(typename std::make_unsigned<T>::type(v));
+}
+
+} // namespace wasm
+
+#endif // wasm_support_bits_h