From 66da1ee9cc70e3848c45745c21a244e54512fa9c Mon Sep 17 00:00:00 2001 From: JF Bastien Date: Tue, 2 Feb 2016 11:12:06 -0800 Subject: Move bits.h to support/ Faster compiles. --- src/support/bits.h | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/support/bits.h (limited to 'src/support/bits.h') 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 +#include + +/* + * 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 int PopCount(T); +template uint32_t BitReverse(T); +template int CountTrailingZeroes(T); +template 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 +inline int PopCount(T v) { + return PopCount(typename std::make_unsigned::type(v)); +} +template +inline int CountTrailingZeroes(T v) { + return CountTrailingZeroes(typename std::make_unsigned::type(v)); +} +template +inline int CountLeadingZeroes(T v) { + return CountLeadingZeroes(typename std::make_unsigned::type(v)); +} + +} // namespace wasm + +#endif // wasm_support_bits_h -- cgit v1.2.3