Line 29... |
Line 29... |
#include <iostream>
|
#include <iostream>
|
#include <cstring>
|
#include <cstring>
|
|
|
#include "SprCache.h"
|
#include "SprCache.h"
|
|
|
|
|
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
//! Constructor
|
//! Constructor
|
|
|
//! Allocate tables and clear the cache
|
//! Allocate tables and clear the cache
|
|
|
Line 49... |
Line 48... |
|
|
clear ();
|
clear ();
|
|
|
} // SprCache ()
|
} // SprCache ()
|
|
|
|
|
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
//! Destructor
|
//! Destructor
|
|
|
//! Free up the tables
|
//! Free up the tables
|
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
Line 63... |
Line 61... |
delete [] sprKeyNum;
|
delete [] sprKeyNum;
|
delete [] sprIsValid;
|
delete [] sprIsValid;
|
|
|
} // ~SprCache ()
|
} // ~SprCache ()
|
|
|
|
|
//! Empty the hash table
|
//! Empty the hash table
|
|
|
//! Only need to worry about the validity field
|
//! Only need to worry about the validity field
|
void
|
void
|
SprCache::clear ()
|
SprCache::clear ()
|
Line 77... |
Line 74... |
// No more than 70% full
|
// No more than 70% full
|
maxToUse = tableSize * 7 / 10;
|
maxToUse = tableSize * 7 / 10;
|
|
|
} // clear ()
|
} // clear ()
|
|
|
|
|
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
//! Write a new value into the cache
|
//! Write a new value into the cache
|
|
|
//! If the hash table is full silently does nothing, unless the force
|
//! If the hash table is full silently does nothing, unless the force
|
//! parameter is set to TRUE. Under this circumstance the value WILL be
|
//! parameter is set to TRUE. Under this circumstance the value WILL be
|
Line 91... |
Line 87... |
//! @param[in] spr The SPR being written to
|
//! @param[in] spr The SPR being written to
|
//! @param[in] value The value to write
|
//! @param[in] value The value to write
|
//! @param[in] force If TRUE the value will be written to the hash table,
|
//! @param[in] force If TRUE the value will be written to the hash table,
|
//! even if it is too full.
|
//! even if it is too full.
|
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
void
|
void SprCache::write(uint16_t sprNum, uint32_t value, bool force)
|
SprCache::write (uint16_t sprNum,
|
|
uint32_t value,
|
|
bool force)
|
|
{
|
|
if (maxToUse <= 0)
|
|
{
|
{
|
|
if (maxToUse <= 0) {
|
return; // Table is full
|
return; // Table is full
|
}
|
}
|
|
|
int hv = sprNum % tableSize;
|
int hv = sprNum % tableSize;
|
|
|
// We can use the slot if either it is empty, or it is full and the key
|
// We can use the slot if either it is empty, or it is full and the key
|
// number matches.
|
// number matches.
|
while (sprIsValid[hv] && (sprKeyNum[hv] != sprNum))
|
while (sprIsValid[hv] && (sprKeyNum[hv] != sprNum)) {
|
{
|
|
hv = (hv + 1) % tableSize;
|
hv = (hv + 1) % tableSize;
|
}
|
}
|
|
|
sprIsValid[hv] = true;
|
sprIsValid[hv] = true;
|
sprKeyNum[hv] = sprNum;
|
sprKeyNum[hv] = sprNum;
|
sprValue[hv] = value;
|
sprValue[hv] = value;
|
maxToUse--;
|
maxToUse--;
|
|
|
} // write ()
|
} // write ()
|
|
|
|
|
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
//! Try to read a value from the cache
|
//! Try to read a value from the cache
|
|
|
//! The entry must be valid.
|
//! The entry must be valid.
|
|
|
Line 129... |
Line 119... |
//! @param[out] value The value read. Will be written, even if the value is
|
//! @param[out] value The value read. Will be written, even if the value is
|
//! not valid.
|
//! not valid.
|
|
|
//! @return True if the value was found in the hash table
|
//! @return True if the value was found in the hash table
|
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
bool
|
bool SprCache::read(uint16_t sprNum, uint32_t & value)
|
SprCache::read (uint16_t sprNum,
|
|
uint32_t &value)
|
|
{
|
{
|
int hv = sprNum % tableSize;
|
int hv = sprNum % tableSize;
|
|
|
// Look for either an empty slot (we are not there) or a matching key (we
|
// Look for either an empty slot (we are not there) or a matching key (we
|
// are there)
|
// are there)
|
while (sprIsValid[hv] && (sprKeyNum[hv] != sprNum))
|
while (sprIsValid[hv] && (sprKeyNum[hv] != sprNum)) {
|
{
|
|
hv = (hv + 1) % tableSize;
|
hv = (hv + 1) % tableSize;
|
}
|
}
|
|
|
value = sprValue[hv];
|
value = sprValue[hv];
|
return sprIsValid[hv];
|
return sprIsValid[hv];
|