test/hotspot/gtest/utilities/test_population_count.cpp
author manc
Mon, 14 Oct 2019 18:48:10 -0700
changeset 58652 9b67dd88a931
parent 53450 cad3e5102aa6
child 58962 2dcfc28a314d
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:
53443
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
     1
/*
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
     4
 *
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
     7
 * published by the Free Software Foundation.
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
     8
 *
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    13
 * accompanied this code).
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    14
 *
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    18
 *
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    21
 * questions.
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    22
 *
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    23
 */
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    24
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    25
#include "precompiled.hpp"
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    26
#include "libadt/vectset.hpp"
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    27
#include "runtime/os.hpp"
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    28
#include "utilities/population_count.hpp"
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    29
#include "utilities/globalDefinitions.hpp"
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    30
#include "unittest.hpp"
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    31
53450
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    32
uint8_t test_popcnt_bitsInByte[BITS_IN_BYTE_ARRAY_SIZE] = {
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    33
        0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    34
        1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    35
        1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    36
        2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    37
        1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    38
        2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    39
        2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    40
        3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    41
        1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    42
        2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    43
        2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    44
        3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    45
        2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    46
        3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    47
        3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    48
        4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    49
};
53443
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    50
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    51
TEST(population_count, sparse) {
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    52
  // Step through the entire input range from a random starting point,
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    53
  // verify population_count return values against the lookup table
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    54
  // approach used historically
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    55
  uint32_t step = 4711;
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    56
  for (uint32_t value = os::random() % step; value < UINT_MAX - step; value += step) {
53450
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    57
    uint32_t lookup = test_popcnt_bitsInByte[(value >> 24) & 0xff] +
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    58
                      test_popcnt_bitsInByte[(value >> 16) & 0xff] +
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    59
                      test_popcnt_bitsInByte[(value >> 8)  & 0xff] +
cad3e5102aa6 8217639: Minimal and Zero builds fail after JDK-8217519 (Improve RegMask population count calculation)
shade
parents: 53443
diff changeset
    60
                      test_popcnt_bitsInByte[ value        & 0xff];
53443
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    61
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    62
    EXPECT_EQ(lookup, population_count(value))
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    63
        << "value = " << value;
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    64
  }
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    65
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    66
  // Test a few edge cases
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    67
  EXPECT_EQ(0u, population_count(0u))
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    68
      << "value = " << 0;
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    69
  EXPECT_EQ(1u, population_count(1u))
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    70
      << "value = " << 1;
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    71
  EXPECT_EQ(1u, population_count(2u))
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    72
      << "value = " << 2;
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    73
  EXPECT_EQ(32u, population_count(UINT_MAX))
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    74
      << "value = " << UINT_MAX;
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    75
  EXPECT_EQ(31u, population_count(UINT_MAX - 1))
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    76
      << "value = " << (UINT_MAX - 1);
675d857f5ee3 8217519: Improve RegMask population count calculation
redestad
parents:
diff changeset
    77
}