-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1342 lines (1247 loc) · 61.8 KB
/
main.cpp
File metadata and controls
1342 lines (1247 loc) · 61.8 KB
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// main.cpp
// IR/Distance/Resistance feature extractor (standalone)
// - Presets: sky130 / asap7 / nangate45 / custom
// - Layer name normalization: m1 / M1 / met1 → met1
// - gzip(.sp.gz) & plain(.sp)
// - Shared payload to preserve cross-layer current/IR coupling
// - Optional auto-orientation (--auto-ori), stats/CSV dump
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#include <filesystem>
#include <boost/multi_array.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/device/file.hpp>
using namespace std;
#define VERTICAL 1
#define HORIZONTAL 2
// ---------------- small utils ----------------
struct coordinate { int x{0}; int y{0}; };
static inline bool operator==(const coordinate& a, const coordinate& b){ return a.x==b.x && a.y==b.y; }
template<class T> static inline T clampT(T v, T lo, T hi){ return v<lo?lo:(v>hi?hi:v); }
static inline int rint_div(float v, float gap){ return (int)std::floor(v/gap + 0.5f); }
static inline string tolower_copy(string s){ for(char& c:s) c=(char)std::tolower((unsigned char)c); return s; }
static inline bool starts_with(const string& s, const string& p){ return s.rfind(p,0)==0; }
static inline bool ends_with(const string& s, const string& suf){ return s.size()>=suf.size() && s.compare(s.size()-suf.size(), suf.size(), suf)==0; }
static inline string norm_layer(string s){
s = tolower_copy(std::move(s));
if (starts_with(s, "met")) return s;
if (!s.empty() && s[0]=='m') return "met"+s.substr(1);
return s;
}
static inline int layer_num(const string& s_norm){
// s_norm like "met1"
if (starts_with(s_norm, "met")){
try { return stoi(s_norm.substr(3)); } catch(...) { return INT_MAX/4; }
}
return INT_MAX/3;
}
static vector<string_view> MultiSplitStringV2(string_view input, string_view delims){
vector<string_view> out;
const char* b = input.data();
const char* p = b;
const char* e = b + input.size();
for(; p!=e; ++p){
bool hit=false;
for(char d: delims){ if (*p==d){ hit=true; break; } }
if (hit){ if (p>b) out.emplace_back(b, p-b); b=p+1; }
}
if (p>b) out.emplace_back(b, p-b);
return out;
}
struct wire {
coordinate c1{}, c2{};
float resistance{0.f};
float resistance_per_unit{0.f};
};
struct h_wire { int h{0}, w1{0}, w2{0}; };
struct v_wire { int w{0}, h1{0}, h2{0}; };
struct NodePayload {
std::vector<float> ir_drop; // size = 2*M - 1
float current_source{0.f};
explicit NodePayload(int ch=0) : ir_drop(ch, 0.f) {}
};
struct node {
coordinate c{};
int R1{0}, R2{0};
float current{0.f};
float resistance{0.f};
std::shared_ptr<NodePayload> payload; // shared among copies
node() = default;
explicit node(int ch) : payload(std::make_shared<NodePayload>(ch)) {}
};
static inline bool in_range(const coordinate& c, const wire& w){
return w.c1.x<=c.x && c.x<=w.c2.x && w.c1.y<=c.y && c.y<=w.c2.y;
}
static void sort_node_vertical(vector<node>& v){
sort(v.begin(), v.end(), [](auto& a, auto& b){
if (a.c.y != b.c.y) return a.c.y < b.c.y;
if (a.c.x != b.c.x) return a.c.x < b.c.x;
return false;
});
}
static void sort_node_horizontal(vector<node>& v){
sort(v.begin(), v.end(), [](auto& a, auto& b){
if (a.c.x != b.c.x) return a.c.x < b.c.x;
if (a.c.y != b.c.y) return a.c.y < b.c.y;
return false;
});
}
static void sort_wire_vertical(vector<wire>& v){
sort(v.begin(), v.end(), [](auto& a, auto& b){
if (a.c1.y != b.c1.y) return a.c1.y < b.c1.y;
if (a.c1.x != b.c1.x) return a.c1.x < b.c1.x;
return false;
});
}
static void sort_wire_horizontal(vector<wire>& v){
sort(v.begin(), v.end(), [](auto& a, auto& b){
if (a.c1.x != b.c1.x) return a.c1.x < b.c1.x;
if (a.c1.y != b.c1.y) return a.c1.y < b.c1.y;
return false;
});
}
// ---------------- circuit ----------------
struct local_partition {
bool v1{false}, v2{false};
float resistance_per_unit{0.f};
vector<node> nodes;
};
class Circuit {
public:
std::map<string,int> metal_idx; // "met1" -> 0
std::map<string,int> via_idx; // "met1met4" -> 0 (between consecutive)
vector<int> orientation; // per metal
vector<vector<node>> Vmap_raw, Imap_raw;
vector<vector<wire>> R_raw, Via_raw;
vector<vector<node>> current_map, voltage_map;
vector<vector<wire>> wire_map;
vector<vector<local_partition>> part;
int x_max=0, y_max=0;
int H=0, W=0;
int IR_CH=0; // 2*M - 1
void set_orientation(const vector<string>& metals, const vector<int>& ori){
if (metals.empty() || metals.size()!=ori.size())
throw runtime_error("invalid metals/orientation");
metal_idx.clear(); via_idx.clear(); orientation.clear();
for (int i=0;i<(int)metals.size();++i){
string k = norm_layer(metals[i]);
if (metal_idx.count(k)) throw runtime_error("duplicate metal: "+k);
metal_idx[k]=i;
orientation.push_back(ori[i]);
}
for (int i=0;i<(int)metals.size()-1;++i){
string k = norm_layer(metals[i]) + norm_layer(metals[i+1]);
via_idx[k]=i;
}
IR_CH = 2*(int)metals.size()-1;
}
void set_size(int h, int w){ if (h<=0||w<=0) throw runtime_error("invalid H/W"); H=h; W=w; }
void read_data(const string& path){
R_raw.assign(metal_idx.size(), {});
Via_raw.assign(via_idx.size(), {});
Imap_raw.assign(metal_idx.size(), {});
Vmap_raw.assign(metal_idx.size(), {});
namespace io = boost::iostreams;
auto handle_line = [&](const string& line){
auto toks = MultiSplitStringV2(line, " _");
if (toks.empty()) return;
char head = toks[0].empty()?'\0':toks[0][0];
if (head=='R'){
if (toks.size()<10) return;
wire w;
w.c1.x = atoi(string(toks[3]).c_str());
w.c1.y = atoi(string(toks[4]).c_str());
w.c2.x = atoi(string(toks[7]).c_str());
w.c2.y = atoi(string(toks[8]).c_str());
w.resistance = (float)atof(string(toks[9]).c_str());
string m1 = norm_layer(string(toks[2]));
string m2 = norm_layer(string(toks[6]));
if (m1!=m2){
if (stoi(m1.substr(3)) > stoi(m2.substr(3))) std::swap(w.c1,w.c2);
auto it = via_idx.find(m1+m2);
if (it!=via_idx.end()) Via_raw[it->second].push_back(w);
}else if (w.c1.x != w.c2.x){
if (w.c1.x > w.c2.x) std::swap(w.c1,w.c2);
auto it = metal_idx.find(m1); if (it!=metal_idx.end()) R_raw[it->second].push_back(w);
}else if (w.c1.y != w.c2.y){
if (w.c1.y > w.c2.y) std::swap(w.c1,w.c2);
auto it = metal_idx.find(m1); if (it!=metal_idx.end()) R_raw[it->second].push_back(w);
}
x_max = max(x_max, w.c2.x);
y_max = max(y_max, w.c2.y);
}else if (head=='I'){
if (toks.size()<7) return;
node n(IR_CH);
n.c.x = atoi(string(toks[3]).c_str());
n.c.y = atoi(string(toks[4]).c_str());
n.payload->current_source = (float)atof(string(toks[6]).c_str());
string m1 = norm_layer(string(toks[2]));
auto it = metal_idx.find(m1);
if (it!=metal_idx.end()) Imap_raw[it->second].push_back(std::move(n));
}else if (head=='V'){
if (toks.size()<5) return;
node n(IR_CH);
n.c.x = atoi(string(toks[3]).c_str());
n.c.y = atoi(string(toks[4]).c_str());
string m1 = norm_layer(string(toks[2]));
auto it = metal_idx.find(m1);
if (it!=metal_idx.end()) Vmap_raw[it->second].push_back(std::move(n));
}
};
if (ends_with(path, ".gz")){
io::filtering_istream in;
in.push(io::gzip_decompressor());
in.push(io::file_source(path));
if (!in.good()) throw runtime_error("open gz failed: "+path);
string line; size_t cnt=0;
while (getline(in,line)){ if(!line.empty()) handle_line(line); ++cnt; }
if (cnt==0) throw runtime_error("empty gz file");
}else{
ifstream in(path);
if (!in) throw runtime_error("open failed: "+path);
string line; size_t cnt=0;
while (getline(in,line)){ if(!line.empty()) handle_line(line); ++cnt; }
if (cnt==0) throw runtime_error("empty file");
}
if (x_max<=0 || y_max<=0) throw runtime_error("parsed bounds are zero; check file");
}
void merge_wires(){
wire_map.assign(R_raw.size(), {});
for (int m=0; m<(int)metal_idx.size(); ++m){
auto& r = R_raw[m];
if (r.empty()){ wire_map[m].clear(); continue; }
if (orientation[m]==VERTICAL) sort_wire_vertical(r);
else sort_wire_horizontal(r);
vector<wire> nw;
wire w=r[0];
for (int i=1; i<(int)r.size(); ++i){
if (w.c2==r[i].c1){
w.c2=r[i].c2; w.resistance+=r[i].resistance;
}else{
float len = (float)(w.c2.x-w.c1.x + w.c2.y-w.c1.y);
w.resistance_per_unit = len>0? (w.resistance/len) : 0.f;
nw.push_back(w); w=r[i];
}
}
{ float len = (float)(w.c2.x-w.c1.x + w.c2.y-w.c1.y);
w.resistance_per_unit = len>0? (w.resistance/len) : 0.f;
nw.push_back(w); }
wire_map[m] = std::move(nw);
}
}
void merge_nodes(){
current_map.assign(metal_idx.size(), {});
for (int m=0;m<(int)metal_idx.size();++m)
for (auto n: Imap_raw[m]) current_map[m].push_back(std::move(n));
// vias become nodes placed to (vi+1)-th metal (original rule)
for (int vi=0; vi<(int)via_idx.size(); ++vi){
for (auto& w: Via_raw[vi]){
node n(IR_CH);
n.c = (vi%2==0)? w.c2 : w.c1;
n.resistance = w.resistance;
int target_metal = vi+1;
if (0<=target_metal && target_metal<(int)metal_idx.size())
current_map[target_metal].push_back(std::move(n));
}
}
voltage_map = Vmap_raw;
for (int m=0; m<(int)metal_idx.size(); ++m){
if (orientation[m]==VERTICAL) sort_node_vertical(current_map[m]);
else sort_node_horizontal(current_map[m]);
}
}
void build_partitions(){
part.assign(metal_idx.size(), {});
if (wire_map.empty()) return;
for (int m=(int)metal_idx.size()-1; m>=0; --m){
auto& wires = wire_map[m];
auto& cnodes = current_map[m];
bool isV=false; int ci=0, vi=0;
if (orientation[m]==VERTICAL){
sort_node_vertical(voltage_map[m]);
auto& vnodes = voltage_map[m];
for (auto& w: wires){
while (true){
if ((int)vnodes.size()==vi && (int)cnodes.size()==ci) break;
node n;
if ((int)vnodes.size()==vi){ n=cnodes[ci]; isV=false; }
else if ((int)cnodes.size()==ci){ n=vnodes[vi]; isV=true; }
else if (vnodes[vi].c.y < cnodes[ci].c.y){ n=vnodes[vi]; isV=true; }
else if (vnodes[vi].c.y > cnodes[ci].c.y){ n=cnodes[ci]; isV=false; }
else { if (vnodes[vi].c.x < cnodes[ci].c.x){ n=vnodes[vi]; isV=true; } else { n=cnodes[ci]; isV=false; } }
if (!in_range(n.c, w)){
if (n.c.y < w.c1.y || (n.c.y==w.c1.y && n.c.x < w.c1.x)){ if (isV) ++vi; else ++ci; continue; }
else break;
}
local_partition p; p.resistance_per_unit=w.resistance_per_unit; p.v1=isV;
while (true){
p.nodes.push_back(n); p.v2=isV;
if (isV) ++vi; else ++ci;
bool vend=((int)vnodes.size()==vi), cend=((int)cnodes.size()==ci);
if (vend && cend) break;
if (vend){ n=cnodes[ci]; isV=false; }
else if (cend){ n=vnodes[vi]; isV=true; }
else if (vnodes[vi].c.y < cnodes[ci].c.y){ n=vnodes[vi]; isV=true; }
else if (vnodes[vi].c.y > cnodes[ci].c.y){ n=cnodes[ci]; isV=false; }
else { if (vnodes[vi].c.x < cnodes[ci].c.x){ n=vnodes[vi]; isV=true; } else { n=cnodes[ci]; isV=false; } }
if (in_range(n.c,w)){ if (isV){ p.nodes.push_back(n); p.v2=isV; break; } }
else break;
}
if (p.nodes.size()>1 && (p.v1||p.v2)){
part[m].push_back(std::move(p));
if (m!=0){
auto& src = part[m].back().nodes;
int s = part[m].back().v1?1:0;
int e = (int)src.size() - (part[m].back().v2?1:0);
if (s<e) voltage_map[m-1].insert(voltage_map[m-1].end(), src.begin()+s, src.begin()+e);
}
}
}
}
}else{ // HORIZONTAL
sort_node_horizontal(voltage_map[m]);
auto& vnodes = voltage_map[m];
for (auto& w: wires){
while (true){
if ((int)vnodes.size()==vi && (int)cnodes.size()==ci) break;
node n;
if ((int)vnodes.size()==vi){ n=cnodes[ci]; isV=false; }
else if ((int)cnodes.size()==ci){ n=vnodes[vi]; isV=true; }
else if (vnodes[vi].c.x < cnodes[ci].c.x){ n=vnodes[vi]; isV=true; }
else if (vnodes[vi].c.x > cnodes[ci].c.x){ n=cnodes[ci]; isV=false; }
else { if (vnodes[vi].c.y < cnodes[ci].c.y){ n=vnodes[vi]; isV=true; } else { n=cnodes[ci]; isV=false; } }
if (!in_range(n.c, w)){
if (n.c.x < w.c1.x || (n.c.x==w.c1.x && n.c.y < w.c1.y)){ if (isV) ++vi; else ++ci; continue; }
else break;
}
local_partition p; p.resistance_per_unit=w.resistance_per_unit; p.v1=isV;
while (true){
p.nodes.push_back(n); p.v2=isV;
if (isV) ++vi; else ++ci;
bool vend=((int)vnodes.size()==vi), cend=((int)cnodes.size()==ci);
if (vend && cend) break;
if (vend){ n=cnodes[ci]; isV=false; }
else if (cend){ n=vnodes[vi]; isV=true; }
else if (vnodes[vi].c.x < cnodes[ci].c.x){ n=vnodes[vi]; isV=true; }
else if (vnodes[vi].c.x > cnodes[ci].c.x){ n=cnodes[ci]; isV=false; }
else { if (vnodes[vi].c.y < cnodes[ci].c.y){ n=vnodes[vi]; isV=true; } else { n=cnodes[ci]; isV=false; } }
if (in_range(n.c, w)){ if (isV){ p.nodes.push_back(n); p.v2=isV; break; } }
else break;
}
if (p.nodes.size()>1 && (p.v1||p.v2)){
part[m].push_back(std::move(p));
if (m!=0){
auto& src = part[m].back().nodes;
int s = part[m].back().v1?1:0;
int e = (int)src.size() - (part[m].back().v2?1:0);
if (s<e) voltage_map[m-1].insert(voltage_map[m-1].end(), src.begin()+s, src.begin()+e);
}
}
}
}
}
}
}
void compute_currents(){
for (int m=0;m<(int)metal_idx.size();++m){
auto& vec = part[m];
if (vec.empty()) continue;
if (orientation[m]==VERTICAL){
for (auto& p: vec){
int x1=p.nodes.front().c.x, x2=p.nodes.back().c.x;
for (int i=(int)p.v1; i<(int)p.nodes.size()-(int)p.v2; ++i){
int R1=p.nodes[i].c.x-x1, R2=x2-p.nodes[i].c.x, R=R1+R2;
p.nodes[i].R1=R1; p.nodes[i].R2=R2;
float I1=0.f, I2=0.f;
if (p.v1 && !p.v2){ I1=p.nodes[i].payload->current_source; }
else if (!p.v1 && p.v2){ I2=p.nodes[i].payload->current_source; }
else if (R>0){ I1=p.nodes[i].payload->current_source*(float)R2/(float)R;
I2=p.nodes[i].payload->current_source*(float)R1/(float)R; }
for (int j=0;j<=i;++j) p.nodes[j].current += I1;
for (int j=i+1;j<(int)p.nodes.size();++j) p.nodes[j].current -= I2;
}
if (p.v1) p.nodes.front().payload->current_source += std::abs(p.nodes.front().current);
if (p.v2) p.nodes.back(). payload->current_source += std::abs(p.nodes.back().current);
}
}else{
for (auto& p: vec){
int y1=p.nodes.front().c.y, y2=p.nodes.back().c.y;
for (int i=(int)p.v1; i<(int)p.nodes.size()-(int)p.v2; ++i){
int R1=p.nodes[i].c.y-y1, R2=y2-p.nodes[i].c.y, R=R1+R2;
p.nodes[i].R1=R1; p.nodes[i].R2=R2;
float I1=0.f, I2=0.f;
if (p.v1 && !p.v2){ I1=p.nodes[i].payload->current_source; }
else if (!p.v1 && p.v2){ I2=p.nodes[i].payload->current_source; }
else if (R>0){ I1=p.nodes[i].payload->current_source*(float)R2/(float)R;
I2=p.nodes[i].payload->current_source*(float)R1/(float)R; }
for (int j=0;j<=i;++j) p.nodes[j].current += I1;
for (int j=i+1;j<(int)p.nodes.size();++j) p.nodes[j].current -= I2;
}
if (p.v1) p.nodes.front().payload->current_source += std::abs(p.nodes.front().current);
if (p.v2) p.nodes.back(). payload->current_source += std::abs(p.nodes.back().current);
}
}
}
}
void compute_ir_drop(){
int M=(int)metal_idx.size();
for (int m=M-1;m>=0;--m){
auto& vec = part[m];
if (vec.empty()) continue;
if (orientation[m]==VERTICAL){
for (auto& p: vec){
if (p.nodes.size()<2) continue;
if (p.v1 && p.v2){
auto& a = p.nodes.front().payload->ir_drop;
auto& b = p.nodes.back(). payload->ir_drop;
for (int i=1;i<(int)p.nodes.size()-1;++i){
p.nodes[i].payload->ir_drop[m*2] =
p.nodes[i-1].payload->ir_drop[m*2] +
p.nodes[i-1].current * (float)(p.nodes[i].c.x - p.nodes[i-1].c.x) * p.resistance_per_unit;
if (m!=0)
p.nodes[i].payload->ir_drop[m*2-1] =
p.nodes[i].payload->current_source * p.nodes[i].resistance;
float denom=(float)(p.nodes[i].R1+p.nodes[i].R2);
float W = denom>0.f? (float)p.nodes[i].R1/denom : 0.f;
for (int mm=M-1; mm>m; --mm){
p.nodes[i].payload->ir_drop[mm*2] = a[mm*2] + W*(b[mm*2]-a[mm*2]);
p.nodes[i].payload->ir_drop[mm*2-1] = a[mm*2-1] + W*(b[mm*2-1]-a[mm*2-1]);
}
}
}else if (p.v1){
auto& a = p.nodes.front().payload->ir_drop;
for (int i=1;i<(int)p.nodes.size();++i){
p.nodes[i].payload->ir_drop[m*2] =
p.nodes[i-1].payload->ir_drop[m*2] +
p.nodes[i-1].current * (float)(p.nodes[i].c.x - p.nodes[i-1].c.x) * p.resistance_per_unit;
if (m!=0)
p.nodes[i].payload->ir_drop[m*2-1] =
p.nodes[i].payload->current_source * p.nodes[i].resistance;
for (int mm=M-1; mm>m; --mm){
p.nodes[i].payload->ir_drop[mm*2] = a[mm*2];
p.nodes[i].payload->ir_drop[mm*2-1] = a[mm*2-1];
}
}
}else if (p.v2){
auto& b = p.nodes.back().payload->ir_drop;
for (int i=(int)p.nodes.size()-2; i>=0; --i){
p.nodes[i].payload->ir_drop[m*2] =
p.nodes[i+1].payload->ir_drop[m*2] -
p.nodes[i+1].current * (float)(p.nodes[i+1].c.x - p.nodes[i].c.x) * p.resistance_per_unit;
if (m!=0)
p.nodes[i].payload->ir_drop[m*2-1] =
p.nodes[i].payload->current_source * p.nodes[i].resistance;
for (int mm=M-1; mm>m; --mm){
p.nodes[i].payload->ir_drop[mm*2] = b[mm*2];
p.nodes[i].payload->ir_drop[mm*2-1] = b[mm*2-1];
}
}
}
}
}else{ // HORIZONTAL
for (auto& p: vec){
if (p.nodes.size()<2) continue;
if (p.v1 && p.v2){
auto& a = p.nodes.front().payload->ir_drop;
auto& b = p.nodes.back(). payload->ir_drop;
for (int i=1;i<(int)p.nodes.size()-1;++i){
p.nodes[i].payload->ir_drop[m*2] =
p.nodes[i-1].payload->ir_drop[m*2] +
p.nodes[i-1].current * (float)(p.nodes[i].c.y - p.nodes[i-1].c.y) * p.resistance_per_unit;
if (m!=0)
p.nodes[i].payload->ir_drop[m*2-1] =
p.nodes[i].payload->current_source * p.nodes[i].resistance;
float denom=(float)(p.nodes[i].R1+p.nodes[i].R2);
float W = denom>0.f? (float)p.nodes[i].R1/denom : 0.f;
for (int mm=M-1; mm>m; --mm){
p.nodes[i].payload->ir_drop[mm*2] = a[mm*2] + W*(b[mm*2]-a[mm*2]);
p.nodes[i].payload->ir_drop[mm*2-1] = a[mm*2-1] + W*(b[mm*2-1]-a[mm*2-1]);
}
}
}else if (p.v1){
auto& a = p.nodes.front().payload->ir_drop;
for (int i=1;i<(int)p.nodes.size();++i){
p.nodes[i].payload->ir_drop[m*2] =
p.nodes[i-1].payload->ir_drop[m*2] +
p.nodes[i-1].current * (float)(p.nodes[i].c.y - p.nodes[i-1].c.y) * p.resistance_per_unit;
if (m!=0)
p.nodes[i].payload->ir_drop[m*2-1] =
p.nodes[i].payload->current_source * p.nodes[i].resistance;
for (int mm=M-1; mm>m; --mm){
p.nodes[i].payload->ir_drop[mm*2] = a[mm*2];
p.nodes[i].payload->ir_drop[mm*2-1] = a[mm*2-1];
}
}
}else if (p.v2){
auto& b = p.nodes.back().payload->ir_drop;
for (int i=(int)p.nodes.size()-2; i>=0; --i){
p.nodes[i].payload->ir_drop[m*2] =
p.nodes[i+1].payload->ir_drop[m*2] -
p.nodes[i+1].current * (float)(p.nodes[i+1].c.y - p.nodes[i].c.y) * p.resistance_per_unit;
if (m!=0)
p.nodes[i].payload->ir_drop[m*2-1] =
p.nodes[i].payload->current_source * p.nodes[i].resistance;
for (int mm=M-1; mm>m; --mm){
p.nodes[i].payload->ir_drop[mm*2] = b[mm*2];
p.nodes[i].payload->ir_drop[mm*2-1] = b[mm*2-1];
}
}
}
}
}
}
}
vector<vector<node>> collect_ir_columns(){
if (part.empty() || part[0].empty())
throw runtime_error("no partitions on metal[0]");
vector<vector<node>> out; vector<node> col;
int prev_y = part[0].front().nodes.front().c.y;
for (auto& p: part[0]){
if (prev_y != p.nodes.front().c.y){
if (!col.empty()) out.push_back(col);
col.clear(); prev_y = p.nodes.front().c.y;
}
for (auto nd: p.nodes) col.push_back(nd);
}
if (!col.empty()) out.push_back(col);
return out;
}
boost::multi_array<float,3> build_ir_map(const vector<vector<node>>& chip){
int M=(int)metal_idx.size();
int Vias=(int)via_idx.size();
boost::multi_array<float,3> out(boost::extents[M+Vias][H][W]);
float h_gap = (float)x_max/(float)H;
float w_gap = (float)y_max/(float)W;
vector<vector<vector<float>>> value_map;
vector<float> y_map;
// vertical integrate per column
for (auto& c: chip){
vector<vector<float>> values; values.reserve(H);
int idx=0; float x1,x2,w,v1,v2;
auto val=[&](int i,int ch){ return c[i].payload->ir_drop[ch]; };
for (int h=0; h<H; ++h){
vector<float> value((size_t)(M+Vias), 0.f);
x1=h*h_gap; x2=x1+h_gap;
idx = clampT(idx, 0, (int)c.size()-1);
if (idx==(int)c.size()-1){
if (x1<=c[idx].c.x && c[idx].c.x<=x2){
for (int m=0;m<M+Vias;++m){
if (idx-1>=0){
float denom=(float)(c[idx].c.x - c[idx-1].c.x);
if (fabs(denom)>1e-6f){
w=(val(idx,m)-val(idx-1,m))/denom;
v1=(x1 - c[idx-1].c.x)*w + val(idx-1,m);
value[m] += (v1 + val(idx,m)) * (c[idx].c.x - x1) * 0.5f;
value[m] += val(idx,m) * (x2 - c[idx].c.x);
value[m] /= h_gap;
}else value[m]=val(idx,m);
}else value[m]=val(idx,m);
}
}else if (x2<c[idx].c.x){
for (int m=0;m<M+Vias;++m){
if (idx-1>=0){
float denom=(float)(c[idx].c.x - c[idx-1].c.x);
if (fabs(denom)>1e-6f){
w=(val(idx,m)-val(idx-1,m))/denom;
v1=(x1 - c[idx-1].c.x)*w + val(idx-1,m);
v2=(x2 - c[idx-1].c.x)*w + val(idx-1,m);
value[m] = 0.5f*(v1+v2);
}else value[m]=val(idx,m);
}else value[m]=val(idx,m);
}
}else{
for (int m=0;m<M+Vias;++m) value[m]=val(idx,m);
}
values.push_back(value);
continue;
}
if (idx==0){
if (x1<=c[idx].c.x && c[idx].c.x<=x2){
for (int m=0;m<M+Vias;++m) value[m]+= val(idx,m) * (c[idx].c.x - x1);
}else{
for (int m=0;m<M+Vias;++m) value[m]=val(idx,m);
values.push_back(value); continue;
}
}else{
if (x1<=c[idx].c.x && c[idx].c.x<=x2){
for (int m=0;m<M+Vias;++m){
float denom=(float)(c[idx].c.x - c[idx-1].c.x);
if (fabs(denom)>1e-6f){
w=(val(idx,m)-val(idx-1,m))/denom;
v1=(x1 - c[idx-1].c.x)*w + val(idx-1,m);
value[m] += (v1 + val(idx,m)) * (c[idx].c.x - x1) * 0.5f;
}else value[m] += val(idx,m)*(c[idx].c.x - x1);
}
}else{
for (int m=0;m<M+Vias;++m){
float denom=(float)(c[idx].c.x - c[idx-1].c.x);
if (fabs(denom)>1e-6f){
w=(val(idx,m)-val(idx-1,m))/denom;
v1=(x1 - c[idx-1].c.x)*w + val(idx-1,m);
v2=(x2 - c[idx-1].c.x)*w + val(idx-1,m);
value[m] = 0.5f*(v1+v2);
}else value[m]=val(idx,m);
}
values.push_back(value); continue;
}
}
while (x1<=c[idx].c.x && (idx+1)<(int)c.size() && c[idx+1].c.x<=x2){
for (int m=0;m<M+Vias;++m)
value[m] += (float)(c[idx+1].c.x - c[idx].c.x) * (val(idx+1,m)+val(idx,m)) * 0.5f;
++idx; if (idx==(int)c.size()-1) break;
}
if (idx==(int)c.size()-1){
for (int m=0;m<M+Vias;++m) value[m] += val(idx,m) * (x2 - c[idx].c.x);
}else{
for (int m=0;m<M+Vias;++m){
float denom=(float)(c[idx+1].c.x - c[idx].c.x);
if (fabs(denom)>1e-6f){
w=(val(idx+1,m)-val(idx,m))/denom;
v2=(x2 - c[idx].c.x)*w + val(idx,m);
value[m] += (val(idx,m)+v2)*(x2 - c[idx].c.x)*0.5f;
}else value[m] += val(idx,m)*(x2 - c[idx].c.x);
}
}
for (int m=0;m<M+Vias;++m) value[m]/=h_gap;
values.push_back(value);
if (idx!=(int)c.size()-1) ++idx;
}
value_map.push_back(std::move(values));
y_map.push_back((float)c.front().c.y);
}
// horizontal integrate
int idx=0; float y1,y2,w,v1,v2;
auto val=[&](int col,int hh,int ch){ return value_map[col][hh][ch]; };
for (int ww=0; ww<W; ++ww){
y1=ww*w_gap; y2=y1+w_gap;
idx = clampT(idx, 0, (int)y_map.size()-1);
if (idx==(int)y_map.size()-1){
if (y1<=y_map[idx] && y_map[idx]<=y2){
for (int h=0;h<H;++h)
for (int m=0;m<M+Vias;++m){
if (idx-1>=0){
float denom=(y_map[idx]-y_map[idx-1]);
if (fabs(denom)>1e-6f){
w=(val(idx,h,m)-val(idx-1,h,m))/denom;
v1=(y1 - y_map[idx-1])*w + val(idx-1,h,m);
out[m][h][ww] += (v1 + val(idx,h,m))*(y_map[idx]-y1)*0.5f;
out[m][h][ww] += val(idx,h,m)*(y2 - y_map[idx]);
out[m][h][ww] /= w_gap;
}else out[m][h][ww]=val(idx,h,m);
}else out[m][h][ww]=val(idx,h,m);
}
}else if (y2<y_map[idx]){
for (int h=0;h<H;++h)
for (int m=0;m<M+Vias;++m){
if (idx-1>=0){
float denom=(y_map[idx]-y_map[idx-1]);
if (fabs(denom)>1e-6f){
w=(val(idx,h,m)-val(idx-1,h,m))/denom;
v1=(y1 - y_map[idx-1])*w + val(idx-1,h,m);
v2=(y2 - y_map[idx-1])*w + val(idx-1,h,m);
out[m][h][ww] = 0.5f*(v1+v2);
}else out[m][h][ww]=val(idx,h,m);
}else out[m][h][ww]=val(idx,h,m);
}
}else{
for (int h=0;h<H;++h)
for (int m=0;m<M+Vias;++m) out[m][h][ww]=val(idx,h,m);
}
continue;
}
if (idx==0){
if (y1<=y_map[idx] && y_map[idx]<=y2){
for (int h=0;h<H;++h)
for (int m=0;m<M+Vias;++m)
out[m][h][ww] += val(idx,h,m)*(y_map[idx]-y1);
}else{
for (int h=0;h<H;++h)
for (int m=0;m<M+Vias;++m)
out[m][h][ww] = val(idx,h,m);
continue;
}
}else{
if (y1<=y_map[idx] && y_map[idx]<=y2){
for (int h=0;h<H;++h)
for (int m=0;m<M+Vias;++m){
float denom=(y_map[idx]-y_map[idx-1]);
if (fabs(denom)>1e-6f){
w=(val(idx,h,m)-val(idx-1,h,m))/denom;
v1=(y1 - y_map[idx-1])*w + val(idx-1,h,m);
out[m][h][ww] += (v1 + val(idx,h,m))*(y_map[idx]-y1)*0.5f;
}else out[m][h][ww] += val(idx,h,m)*(y_map[idx]-y1);
}
}else{
for (int h=0;h<H;++h)
for (int m=0;m<M+Vias;++m){
float denom=(y_map[idx]-y_map[idx-1]);
if (fabs(denom)>1e-6f){
w=(val(idx,h,m)-val(idx-1,h,m))/denom;
v1=(y1 - y_map[idx-1])*w + val(idx-1,h,m);
v2=(y2 - y_map[idx-1])*w + val(idx-1,h,m);
out[m][h][ww] = 0.5f*(v1+v2);
}else out[m][h][ww] = val(idx,h,m);
}
continue;
}
}
while (y1<=y_map[idx] && (idx+1)<(int)y_map.size() && y_map[idx+1]<=y2){
for (int h=0;h<H;++h)
for (int m=0;m<M+Vias;++m)
out[m][h][ww] += (val(idx+1,h,m)+val(idx,h,m))*(y_map[idx+1]-y_map[idx])*0.5f;
++idx; if (idx==(int)y_map.size()-1) break;
}
if (idx==(int)y_map.size()-1){
for (int h=0;h<H;++h)
for (int m=0;m<M+Vias;++m)
out[m][h][ww] += val(idx,h,m)*(y2 - y_map[idx]);
}else{
for (int h=0;h<H;++h)
for (int m=0;m<M+Vias;++m){
float denom=(y_map[idx+1]-y_map[idx]);
if (fabs(denom)>1e-6f){
w=(val(idx+1,h,m)-val(idx,h,m))/denom;
v2=(y2 - y_map[idx])*w + val(idx,h,m);
out[m][h][ww] += (val(idx,h,m)+v2)*(y2 - y_map[idx])*0.5f;
}else out[m][h][ww] += val(idx,h,m)*(y2 - y_map[idx]);
}
}
for (int h=0;h<H;++h)
for (int m=0;m<M+Vias;++m)
out[m][h][ww] /= w_gap;
if (idx!=(int)y_map.size()-1) ++idx;
}
return out;
}
boost::multi_array<float,3> build_distance(){
int M=(int)metal_idx.size();
if (M<3) return boost::multi_array<float,3>(boost::extents[0][H][W]);
boost::multi_array<float,3> out(boost::extents[M*2-3][H][W]);
float h_gap=(float)x_max/(float)H;
float w_gap=(float)y_max/(float)W;
vector<h_wire> h_wires,new_h,no_dist_h;
vector<v_wire> v_wires,new_v,no_dist_v;
int cnt=0;
for (int m=1;m<M;++m){
auto& ws = wire_map[m];
if (orientation[m]==HORIZONTAL){
h_wires.clear(); new_h.clear();
h_wires.push_back({0,0,y_max});
for (auto& wr: ws){
int wh1=wr.c1.x, ww1=wr.c1.y, ww2=wr.c2.y;
for (auto& hw: h_wires){
int h1=hw.h, w1=hw.w1, w2=hw.w2;
if (w2<ww1 || ww2<w1){
new_h.push_back({h1,w1,w2});
}else if (ww1<=w1 && w2<=ww2){
int hh1=clampT(rint_div(h1,h_gap),0,H);
int hh2=clampT((int)floor(wh1/h_gap+0.5f),0,H);
int wl =clampT(rint_div(w1,w_gap),0,W);
int wrx=clampT((int)floor(w2 /w_gap+0.5f),0,W);
for(int h=hh1;h<hh2;++h){ float d=h*h_gap-h1; for(int w=wl;w<wrx;++w) out[cnt][h][w]=d; }
}else if (w1<ww1 && ww2<w2){
int hh1=clampT(rint_div(h1,h_gap),0,H);
int hh2=clampT((int)floor(wh1/h_gap+0.5f),0,H);
int wl =clampT(rint_div(ww1,w_gap),0,W);
int wrx=clampT((int)floor(ww2/w_gap+0.5f),0,W);
for(int h=hh1;h<hh2;++h){ float d=h*h_gap-h1; for(int w=wl;w<wrx;++w) out[cnt][h][w]=d; }
new_h.push_back({h1,w1,ww1});
new_h.push_back({h1,ww2,w2});
}else if (ww2<w2){
int hh1=clampT(rint_div(h1,h_gap),0,H);
int hh2=clampT((int)floor(wh1/h_gap+0.5f),0,H);
int wl =clampT(rint_div(w1,w_gap),0,W);
int wrx=clampT((int)floor(ww2/w_gap+0.5f),0,W);
for(int h=hh1;h<hh2;++h){ float d=h*h_gap-h1; for(int w=wl;w<wrx;++w) out[cnt][h][w]=d; }
new_h.push_back({h1,ww2,w2});
}else{ // w1<ww1
int hh1=clampT(rint_div(h1,h_gap),0,H);
int hh2=clampT((int)floor(wh1/h_gap+0.5f),0,H);
int wl =clampT(rint_div(ww1,w_gap),0,W);
int wrx=clampT((int)floor(w2 /w_gap+0.5f),0,W);
for(int h=hh1;h<hh2;++h){ float d=h*h_gap-h1; for(int w=wl;w<wrx;++w) out[cnt][h][w]=d; }
new_h.push_back({h1,w1,ww1});
}
}
h_wires.swap(new_h); new_h.clear();
}
no_dist_h = h_wires;
for (int idx=0; idx<(int)ws.size(); ++idx){
h_wires.clear(); new_h.clear();
h_wires.push_back({ws[idx].c1.x, ws[idx].c1.y, ws[idx].c2.y});
for (int i=idx+1;i<(int)ws.size();++i){
auto& wr=ws[i]; int wh1=wr.c1.x, ww1=wr.c1.y, ww2=wr.c2.y;
for (auto& hw: h_wires){
int h1=hw.h, w1=hw.w1, w2=hw.w2;
if (w2<ww1 || ww2<w1){
new_h.push_back({h1,w1,w2});
}else if (ww1<=w1 && w2<=ww2){
int hh1=clampT(rint_div(h1,h_gap),0,H);
int hh2=clampT((int)floor(wh1/h_gap+0.5f),0,H);
int wl =clampT(rint_div(w1,w_gap),0,W);
int wrx=clampT((int)floor(w2 /w_gap+0.5f),0,W);
for (int h=hh1;h<hh2;++h){ float d=min(h*h_gap-h1, (float)wh1-h*h_gap); for(int w=wl;w<wrx;++w) out[cnt][h][w]=d; }
}else if (w1<ww1 && ww2<w2){
int hh1=clampT(rint_div(h1,h_gap),0,H);
int hh2=clampT((int)floor(wh1/h_gap+0.5f),0,H);
int wl =clampT(rint_div(ww1,w_gap),0,W);
int wrx=clampT((int)floor(ww2/w_gap+0.5f),0,W);
for (int h=hh1;h<hh2;++h){ float d=min(h*h_gap-h1, (float)wh1-h*h_gap); for(int w=wl;w<wrx;++w) out[cnt][h][w]=d; }
new_h.push_back({h1,w1,ww1});
new_h.push_back({h1,ww2,w2});
}else if (ww2<w2){
int hh1=clampT(rint_div(h1,h_gap),0,H);
int hh2=clampT((int)floor(wh1/h_gap+0.5f),0,H);
int wl =clampT(rint_div(w1,w_gap),0,W);
int wrx=clampT((int)floor(ww2/w_gap+0.5f),0,W);
for (int h=hh1;h<hh2;++h){ float d=min(h*h_gap-h1, (float)wh1-h*h_gap); for(int w=wl;w<wrx;++w) out[cnt][h][w]=d; }
new_h.push_back({h1,ww2,w2});
}else{
int hh1=clampT(rint_div(h1,h_gap),0,H);
int hh2=clampT((int)floor(wh1/h_gap+0.5f),0,H);
int wl =clampT(rint_div(ww1,w_gap),0,W);
int wrx=clampT((int)floor(w2 /w_gap+0.5f),0,W);
for (int h=hh1;h<hh2;++h){ float d=min(h*h_gap-h1, (float)wh1-h*h_gap); for(int w=wl;w<wrx;++w) out[cnt][h][w]=d; }
new_h.push_back({h1,w1,ww1});
}
}
h_wires.swap(new_h); new_h.clear();
}
for (auto& hw: h_wires){
int h1=hw.h, w1=hw.w1, w2=hw.w2;
int hh1=clampT(rint_div(h1,h_gap),0,H);
int wl =clampT(rint_div(w1,w_gap),0,W);
int wrx=clampT((int)floor(w2/w_gap+0.5f),0,W);
for (int h=hh1;h<H;++h){ float d=h*h_gap-h1; for(int w=wl;w<wrx;++w) out[cnt][h][w]=d; }
}
}
for (auto& hw: no_dist_h){
int w1=hw.w1, w2=hw.w2;
int wl=clampT(rint_div(w1,w_gap),0,W);
int wrx=clampT((int)floor(w2/w_gap+0.5f),0,W);
for (int h=0;h<H;++h){
float d=0.f;
if (wl<=0 && wrx<W) d=out[cnt][h][wrx];
else if (wl>0) d=out[cnt][h][wl-1];
for (int w=wl; w<wrx; ++w) out[cnt][h][w]=d;
}
}
}else{ // VERTICAL
v_wires.clear(); new_v.clear();
v_wires.push_back({0,0,x_max});
for (auto& wr: ws){
int ww1=wr.c1.y, wh1=wr.c1.x, wh2=wr.c2.x;
for (auto& vw: v_wires){
int w1=vw.w, h1=vw.h1, h2=vw.h2;
if (h2<wh1 || wh2<h1){
new_v.push_back({w1,h1,h2});
}else if (wh1<=h1 && h2<=wh2){
int wl =clampT(rint_div(w1,w_gap),0,W);
int wrx=clampT((int)floor(ww1/w_gap+0.5f),0,W);
int hh1=clampT(rint_div(h1,h_gap),0,H);
int hh2=clampT((int)floor(h2 /h_gap+0.5f),0,H);
for (int w=wl;w<wrx;++w){ float d=(w+0.5f)*w_gap - w1; for (int h=hh1;h<hh2;++h) out[cnt][h][w]=d; }
}else if (h1<wh1 && wh2<h2){
int wl =clampT(rint_div(w1,w_gap),0,W);
int wrx=clampT((int)floor(ww1/w_gap+0.5f),0,W);
int hh1=clampT(rint_div(h1,h_gap),0,H);
int hh2=clampT((int)floor(h2 /h_gap+0.5f),0,H);
for (int w=wl;w<wrx;++w){ float d=(w+0.5f)*w_gap - w1; for (int h=hh1;h<hh2;++h) out[cnt][h][w]=d; }
new_v.push_back({w1,h1,wh1});
new_v.push_back({w1,wh2,h2});
}else if (wh2<h2){
int wl =clampT(rint_div(w1,w_gap),0,W);
int wrx=clampT((int)floor(ww1/w_gap+0.5f),0,W);
int hh1=clampT(rint_div(h1,h_gap),0,H);
int hh2=clampT((int)floor(wh2/h_gap+0.5f),0,H);
for (int w=wl;w<wrx;++w){ float d=(w+0.5f)*w_gap - w1; for (int h=hh1;h<hh2;++h) out[cnt][h][w]=d; }
new_v.push_back({w1,wh2,h2});
}else{
int wl =clampT(rint_div(w1,w_gap),0,W);
int wrx=clampT((int)floor(ww1/w_gap+0.5f),0,W);
int hh1=clampT(rint_div(wh1,h_gap),0,H);
int hh2=clampT((int)floor(h2 /h_gap+0.5f),0,H);
for (int w=wl;w<wrx;++w){ float d=(w+0.5f)*w_gap - w1; for (int h=hh1;h<hh2;++h) out[cnt][h][w]=d; }
new_v.push_back({w1,h1,wh1});
}
}
v_wires.swap(new_v); new_v.clear();
}
no_dist_v = v_wires;
for (int idx=0; idx<(int)ws.size(); ++idx){
v_wires.clear(); new_v.clear();
v_wires.push_back({ws[idx].c1.y, ws[idx].c1.x, ws[idx].c2.x});
for (int i=idx+1;i<(int)ws.size();++i){
auto& wr=ws[i]; int ww1=wr.c1.y, wh1=wr.c1.x, wh2=wr.c2.x;
for (auto& vw: v_wires){
int w1=vw.w, h1=vw.h1, h2=vw.h2;
if (h2<wh1 || wh2<h1){
new_v.push_back({w1,h1,h2});
}else if (wh1<=h1 && h2<=wh2){
int wl =clampT(rint_div(w1,w_gap),0,W);
int wrx=clampT((int)floor(ww1/w_gap+0.5f),0,W);
int hh1=clampT(rint_div(h1,h_gap),0,H);
int hh2=clampT((int)floor(h2 /h_gap+0.5f),0,H);
for (int w=wl;w<wrx;++w){ float d=min((w+0.5f)*w_gap - w1, (float)ww1 - (w+0.5f)*w_gap);
for (int h=hh1;h<hh2;++h) out[cnt][h][w]=d; }
}else if (h1<wh1 && wh2<h2){
int wl =clampT(rint_div(w1,w_gap),0,W);
int wrx=clampT((int)floor(ww1/w_gap+0.5f),0,W);
int hh1=clampT(rint_div(h1,h_gap),0,H);
int hh2=clampT((int)floor(h2 /h_gap+0.5f),0,H);
for (int w=wl;w<wrx;++w){ float d=min((w+0.5f)*w_gap - w1, (float)ww1 - (w+0.5f)*w_gap);
for (int h=hh1;h<hh2;++h) out[cnt][h][w]=d; }
new_v.push_back({w1,h1,wh1});
new_v.push_back({w1,wh2,h2});
}else if (wh2<h2){
int wl =clampT(rint_div(w1,w_gap),0,W);
int wrx=clampT((int)floor(ww1/w_gap+0.5f),0,W);
int hh1=clampT(rint_div(h1,h_gap),0,H);
int hh2=clampT((int)floor(wh2/h_gap+0.5f),0,H);
for (int w=wl;w<wrx;++w){ float d=min((w+0.5f)*w_gap - w1, (float)ww1 - (w+0.5f)*w_gap);
for (int h=hh1;h<hh2;++h) out[cnt][h][w]=d; }
new_v.push_back({w1,wh2,h2});
}else{
int wl =clampT(rint_div(w1,w_gap),0,W);
int wrx=clampT((int)floor(ww1/w_gap+0.5f),0,W);
int hh1=clampT(rint_div(wh1,h_gap),0,H);
int hh2=clampT((int)floor(h2 /h_gap+0.5f),0,H);
for (int w=wl;w<wrx;++w){ float d=min((w+0.5f)*w_gap - w1, (float)ww1 - (w+0.5f)*w_gap);
for (int h=hh1;h<hh2;++h) out[cnt][h][w]=d; }
new_v.push_back({w1,h1,wh1});
}
}
v_wires.swap(new_v); new_v.clear();
}
for (auto& vw: v_wires){
int w1=vw.w, h1=vw.h1, h2=vw.h2;
int wl =clampT(rint_div(w1,w_gap),0,W);
int hh1=clampT(rint_div(h1,h_gap),0,H);