format code
authorJacob Lifshay <programmerjake@gmail.com>
Wed, 13 Oct 2021 00:55:19 +0000 (17:55 -0700)
committerJacob Lifshay <programmerjake@gmail.com>
Wed, 13 Oct 2021 00:55:19 +0000 (17:55 -0700)
src/ieee754/part/layout_experiment.py

index e76e85b9a6042a15c4fc6a91ca8300477a4e6a95..b4e7de156c62397ff3cf6fbb10dd0a877bbadb55 100644 (file)
@@ -33,9 +33,9 @@ def layout(elwid, signed, part_counts, lane_shapes=None, fixed_width=None):
     # https://bugs.libre-soc.org/show_bug.cgi?id=713#c67
     if lane_shapes is None:
         assert fixed_width is not None, \
-                "both fixed_width and lane_shapes cannot be None"
+            "both fixed_width and lane_shapes cannot be None"
         lane_shapes = {i: fixed_width // part_counts[i] for i in part_counts}
-        print ("lane_shapes", fixed_width, lane_shapes)
+        print("lane_shapes", fixed_width, lane_shapes)
     # identify if the lane_shapes is a mapping (dict, etc.)
     # if not, then assume that it is an integer (width) that
     # needs to be requested across all partitions
@@ -43,38 +43,38 @@ def layout(elwid, signed, part_counts, lane_shapes=None, fixed_width=None):
         lane_shapes = {i: lane_shapes for i in part_counts}
     # compute a set of partition widths
     cpart_wid = [-lane_shapes[i] // c for i, c in part_counts.items()]
-    print ("cpart_wid", cpart_wid, "part_counts", part_counts)
+    print("cpart_wid", cpart_wid, "part_counts", part_counts)
     cpart_wid = -min(cpart_wid)
     part_count = max(part_counts.values())
     # calculate the minumum width required
     width = cpart_wid * part_count
-    print ("width", width, cpart_wid, part_count)
-    if fixed_width is not None: # override the width and part_wid
+    print("width", width, cpart_wid, part_count)
+    if fixed_width is not None:  # override the width and part_wid
         assert width < fixed_width, "not enough space to fit partitions"
         part_wid = fixed_width // part_count
         assert part_wid * part_count == fixed_width, \
-                    "calculated width not aligned multiples"
+            "calculated width not aligned multiples"
         width = fixed_width
-        print ("part_wid", part_wid, "count", part_count)
+        print("part_wid", part_wid, "count", part_count)
     else:
         # go with computed width
         part_wid = cpart_wid
     # create the breakpoints dictionary.
     # do multi-stage version https://bugs.libre-soc.org/show_bug.cgi?id=713#c34
     # https://stackoverflow.com/questions/26367812/
-    dpoints = defaultdict(list) # if empty key, create a (empty) list
+    dpoints = defaultdict(list)  # if empty key, create a (empty) list
     for i, c in part_counts.items():
         def add_p(p):
-            dpoints[p].append(i) # auto-creates list if key non-existent
+            dpoints[p].append(i)  # auto-creates list if key non-existent
         for start in range(0, part_count, c):
-            add_p(start * part_wid) # start of lane
-            add_p(start * part_wid + lane_shapes[i]) # start of padding
+            add_p(start * part_wid)  # start of lane
+            add_p(start * part_wid + lane_shapes[i])  # start of padding
     # do not need the breakpoints at the very start or the very end
     dpoints.pop(0, None)
     dpoints.pop(width, None)
     plist = list(dpoints.keys())
     plist.sort()
-    print ("dpoints")
+    print("dpoints")
     pprint(dict(dpoints))
     # second stage, add (map to) the elwidth==i expressions.
     # TODO: use nmutil.treereduce?
@@ -90,15 +90,15 @@ def layout(elwid, signed, part_counts, lane_shapes=None, fixed_width=None):
         bitp[i] = 0
         for p, elwidths in dpoints.items():
             if i in elwidths:
-               bitpos = plist.index(p)
-               bitp[i] |= 1<< bitpos
+                bitpos = plist.index(p)
+                bitp[i] |= 1 << bitpos
     # fourth stage: determine which partitions are 100% unused.
     # these can then be "blanked out"
-    bmask = (1<<len(plist))-1
+    bmask = (1 << len(plist))-1
     for p in bitp.values():
         bmask &= ~p
     return (PartitionPoints(points), bitp, bmask, width, lane_shapes,
-        part_wid, part_count)
+            part_wid, part_count)
 
 
 if __name__ == '__main__':
@@ -133,7 +133,7 @@ if __name__ == '__main__':
     # elwidth=0b11 3x 24-bit    | ..8| ..8 | ..8 |..8 |
 
     #print ("maximum allocation from fixed_width=32")
-    #for i in range(4):
+    # for i in range(4):
     #    pprint((i, layout(i, True, part_counts, fixed_width=32)))
 
     # specify that the length is to be *different* at each of the elwidths.
@@ -157,22 +157,24 @@ if __name__ == '__main__':
     # https://bugs.libre-soc.org/show_bug.cgi?id=713#c30
 
     elwid = Signal(2)
-    pp,bitp,bm,b,c,d,e = layout(elwid, False, part_counts, widths_at_elwidth)
-    pprint ((pp,b,c,d,e))
+    pp, bitp, bm, b, c, d, e = layout(
+        elwid, False, part_counts, widths_at_elwidth)
+    pprint((pp, b, c, d, e))
     for k, v in bitp.items():
-        print ("bitp elwidth=%d" % k, bin(v))
-    print ("bmask", bin(bm))
+        print("bitp elwidth=%d" % k, bin(v))
+    print("bmask", bin(bm))
 
     m = Module()
+
     def process():
         for i in range(4):
             yield elwid.eq(i)
             yield Settle()
             ppt = []
             for pval in list(pp.values()):
-                val = yield pval # get nmigen to evaluate pp
+                val = yield pval  # get nmigen to evaluate pp
                 ppt.append(val)
-            pprint((i, (ppt,b,c,d,e)))
+            pprint((i, (ppt, b, c, d, e)))
             # check the results against bitp static-expected partition points
             # https://bugs.libre-soc.org/show_bug.cgi?id=713#c47
             # https://stackoverflow.com/a/27165694
@@ -188,30 +190,31 @@ if __name__ == '__main__':
     # https://bugs.libre-soc.org/show_bug.cgi?id=713#c22
 
     elwid = Signal(2)
-    pp,bitp,bm,b,c,d,e = layout(elwid, False, part_counts, widths_at_elwidth,
-                             fixed_width=64)
-    pprint ((pp,b,c,d,e))
+    pp, bitp, bm, b, c, d, e = layout(elwid, False, part_counts, widths_at_elwidth,
+                                      fixed_width=64)
+    pprint((pp, b, c, d, e))
     for k, v in bitp.items():
-        print ("bitp elwidth=%d" % k, bin(v))
-    print ("bmask", bin(bm))
+        print("bitp elwidth=%d" % k, bin(v))
+    print("bmask", bin(bm))
 
     m = Module()
+
     def process():
         for i in range(4):
             yield elwid.eq(i)
             yield Settle()
             ppt = []
             for pval in list(pp.values()):
-                val = yield pval # get nmigen to evaluate pp
+                val = yield pval  # get nmigen to evaluate pp
                 ppt.append(val)
-            print ("test elwidth=%d" % i)
-            pprint((i, (ppt,b,c,d,e)))
+            print("test elwidth=%d" % i)
+            pprint((i, (ppt, b, c, d, e)))
             # check the results against bitp static-expected partition points
             # https://bugs.libre-soc.org/show_bug.cgi?id=713#c47
             # https://stackoverflow.com/a/27165694
             ival = int(''.join(map(str, ppt[::-1])), 2)
             assert ival == bitp[i], "ival %s actual %s" % (bin(ival),
-                                                bin(bitp[i]))
+                                                           bin(bitp[i]))
 
     sim = Simulator(m)
     sim.add_process(process)