| 1 | /* |
| 2 | * ========== Copyright Header Begin ========================================== |
| 3 | * |
| 4 | * OpenSPARC T2 Processor File: rz3utils.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 | /* rz3utils.h |
| 24 | * utility programs for rstzip3 |
| 25 | * |
| 26 | * Copyright (C) 2003 Sun Microsystems, Inc. |
| 27 | * All Rights Reserved |
| 28 | */ |
| 29 | |
| 30 | #ifndef _rz3utils_h_ |
| 31 | #define _rz3utils_h_ |
| 32 | |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <assert.h> |
| 36 | |
| 37 | |
| 38 | #include "rz3iu.h" |
| 39 | #include "rz3_bitarray.h" |
| 40 | #include "rz3_valuecache.h" |
| 41 | |
| 42 | |
| 43 | static int rz3_nbits(uint64_t v) { |
| 44 | int rv = 1; |
| 45 | while(v >> rv) { |
| 46 | rv++; |
| 47 | } |
| 48 | return rv; |
| 49 | } // rz3_nbits() |
| 50 | |
| 51 | |
| 52 | template <class vtype> struct rz3_tagged_table { |
| 53 | |
| 54 | rz3_tagged_table(int size) { |
| 55 | sz = size; |
| 56 | tags = new uint64_t [sz]; |
| 57 | data= new vtype [sz]; |
| 58 | |
| 59 | clear(); |
| 60 | } |
| 61 | |
| 62 | ~rz3_tagged_table() { |
| 63 | delete [] tags; tags = NULL; |
| 64 | delete [] data; data = NULL; |
| 65 | } |
| 66 | |
| 67 | void clear() { |
| 68 | bzero(tags, sz*sizeof(uint64_t)); |
| 69 | bzero(data, sz*sizeof(vtype)); |
| 70 | } |
| 71 | |
| 72 | void set(uint64_t key, const vtype value) { |
| 73 | int idx = (key & (sz-1)); |
| 74 | tags[idx] = key; |
| 75 | data[idx] = value; |
| 76 | } |
| 77 | |
| 78 | vtype get(uint64_t key) { |
| 79 | int idx = key & (sz-1); |
| 80 | if (tags[idx] == key) { |
| 81 | return data[idx]; |
| 82 | } else { |
| 83 | return 0; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | int sz; |
| 88 | uint64_t *tags; |
| 89 | vtype *data; |
| 90 | }; // template <class vtype> struct rz3_tagged_table |
| 91 | |
| 92 | |
| 93 | template <class vtype> struct rz3_table { |
| 94 | |
| 95 | rz3_table(int size) { |
| 96 | sz = size; |
| 97 | data= new vtype [sz]; |
| 98 | |
| 99 | clear(); |
| 100 | } |
| 101 | |
| 102 | ~rz3_table() { |
| 103 | delete [] data; data = NULL; |
| 104 | } |
| 105 | |
| 106 | void clear() { |
| 107 | bzero(data, sz*sizeof(vtype)); |
| 108 | } |
| 109 | |
| 110 | void set(uint64_t key, const vtype value) { |
| 111 | int idx = (key & (sz-1)); |
| 112 | data[idx] = value; |
| 113 | } |
| 114 | |
| 115 | vtype get(uint64_t key) { |
| 116 | int idx = key & (sz-1); |
| 117 | return data[idx]; |
| 118 | } |
| 119 | |
| 120 | int sz; |
| 121 | vtype *data; |
| 122 | }; // template <class vtype> struct rz3_table |
| 123 | |
| 124 | |
| 125 | |
| 126 | // proximity list - used in rstzip3 to exploit temporal-spatial locality |
| 127 | // to compress ea_va and regval/memval values |
| 128 | struct rz3_prox_elem { |
| 129 | int valid; |
| 130 | uint64_t data; |
| 131 | rz3_prox_elem * next; |
| 132 | rz3_prox_elem * prev; |
| 133 | }; // struct rz3_prox_elem |
| 134 | |
| 135 | |
| 136 | struct rz3_prox_list { |
| 137 | rz3_prox_list(int size) { |
| 138 | sz = size; |
| 139 | arr = new rz3_prox_elem[sz]; |
| 140 | |
| 141 | clear(); |
| 142 | } // rz3_prox_list::() |
| 143 | |
| 144 | |
| 145 | void clear() { |
| 146 | int i; |
| 147 | for (i=0; i<sz; i++) { |
| 148 | |
| 149 | if (i == 0) { |
| 150 | arr[i].prev = NULL; |
| 151 | } else { |
| 152 | arr[i].prev = &(arr[i-1]); |
| 153 | } |
| 154 | |
| 155 | if (i == (sz-1)) { |
| 156 | arr[i].next = NULL; |
| 157 | } else { |
| 158 | arr[i].next = &(arr[i+1]); |
| 159 | } |
| 160 | |
| 161 | arr[i].data = 0x0; |
| 162 | } // for each allocated elem |
| 163 | |
| 164 | } // clear(); |
| 165 | |
| 166 | uint64_t lookup(int idx) { |
| 167 | int i; |
| 168 | rz3_prox_elem *pe = arr; |
| 169 | for (i=0; i<idx; i++) { |
| 170 | pe = pe->next; |
| 171 | } |
| 172 | return pe->data; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | // returns the index if match, -1 otherwise |
| 177 | // if v is found in the prox list, it is moved |
| 178 | // to the head of the list ("most recent" position) |
| 179 | int ref(uint64_t v) { |
| 180 | int i; |
| 181 | rz3_prox_elem * pe = arr; |
| 182 | assert(pe->prev == NULL); |
| 183 | for (i=0; i<sz; i++) { |
| 184 | if (pe->data == v) break; |
| 185 | if (pe->next) { |
| 186 | pe = pe->next; |
| 187 | } else { |
| 188 | assert(i==(sz-1)); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if (i==0) { |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | if (i < sz) { // hit |
| 197 | pe->prev->next = pe->next; |
| 198 | |
| 199 | if (pe->next) { |
| 200 | pe->next->prev = pe->prev; |
| 201 | } |
| 202 | |
| 203 | pe->prev = NULL; |
| 204 | pe->next = arr; |
| 205 | arr->prev = pe; |
| 206 | arr = pe; |
| 207 | |
| 208 | return i; |
| 209 | } else { // miss |
| 210 | // pe is last - move it to the head |
| 211 | pe->data = v; |
| 212 | pe->prev->next = NULL; |
| 213 | pe->next = arr; |
| 214 | pe->prev = NULL; |
| 215 | arr->prev = pe; |
| 216 | arr = pe; |
| 217 | return -1; |
| 218 | } |
| 219 | } // int ref(uint64_t v) |
| 220 | |
| 221 | ~rz3_prox_list() { |
| 222 | delete [] arr; |
| 223 | } |
| 224 | |
| 225 | int sz; |
| 226 | rz3_prox_elem * arr; |
| 227 | |
| 228 | }; // struct rz3_prox_list |
| 229 | |
| 230 | |
| 231 | #endif // _rz3utils_h_ |