OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 13 matching lines...) Expand all Loading... | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #ifndef WTF_BitwiseOperations_h | 31 #ifndef WTF_BitwiseOperations_h |
32 #define WTF_BitwiseOperations_h | 32 #define WTF_BitwiseOperations_h |
33 | 33 |
34 // DESCRIPTION | 34 #include "base/bits.h" |
35 // countLeadingZeros() is a bitwise operation that counts the number of leading | 35 #include "wtf/CPU.h" |
36 // zeros in a binary value, starting with the most significant bit. C does not | |
37 // have an operator to do this, but fortunately the various compilers have | |
38 // built-ins that map to fast underlying processor instructions. | |
39 | 36 |
40 #include "wtf/CPU.h" | |
41 #include "wtf/Compiler.h" | |
42 #include <cstddef> | |
43 #include <stdint.h> | 37 #include <stdint.h> |
44 | 38 |
45 #if COMPILER(MSVC) | |
46 #include <intrin.h> | |
47 #endif | |
48 | |
49 namespace WTF { | 39 namespace WTF { |
50 | 40 |
51 #if COMPILER(MSVC) | 41 ALWAYS_INLINE uint32_t CountLeadingZeroBits32(uint32_t x) { |
52 | 42 return base::bits::CountLeadingZeroBits32(x); |
esprehn
2016/11/17 07:49:42
Can we write
using base::bits instead?
| |
53 ALWAYS_INLINE uint32_t countLeadingZeros32(uint32_t x) { | |
54 unsigned long index; | |
55 return LIKELY(_BitScanReverse(&index, x)) ? (31 - index) : 32; | |
56 } | 43 } |
57 | 44 |
58 #if CPU(64BIT) | 45 #if CPU(64BIT) |
59 | 46 ALWAYS_INLINE uint64_t CountLeadingZeroBits64(uint64_t x) { |
60 // MSVC only supplies _BitScanForward64 when building for a 64-bit target. | 47 return base::bits::CountLeadingZeroBits64(x); |
61 ALWAYS_INLINE uint64_t countLeadingZeros64(uint64_t x) { | |
62 unsigned long index; | |
63 return LIKELY(_BitScanReverse64(&index, x)) ? (63 - index) : 64; | |
64 } | 48 } |
65 | |
66 #endif | 49 #endif |
67 | 50 |
68 #elif COMPILER(GCC) | 51 ALWAYS_INLINE size_t CountLeadingZeroBitsSizeT(size_t x) { |
69 | 52 return base::bits::CountLeadingZeroBitsSizeT(x); |
70 // This is very annoying. __builtin_clz has undefined behaviour for an input of | |
71 // 0, even though these's clearly a return value that makes sense, and even | |
72 // though nascent processor clz instructions have defined behaviour for 0. | |
73 // We could drop to raw __asm__ to do better, but we'll avoid doing that unless | |
74 // we see proof that we need to. | |
75 ALWAYS_INLINE uint32_t countLeadingZeros32(uint32_t x) { | |
76 return LIKELY(x) ? __builtin_clz(x) : 32; | |
77 } | 53 } |
78 | 54 |
79 ALWAYS_INLINE uint64_t countLeadingZeros64(uint64_t x) { | |
80 return LIKELY(x) ? __builtin_clzll(x) : 64; | |
81 } | |
82 | |
83 #endif | |
84 | |
85 #if CPU(64BIT) | |
86 | |
87 ALWAYS_INLINE size_t countLeadingZerosSizet(size_t x) { | |
88 return countLeadingZeros64(x); | |
89 } | |
90 | |
91 #else | |
92 | |
93 ALWAYS_INLINE size_t countLeadingZerosSizet(size_t x) { | |
94 return countLeadingZeros32(x); | |
95 } | |
96 | |
97 #endif | |
98 | |
99 } // namespace WTF | 55 } // namespace WTF |
100 | 56 |
101 #endif // WTF_BitwiseOperations_h | 57 #endif // WTF_BitwiseOperations_h |
OLD | NEW |