1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, SAP and/or its affiliates.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include "precompiled.hpp"
#include "memory/allocation.hpp"
#include "runtime/os.hpp"
#include "utilities/bitMap.inline.hpp"
#include "utilities/ostream.hpp"
#include "unittest.hpp"
// A simple stupid bitmap to mimic BitMap operations.
class SimpleFakeBitmap {
const int _size;
char* _buffer;
public:
SimpleFakeBitmap(int size) : _size(size), _buffer(NEW_C_HEAP_ARRAY(char, size, mtInternal)) {
clear();
}
~SimpleFakeBitmap() {
FREE_C_HEAP_ARRAY(char, _buffer);
}
// Set or clear the specified bit.
void set_range(int beg, int end) { ::memset(_buffer + beg, 'X', end - beg); }
void clear_range(int beg, int end) { ::memset(_buffer + beg, ' ', end - beg); }
void clear() { clear_range(0, _size); }
bool at(int idx) const { return _buffer[idx] == 'X'; }
unsigned popcnt(int beg, int end) const {
int sum = 0;
for (int i = beg; i < end; i ++) {
if (at(i)) {
sum ++;
}
}
return sum;
}
unsigned popcnt() const { return popcnt(0, _size); }
};
#define ASSERT_POPCNT_ALL(bm, expected) \
ASSERT_EQ(bm.count_one_bits(), (BitMap::idx_t)(expected));
#define ASSERT_POPCNT_RANGE(bm, beg, end, expected) \
ASSERT_EQ(bm.count_one_bits(beg, end), (BitMap::idx_t)(expected));
#define ASSERT_POPCNT_ALL_CMP(bm, fake_bm) \
ASSERT_EQ(bm.count_one_bits(), fake_bm.popcnt());
#define ASSERT_POPCNT_RANGE_CMP(bm, beg, end, fake_bm) \
ASSERT_EQ(bm.count_one_bits(beg, end), fake_bm.popcnt(beg, end));
static void set_or_clear_random_range(BitMap& bm, SimpleFakeBitmap& fbm, int beg, int end) {
int range = end - beg;
if (range > 0) {
int from = os::random() % range;
int to = os::random() % range;
if (from > to) {
int s = from;
from = to;
to = s;
}
from += beg;
to += beg;
if ((os::random() % 10) > 5) {
bm.set_range(from, to);
fbm.set_range(from, to);
} else {
bm.clear_range(from, to);
fbm.clear_range(from, to);
}
}
}
static void test_bitmap_popcnt(int bitsize) {
CHeapBitMap bm(bitsize, mtTest);
SimpleFakeBitmap fbm(bitsize);
ASSERT_POPCNT_ALL(bm, 0);
ASSERT_POPCNT_RANGE(bm, 0, 0, 0);
ASSERT_POPCNT_RANGE(bm, 0, bitsize, 0);
const int stepsize = bitsize > 64 ? 5 : 1;
for (int beg = 0; beg < bitsize; beg += stepsize) {
for (int end = beg; end < bitsize; end += stepsize) {
bm.clear();
bm.set_range(beg, end);
fbm.clear();
fbm.set_range(beg, end);
ASSERT_POPCNT_ALL_CMP(bm, fbm);
for (int beg_query_range = 0; beg_query_range < bitsize; beg_query_range += stepsize) {
for (int end_query_range = beg_query_range; end_query_range < bitsize; end_query_range += stepsize) {
ASSERT_POPCNT_RANGE_CMP(bm, beg_query_range, end_query_range, fbm);
// Clear some random ranges and retest
for (int n = 0; n < 3; n ++) {
set_or_clear_random_range(bm, fbm, beg, end);
ASSERT_POPCNT_RANGE_CMP(bm, beg_query_range, end_query_range, fbm);
}
}
}
}
}
}
TEST_VM(BitMap, popcnt_1) { test_bitmap_popcnt(1); }
TEST_VM(BitMap, popcnt_8) { test_bitmap_popcnt(8); }
TEST_VM(BitMap, popcnt_15) { test_bitmap_popcnt(15); }
TEST_VM(BitMap, popcnt_19) { test_bitmap_popcnt(17); }
TEST_VM(BitMap, popcnt_63) { test_bitmap_popcnt(63); }
TEST_VM(BitMap, popcnt_300) { test_bitmap_popcnt(300); }
TEST_VM(BitMap, popcnt_large) {
CHeapBitMap bm(64 * K, mtTest);
ASSERT_POPCNT_ALL(bm, 0);
ASSERT_POPCNT_RANGE(bm, 0, 64 * K, 0);
ASSERT_POPCNT_RANGE(bm, 47, 199, 0);
bm.set_bit(100);
ASSERT_POPCNT_ALL(bm, 1);
ASSERT_POPCNT_RANGE(bm, 0, 64 * K, 1);
ASSERT_POPCNT_RANGE(bm, 47, 199, 1);
ASSERT_POPCNT_RANGE(bm, 199, 299, 0 );
bm.set_range(0, 64 * K);
ASSERT_POPCNT_ALL(bm, 64 * K);
ASSERT_POPCNT_RANGE(bm, 0, 64 * K, 64 * K);
ASSERT_POPCNT_RANGE(bm, 47, 199, 152);
ASSERT_POPCNT_RANGE(bm, 199, 299, 100);
}
|