Path: blob/master/test/hotspot/gtest/utilities/test_population_count.cpp
41144 views
/*1* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "runtime/os.hpp"26#include "utilities/population_count.hpp"27#include "utilities/powerOfTwo.hpp"28#include "utilities/globalDefinitions.hpp"29#include <limits>30#include "unittest.hpp"3132#define BITS_IN_BYTE_ARRAY_SIZE 2563334const uint8_t test_popcnt_bitsInByte[BITS_IN_BYTE_ARRAY_SIZE] = {350, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,361, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,371, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,382, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,391, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,402, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,412, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,423, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,431, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,442, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,452, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,463, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,472, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,483, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,493, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,504, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 851};5253template <typename T>54static void sparse() {55const T max_val = std::numeric_limits<T>::max();5657// Step through the entire input range from a random starting point,58// verify population_count return values against the lookup table59// approach used historically60T step = T(1) << ((sizeof(T) * 8) - 7);6162for (T value = os::random() % step; value < max_val - step; value += step) {63uint64_t v = (uint64_t)value;64unsigned lookup = 0u;65for (unsigned i = 0u; i < sizeof(T); i++) {66lookup += test_popcnt_bitsInByte[v & 0xff];67v >>= 8u;68}69EXPECT_EQ(lookup, population_count(value))70<< "value = " << value;71}7273// Test a few edge cases74EXPECT_EQ(0u, population_count(T(0u)))75<< "value = " << 0;76EXPECT_EQ(1u, population_count(T(1u)))77<< "value = " << 1;78EXPECT_EQ(1u, population_count(T(2u)))79<< "value = " << 2;80EXPECT_EQ(T(sizeof(T) * BitsPerByte), population_count(max_val))81<< "value = " << max_val;82EXPECT_EQ(T(sizeof(T) * BitsPerByte - 1u), population_count(T(max_val - 1u)))83<< "value = " << (max_val - 1u);84}858687TEST(population_count, sparse8) {88sparse<uint8_t>();89}90TEST(population_count, sparse16) {91sparse<uint16_t>();92}93TEST(population_count, sparse32) {94sparse<uint32_t>();95}96TEST(population_count, sparse64) {97sparse<uint64_t>();98}99100101