Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / rst / rstzip3 / rstzip_v2 / VCache.h
CommitLineData
920dae64
AT
1/*
2* ========== Copyright Header Begin ==========================================
3*
4* OpenSPARC T2 Processor File: VCache.h
5* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
6* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
7*
8* The above named program is free software; you can redistribute it and/or
9* modify it under the terms of the GNU General Public
10* License version 2 as published by the Free Software Foundation.
11*
12* The above named program is distributed in the hope that it will be
13* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15* General Public License for more details.
16*
17* You should have received a copy of the GNU General Public
18* License along with this work; if not, write to the Free Software
19* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20*
21* ========== Copyright Header End ============================================
22*/
23#ifndef INCLUDED_VALUECACHE_H
24#define INCLUDED_VALUECACHE_H
25#include <sys/types.h>
26#include <assert.h>
27#include <stdio.h>
28
29struct VCEntry {
30 VCEntry() : valid( false ), counter( 0 )
31 {}
32
33 void invalidate() { valid = false; }
34 void incCounter() { ++counter; }
35 bool hit( uint64_t v ) { ++counter; return valid && value == v; }
36 void insert( uint64_t d ) { value = d; valid = true; counter = 1; }
37
38 uint64_t value;
39 bool valid;
40 uint32_t counter;
41};
42
43class InvalidIndexException {
44
45 public:
46 InvalidIndexException( int i ) : idx( i )
47 {}
48
49 int idx;
50
51};
52
53class VCache {
54
55 public:
56 typedef uint16_t IdxT;
57 typedef int16_t DiffT;
58 enum { VALUE_NOT_FOUND = -1 };
59
60 VCache( unsigned size = 65535 ) : evictions(0), vcacheSize(size)
61 {
62 cache = new VCEntry[vcacheSize];
63 }
64
65 ~VCache()
66 {
67 delete [] cache;
68 }
69
70 void reset()
71 {
72 for( IdxT i = 0; i < vcacheSize; ++i ){
73 cache[i].invalidate();
74 }
75 }
76
77 bool hit( uint64_t d, IdxT &idx )
78 {
79 idx = hash( d );
80
81 return cache[idx].hit( d );
82 }
83
84 // compatability w/ existing code
85 int hit( uint64_t d )
86 {
87 IdxT i;
88
89 if( hit( d, i ) ){
90 return i;
91 } else {
92 return -1;
93 }
94 }
95
96 IdxT insert( uint64_t d )
97 {
98 IdxT idx = hash( d );
99
100 if( cache[idx].valid ){
101 ++evictions;
102 }
103
104 cache[idx].insert( d );
105
106 return idx;
107 }
108
109
110 IdxT hash( uint64_t d )
111 {
112 //return (d ^ (d & 0x0FUL)) % vcacheSize;
113 return (IdxT)(d % vcacheSize);
114 }
115
116 uint64_t operator[](IdxT idx)
117 {
118 assert( idx >= 0 && idx < vcacheSize );
119
120 if ( !cache[idx].valid ) {
121 throw InvalidIndexException( idx );
122 }
123
124 return cache[idx].value;
125 }
126
127 int conflicts(){ return evictions; }
128
129 private:
130 unsigned vcacheSize;
131 VCEntry *cache;
132 int evictions;
133};
134
135#endif /* INCLUDED_VALUECACHE_H */
136