Path: blob/master/test/hotspot/gtest/utilities/test_bitMap_popcnt.cpp
41144 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2020, SAP and/or its affiliates.3*4* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.5*6* This code is free software; you can redistribute it and/or modify it7* under the terms of the GNU General Public License version 2 only, as8* published by the Free Software Foundation.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "precompiled.hpp"26#include "memory/allocation.hpp"27#include "runtime/os.hpp"28#include "utilities/bitMap.inline.hpp"29#include "utilities/ostream.hpp"30#include "unittest.hpp"3132// A simple stupid bitmap to mimic BitMap operations.33class SimpleFakeBitmap {3435const int _size;36char* _buffer;3738public:3940SimpleFakeBitmap(int size) : _size(size), _buffer(NEW_C_HEAP_ARRAY(char, size, mtInternal)) {41clear();42}4344~SimpleFakeBitmap() {45FREE_C_HEAP_ARRAY(char, _buffer);46}4748// Set or clear the specified bit.49void set_range(int beg, int end) { ::memset(_buffer + beg, 'X', end - beg); }50void clear_range(int beg, int end) { ::memset(_buffer + beg, ' ', end - beg); }51void clear() { clear_range(0, _size); }5253bool at(int idx) const { return _buffer[idx] == 'X'; }5455unsigned popcnt(int beg, int end) const {56int sum = 0;57for (int i = beg; i < end; i ++) {58if (at(i)) {59sum ++;60}61}62return sum;63}6465unsigned popcnt() const { return popcnt(0, _size); }6667};6869#define ASSERT_POPCNT_ALL(bm, expected) \70ASSERT_EQ(bm.count_one_bits(), (BitMap::idx_t)(expected));7172#define ASSERT_POPCNT_RANGE(bm, beg, end, expected) \73ASSERT_EQ(bm.count_one_bits(beg, end), (BitMap::idx_t)(expected));7475#define ASSERT_POPCNT_ALL_CMP(bm, fake_bm) \76ASSERT_EQ(bm.count_one_bits(), fake_bm.popcnt());7778#define ASSERT_POPCNT_RANGE_CMP(bm, beg, end, fake_bm) \79ASSERT_EQ(bm.count_one_bits(beg, end), fake_bm.popcnt(beg, end));8081static void set_or_clear_random_range(BitMap& bm, SimpleFakeBitmap& fbm, int beg, int end) {82int range = end - beg;83if (range > 0) {84int from = os::random() % range;85int to = os::random() % range;86if (from > to) {87int s = from;88from = to;89to = s;90}91from += beg;92to += beg;93if ((os::random() % 10) > 5) {94bm.set_range(from, to);95fbm.set_range(from, to);96} else {97bm.clear_range(from, to);98fbm.clear_range(from, to);99}100}101}102103static void test_bitmap_popcnt(int bitsize) {104CHeapBitMap bm(bitsize);105SimpleFakeBitmap fbm(bitsize);106107ASSERT_POPCNT_ALL(bm, 0);108ASSERT_POPCNT_RANGE(bm, 0, 0, 0);109ASSERT_POPCNT_RANGE(bm, 0, bitsize, 0);110111const int stepsize = bitsize > 64 ? 5 : 1;112113for (int beg = 0; beg < bitsize; beg += stepsize) {114for (int end = beg; end < bitsize; end += stepsize) {115116bm.clear();117bm.set_range(beg, end);118119fbm.clear();120fbm.set_range(beg, end);121122ASSERT_POPCNT_ALL_CMP(bm, fbm);123124for (int beg_query_range = 0; beg_query_range < bitsize; beg_query_range += stepsize) {125for (int end_query_range = beg_query_range; end_query_range < bitsize; end_query_range += stepsize) {126ASSERT_POPCNT_RANGE_CMP(bm, beg_query_range, end_query_range, fbm);127128// Clear some random ranges and retest129for (int n = 0; n < 3; n ++) {130set_or_clear_random_range(bm, fbm, beg, end);131ASSERT_POPCNT_RANGE_CMP(bm, beg_query_range, end_query_range, fbm);132}133134}135}136}137}138139}140141TEST_VM(BitMap, popcnt_1) { test_bitmap_popcnt(1); }142TEST_VM(BitMap, popcnt_8) { test_bitmap_popcnt(8); }143TEST_VM(BitMap, popcnt_15) { test_bitmap_popcnt(15); }144TEST_VM(BitMap, popcnt_19) { test_bitmap_popcnt(17); }145TEST_VM(BitMap, popcnt_63) { test_bitmap_popcnt(63); }146TEST_VM(BitMap, popcnt_300) { test_bitmap_popcnt(300); }147148TEST_VM(BitMap, popcnt_large) {149150CHeapBitMap bm(64 * K);151152ASSERT_POPCNT_ALL(bm, 0);153ASSERT_POPCNT_RANGE(bm, 0, 64 * K, 0);154ASSERT_POPCNT_RANGE(bm, 47, 199, 0);155156bm.set_bit(100);157158ASSERT_POPCNT_ALL(bm, 1);159ASSERT_POPCNT_RANGE(bm, 0, 64 * K, 1);160ASSERT_POPCNT_RANGE(bm, 47, 199, 1);161ASSERT_POPCNT_RANGE(bm, 199, 299, 0 );162163bm.set_range(0, 64 * K);164165ASSERT_POPCNT_ALL(bm, 64 * K);166ASSERT_POPCNT_RANGE(bm, 0, 64 * K, 64 * K);167ASSERT_POPCNT_RANGE(bm, 47, 199, 152);168ASSERT_POPCNT_RANGE(bm, 199, 299, 100);169170}171172173174