| 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 #include <stdint.h> | 43 #include <stdint.h> |
| 44 | 44 |
| 45 #if COMPILER(MSVC) | 45 #if COMPILER(MSVC) |
| 46 #include <intrin.h> | 46 #include <intrin.h> |
| 47 #endif | 47 #endif |
| 48 | 48 |
| 49 namespace WTF { | 49 namespace WTF { |
| 50 | 50 |
| 51 #if COMPILER(MSVC) | 51 #if COMPILER(MSVC) |
| 52 | 52 |
| 53 ALWAYS_INLINE uint32_t countLeadingZeros32(uint32_t x) | 53 ALWAYS_INLINE uint32_t countLeadingZeros32(uint32_t x) { |
| 54 { | 54 unsigned long index; |
| 55 unsigned long index; | 55 return LIKELY(_BitScanReverse(&index, x)) ? (31 - index) : 32; |
| 56 return LIKELY(_BitScanReverse(&index, x)) ? (31 - index) : 32; | |
| 57 } | 56 } |
| 58 | 57 |
| 59 #if CPU(64BIT) | 58 #if CPU(64BIT) |
| 60 | 59 |
| 61 // MSVC only supplies _BitScanForward64 when building for a 64-bit target. | 60 // MSVC only supplies _BitScanForward64 when building for a 64-bit target. |
| 62 ALWAYS_INLINE uint64_t countLeadingZeros64(uint64_t x) | 61 ALWAYS_INLINE uint64_t countLeadingZeros64(uint64_t x) { |
| 63 { | 62 unsigned long index; |
| 64 unsigned long index; | 63 return LIKELY(_BitScanReverse64(&index, x)) ? (63 - index) : 64; |
| 65 return LIKELY(_BitScanReverse64(&index, x)) ? (63 - index) : 64; | |
| 66 } | 64 } |
| 67 | 65 |
| 68 #endif | 66 #endif |
| 69 | 67 |
| 70 #elif COMPILER(GCC) | 68 #elif COMPILER(GCC) |
| 71 | 69 |
| 72 // This is very annoying. __builtin_clz has undefined behaviour for an input of | 70 // This is very annoying. __builtin_clz has undefined behaviour for an input of |
| 73 // 0, even though these's clearly a return value that makes sense, and even | 71 // 0, even though these's clearly a return value that makes sense, and even |
| 74 // though nascent processor clz instructions have defined behaviour for 0. | 72 // though nascent processor clz instructions have defined behaviour for 0. |
| 75 // We could drop to raw __asm__ to do better, but we'll avoid doing that unless | 73 // We could drop to raw __asm__ to do better, but we'll avoid doing that unless |
| 76 // we see proof that we need to. | 74 // we see proof that we need to. |
| 77 ALWAYS_INLINE uint32_t countLeadingZeros32(uint32_t x) | 75 ALWAYS_INLINE uint32_t countLeadingZeros32(uint32_t x) { |
| 78 { | 76 return LIKELY(x) ? __builtin_clz(x) : 32; |
| 79 return LIKELY(x) ? __builtin_clz(x) : 32; | |
| 80 } | 77 } |
| 81 | 78 |
| 82 ALWAYS_INLINE uint64_t countLeadingZeros64(uint64_t x) | 79 ALWAYS_INLINE uint64_t countLeadingZeros64(uint64_t x) { |
| 83 { | 80 return LIKELY(x) ? __builtin_clzll(x) : 64; |
| 84 return LIKELY(x) ? __builtin_clzll(x) : 64; | |
| 85 } | 81 } |
| 86 | 82 |
| 87 #endif | 83 #endif |
| 88 | 84 |
| 89 #if CPU(64BIT) | 85 #if CPU(64BIT) |
| 90 | 86 |
| 91 ALWAYS_INLINE size_t countLeadingZerosSizet(size_t x) { return countLeadingZeros
64(x); } | 87 ALWAYS_INLINE size_t countLeadingZerosSizet(size_t x) { |
| 88 return countLeadingZeros64(x); |
| 89 } |
| 92 | 90 |
| 93 #else | 91 #else |
| 94 | 92 |
| 95 ALWAYS_INLINE size_t countLeadingZerosSizet(size_t x) { return countLeadingZeros
32(x); } | 93 ALWAYS_INLINE size_t countLeadingZerosSizet(size_t x) { |
| 94 return countLeadingZeros32(x); |
| 95 } |
| 96 | 96 |
| 97 #endif | 97 #endif |
| 98 | 98 |
| 99 } // namespace WTF | 99 } // namespace WTF |
| 100 | 100 |
| 101 #endif // WTF_BitwiseOperations_h | 101 #endif // WTF_BitwiseOperations_h |
| OLD | NEW |