| 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 24 matching lines...) Expand all Loading... |
| 35 // countLeadingZeros() is a bitwise operation that counts the number of leading | 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 | 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 | 37 // have an operator to do this, but fortunately the various compilers have |
| 38 // built-ins that map to fast underlying processor instructions. | 38 // built-ins that map to fast underlying processor instructions. |
| 39 | 39 |
| 40 #include "wtf/CPU.h" | 40 #include "wtf/CPU.h" |
| 41 #include "wtf/Compiler.h" | 41 #include "wtf/Compiler.h" |
| 42 | 42 |
| 43 #include <stdint.h> | 43 #include <stdint.h> |
| 44 | 44 |
| 45 #if COMPILER(MSVC) | |
| 46 #include <intrin.h> | |
| 47 #endif | |
| 48 | |
| 49 namespace WTF { | 45 namespace WTF { |
| 50 | 46 |
| 51 #if COMPILER(MSVC) | 47 #if COMPILER(GCC) |
| 52 | |
| 53 ALWAYS_INLINE uint32_t countLeadingZeros32(uint32_t x) | |
| 54 { | |
| 55 unsigned long index; | |
| 56 return LIKELY(_BitScanReverse(&index, x)) ? (31 - index) : 32; | |
| 57 } | |
| 58 | |
| 59 #if CPU(64BIT) | |
| 60 | |
| 61 // MSVC only supplies _BitScanForward64 when building for a 64-bit target. | |
| 62 ALWAYS_INLINE uint64_t countLeadingZeros64(uint64_t x) | |
| 63 { | |
| 64 unsigned long index; | |
| 65 return LIKELY(_BitScanReverse64(&index, x)) ? (63 - index) : 64; | |
| 66 } | |
| 67 | |
| 68 #endif | |
| 69 | |
| 70 #elif COMPILER(GCC) | |
| 71 | 48 |
| 72 // This is very annoying. __builtin_clz has undefined behaviour for an input of | 49 // 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 | 50 // 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. | 51 // 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 | 52 // We could drop to raw __asm__ to do better, but we'll avoid doing that unless |
| 76 // we see proof that we need to. | 53 // we see proof that we need to. |
| 77 ALWAYS_INLINE uint32_t countLeadingZeros32(uint32_t x) | 54 ALWAYS_INLINE uint32_t countLeadingZeros32(uint32_t x) |
| 78 { | 55 { |
| 79 return LIKELY(x) ? __builtin_clz(x) : 32; | 56 return LIKELY(x) ? __builtin_clz(x) : 32; |
| 80 } | 57 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 92 | 69 |
| 93 #else | 70 #else |
| 94 | 71 |
| 95 ALWAYS_INLINE size_t countLeadingZerosSizet(size_t x) { return countLeadingZeros
32(x); } | 72 ALWAYS_INLINE size_t countLeadingZerosSizet(size_t x) { return countLeadingZeros
32(x); } |
| 96 | 73 |
| 97 #endif | 74 #endif |
| 98 | 75 |
| 99 } // namespace WTF | 76 } // namespace WTF |
| 100 | 77 |
| 101 #endif // WTF_BitwiseOperations_h | 78 #endif // WTF_BitwiseOperations_h |
| OLD | NEW |