2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
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.
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.
29 // modified (rewritten) 05/20/05 by Dan Gibson to accomimdate FASTER
34 #include "base/misc.hh"
35 #include "mem/ruby/common/Set.hh"
36 #include "mem/ruby/system/System.hh"
45 Set::Set(const Set
& obj
)
50 // copy from the host to this array
51 for (int i
= 0; i
< m_nArrayLen
; i
++)
52 m_p_nArray
[i
] = obj
.m_p_nArray
[i
];
66 if (m_p_nArray
&& m_p_nArray
!= &m_p_nArray_Static
[0])
74 // now just ensure that no bits over the maximum size were set
76 long mask
= 0x7FFFFFFFFFFFFFFF;
78 long mask
= 0x7FFFFFFF;
81 // the number of populated spaces in the higest-order array slot
82 // is: m_nSize % LONG_BITS, so the uppermost LONG_BITS -
83 // m_nSize%64 bits should be cleared
84 if ((m_nSize
% LONG_BITS
) != 0) {
85 for (int j
= 0; j
< 64 - (m_nSize
& INDEX_MASK
); j
++) {
86 m_p_nArray
[m_nArrayLen
- 1] &= mask
;
94 * This function should set all the bits in the current set that are
95 * already set in the parameter set
98 Set::addSet(const Set
& set
)
100 assert(getSize()==set
.getSize());
101 for (int i
= 0; i
< m_nArrayLen
; i
++)
102 m_p_nArray
[i
] |= set
.m_p_nArray
[i
];
106 * This function should randomly assign 1 to the bits in the set--it
107 * should not clear the bits bits first, though?
113 for (int i
= 0; i
< m_nArrayLen
; i
++) {
114 // this ensures that all 32 bits are subject to random effects,
115 // as RAND_MAX typically = 0x7FFFFFFF
116 m_p_nArray
[i
] |= random() ^ (random() << 4);
122 * This function clears bits that are =1 in the parameter set
125 Set::removeSet(const Set
& set
)
127 assert(m_nSize
== set
.m_nSize
);
128 for (int i
= 0; i
< m_nArrayLen
; i
++)
129 m_p_nArray
[i
] &= ~set
.m_p_nArray
[i
];
133 * this function sets all bits in the set
138 for (int i
= 0; i
< m_nArrayLen
; i
++)
139 m_p_nArray
[i
] = -1; // note that -1 corresponds to all 1's in 2's comp.
145 * This function returns the population count of 1's in the set
153 for (int i
= 0; i
< m_nArrayLen
; i
++) {
156 for (int j
= 0; j
< LONG_BITS
; j
++) {
157 // FIXME - significant performance loss when array
158 // population << LONG_BITS
159 if ((m_p_nArray
[i
] & mask
) != 0) {
170 * This function checks for set equality
173 Set::isEqual(const Set
& set
) const
175 assert(m_nSize
== set
.m_nSize
);
177 for (int i
= 0; i
< m_nArrayLen
; i
++)
178 if (m_p_nArray
[i
] != set
.m_p_nArray
[i
])
185 * This function returns the NodeID (int) of the least set bit
188 Set::smallestElement() const
192 for (int i
= 0; i
< m_nArrayLen
; i
++) {
193 if (m_p_nArray
[i
] != 0) {
194 // the least-set bit must be in here
197 for (int j
= 0; j
< LONG_BITS
; j
++) {
198 if (x
& (unsigned long)1) {
199 return LONG_BITS
* i
+ j
;
205 panic("No smallest element of an empty set.");
209 panic("No smallest element of an empty set.");
213 * this function returns true iff all bits are set
216 Set::isBroadcast() const
218 // check the fully-loaded words by equal to 0xffffffff
219 // only the last word may not be fully loaded, it is not
220 // fully loaded iff m_nSize % 32 or 64 !=0 => fully loaded iff
221 // m_nSize % 32 or 64 == 0
223 int max
= (m_nSize
% LONG_BITS
) == 0 ? m_nArrayLen
: m_nArrayLen
- 1;
224 for (int i
= 0; i
< max
; i
++) {
225 if (m_p_nArray
[i
] != -1) {
230 // now check the last word, which may not be fully loaded
232 for (int j
= 0; j
< (m_nSize
% LONG_BITS
); j
++) {
233 if ((mask
& m_p_nArray
[m_nArrayLen
-1]) == 0) {
243 * this function returns true iff no bits are set
248 // here we can simply check if all = 0, since we ensure
249 // that "extra slots" are all zero
250 for (int i
= 0; i
< m_nArrayLen
; i
++)
257 // returns the logical OR of "this" set and orSet
259 Set::OR(const Set
& orSet
) const
262 assert(m_nSize
== orSet
.m_nSize
);
263 for (int i
= 0; i
< m_nArrayLen
; i
++)
264 result
.m_p_nArray
[i
] = m_p_nArray
[i
] | orSet
.m_p_nArray
[i
];
269 // returns the logical AND of "this" set and andSet
271 Set::AND(const Set
& andSet
) const
274 assert(m_nSize
== andSet
.m_nSize
);
276 for (int i
= 0; i
< m_nArrayLen
; i
++) {
277 result
.m_p_nArray
[i
] = m_p_nArray
[i
] & andSet
.m_p_nArray
[i
];
284 * Returns false if a bit is set in the parameter set that is NOT set
288 Set::isSuperset(const Set
& test
) const
290 assert(m_nSize
== test
.m_nSize
);
292 for (int i
= 0; i
< m_nArrayLen
; i
++)
293 if (((test
.m_p_nArray
[i
] & m_p_nArray
[i
]) | ~test
.m_p_nArray
[i
]) != -1)
300 Set::setSize(int size
)
303 m_nArrayLen
= (m_nSize
+ LONG_BITS
- 1) / LONG_BITS
;
305 // decide whether to use dynamic or static alloction
306 if (m_nArrayLen
<= NUMBER_WORDS_PER_SET
) {
307 // constant defined in RubySystem.hh
308 // its OK to use the static allocation, and it will
309 // probably be faster (as m_nArrayLen is already in the
310 // cache and they will probably share the same cache line)
312 // if switching from dyanamic to static allocation (which
313 // is probably rare, but why not be complete?), must delete
314 // the dynamically allocated space
315 if (m_p_nArray
&& m_p_nArray
!= &m_p_nArray_Static
[0])
316 delete [] m_p_nArray
;
318 m_p_nArray
= &m_p_nArray_Static
[0];
320 // can't use static allocation...simply not enough room
321 // so dynamically allocate some space
322 if (m_p_nArray
&& m_p_nArray
!= &m_p_nArray_Static
[0])
323 delete [] m_p_nArray
;
325 m_p_nArray
= new long[m_nArrayLen
];
332 Set::operator=(const Set
& obj
)
336 setSize(obj
.getSize());
338 // copy the elements from obj to this
339 for (int i
= 0; i
< m_nArrayLen
; i
++)
340 m_p_nArray
[i
] = obj
.m_p_nArray
[i
];
347 Set::print(std::ostream
& out
) const
350 out
<< "[Set {Empty}]";
355 out
<< "[Set (" << m_nSize
<< ")";
356 for (int i
= m_nArrayLen
- 1; i
>= 0; i
--) {
357 csprintf(buff
," 0x%08X", m_p_nArray
[i
]);