test/hotspot/gtest/utilities/test_count_trailing_zeros.cpp
author stuefe
Mon, 23 Apr 2018 16:25:16 +0200
changeset 49852 4d3218e5f170
parent 47216 71c04702a3d5
child 53532 bc20d0376402
permissions -rw-r--r--
8202073: MetaspaceAllocationTest gtest shall lock during space creation Reviewed-by: coleenp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46437
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
     1
/*
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
     4
 *
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
     7
 * published by the Free Software Foundation.
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
     8
 *
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    13
 * accompanied this code).
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    14
 *
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    18
 *
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    21
 * questions.
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    22
 *
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    23
 */
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    24
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    25
#include "precompiled.hpp"
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    26
#include "utilities/count_trailing_zeros.hpp"
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    27
#include "utilities/globalDefinitions.hpp"
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    28
#include "unittest.hpp"
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    29
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    30
TEST(count_trailing_zeros, one_or_two_set_bits) {
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    31
  unsigned i = 0;               // Position of a set bit.
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    32
  for (uintx ix = 1; ix != 0; ix <<= 1, ++i) {
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    33
    unsigned j = 0;             // Position of a set bit.
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    34
    for (uintx jx = 1; jx != 0; jx <<= 1, ++j) {
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    35
      uintx value = ix | jx;
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    36
      EXPECT_EQ(MIN2(i, j), count_trailing_zeros(value))
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    37
        << "value = " << value;
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    38
    }
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    39
  }
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    40
}
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    41
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    42
TEST(count_trailing_zeros, all_ones_followed_by_all_zeros) {
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    43
  unsigned i = BitsPerWord - 1; // Index of most significant set bit.
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    44
  uintx value = ~(uintx)0;
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    45
  for ( ; value != 0; value >>= 1, --i) {
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    46
    EXPECT_EQ(0u, count_trailing_zeros(value))
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    47
      << "value = " << value;
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    48
  }
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    49
}
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    50
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    51
TEST(count_trailing_zeros, all_zeros_followed_by_all_ones) {
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    52
  unsigned i = 0;               // Index of least significant set bit.
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    53
  uintx value = ~(uintx)0;
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    54
  for ( ; value != 0; value <<= 1, ++i) {
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    55
    EXPECT_EQ(i, count_trailing_zeros(value))
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    56
      << "value = " << value;
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    57
  }
cda23e690843 8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff changeset
    58
}