summaryrefslogtreecommitdiff
path: root/ext/mcpat/cacti/htree2.cc
blob: 55724c39774ac918984d5fea62dec679b779b97d (plain)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/*****************************************************************************
 *                                McPAT/CACTI
 *                      SOFTWARE LICENSE AGREEMENT
 *            Copyright 2012 Hewlett-Packard Development Company, L.P.
 *            Copyright (c) 2010-2013 Advanced Micro Devices, Inc.
 *                          All Rights Reserved
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.

 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ***************************************************************************/



#include <cassert>
#include <iostream>

#include "htree2.h"
#include "wire.h"

Htree2::Htree2(
    enum Wire_type wire_model, double mat_w, double mat_h,
    int a_bits, int d_inbits, int search_data_in, int d_outbits,
    int search_data_out, int bl, int wl, enum Htree_type htree_type,
    bool uca_tree_, bool search_tree_, TechnologyParameter::DeviceType *dt)
    : in_rise_time(0), out_rise_time(0),
      tree_type(htree_type), mat_width(mat_w), mat_height(mat_h),
      add_bits(a_bits), data_in_bits(d_inbits),
      search_data_in_bits(search_data_in), data_out_bits(d_outbits),
      search_data_out_bits(search_data_out), ndbl(bl), ndwl(wl),
      uca_tree(uca_tree_), search_tree(search_tree_), wt(wire_model),
      deviceType(dt) {
    assert(ndbl >= 2 && ndwl >= 2);

//  if (ndbl == 1 && ndwl == 1)
//  {
//    delay = 0;
//    power.readOp.dynamic = 0;
//    power.readOp.leakage = 0;
//    area.w = mat_w;
//    area.h = mat_h;
//    return;
//  }
//  if (ndwl == 1) ndwl++;
//  if (ndbl == 1) ndbl++;

    max_unpipelined_link_delay = 0; //TODO
    min_w_nmos = g_tp.min_w_nmos_;
    min_w_pmos = deviceType->n_to_p_eff_curr_drv_ratio * min_w_nmos;

    switch (htree_type) {
    case Add_htree:
        wire_bw = init_wire_bw = add_bits;
        in_htree();
        break;
    case Data_in_htree:
        wire_bw = init_wire_bw = data_in_bits;
        in_htree();
        break;
    case Data_out_htree:
        wire_bw = init_wire_bw = data_out_bits;
        out_htree();
        break;
    case Search_in_htree:
        wire_bw = init_wire_bw = search_data_in_bits;//in_search_tree is broad cast, out_htree is not.
        in_htree();
        break;
    case Search_out_htree:
        wire_bw = init_wire_bw = search_data_out_bits;
        out_htree();
        break;
    default:
        assert(0);
        break;
    }

    power_bit = power;
    power.readOp.dynamic *= init_wire_bw;

    assert(power.readOp.dynamic >= 0);
    assert(power.readOp.leakage >= 0);
}



// nand gate sizing calculation
void Htree2::input_nand(double s1, double s2, double l_eff) {
    Wire w1(wt, l_eff);
    double pton_size = deviceType->n_to_p_eff_curr_drv_ratio;
    // input capacitance of a repeater  = input capacitance of nand.
    double nsize = s1 * (1 + pton_size) / (2 + pton_size);
    nsize = (nsize < 1) ? 1 : nsize;

    double tc = 2 * tr_R_on(nsize * min_w_nmos, NCH, 1) *
        (drain_C_(nsize * min_w_nmos, NCH, 1, 1, g_tp.cell_h_def) * 2 +
         2 * gate_C(s2 * (min_w_nmos + min_w_pmos), 0));
    delay += horowitz(w1.out_rise_time, tc,
                      deviceType->Vth / deviceType->Vdd, deviceType->Vth /
                      deviceType->Vdd, RISE);
    power.readOp.dynamic += 0.5 *
        (2 * drain_C_(pton_size * nsize * min_w_pmos, PCH, 1, 1, g_tp.cell_h_def)
         + drain_C_(nsize * min_w_nmos, NCH, 1, 1, g_tp.cell_h_def)
         + 2 * gate_C(s2 * (min_w_nmos + min_w_pmos), 0)) *
        deviceType->Vdd * deviceType->Vdd;

    power.searchOp.dynamic += 0.5 *
        (2 * drain_C_(pton_size * nsize * min_w_pmos, PCH, 1, 1, g_tp.cell_h_def)
         + drain_C_(nsize * min_w_nmos, NCH, 1, 1, g_tp.cell_h_def)
         + 2 * gate_C(s2 * (min_w_nmos + min_w_pmos), 0)) *
        deviceType->Vdd * deviceType->Vdd * wire_bw ;
    power.readOp.leakage += (wire_bw *
                             cmos_Isub_leakage(min_w_nmos * (nsize * 2),
                                               min_w_pmos * nsize * 2, 2,
                                               nand)) * deviceType->Vdd;
    power.readOp.gate_leakage += (wire_bw *
                                  cmos_Ig_leakage(min_w_nmos * (nsize * 2),
                                                  min_w_pmos * nsize * 2, 2,
                                                  nand)) * deviceType->Vdd;
}



// tristate buffer model consisting of not, nand, nor, and driver transistors
void Htree2::output_buffer(double s1, double s2, double l_eff) {
    Wire w1(wt, l_eff);
    double pton_size = deviceType->n_to_p_eff_curr_drv_ratio;
    // input capacitance of repeater = input capacitance of nand + nor.
    double size = s1 * (1 + pton_size) / (2 + pton_size + 1 + 2 * pton_size);
    double s_eff =  //stage eff of a repeater in a wire
        (gate_C(s2 * (min_w_nmos + min_w_pmos), 0) + w1.wire_cap(l_eff * 1e-6,
                                                                 true)) /
        gate_C(s2 * (min_w_nmos + min_w_pmos), 0);
    double tr_size = gate_C(s1 * (min_w_nmos + min_w_pmos), 0) * 1 / 2 /
        (s_eff * gate_C(min_w_pmos, 0));
    size = (size < 1) ? 1 : size;

    double res_nor = 2 * tr_R_on(size * min_w_pmos, PCH, 1);
    double res_ptrans = tr_R_on(tr_size * min_w_nmos, NCH, 1);
    double cap_nand_out =
        drain_C_(size * min_w_nmos, NCH, 1, 1, g_tp.cell_h_def) +
        drain_C_(size * min_w_pmos, PCH, 1, 1, g_tp.cell_h_def) * 2 +
        gate_C(tr_size * min_w_pmos, 0);
    double cap_ptrans_out = 2 *
        (drain_C_(tr_size * min_w_pmos, PCH, 1, 1, g_tp.cell_h_def) +
         drain_C_(tr_size * min_w_nmos, NCH, 1, 1, g_tp.cell_h_def)) +
        gate_C(s1 * (min_w_nmos + min_w_pmos), 0);

    double tc = res_nor * cap_nand_out + (res_nor + res_ptrans) * cap_ptrans_out;


    delay += horowitz(w1.out_rise_time, tc,
                      deviceType->Vth / deviceType->Vdd, deviceType->Vth /
                      deviceType->Vdd, RISE);

    //nand
    power.readOp.dynamic += 0.5 *
        (2 * drain_C_(size * min_w_pmos, PCH, 1, 1, g_tp.cell_h_def) +
         drain_C_(size * min_w_nmos, NCH, 1, 1, g_tp.cell_h_def) +
         gate_C(tr_size * (min_w_pmos), 0)) *
        deviceType->Vdd * deviceType->Vdd;

    power.searchOp.dynamic += 0.5 *
        (2 * drain_C_(size * min_w_pmos, PCH, 1, 1, g_tp.cell_h_def) +
         drain_C_(size * min_w_nmos, NCH, 1, 1, g_tp.cell_h_def) +
         gate_C(tr_size * (min_w_pmos), 0)) *
        deviceType->Vdd * deviceType->Vdd * init_wire_bw;

    //not
    power.readOp.dynamic += 0.5 *
        (drain_C_(size * min_w_pmos, PCH, 1, 1, g_tp.cell_h_def)
         + drain_C_(size * min_w_nmos, NCH, 1, 1, g_tp.cell_h_def)
         + gate_C(size * (min_w_nmos + min_w_pmos), 0)) *
        deviceType->Vdd * deviceType->Vdd;

    power.searchOp.dynamic += 0.5 *
        (drain_C_(size * min_w_pmos, PCH, 1, 1, g_tp.cell_h_def)
         + drain_C_(size * min_w_nmos, NCH, 1, 1, g_tp.cell_h_def)
         + gate_C(size * (min_w_nmos + min_w_pmos), 0)) *
        deviceType->Vdd * deviceType->Vdd * init_wire_bw;

    //nor
    power.readOp.dynamic += 0.5 *
        (drain_C_(size * min_w_pmos, PCH, 1, 1, g_tp.cell_h_def)
         + 2 * drain_C_(size * min_w_nmos, NCH, 1, 1, g_tp.cell_h_def)
         + gate_C(tr_size * (min_w_nmos + min_w_pmos), 0)) *
        deviceType->Vdd * deviceType->Vdd;

    power.searchOp.dynamic += 0.5 *
        (drain_C_(size * min_w_pmos, PCH, 1, 1, g_tp.cell_h_def)
         + 2 * drain_C_(size * min_w_nmos, NCH, 1, 1, g_tp.cell_h_def)
         + gate_C(tr_size * (min_w_nmos + min_w_pmos), 0)) *
        deviceType->Vdd * deviceType->Vdd * init_wire_bw;

    //output transistor
    power.readOp.dynamic += 0.5 *
        ((drain_C_(tr_size * min_w_pmos, PCH, 1, 1, g_tp.cell_h_def)
          + drain_C_(tr_size * min_w_nmos, NCH, 1, 1, g_tp.cell_h_def)) * 2
         + gate_C(s1 * (min_w_nmos + min_w_pmos), 0)) *
        deviceType->Vdd * deviceType->Vdd;

    power.searchOp.dynamic += 0.5 *
        ((drain_C_(tr_size * min_w_pmos, PCH, 1, 1, g_tp.cell_h_def)
          + drain_C_(tr_size * min_w_nmos, NCH, 1, 1, g_tp.cell_h_def)) * 2
         + gate_C(s1 * (min_w_nmos + min_w_pmos), 0)) *
        deviceType->Vdd * deviceType->Vdd * init_wire_bw;

    if (uca_tree) {
        power.readOp.leakage +=
            cmos_Isub_leakage(min_w_nmos * tr_size * 2, min_w_pmos * tr_size *
                              2, 1, inv) *
            deviceType->Vdd * wire_bw;/*inverter + output tr*/
        power.readOp.leakage +=
            cmos_Isub_leakage(min_w_nmos * size * 3, min_w_pmos * size * 3, 2,
                              nand) * deviceType->Vdd * wire_bw;//nand
        power.readOp.leakage +=
            cmos_Isub_leakage(min_w_nmos * size * 3, min_w_pmos * size * 3, 2,
                              nor) * deviceType->Vdd * wire_bw;//nor

        power.readOp.gate_leakage +=
            cmos_Ig_leakage(min_w_nmos * tr_size * 2, min_w_pmos * tr_size * 2,
                            1, inv) *
            deviceType->Vdd * wire_bw;/*inverter + output tr*/
        power.readOp.gate_leakage +=
            cmos_Ig_leakage(min_w_nmos * size * 3, min_w_pmos * size * 3, 2,
                            nand) * deviceType->Vdd * wire_bw;//nand
        power.readOp.gate_leakage +=
            cmos_Ig_leakage(min_w_nmos * size * 3, min_w_pmos * size * 3, 2,
                            nor) * deviceType->Vdd * wire_bw;//nor
    } else {
        power.readOp.leakage +=
            cmos_Isub_leakage(min_w_nmos * tr_size * 2, min_w_pmos * tr_size *
                              2, 1, inv) *
            deviceType->Vdd * wire_bw;/*inverter + output tr*/
        power.readOp.leakage +=
            cmos_Isub_leakage(min_w_nmos * size * 3, min_w_pmos * size * 3, 2,
                              nand) * deviceType->Vdd * wire_bw;//nand
        power.readOp.leakage +=
            cmos_Isub_leakage(min_w_nmos * size * 3, min_w_pmos * size * 3, 2,
                              nor) * deviceType->Vdd * wire_bw;//nor

        power.readOp.gate_leakage +=
            cmos_Ig_leakage(min_w_nmos * tr_size * 2, min_w_pmos * tr_size * 2,
                            1, inv) *
            deviceType->Vdd * wire_bw;/*inverter + output tr*/
        power.readOp.gate_leakage +=
            cmos_Ig_leakage(min_w_nmos * size * 3, min_w_pmos * size * 3, 2,
                            nand) * deviceType->Vdd * wire_bw;//nand
        power.readOp.gate_leakage +=
            cmos_Ig_leakage(min_w_nmos * size * 3, min_w_pmos * size * 3, 2,
                            nor) * deviceType->Vdd * wire_bw;//nor
    }
}



/* calculates the input h-tree delay/power
 * A nand gate is used at each node to
 * limit the signal
 * The area of an unbalanced htree (rows != columns)
 * depends on how data is traversed.
 * In the following function, if ( no. of rows < no. of columns),
 * then data first traverse in excess hor. links until vertical
 * and horizontal nodes are same.
 * If no. of rows is bigger, then data traverse in
 * a hor. link followed by a ver. link in a repeated
 * fashion (similar to a balanced tree) until there are no
 * hor. links left. After this it goes through the remaining vertical
 * links.
 */
void
Htree2::in_htree() {
    //temp var
    double s1 = 0, s2 = 0, s3 = 0;
    double l_eff = 0;
    Wire *wtemp1 = 0, *wtemp2 = 0, *wtemp3 = 0;
    double len = 0, ht = 0;
    int option = 0;

    int h = (int) _log2(ndwl / 2); // horizontal nodes
    int v = (int) _log2(ndbl / 2); // vertical nodes
    double len_temp;
    double ht_temp;
    if (uca_tree) {
        //Sheng: this computation do not consider the wires that route from
        //edge to middle.
        ht_temp = (mat_height * ndbl / 2 +
                   /* since uca_tree models interbank tree,
                      mat_height => bank height */
                   ((add_bits + data_in_bits + data_out_bits +
                     (search_data_in_bits + search_data_out_bits)) *
                    g_tp.wire_outside_mat.pitch *
                    2 * (1 - pow(0.5, h)))) / 2;
        len_temp = (mat_width * ndwl / 2 +
                    ((add_bits + data_in_bits + data_out_bits +
                      (search_data_in_bits + search_data_out_bits)) *
                     g_tp.wire_outside_mat.pitch *
                     2 * (1 - pow(0.5, v)))) / 2;
    } else {
        if (ndwl == ndbl) {
            ht_temp = ((mat_height * ndbl / 2) +
                       ((add_bits + (search_data_in_bits +
                                     search_data_out_bits)) * (ndbl / 2 - 1) *
                        g_tp.wire_outside_mat.pitch) +
                       ((data_in_bits + data_out_bits) *
                        g_tp.wire_outside_mat.pitch * h)
                      ) / 2;
            len_temp = (mat_width * ndwl / 2 +
                        ((add_bits + (search_data_in_bits +
                                      search_data_out_bits)) * (ndwl / 2 - 1) *
                         g_tp.wire_outside_mat.pitch) +
                        ((data_in_bits + data_out_bits) *
                         g_tp.wire_outside_mat.pitch * v)) / 2;
        } else if (ndwl > ndbl) {
            double excess_part = (_log2(ndwl / 2) - _log2(ndbl / 2));
            ht_temp = ((mat_height * ndbl / 2) +
                       ((add_bits + + (search_data_in_bits +
                                       search_data_out_bits)) *
                        ((ndbl / 2 - 1) + excess_part) *
                        g_tp.wire_outside_mat.pitch) +
                       (data_in_bits + data_out_bits) *
                       g_tp.wire_outside_mat.pitch *
                       (2 * (1 - pow(0.5, h - v)) + pow(0.5, v - h) * v)) / 2;
            len_temp = (mat_width * ndwl / 2 +
                        ((add_bits +
                          (search_data_in_bits + search_data_out_bits)) *
                         (ndwl / 2 - 1) * g_tp.wire_outside_mat.pitch) +
                        ((data_in_bits + data_out_bits) *
                         g_tp.wire_outside_mat.pitch * v)) / 2;
        } else {
            double excess_part = (_log2(ndbl / 2) - _log2(ndwl / 2));
            ht_temp = ((mat_height * ndbl / 2) +
                       ((add_bits +
                         (search_data_in_bits + search_data_out_bits)) *
                        ((ndwl / 2 - 1) + excess_part) *
                        g_tp.wire_outside_mat.pitch) +
                       ((data_in_bits + data_out_bits) *
                        g_tp.wire_outside_mat.pitch * h)
                      ) / 2;
            len_temp = (mat_width * ndwl / 2 +
                        ((add_bits +
                          (search_data_in_bits + search_data_out_bits)) *
                         ((ndwl / 2 - 1) + excess_part) *
                         g_tp.wire_outside_mat.pitch) +
                        (data_in_bits + data_out_bits) *
                        g_tp.wire_outside_mat.pitch *
                        (h + 2 * (1 - pow(0.5, v - h)))) / 2;
        }
    }

    area.h   = ht_temp * 2;
    area.w   = len_temp * 2;
    delay = 0;
    power.readOp.dynamic = 0;
    power.readOp.leakage = 0;
    power.searchOp.dynamic = 0;
    len = len_temp;
    ht  = ht_temp / 2;

    while (v > 0 || h > 0) {
        if (wtemp1) delete wtemp1;
        if (wtemp2) delete wtemp2;
        if (wtemp3) delete wtemp3;

        if (h > v) {
            //the iteration considers only one horizontal link
            wtemp1 = new Wire(wt, len); // hor
            wtemp2 = new Wire(wt, len / 2);  // ver
            len_temp = len;
            len /= 2;
            wtemp3 = 0;
            h--;
            option = 0;
        } else if (v > 0 && h > 0) {
            //considers one horizontal link and one vertical link
            wtemp1 = new Wire(wt, len); // hor
            wtemp2 = new Wire(wt, ht);  // ver
            wtemp3 = new Wire(wt, len / 2);  // next hor
            len_temp = len;
            ht_temp = ht;
            len /= 2;
            ht  /= 2;
            v--;
            h--;
            option = 1;
        } else {
            // considers only one vertical link
            assert(h == 0);
            wtemp1 = new Wire(wt, ht); // ver
            wtemp2 = new Wire(wt, ht / 2);  // hor
            ht_temp = ht;
            ht /= 2;
            wtemp3 = 0;
            v--;
            option = 2;
        }

        delay += wtemp1->delay;
        power.readOp.dynamic += wtemp1->power.readOp.dynamic;
        power.searchOp.dynamic += wtemp1->power.readOp.dynamic * wire_bw;
        power.readOp.leakage += wtemp1->power.readOp.leakage * wire_bw;
        power.readOp.gate_leakage += wtemp1->power.readOp.gate_leakage * wire_bw;
        if ((uca_tree == false && option == 2) || search_tree == true) {
            wire_bw *= 2;  // wire bandwidth doubles only for vertical branches
        }

        if (uca_tree == false) {
            if (len_temp > wtemp1->repeater_spacing) {
                s1 = wtemp1->repeater_size;
                l_eff = wtemp1->repeater_spacing;
            } else {
                s1 = (len_temp / wtemp1->repeater_spacing) *
                    wtemp1->repeater_size;
                l_eff = len_temp;
            }

            if (ht_temp > wtemp2->repeater_spacing) {
                s2 = wtemp2->repeater_size;
            } else {
                s2 = (len_temp / wtemp2->repeater_spacing) *
                    wtemp2->repeater_size;
            }
            // first level
            input_nand(s1, s2, l_eff);
        }


        if (option != 1) {
            continue;
        }

        // second level
        delay += wtemp2->delay;
        power.readOp.dynamic += wtemp2->power.readOp.dynamic;
        power.searchOp.dynamic += wtemp2->power.readOp.dynamic * wire_bw;
        power.readOp.leakage += wtemp2->power.readOp.leakage * wire_bw;
        power.readOp.gate_leakage += wtemp2->power.readOp.gate_leakage * wire_bw;

        if (uca_tree) {
            power.readOp.leakage += (wtemp2->power.readOp.leakage * wire_bw);
            power.readOp.gate_leakage +=
                wtemp2->power.readOp.gate_leakage * wire_bw;
        } else {
            power.readOp.leakage += (wtemp2->power.readOp.leakage * wire_bw);
            power.readOp.gate_leakage +=
                wtemp2->power.readOp.gate_leakage * wire_bw;
            wire_bw *= 2;

            if (ht_temp > wtemp3->repeater_spacing) {
                s3    = wtemp3->repeater_size;
                l_eff = wtemp3->repeater_spacing;
            } else {
                s3 = (len_temp / wtemp3->repeater_spacing) *
                    wtemp3->repeater_size;
                l_eff = ht_temp;
            }

            input_nand(s2, s3, l_eff);
        }
    }

    if (wtemp1) delete wtemp1;
    if (wtemp2) delete wtemp2;
    if (wtemp3) delete wtemp3;
}



/* a tristate buffer is used to handle fan-ins
 * The area of an unbalanced htree (rows != columns)
 * depends on how data is traversed.
 * In the following function, if ( no. of rows < no. of columns),
 * then data first traverse in excess hor. links until vertical
 * and horizontal nodes are same.
 * If no. of rows is bigger, then data traverse in
 * a hor. link followed by a ver. link in a repeated
 * fashion (similar to a balanced tree) until there are no
 * hor. links left. After this it goes through the remaining vertical
 * links.
 */
void Htree2::out_htree() {
    //temp var
    double s1 = 0, s2 = 0, s3 = 0;
    double l_eff = 0;
    Wire *wtemp1 = 0, *wtemp2 = 0, *wtemp3 = 0;
    double len = 0, ht = 0;
    int option = 0;

    int h = (int) _log2(ndwl / 2);
    int v = (int) _log2(ndbl / 2);
    double len_temp;
    double ht_temp;
    if (uca_tree) {
        ht_temp = (mat_height * ndbl / 2 +
                   /* since uca_tree models interbank tree,
                      mat_height => bank height */
                   ((add_bits + data_in_bits + data_out_bits +
                     (search_data_in_bits + search_data_out_bits)) *
                    g_tp.wire_outside_mat.pitch *
                    2 * (1 - pow(0.5, h)))) / 2;
        len_temp = (mat_width * ndwl / 2 +
                    ((add_bits + data_in_bits + data_out_bits +
                      (search_data_in_bits + search_data_out_bits)) *
                     g_tp.wire_outside_mat.pitch *
                     2 * (1 - pow(0.5, v)))) / 2;
    } else {
        if (ndwl == ndbl) {
            ht_temp = ((mat_height * ndbl / 2) +
                       ((add_bits + (search_data_in_bits +
                                     search_data_out_bits)) *
                        (ndbl / 2 - 1) * g_tp.wire_outside_mat.pitch) +
                       ((data_in_bits + data_out_bits) *
                        g_tp.wire_outside_mat.pitch * h)
                ) / 2;
            len_temp = (mat_width * ndwl / 2 +
                        ((add_bits + (search_data_in_bits +
                                      search_data_out_bits)) * (ndwl / 2 - 1) *
                         g_tp.wire_outside_mat.pitch) +
                        ((data_in_bits + data_out_bits) *
                         g_tp.wire_outside_mat.pitch * v)) / 2;

        } else if (ndwl > ndbl) {
            double excess_part = (_log2(ndwl / 2) - _log2(ndbl / 2));
            ht_temp = ((mat_height * ndbl / 2) +
                       ((add_bits +
                         (search_data_in_bits + search_data_out_bits)) *
                        ((ndbl / 2 - 1) + excess_part) *
                        g_tp.wire_outside_mat.pitch) +
                       (data_in_bits + data_out_bits) *
                       g_tp.wire_outside_mat.pitch *
                       (2 * (1 - pow(0.5, h - v)) + pow(0.5, v - h) * v)) / 2;
            len_temp = (mat_width * ndwl / 2 +
                        ((add_bits +
                          (search_data_in_bits + search_data_out_bits)) *
                         (ndwl / 2 - 1) * g_tp.wire_outside_mat.pitch) +
                        ((data_in_bits + data_out_bits) *
                         g_tp.wire_outside_mat.pitch * v)) / 2;
        } else {
            double excess_part = (_log2(ndbl / 2) - _log2(ndwl / 2));
            ht_temp = ((mat_height * ndbl / 2) +
                       ((add_bits +
                         (search_data_in_bits + search_data_out_bits)) *
                        ((ndwl / 2 - 1) + excess_part) *
                        g_tp.wire_outside_mat.pitch) +
                       ((data_in_bits + data_out_bits) *
                        g_tp.wire_outside_mat.pitch * h)
                      ) / 2;
            len_temp = (mat_width * ndwl / 2 +
                        ((add_bits + (search_data_in_bits +
                                      search_data_out_bits)) *
                         ((ndwl / 2 - 1) + excess_part) *
                         g_tp.wire_outside_mat.pitch) +
                        (data_in_bits + data_out_bits) *
                        g_tp.wire_outside_mat.pitch *
                        (h + 2 * (1 - pow(0.5, v - h)))) / 2;
        }
    }
    area.h = ht_temp * 2;
    area.w = len_temp * 2;
    delay = 0;
    power.readOp.dynamic = 0;
    power.readOp.leakage = 0;
    power.readOp.gate_leakage = 0;
    //cout<<"power.readOp.gate_leakage"<<power.readOp.gate_leakage<<endl;
    len = len_temp;
    ht = ht_temp / 2;

    while (v > 0 || h > 0) { //finds delay/power of each link in the tree
        if (wtemp1) delete wtemp1;
        if (wtemp2) delete wtemp2;
        if (wtemp3) delete wtemp3;

        if (h > v) {
            //the iteration considers only one horizontal link
            wtemp1 = new Wire(wt, len); // hor
            wtemp2 = new Wire(wt, len / 2);  // ver
            len_temp = len;
            len /= 2;
            wtemp3 = 0;
            h--;
            option = 0;
        } else if (v > 0 && h > 0) {
            //considers one horizontal link and one vertical link
            wtemp1 = new Wire(wt, len); // hor
            wtemp2 = new Wire(wt, ht);  // ver
            wtemp3 = new Wire(wt, len / 2);  // next hor
            len_temp = len;
            ht_temp = ht;
            len /= 2;
            ht /= 2;
            v--;
            h--;
            option = 1;
        } else {
            // considers only one vertical link
            assert(h == 0);
            wtemp1 = new Wire(wt, ht); // hor
            wtemp2 = new Wire(wt, ht / 2);  // ver
            ht_temp = ht;
            ht /= 2;
            wtemp3 = 0;
            v--;
            option = 2;
        }
        delay += wtemp1->delay;
        power.readOp.dynamic += wtemp1->power.readOp.dynamic;
        power.searchOp.dynamic += wtemp1->power.readOp.dynamic * init_wire_bw;
        power.readOp.leakage += wtemp1->power.readOp.leakage * wire_bw;
        power.readOp.gate_leakage += wtemp1->power.readOp.gate_leakage * wire_bw;
        if ((uca_tree == false && option == 2) || search_tree == true) {
            wire_bw *= 2;
        }

        if (uca_tree == false) {
            if (len_temp > wtemp1->repeater_spacing) {
                s1 = wtemp1->repeater_size;
                l_eff = wtemp1->repeater_spacing;
            } else {
                s1 = (len_temp / wtemp1->repeater_spacing) *
                    wtemp1->repeater_size;
                l_eff = len_temp;
            }
            if (ht_temp > wtemp2->repeater_spacing) {
                s2 = wtemp2->repeater_size;
            } else {
                s2 = (len_temp / wtemp2->repeater_spacing) *
                    wtemp2->repeater_size;
            }
            // first level
            output_buffer(s1, s2, l_eff);
        }


        if (option != 1) {
            continue;
        }

        // second level
        delay += wtemp2->delay;
        power.readOp.dynamic += wtemp2->power.readOp.dynamic;
        power.searchOp.dynamic += wtemp2->power.readOp.dynamic * init_wire_bw;
        power.readOp.leakage += wtemp2->power.readOp.leakage * wire_bw;
        power.readOp.gate_leakage += wtemp2->power.readOp.gate_leakage * wire_bw;
        //cout<<"power.readOp.gate_leakage"<<power.readOp.gate_leakage<<endl;
        if (uca_tree) {
            power.readOp.leakage += (wtemp2->power.readOp.leakage * wire_bw);
            power.readOp.gate_leakage +=
                wtemp2->power.readOp.gate_leakage * wire_bw;
        } else {
            power.readOp.leakage += (wtemp2->power.readOp.leakage * wire_bw);
            power.readOp.gate_leakage +=
                wtemp2->power.readOp.gate_leakage * wire_bw;
            wire_bw *= 2;

            if (ht_temp > wtemp3->repeater_spacing) {
                s3 = wtemp3->repeater_size;
                l_eff = wtemp3->repeater_spacing;
            } else {
                s3 = (len_temp / wtemp3->repeater_spacing) *
                    wtemp3->repeater_size;
                l_eff = ht_temp;
            }

            output_buffer(s2, s3, l_eff);
        }
        //cout<<"power.readOp.leakage"<<power.readOp.leakage<<endl;
        //cout<<"power.readOp.gate_leakage"<<power.readOp.gate_leakage<<endl;
        //cout<<"wtemp2->power.readOp.gate_leakage"<<wtemp2->power.readOp.gate_leakage<<endl;
    }

    if (wtemp1) delete wtemp1;
    if (wtemp2) delete wtemp2;
    if (wtemp3) delete wtemp3;
}