1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_UTILITIES_COUNTTRAILINGZEROS_HPP 26 #define SHARE_VM_UTILITIES_COUNTTRAILINGZEROS_HPP 27 28 #include "utilities/debug.hpp" 29 #include "utilities/globalDefinitions.hpp" 30 31 // unsigned count_trailing_zeros(uintx x) 32 // Return the number of trailing zeros in x, e.g. the zero-based index 33 // of the least significant set bit in x. 34 // Precondition: x != 0. 35 36 // Dispatch on toolchain to select implementation. 37 38 ////////////////////////////////////////////////////////////////////////////// 39 #if defined(TARGET_COMPILER_gcc) 40 41 inline unsigned count_trailing_zeros(uintx x) { 42 STATIC_ASSERT(sizeof(unsigned long) == sizeof(uintx)); 43 assert(x != 0, "precondition"); 44 return __builtin_ctzl(x); 45 } 46 47 ////////////////////////////////////////////////////////////////////////////// 48 #elif defined(TARGET_COMPILER_visCPP) 49 50 #include <intrin.h> 51 52 #ifdef _LP64 53 #pragma intrinsic(_BitScanForward64) 54 #else 55 #pragma intrinsic(_BitScanForward) 56 #endif 57 58 inline unsigned count_trailing_zeros(uintx x) { 59 assert(x != 0, "precondition"); 60 unsigned long index; 61 #ifdef _LP64 62 _BitScanForward64(&index, x); 63 #else 64 _BitScanForward(&index, x); 65 #endif 66 return index; 67 } 68 69 ////////////////////////////////////////////////////////////////////////////// 70 #elif defined(TARGET_COMPILER_xlc) 71 72 #include <builtins.h> 73 74 inline unsigned count_trailing_zeros(uintx x) { 75 assert(x != 0, "precondition"); 76 #ifdef _LP64 77 return __cnttz8(x); 78 #else 79 return __cnttz4(x); 80 #endif 81 } 82 83 ////////////////////////////////////////////////////////////////////////////// 84 #elif defined(TARGET_COMPILER_sparcWorks) 85 86 #include "utilities/macros.hpp" 87 88 #include OS_CPU_HEADER(count_trailing_zeros) 89 90 ////////////////////////////////////////////////////////////////////////////// 91 #else 92 #error Unknown TARGET_COMPILER 93 94 #endif // Toolchain dispatch 95 96 #endif // include guard