src/hotspot/share/utilities/count_leading_zeros.hpp
author redestad
Mon, 28 Jan 2019 23:00:31 +0100
changeset 53532 bc20d0376402
permissions -rw-r--r--
8217869: Add count_leading_zeros utility Reviewed-by: neliasso, thartmann
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53532
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
     1
/*
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
     4
 *
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
     7
 * published by the Free Software Foundation.
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
     8
 *
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    13
 * accompanied this code).
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    14
 *
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    18
 *
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    21
 * questions.
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    22
 *
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    23
 */
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    24
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    25
#ifndef SHARE_UTILITIES_COUNT_LEADING_ZEROS_HPP
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    26
#define SHARE_UTILITIES_COUNT_LEADING_ZEROS_HPP
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    27
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    28
#include "utilities/debug.hpp"
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    29
#include "utilities/globalDefinitions.hpp"
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    30
#include "utilities/count_trailing_zeros.hpp"
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    31
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    32
#if defined(TARGET_COMPILER_visCPP)
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    33
#include <intrin.h>
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    34
#pragma intrinsic(_BitScanReverse)
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    35
#elif defined(TARGET_COMPILER_xlc)
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    36
#include <builtins.h>
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    37
#endif
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    38
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    39
// uint32_t count_leading_zeros(uint32_t x)
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    40
// Return the number of leading zeros in x, e.g. the zero-based index
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    41
// of the most significant set bit in x.  Undefined for 0.
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    42
inline uint32_t count_leading_zeros(uint32_t x) {
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    43
  assert(x != 0, "precondition");
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    44
#if defined(TARGET_COMPILER_gcc)
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    45
  return __builtin_clz(x);
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    46
#elif defined(TARGET_COMPILER_visCPP)
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    47
  unsigned long index;
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    48
  _BitScanReverse(&index, x);
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    49
  return index ^ 31u;
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    50
#elif defined(TARGET_COMPILER_xlc)
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    51
  return __cntlz4(x);
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    52
#else
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    53
  // Efficient and portable fallback implementation:
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    54
  // http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    55
  // - with positions xor'd by 31 to get number of leading zeros
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    56
  // rather than position of highest bit.
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    57
  static const int MultiplyDeBruijnBitPosition[32] = {
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    58
      31, 22, 30, 21, 18, 10, 29,  2, 20, 17, 15, 13, 9,  6, 28, 1,
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    59
      23, 19, 11,  3, 16, 14,  7, 24, 12,  4,  8, 25, 5, 26, 27, 0
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    60
  };
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    61
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    62
  x |= x >> 1; // first round down to one less than a power of 2
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    63
  x |= x >> 2;
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    64
  x |= x >> 4;
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    65
  x |= x >> 8;
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    66
  x |= x >> 16;
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    67
  return MultiplyDeBruijnBitPosition[(uint32_t)( x * 0x07c4acddu ) >> 27];
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    68
#endif
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    69
}
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    70
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    71
#endif // SHARE_UTILITIES_COUNT_LEADING_ZEROS_HPP