File size: 3,892 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
#include <ot/timer/cppr.hpp>
#include <ot/timer/timer.hpp>

namespace ot {

// Constructor
CpprCache::CpprCache(size_t N) {
  resize_to_fit(N, __capp);
  // debug
  //for(const auto& i : __capp) assert(!i);
}

// Move constructor
CpprCache::CpprCache(CpprCache&& rhs) : 
  _capb {rhs._capb},
  _cape {rhs._cape},
  _pins {std::move(rhs._pins)} {
}

// Destructor
CpprCache::~CpprCache() {
  for(auto p : _pins) {
    __capp[p].reset();
  }
}

// ------------------------------------------------------------------------------------------------

// Function: _cppr_cache
// Obtain a CPPR cache for a given test.
CpprCache Timer::_cppr_cache(const Test& test, Split el, Tran rf) const {

  // Create a cppr handle.
  auto cppr = CpprCache(_idx2pin.size() << 1);

  // Find the timing
  auto tv  = test._arc.timing_view();
  assert(tv[el]);

  // Find the capture path
  auto v   = &(test._arc._from);
  auto vel = (el == MIN) ? MAX : MIN;
  auto vrf = tv[el]->is_rising_edge_triggered() ? RISE : FALL;

  cppr._cape = _encode_pin(*v, vrf);

  while(v && v->_at[vel][vrf]) {

    auto vid = _encode_pin(*v, vrf);    
    cppr._pins.insert(vid);

    if(auto arc = v->_at[vel][vrf]->pi_arc; arc) {
      // Cacth the data to local to avoid messing up swap.
      auto u = &(arc->_from);
      auto uel = v->_at[vel][vrf]->pi_el;
      auto urf = v->_at[vel][vrf]->pi_rf;

      // Record the path parent.
      cppr.__capp[vid] = _encode_pin(*u, urf);
      
      // Move the pointer
      vel = uel;
      vrf = urf;
      v   = u;
    }
    else {
      cppr._capb = vid;
      cppr.__capp[vid] = vid;
      break;
    }
  }

  return cppr;
}

// Function: _cppr_credit
std::optional<float> Timer::_cppr_credit(const Test& test, Split el, Tran rf) const {

  assert(_cppr_analysis);

  // Create a suffix tree
  auto sfxt = _sfxt_cache(test, el, rf);

  // compute the cppr credit
  if(sfxt.slack()) {
    auto tat = *test._arc._to._at[el][rf];
    auto rat = (el == MIN) ? tat - *sfxt.slack() : *sfxt.slack() + tat;
    return rat - *test._rat[el][rf];
  }
  else {
    return std::nullopt;
  }
}

// Procedure: _cppr_credit
std::optional<float> Timer::_cppr_credit(const CpprCache& cppr, Pin& pin, Split el, Tran rf) const {

  assert(_cppr_analysis);

  // back-trace to find the common point. 
  auto v = &pin;
  auto vel = el;
  auto vrf = rf;

  while(v && v->_at[vel][vrf]) {

    auto vid = _encode_pin(*v, vrf);    
    
    // Find a converging point.
    if(cppr.__capp[vid]) {
      
      assert(vel == el);

      auto dv = v->_delta_at(MAX, vrf, MIN, vrf);

      // Return the credit for the early (hold) test.  
      if(el == MIN) {
        return dv;
      }
      // Return the credit for the late (setup) test.
      else {
        auto [r, rrf] = _decode_pin(cppr._capb);
        auto dr = r->_delta_at(MAX, rrf, MIN, rrf);
        if(dv && dr) {
          return *dv - *dr;
        }
        else {
          return std::nullopt;
        }
      }
    }
    
    // Go up to the parent.
    if(auto arc = v->_at[vel][vrf]->pi_arc; arc) {
      // Cacahe the local data to avoid swap error.
      auto u = &(arc->_from);
      auto uel = v->_at[vel][vrf]->pi_el;
      auto urf = v->_at[vel][vrf]->pi_rf;
      // Move the pointer
      vel = uel;
      vrf = urf;
      v   = u;
    }
    else break;
  }

  return std::nullopt;
}

// Function: _cppr_offset
std::optional<float> Timer::_cppr_offset(const CpprCache& cppr, Pin& pin, Split el, Tran rf) const {

  assert(_cppr_analysis);

  if(auto at = pin._at[el][rf]; !at) {
    return std::nullopt;
  }
  else {
    if(auto credit = _cppr_credit(cppr, pin, el, rf); credit) {
      return (el == MIN) ? *at + *credit : -(*at) + *credit; 
    }
    else {
      return (el == MIN) ? *at : -(*at);
    }
  }
}


};  // end of namespace ot. -----------------------------------------------------------------------