1 # DCT "REMAP" scheduler
3 # Modifications made to create an in-place iterative DCT:
4 # Copyright (c) 2021 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
8 # Original fastdctlee.py by Nayuki:
9 # Copyright (c) 2020 Project Nayuki. (MIT License)
10 # https://www.nayuki.io/page/fast-discrete-cosine-transform-algorithms
14 # bits of the integer 'val'.
15 def reverse_bits(val
, width
):
17 for _
in range(width
):
18 result
= (result
<< 1) |
(val
& 1)
23 # iterative version of [recursively-applied] half-rev.
24 # relies on the list lengths being power-of-two and the fact
25 # that bit-inversion of a list of binary numbers is the same
26 # as reversing the order of the list
27 # this version is dead easy to implement in hardware.
28 # a big surprise is that the half-reversal can be done with
29 # such a simple XOR. the inverse operation is slightly trickier
30 def halfrev2(vec
, pre_rev
=True):
32 for i
in range(len(vec
)):
34 res
.append(i ^
(i
>>1))
38 for ji
in range(1, bl
):
44 # python "yield" can be iterated. use this to make it clear how
45 # the indices are generated by using natural-looking nested loops
46 def iterate_dct_inner_butterfly_indices(SVSHAPE
):
47 # get indices to iterate over, in the required order
49 # createing lists of indices to iterate over in each dimension
50 # has to be done dynamically, because it depends on the size
51 # first, the size-based loop (which can be done statically)
57 # invert order if requested
64 # reference (read/write) the in-place data in *reverse-bit-order*
66 if SVSHAPE
.submode2
== 0b01:
67 levels
= n
.bit_length() - 1
68 ri
= [ri
[reverse_bits(i
, levels
)] for i
in range(n
)]
70 # reference list for not needing to do data-swaps, just swap what
71 # *indices* are referenced (two levels of indirection at the moment)
72 # pre-reverse the data-swap list so that it *ends up* in the order 0123..
74 inplace_mode
= SVSHAPE
.submode2
== 0b01
75 # and SVSHAPE.skip not in [0b10, 0b11]
77 #print ("inplace mode")
78 ji
= halfrev2(ji
, True)
83 # start an infinite (wrapping) loop
86 for size
in x_r
: # loop over 3rd order dimension (size)
87 x_end
= size
== x_r
[-1]
88 # y_r schedule depends on size
91 for i
in range(0, n
, size
):
94 if SVSHAPE
.invxyz
[1]: y_r
.reverse()
95 for i
in y_r
: # loop over 2nd order dimension
97 # two lists of half-range indices, e.g. j 0123, jr 7654
98 j
= list(range(i
, i
+ halfsize
))
99 jr
= list(range(i
+halfsize
, i
+ size
))
101 # invert if requested
102 if SVSHAPE
.invxyz
[2]: j_r
.reverse()
103 hz2
= halfsize
// 2 # zero stops reversing 1-item lists
104 # if you *really* want to do the in-place swapping manually,
105 # this allows you to do it. good luck...
106 if SVSHAPE
.submode2
== 0b01 and not inplace_mode
:
109 #print ("xform jr", jr)
110 # loop over 1st order dimension
111 for ci
, (jl
, jh
) in enumerate(zip(j
, jr
)):
113 # now depending on MODE return the index. inner butterfly
114 if SVSHAPE
.skip
== 0b00: # in [0b00, 0b10]:
115 result
= ri
[ji
[jl
]] # lower half
116 elif SVSHAPE
.skip
== 0b01: # in [0b01, 0b11]:
117 result
= ri
[ji
[jh
]] # upper half, reverse order
118 elif SVSHAPE
.skip
== 0b10: #
119 result
= ci
# coefficient helper
120 elif SVSHAPE
.skip
== 0b11: #
121 result
= size
# coefficient helper
123 ((y_end
and z_end
)<<1) |
124 ((y_end
and x_end
and z_end
)<<2))
126 yield result
+ SVSHAPE
.offset
, loopends
130 for ci
, (jl
, jh
) in enumerate(zip(j
[:hz2
], jr
[:hz2
])):
132 #print ("inplace swap", jh, jlh)
133 tmp1
, tmp2
= ji
[jlh
], ji
[jh
]
134 ji
[jlh
], ji
[jh
] = tmp2
, tmp1
137 # python "yield" can be iterated. use this to make it clear how
138 # the indices are generated by using natural-looking nested loops
139 def iterate_dct_outer_butterfly_indices(SVSHAPE
):
140 # get indices to iterate over, in the required order
142 # createing lists of indices to iterate over in each dimension
143 # has to be done dynamically, because it depends on the size
144 # first, the size-based loop (which can be done statically)
150 # invert order if requested
151 if SVSHAPE
.invxyz
[0]:
157 #print ("outer butterfly")
159 # reference (read/write) the in-place data in *reverse-bit-order*
161 if SVSHAPE
.submode2
== 0b11:
162 levels
= n
.bit_length() - 1
163 ri
= [ri
[reverse_bits(i
, levels
)] for i
in range(n
)]
165 # reference list for not needing to do data-swaps, just swap what
166 # *indices* are referenced (two levels of indirection at the moment)
167 # pre-reverse the data-swap list so that it *ends up* in the order 0123..
169 inplace_mode
= False # need the space... SVSHAPE.skip in [0b10, 0b11]
171 #print ("inplace mode", SVSHAPE.skip)
172 ji
= halfrev2(ji
, True)
177 # start an infinite (wrapping) loop
180 for size
in x_r
: # loop over 3rd order dimension (size)
182 x_end
= size
== x_r
[-1]
183 y_r
= list(range(0, halfsize
))
184 #print ("itersum", halfsize, size, y_r)
185 # invert if requested
186 if SVSHAPE
.invxyz
[1]: y_r
.reverse()
187 for i
in y_r
: # loop over 2nd order dimension
189 # one list to create iterative-sum schedule
190 jr
= list(range(i
+halfsize
, i
+n
-halfsize
, size
))
191 #print ("itersum jr", i+halfsize, i+size, jr)
192 # invert if requested
193 if SVSHAPE
.invxyz
[2]: j_r
.reverse()
194 hz2
= halfsize
// 2 # zero stops reversing 1-item lists
195 for ci
, jh
in enumerate(jr
): # loop over 1st order dimension
197 #print (" itersum", size, i, jh, jh+size)
198 if SVSHAPE
.skip
== 0b00: # in [0b00, 0b10]:
199 result
= ri
[ji
[jh
]] # lower half
200 elif SVSHAPE
.skip
== 0b01: # in [0b01, 0b11]:
201 result
= ri
[ji
[jh
+size
]] # upper half
202 elif SVSHAPE
.skip
== 0b10: #
203 result
= ci
# coefficient helper
204 elif SVSHAPE
.skip
== 0b11: #
205 result
= size
# coefficient helper
207 ((y_end
and z_end
)<<1) |
208 ((y_end
and x_end
and z_end
)<<2))
210 yield result
+ SVSHAPE
.offset
, loopends
213 if SVSHAPE
.submode2
== 0b11 and inplace_mode
:
214 j
= list(range(i
, i
+ halfsize
))
215 jr
= list(range(i
+halfsize
, i
+ size
))
217 for ci
, (jl
, jh
) in enumerate(zip(j
[:hz2
], jr
[:hz2
])):
219 #print ("inplace swap", jh, jlh)
220 tmp1
, tmp2
= ji
[jlh
], ji
[jh
]
221 ji
[jlh
], ji
[jh
] = tmp2
, tmp1
224 def pprint_schedule(schedule
, n
):
229 tablestep
= n
// size
230 print ("size %d halfsize %d tablestep %d" % \
231 (size
, halfsize
, tablestep
))
232 for i
in range(0, n
, size
):
233 prefix
= "i %d\t" % i
234 for j
in range(i
, i
+ halfsize
):
235 (jl
, je
), (jh
, he
) = schedule
[idx
]
236 print (" %-3d\t%s j=%-2d jh=%-2d "
237 "j[jl=%-2d] j[jh=%-2d]" % \
238 (idx
, prefix
, j
, j
+halfsize
,
241 "end", bin(je
)[2:], bin(je
)[2:])
245 def pprint_schedule_outer(schedule
, n
):
250 tablestep
= n
// size
251 print ("size %d halfsize %d tablestep %d" % \
252 (size
, halfsize
, tablestep
))
253 y_r
= list(range(0, halfsize
))
255 prefix
= "i %d\t" % i
256 jr
= list(range(i
+halfsize
, i
+n
-halfsize
, size
))
258 (jl
, je
), (jh
, he
) = schedule
[idx
]
259 print (" %-3d\t%s j=%-2d jh=%-2d "
260 "j[jl=%-2d] j[jh=%-2d]" % \
261 (idx
, prefix
, j
, j
+halfsize
,
264 "end", bin(je
)[2:], bin(je
)[2:])
269 # totally cool *in-place* DCT algorithm using yield REMAPs
275 print ("transform2", n
)
276 levels
= n
.bit_length() - 1
278 # reference (read/write) the in-place data in *reverse-bit-order*
280 ri
= [ri
[reverse_bits(i
, levels
)] for i
in range(n
)]
282 # and pretend we LDed data in half-swapped *and* bit-reversed order as well
283 # TODO: merge these two
284 vec
= halfrev2(vec
, False)
285 vec
= [vec
[ri
[i
]] for i
in range(n
)]
287 # create a cos table: not strictly necessary but here for illustrative
288 # purposes, to demonstrate the point that it really *is* iterative.
289 # this table could be cached and used multiple times rather than
290 # computed every time.
295 for i
in range(n
//size
):
296 for ci
in range(halfsize
):
297 ctable
.append((math
.cos((ci
+ 0.5) * math
.pi
/ size
) * 2.0))
312 SVSHAPE0
.lims
= [xdim
, ydim
, zdim
]
314 SVSHAPE0
.submode2
= 0b01
316 SVSHAPE0
.offset
= 0 # experiment with different offset, here
317 SVSHAPE0
.invxyz
= [1,0,0] # inversion if desired
318 # j+halfstep schedule
320 SVSHAPE1
.lims
= [xdim
, ydim
, zdim
]
322 SVSHAPE1
.submode2
= 0b01
324 SVSHAPE1
.offset
= 0 # experiment with different offset, here
325 SVSHAPE1
.invxyz
= [1,0,0] # inversion if desired
328 SVSHAPE2
.lims
= [xdim
, ydim
, zdim
]
330 SVSHAPE2
.submode2
= 0b01
332 SVSHAPE2
.offset
= 0 # experiment with different offset, here
333 SVSHAPE2
.invxyz
= [1,0,0] # inversion if desired
336 SVSHAPE3
.lims
= [xdim
, ydim
, zdim
]
338 SVSHAPE3
.submode2
= 0b01
340 SVSHAPE3
.offset
= 0 # experiment with different offset, here
341 SVSHAPE3
.invxyz
= [1,0,0] # inversion if desired
343 # enumerate over the iterator function, getting new indices
344 i0
= iterate_dct_inner_butterfly_indices(SVSHAPE0
)
345 i1
= iterate_dct_inner_butterfly_indices(SVSHAPE1
)
346 i2
= iterate_dct_inner_butterfly_indices(SVSHAPE2
)
347 i3
= iterate_dct_inner_butterfly_indices(SVSHAPE3
)
348 for k
, ((jl
, jle
), (jh
, jhe
), (ci
, cie
), (size
, sze
)) in \
349 enumerate(zip(i0
, i1
, i2
, i3
)):
350 t1
, t2
= vec
[jl
], vec
[jh
]
351 print ("xform2", jl
, jh
, ci
, size
)
352 coeff
= (math
.cos((ci
+ 0.5) * math
.pi
/ size
) * 2.0)
353 assert coeff
== ctable
[k
]
355 vec
[jh
] = (t1
- t2
) * (1/coeff
)
356 print ("coeff", size
, i
, "ci", ci
,
358 "i/n", (ci
+0.5)/size
, coeff
, vec
[jl
],
360 "end", bin(jle
), bin(jhe
))
361 if jle
== 0b111: # all loops end
364 print("transform2 pre-itersum", vec
)
366 # now things are in the right order for the outer butterfly.
370 SVSHAPE0
.lims
= [xdim
, ydim
, zdim
]
371 SVSHAPE0
.submode2
= 0b100
374 SVSHAPE0
.offset
= 0 # experiment with different offset, here
375 SVSHAPE0
.invxyz
= [0,0,0] # inversion if desired
376 # j+halfstep schedule
378 SVSHAPE1
.lims
= [xdim
, ydim
, zdim
]
380 SVSHAPE1
.submode2
= 0b100
382 SVSHAPE1
.offset
= 0 # experiment with different offset, here
383 SVSHAPE1
.invxyz
= [0,0,0] # inversion if desired
385 # enumerate over the iterator function, getting new indices
386 i0
= iterate_dct_outer_butterfly_indices(SVSHAPE0
)
387 i1
= iterate_dct_outer_butterfly_indices(SVSHAPE1
)
388 for k
, ((jl
, jle
), (jh
, jhe
)) in enumerate(zip(i0
, i1
)):
389 print ("itersum jr", jl
, jh
,
390 "end", bin(jle
), bin(jhe
))
393 if jle
== 0b111: # all loops end
396 print("transform2 result", vec
)
402 # set the dimension sizes here
405 ydim
= 0 # not needed
406 zdim
= 0 # again, not needed
418 SVSHAPE0
.lims
= [xdim
, ydim
, zdim
]
419 SVSHAPE0
.submode2
= 0b010
422 SVSHAPE0
.offset
= 0 # experiment with different offset, here
423 SVSHAPE0
.invxyz
= [0,0,0] # inversion if desired
424 # j+halfstep schedule
426 SVSHAPE1
.lims
= [xdim
, ydim
, zdim
]
427 SVSHAPE1
.submode2
= 0b010
430 SVSHAPE1
.offset
= 0 # experiment with different offset, here
431 SVSHAPE1
.invxyz
= [0,0,0] # inversion if desired
433 # enumerate over the iterator function, getting new indices
435 i0
= iterate_dct_inner_butterfly_indices(SVSHAPE0
)
436 i1
= iterate_dct_inner_butterfly_indices(SVSHAPE1
)
437 for idx
, (jl
, jh
) in enumerate(zip(i0
, i1
)):
438 schedule
.append((jl
, jh
))
439 if jl
[1] == 0b111: # end
442 # ok now pretty-print the results, with some debug output
443 print ("inner butterfly")
444 pprint_schedule(schedule
, n
)
453 SVSHAPE0
.lims
= [xdim
, ydim
, zdim
]
455 SVSHAPE0
.submode2
= 0b100
457 SVSHAPE0
.offset
= 0 # experiment with different offset, here
458 SVSHAPE0
.invxyz
= [1,0,0] # inversion if desired
459 # j+halfstep schedule
461 SVSHAPE1
.lims
= [xdim
, ydim
, zdim
]
463 SVSHAPE1
.submode2
= 0b100
465 SVSHAPE1
.offset
= 0 # experiment with different offset, here
466 SVSHAPE1
.invxyz
= [1,0,0] # inversion if desired
468 # enumerate over the iterator function, getting new indices
470 i0
= iterate_dct_outer_butterfly_indices(SVSHAPE0
)
471 i1
= iterate_dct_outer_butterfly_indices(SVSHAPE1
)
472 for idx
, (jl
, jh
) in enumerate(zip(i0
, i1
)):
473 schedule
.append((jl
, jh
))
474 if jl
[1] == 0b111: # end
477 # ok now pretty-print the results, with some debug output
478 print ("outer butterfly")
479 pprint_schedule_outer(schedule
, n
)
482 if __name__
== '__main__':