MEM: Introduce the master/slave port sub-classes in C++
[gem5.git] / src / mem / ruby / filters / NonCountingBloomFilter.cc
1 /*
2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "base/intmath.hh"
30 #include "base/str.hh"
31 #include "mem/ruby/filters/NonCountingBloomFilter.hh"
32 #include "mem/ruby/system/System.hh"
33
34 using namespace std;
35
36 NonCountingBloomFilter::NonCountingBloomFilter(string str)
37 {
38 string head, tail;
39 #ifndef NDEBUG
40 bool success =
41 #endif
42 split_first(str, head, tail, '_');
43 assert(success);
44
45 // head contains filter size, tail contains bit offset from block number
46 m_filter_size = atoi(head.c_str());
47 m_offset = atoi(tail.c_str());
48 m_filter_size_bits = floorLog2(m_filter_size);
49
50 m_filter.resize(m_filter_size);
51 clear();
52 }
53
54 NonCountingBloomFilter::~NonCountingBloomFilter()
55 {
56 }
57
58 void
59 NonCountingBloomFilter::clear()
60 {
61 for (int i = 0; i < m_filter_size; i++) {
62 m_filter[i] = 0;
63 }
64 }
65
66 void
67 NonCountingBloomFilter::increment(const Address& addr)
68 {
69 // Not used
70 }
71
72 void
73 NonCountingBloomFilter::decrement(const Address& addr)
74 {
75 // Not used
76 }
77
78 void
79 NonCountingBloomFilter::merge(AbstractBloomFilter *other_filter)
80 {
81 // assumes both filters are the same size!
82 NonCountingBloomFilter * temp = (NonCountingBloomFilter*) other_filter;
83 for(int i = 0; i < m_filter_size; ++i){
84 m_filter[i] |= (*temp)[i];
85 }
86 }
87
88 void
89 NonCountingBloomFilter::set(const Address& addr)
90 {
91 int i = get_index(addr);
92 m_filter[i] = 1;
93 }
94
95 void
96 NonCountingBloomFilter::unset(const Address& addr)
97 {
98 int i = get_index(addr);
99 m_filter[i] = 0;
100 }
101
102 bool
103 NonCountingBloomFilter::isSet(const Address& addr)
104 {
105 int i = get_index(addr);
106 return (m_filter[i]);
107 }
108
109
110 int
111 NonCountingBloomFilter::getCount(const Address& addr)
112 {
113 return m_filter[get_index(addr)];
114 }
115
116 int
117 NonCountingBloomFilter::getTotalCount()
118 {
119 int count = 0;
120
121 for (int i = 0; i < m_filter_size; i++) {
122 count += m_filter[i];
123 }
124 return count;
125 }
126
127 void
128 NonCountingBloomFilter::print(ostream& out) const
129 {
130 }
131
132 int
133 NonCountingBloomFilter::getIndex(const Address& addr)
134 {
135 return get_index(addr);
136 }
137
138 int
139 NonCountingBloomFilter::readBit(const int index)
140 {
141 return m_filter[index];
142 }
143
144 void
145 NonCountingBloomFilter::writeBit(const int index, const int value)
146 {
147 m_filter[index] = value;
148 }
149
150 int
151 NonCountingBloomFilter::get_index(const Address& addr)
152 {
153 return addr.bitSelect(RubySystem::getBlockSizeBits() + m_offset,
154 RubySystem::getBlockSizeBits() + m_offset +
155 m_filter_size_bits - 1);
156 }
157
158