File size: 7,256 Bytes
d1be154 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | #include <ot/liberty/lut.hpp>
namespace ot {
// Function: is_time_lut_var
bool is_time_lut_var(LutVar v) {
switch(v) {
case LutVar::INPUT_NET_TRANSITION:
case LutVar::CONSTRAINED_PIN_TRANSITION:
case LutVar::RELATED_PIN_TRANSITION:
case LutVar::INPUT_TRANSITION_TIME:
return true;
break;
default:
return false;
break;
}
}
// Function: is_capacitance_lut_var
bool is_capacitance_lut_var(LutVar v) {
switch(v) {
case LutVar::TOTAL_OUTPUT_NET_CAPACITANCE:
return true;
break;
default:
return false;
break;
}
}
// Function: to_string
std::string to_string(LutVar v) {
switch(v) {
case LutVar::TOTAL_OUTPUT_NET_CAPACITANCE:
return "total_output_net_capacitance";
break;
case LutVar::INPUT_NET_TRANSITION:
return "input_net_transition";
break;
case LutVar::CONSTRAINED_PIN_TRANSITION:
return "constrained_pin_transition";
break;
case LutVar::RELATED_PIN_TRANSITION:
return "related_pin_transition";
break;
case LutVar::INPUT_TRANSITION_TIME:
return "input_transition_time";
break;
default:
return "undefined";
break;
}
}
// ------------------------------------------------------------------------------------------------
// Operator: <<
std::ostream& operator << (std::ostream& os, const LutTemplate& lut) {
// Write the lut template name.
os << "lu_table_template (" << lut.name << ") {\n";
// Write variables.
if(lut.variable1) {
os << " variable_1: " << to_string(*(lut.variable1)) << ";\n";
}
if(lut.variable2) {
os << " variable_2: " << to_string(*(lut.variable2)) << ";\n";
}
// Write indices.
if(!lut.indices1.empty()) {
os << " index_1 (\"";
for(size_t i=0; i<lut.indices1.size(); i++) {
if(i) {
os << ", ";
}
os << lut.indices1[i];
}
os << "\");\n";
}
if(!lut.indices2.empty()) {
os << " index_2 (\"";
for(size_t i=0; i<lut.indices2.size(); i++) {
if(i) {
os << ", ";
}
os << lut.indices2[i];
}
os << "\");\n";
}
// Write the lut template ending group symbol.
os <<"}\n";
return os;
}
// ------------------------------------------------------------------------------------------------
// Function: scale_time
void Lut::scale_time(float s) {
if(lut_template) {
if(auto v1 = lut_template->variable1; v1 && is_time_lut_var(*v1)) {
for(auto& v : indices1) {
v *= s;
}
}
if(auto v2 = lut_template->variable2; v2 && is_time_lut_var(*v2)) {
for(auto& v : indices2) {
v *= s;
}
}
}
// scale the table
for(auto& v : table) {
v *= s;
}
}
// Function: scale_capacitance
void Lut::scale_capacitance(float s) {
if(lut_template) {
if(auto v1 = lut_template->variable1; v1 && is_capacitance_lut_var(*v1)) {
for(auto& v : indices1) {
v *= s;
}
}
if(auto v2 = lut_template->variable2; v2 && is_capacitance_lut_var(*v2)) {
for(auto& v : indices2) {
v *= s;
}
}
}
}
// Function: is_scalar
bool Lut::is_scalar() const {
return indices1.size() == 1 && indices2.size() == 1;
}
// Function: empty
inline bool Lut::empty() const {
return indices1.size() == 0 && indices2.size() == 0;
}
// Function: lut
// Performs the linear inter/extra polation between a segment (x1, x2) which satisfies the
// function f(x1) = y1 and f(x2) = y2. There are five cases: 1) x < x1, 2) x = x1,
// 3) x1 < x < x2, 4) x = x2, and 5) x > x2. For cases 1) and 5), extra-polation is needed.
// Cases 2) and 4) are boundary cases. Case 3) requires the inter-polation.
float Lut::operator()(float val1, float val2) const {
if(indices1.size() < 1 || indices2.size() < 1) {
OT_LOGF("invalid lut indices size");
}
// Interpolation
constexpr auto interpolate = [] (float x, float x1, float x2, float y1, float y2) {
assert(x1 < x2);
if(x >= std::numeric_limits<float>::max() || x <= std::numeric_limits<float>::lowest()) {
return x;
}
float slope = (y2 - y1) / (x2 - x1);
if(x < x1) return y1 - (x1 - x) * slope; // Extrapolation.
else if(x > x2) return y2 + (x - x2) * slope; // Extrapolation.
else if(x == x1) return y1; // Boundary case.
else if(x == x2) return y2; // Boundary case.
else return y1 + (x - x1) * slope; // Interpolation.
};
// Case 1: scalar
if(is_scalar()) return table[0];
int idx1[2], idx2[2];
idx1[1] = std::lower_bound(indices1.begin(), indices1.end(), val1) - indices1.begin();
idx2[1] = std::lower_bound(indices2.begin(), indices2.end(), val2) - indices2.begin();
// Case 2: linear inter/extra polation.
idx1[1] = std::max(1, std::min(idx1[1], (int)(indices1.size() - 1)));
idx2[1] = std::max(1, std::min(idx2[1], (int)(indices2.size() - 1)));
idx1[0] = idx1[1] - 1;
idx2[0] = idx2[1] - 1;
//printf("Perform the linear interpolation on val1=%.5f (%d %d) and val2=%.5f (%d %d)\n",
// val1, idx1[0], idx1[1], val2, idx2[0], idx2[1]);
// 1xN array (N>=2)
if(indices1.size() == 1) {
return interpolate(
val2,
indices2[idx2[0]],
indices2[idx2[1]],
table[idx2[0]],
table[idx2[1]]
);
}
// Nx1 array (N>=2)
else if(indices2.size() == 1) {
return interpolate(
val1,
indices1[idx1[0]],
indices1[idx1[1]],
table[idx1[0]*indices2.size()],
table[idx1[1]*indices2.size()]
);
}
// NxN array (N>=2)
else {
float numeric[2];
numeric[0] = interpolate(
val1,
indices1[idx1[0]],
indices1[idx1[1]],
table[idx1[0]*indices2.size() + idx2[0]],
table[idx1[1]*indices2.size() + idx2[0]]
);
numeric[1] = interpolate(
val1,
indices1[idx1[0]],
indices1[idx1[1]],
table[idx1[0]*indices2.size() + idx2[1]],
table[idx1[1]*indices2.size() + idx2[1]]
);
return interpolate(val2, indices2[idx2[0]], indices2[idx2[1]], numeric[0], numeric[1]);
}
}
// operator
std::ostream& operator << (std::ostream& os, const Lut& lut) {
// Write the indices1.
if(!lut.indices1.empty()) {
os << " index_1 (\"";
for(size_t i=0; i<lut.indices1.size(); ++i) {
if(i) {
os << ", ";
}
os << lut.indices1[i];
}
os << "\");\n";
}
// Write the indices2.
if(!lut.indices2.empty()) {
os << " index_2 (\"";
for(size_t i=0; i<lut.indices2.size(); ++i) {
if(i) {
os << ", ";
}
os << lut.indices2[i];
}
os << "\");\n";
}
// Write the values.
if(!lut.table.empty()) {
os << " values (\n";
for(size_t i=0; i<lut.indices1.size(); ++i) {
os << " \"";
for(size_t j=0; j<lut.indices2.size(); ++j) {
if(j) {
os << ", ";
}
os << lut.table[i*lut.indices2.size()+j];
}
os << "\",\n";
}
os << " );\n";
}
return os;
}
}; // end of namespace ot ------------------------------------------------------------------------
|