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  * GCC and compatible (including Clang)
  40  *****************************************************************************/
  41 #if defined(TARGET_COMPILER_gcc)
  42 
  43 inline unsigned count_trailing_zeros(uintx x) {
  44   STATIC_ASSERT(sizeof(unsigned long) == sizeof(uintx));
  45   assert(x != 0, "precondition");
  46   return __builtin_ctzl(x);
  47 }
  48 
  49 /*****************************************************************************
  50  * Microsoft Visual Studio
  51  *****************************************************************************/
  52 #elif defined(TARGET_COMPILER_visCPP)
  53 
  54 #include <intrin.h>
  55 
  56 #ifdef _LP64
  57 #pragma intrinsic(_BitScanForward64)
  58 #else
  59 #pragma intrinsic(_BitScanForward)
  60 #endif
  61 
  62 inline unsigned count_trailing_zeros(uintx x) {
  63   assert(x != 0, "precondition");
  64   unsigned long index;
  65 #ifdef _LP64
  66   _BitScanForward64(&index, x);
  67 #else
  68   _BitScanForward(&index, x);
  69 #endif
  70   return index;
  71 }
  72 
  73 /*****************************************************************************
  74  * IBM XL C/C++
  75  *****************************************************************************/
  76 #elif defined(TARGET_COMPILER_xlc)
  77 
  78 #include <builtins.h>
  79 
  80 inline unsigned count_trailing_zeros(uintx x) {
  81   assert(x != 0, "precondition");
  82 #ifdef _LP64
  83   return __cnttz8(x);
  84 #else
  85   return __cnttz4(x);
  86 #endif
  87 }
  88 
  89 /*****************************************************************************
  90  * Oracle Studio
  91  *****************************************************************************/
  92 #elif defined(TARGET_COMPILER_sparcWorks)
  93 
  94 // No compiler built-in / intrinsic, so use inline assembler.
  95 
  96 #include "utilities/macros.hpp"
  97 
  98 #include OS_CPU_HEADER(count_trailing_zeros)
  99 
 100 /*****************************************************************************
 101  * Unknown toolchain
 102  *****************************************************************************/
 103 #else
 104 #error Unknown TARGET_COMPILER
 105 
 106 #endif // Toolchain dispatch
 107 
 108 #endif // include guard