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 10 matching lines...) Expand all Loading... |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
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 // TODO(palmer): The only caller of this code in Blink is PartitionAlloc. When |
| 32 // PA is moved to base, we can remove this file. |
| 33 // https://bugs.chromium.org/p/chromium/issues/detail?id=632441 |
| 34 |
31 #ifndef WTF_BitwiseOperations_h | 35 #ifndef WTF_BitwiseOperations_h |
32 #define WTF_BitwiseOperations_h | 36 #define WTF_BitwiseOperations_h |
33 | 37 |
34 // DESCRIPTION | 38 #include "base/bits.h" |
35 // countLeadingZeros() is a bitwise operation that counts the number of leading | |
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 | |
40 #include "wtf/CPU.h" | 39 #include "wtf/CPU.h" |
41 #include "wtf/Compiler.h" | |
42 #include <cstddef> | |
43 #include <stdint.h> | |
44 | |
45 #if COMPILER(MSVC) | |
46 #include <intrin.h> | |
47 #endif | |
48 | 40 |
49 namespace WTF { | 41 namespace WTF { |
50 | 42 |
51 #if COMPILER(MSVC) | 43 using base::bits::CountLeadingZeroBits32; |
52 | 44 using base::bits::CountLeadingZeroBitsSizeT; |
53 ALWAYS_INLINE uint32_t countLeadingZeros32(uint32_t x) { | |
54 unsigned long index; | |
55 return LIKELY(_BitScanReverse(&index, x)) ? (31 - index) : 32; | |
56 } | |
57 | 45 |
58 #if CPU(64BIT) | 46 #if CPU(64BIT) |
59 | 47 using base::bits::CountLeadingZeroBits64; |
60 // MSVC only supplies _BitScanForward64 when building for a 64-bit target. | |
61 ALWAYS_INLINE uint64_t countLeadingZeros64(uint64_t x) { | |
62 unsigned long index; | |
63 return LIKELY(_BitScanReverse64(&index, x)) ? (63 - index) : 64; | |
64 } | |
65 | |
66 #endif | |
67 | |
68 #elif COMPILER(GCC) | |
69 | |
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 } | |
78 | |
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 | 48 #endif |
98 | 49 |
99 } // namespace WTF | 50 } // namespace WTF |
100 | 51 |
101 #endif // WTF_BitwiseOperations_h | 52 #endif // WTF_BitwiseOperations_h |
OLD | NEW |