changeset 54633:bc20d0376402

8217869: Add count_leading_zeros utility Reviewed-by: neliasso, thartmann
author redestad
date Mon, 28 Jan 2019 23:00:31 +0100
parents fcfeed9fef45
children bce458ffed11 1e1b4f09b869
files src/hotspot/share/opto/regmask.cpp src/hotspot/share/opto/regmask.hpp src/hotspot/share/utilities/count_leading_zeros.hpp test/hotspot/gtest/utilities/test_count_leading_zeros.cpp test/hotspot/gtest/utilities/test_count_trailing_zeros.cpp
diffstat 5 files changed, 136 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/src/hotspot/share/opto/regmask.cpp	Mon Jan 28 21:44:57 2019 +0100
+++ b/src/hotspot/share/opto/regmask.cpp	Mon Jan 28 23:00:31 2019 +0100
@@ -32,36 +32,6 @@
 
 #define RM_SIZE _RM_SIZE /* a constant private to the class RegMask */
 
-//-------------Non-zero bit search methods used by RegMask---------------------
-// Find highest 1, or return 32 if empty
-int find_highest_bit( uint32_t mask ) {
-  int n = 0;
-  if( mask > 0xffff ) {
-    mask >>= 16;
-    n += 16;
-  }
-  if( mask > 0xff ) {
-    mask >>= 8;
-    n += 8;
-  }
-  if( mask > 0xf ) {
-    mask >>= 4;
-    n += 4;
-  }
-  if( mask > 0x3 ) {
-    mask >>= 2;
-    n += 2;
-  }
-  if( mask > 0x1 ) {
-    mask >>= 1;
-    n += 1;
-  }
-  if( mask == 0 ) {
-    n = 32;
-  }
-  return n;
-}
-
 //------------------------------dump-------------------------------------------
 
 #ifndef PRODUCT
--- a/src/hotspot/share/opto/regmask.hpp	Mon Jan 28 21:44:57 2019 +0100
+++ b/src/hotspot/share/opto/regmask.hpp	Mon Jan 28 23:00:31 2019 +0100
@@ -27,6 +27,7 @@
 
 #include "code/vmreg.hpp"
 #include "opto/optoreg.hpp"
+#include "utilities/count_leading_zeros.hpp"
 #include "utilities/count_trailing_zeros.hpp"
 
 // Some fun naming (textual) substitutions:
@@ -50,8 +51,10 @@
 static int find_lowest_bit(uint32_t mask) {
   return count_trailing_zeros(mask);
 }
-// Find highest 1, or return 32 if empty
-int find_highest_bit( uint32_t mask );
+// Find highest 1, undefined if empty/0
+static int find_highest_bit(uint32_t mask) {
+  return count_leading_zeros(mask) ^ 31;
+}
 
 //------------------------------RegMask----------------------------------------
 // The ADL file describes how to print the machine-specific registers, as well
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/hotspot/share/utilities/count_leading_zeros.hpp	Mon Jan 28 23:00:31 2019 +0100
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ *
+ */
+
+#ifndef SHARE_UTILITIES_COUNT_LEADING_ZEROS_HPP
+#define SHARE_UTILITIES_COUNT_LEADING_ZEROS_HPP
+
+#include "utilities/debug.hpp"
+#include "utilities/globalDefinitions.hpp"
+#include "utilities/count_trailing_zeros.hpp"
+
+#if defined(TARGET_COMPILER_visCPP)
+#include <intrin.h>
+#pragma intrinsic(_BitScanReverse)
+#elif defined(TARGET_COMPILER_xlc)
+#include <builtins.h>
+#endif
+
+// uint32_t count_leading_zeros(uint32_t x)
+// Return the number of leading zeros in x, e.g. the zero-based index
+// of the most significant set bit in x.  Undefined for 0.
+inline uint32_t count_leading_zeros(uint32_t x) {
+  assert(x != 0, "precondition");
+#if defined(TARGET_COMPILER_gcc)
+  return __builtin_clz(x);
+#elif defined(TARGET_COMPILER_visCPP)
+  unsigned long index;
+  _BitScanReverse(&index, x);
+  return index ^ 31u;
+#elif defined(TARGET_COMPILER_xlc)
+  return __cntlz4(x);
+#else
+  // Efficient and portable fallback implementation:
+  // http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn
+  // - with positions xor'd by 31 to get number of leading zeros
+  // rather than position of highest bit.
+  static const int MultiplyDeBruijnBitPosition[32] = {
+      31, 22, 30, 21, 18, 10, 29,  2, 20, 17, 15, 13, 9,  6, 28, 1,
+      23, 19, 11,  3, 16, 14,  7, 24, 12,  4,  8, 25, 5, 26, 27, 0
+  };
+
+  x |= x >> 1; // first round down to one less than a power of 2
+  x |= x >> 2;
+  x |= x >> 4;
+  x |= x >> 8;
+  x |= x >> 16;
+  return MultiplyDeBruijnBitPosition[(uint32_t)( x * 0x07c4acddu ) >> 27];
+#endif
+}
+
+#endif // SHARE_UTILITIES_COUNT_LEADING_ZEROS_HPP
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/gtest/utilities/test_count_leading_zeros.cpp	Mon Jan 28 23:00:31 2019 +0100
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ *
+ */
+
+#include "precompiled.hpp"
+#include "utilities/count_leading_zeros.hpp"
+#include "utilities/globalDefinitions.hpp"
+#include "unittest.hpp"
+
+TEST(count_leading_zeros, one_or_two_set_bits) {
+  unsigned i = 0;                  // Position of a set bit.
+  for (uint32_t ix = 1; ix != 0; ix <<= 1, ++i) {
+    unsigned j = 0;                // Position of a set bit.
+    for (uint32_t jx = 1; jx != 0; jx <<= 1, ++j) {
+      uint32_t value = ix | jx;
+      EXPECT_EQ(31u - MAX2(i, j), count_leading_zeros(value))
+        << "value = " << value;
+    }
+  }
+}
+
+TEST(count_leading_zeros, high_zeros_low_ones) {
+  unsigned i = 0;                  // Number of leading zeros
+  uint32_t value = ~(uint32_t)0;
+  for ( ; value != 0; value >>= 1, ++i) {
+    EXPECT_EQ(i, count_leading_zeros(value))
+      << "value = " << value;
+  }
+}
+
+TEST(count_leading_zeros, high_ones_low_zeros) {
+  uint32_t value = ~(uint32_t)0;
+  for ( ; value != 0; value <<= 1) {
+    EXPECT_EQ(0u, count_leading_zeros(value))
+      << "value = " << value;
+  }
+}
--- a/test/hotspot/gtest/utilities/test_count_trailing_zeros.cpp	Mon Jan 28 21:44:57 2019 +0100
+++ b/test/hotspot/gtest/utilities/test_count_trailing_zeros.cpp	Mon Jan 28 23:00:31 2019 +0100
@@ -39,16 +39,15 @@
   }
 }
 
-TEST(count_trailing_zeros, all_ones_followed_by_all_zeros) {
-  unsigned i = BitsPerWord - 1; // Index of most significant set bit.
+TEST(count_trailing_zeros, high_zeros_low_ones) {
   uintx value = ~(uintx)0;
-  for ( ; value != 0; value >>= 1, --i) {
+  for ( ; value != 0; value >>= 1) {
     EXPECT_EQ(0u, count_trailing_zeros(value))
       << "value = " << value;
   }
 }
 
-TEST(count_trailing_zeros, all_zeros_followed_by_all_ones) {
+TEST(count_trailing_zeros, high_ones_low_zeros) {
   unsigned i = 0;               // Index of least significant set bit.
   uintx value = ~(uintx)0;
   for ( ; value != 0; value <<= 1, ++i) {