test/hotspot/gtest/utilities/test_count_leading_zeros.cpp
author manc
Mon, 14 Oct 2019 18:48:10 -0700
changeset 58652 9b67dd88a931
parent 53532 bc20d0376402
permissions -rw-r--r--
8232232: G1RemSetSummary::_rs_threads_vtimes is not initialized to zero Summary: Fix error in "Concurrent refinement threads times" in GC log and cleanup. Reviewed-by: tschatzl, kbarrett
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
#include "precompiled.hpp"
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    26
#include "utilities/count_leading_zeros.hpp"
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    27
#include "utilities/globalDefinitions.hpp"
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    28
#include "unittest.hpp"
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    29
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    30
TEST(count_leading_zeros, one_or_two_set_bits) {
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    31
  unsigned i = 0;                  // Position of a set bit.
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    32
  for (uint32_t ix = 1; ix != 0; ix <<= 1, ++i) {
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    33
    unsigned j = 0;                // Position of a set bit.
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    34
    for (uint32_t jx = 1; jx != 0; jx <<= 1, ++j) {
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    35
      uint32_t value = ix | jx;
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    36
      EXPECT_EQ(31u - MAX2(i, j), count_leading_zeros(value))
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    37
        << "value = " << value;
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    38
    }
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    39
  }
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    40
}
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    41
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    42
TEST(count_leading_zeros, high_zeros_low_ones) {
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    43
  unsigned i = 0;                  // Number of leading zeros
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    44
  uint32_t value = ~(uint32_t)0;
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    45
  for ( ; value != 0; value >>= 1, ++i) {
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    46
    EXPECT_EQ(i, count_leading_zeros(value))
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    47
      << "value = " << value;
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    48
  }
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    49
}
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    50
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    51
TEST(count_leading_zeros, high_ones_low_zeros) {
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    52
  uint32_t value = ~(uint32_t)0;
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    53
  for ( ; value != 0; value <<= 1) {
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    54
    EXPECT_EQ(0u, count_leading_zeros(value))
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    55
      << "value = " << value;
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    56
  }
bc20d0376402 8217869: Add count_leading_zeros utility
redestad
parents:
diff changeset
    57
}