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 and SVSHAPE
.skip
not in [0b10, 0b11]
76 #print ("inplace mode")
77 ji
= halfrev2(ji
, True)
82 # start an infinite (wrapping) loop
85 for size
in x_r
: # loop over 3rd order dimension (size)
86 x_end
= size
== x_r
[-1]
87 # y_r schedule depends on size
90 for i
in range(0, n
, size
):
93 if SVSHAPE
.invxyz
[1]: y_r
.reverse()
94 for i
in y_r
: # loop over 2nd order dimension
96 # two lists of half-range indices, e.g. j 0123, jr 7654
97 j
= list(range(i
, i
+ halfsize
))
98 jr
= list(range(i
+halfsize
, i
+ size
))
100 # invert if requested
101 if SVSHAPE
.invxyz
[2]: j_r
.reverse()
102 hz2
= halfsize
// 2 # zero stops reversing 1-item lists
103 # if you *really* want to do the in-place swapping manually,
104 # this allows you to do it. good luck...
105 if SVSHAPE
.submode2
== 0b01 and not inplace_mode
:
108 #print ("xform jr", jr)
109 for jl
, jh
in zip(j
, jr
): # loop over 1st order dimension
111 # now depending on MODE return the index. inner butterfly
112 if SVSHAPE
.skip
in [0b00, 0b10]:
113 result
= ri
[ji
[jl
]] # lower half
114 elif SVSHAPE
.skip
in [0b01, 0b11]:
115 result
= ri
[ji
[jh
]] # upper half, reverse order
117 ((y_end
and z_end
)<<1) |
118 ((y_end
and x_end
and z_end
)<<2))
120 yield result
+ SVSHAPE
.offset
, loopends
124 for ci
, (jl
, jh
) in enumerate(zip(j
[:hz2
], jr
[:hz2
])):
126 #print ("inplace swap", jh, jlh)
127 tmp1
, tmp2
= ji
[jlh
], ji
[jh
]
128 ji
[jlh
], ji
[jh
] = tmp2
, tmp1
131 # python "yield" can be iterated. use this to make it clear how
132 # the indices are generated by using natural-looking nested loops
133 def iterate_dct_outer_butterfly_indices(SVSHAPE
):
134 # get indices to iterate over, in the required order
136 # createing lists of indices to iterate over in each dimension
137 # has to be done dynamically, because it depends on the size
138 # first, the size-based loop (which can be done statically)
144 # invert order if requested
145 if SVSHAPE
.invxyz
[0]:
151 #print ("outer butterfly")
153 # reference (read/write) the in-place data in *reverse-bit-order*
155 if SVSHAPE
.submode2
== 0b11:
156 levels
= n
.bit_length() - 1
157 ri
= [ri
[reverse_bits(i
, levels
)] for i
in range(n
)]
159 # reference list for not needing to do data-swaps, just swap what
160 # *indices* are referenced (two levels of indirection at the moment)
161 # pre-reverse the data-swap list so that it *ends up* in the order 0123..
163 inplace_mode
= SVSHAPE
.skip
in [0b10, 0b11]
165 #print ("inplace mode", SVSHAPE.skip)
166 ji
= halfrev2(ji
, True)
171 # start an infinite (wrapping) loop
174 for size
in x_r
: # loop over 3rd order dimension (size)
176 x_end
= size
== x_r
[-1]
177 y_r
= list(range(0, halfsize
))
178 #print ("itersum", halfsize, size, y_r)
179 # invert if requested
180 if SVSHAPE
.invxyz
[1]: y_r
.reverse()
181 for i
in y_r
: # loop over 2nd order dimension
183 # one list to create iterative-sum schedule
184 jr
= list(range(i
+halfsize
, i
+n
-halfsize
, size
))
185 #print ("itersum jr", i+halfsize, i+size, jr)
186 # invert if requested
187 if SVSHAPE
.invxyz
[2]: j_r
.reverse()
188 hz2
= halfsize
// 2 # zero stops reversing 1-item lists
189 for jh
in jr
: # loop over 1st order dimension
191 #print (" itersum", size, i, jh, jh+size)
192 if SVSHAPE
.skip
in [0b00, 0b10]:
193 result
= ri
[ji
[jh
]] # lower half
194 elif SVSHAPE
.skip
in [0b01, 0b11]:
195 result
= ri
[ji
[jh
+size
]] # upper half
197 ((y_end
and z_end
)<<1) |
198 ((y_end
and x_end
and z_end
)<<2))
200 yield result
+ SVSHAPE
.offset
, loopends
203 if SVSHAPE
.submode2
== 0b11 and inplace_mode
:
204 j
= list(range(i
, i
+ halfsize
))
205 jr
= list(range(i
+halfsize
, i
+ size
))
207 for ci
, (jl
, jh
) in enumerate(zip(j
[:hz2
], jr
[:hz2
])):
209 #print ("inplace swap", jh, jlh)
210 tmp1
, tmp2
= ji
[jlh
], ji
[jh
]
211 ji
[jlh
], ji
[jh
] = tmp2
, tmp1
214 def pprint_schedule(schedule
, n
):
219 tablestep
= n
// size
220 print ("size %d halfsize %d tablestep %d" % \
221 (size
, halfsize
, tablestep
))
222 for i
in range(0, n
, size
):
223 prefix
= "i %d\t" % i
224 for j
in range(i
, i
+ halfsize
):
225 (jl
, je
), (jh
, he
) = schedule
[idx
]
226 print (" %-3d\t%s j=%-2d jh=%-2d "
227 "j[jl=%-2d] j[jh=%-2d]" % \
228 (idx
, prefix
, j
, j
+halfsize
,
231 "end", bin(je
)[2:], bin(je
)[2:])
235 def pprint_schedule_outer(schedule
, n
):
240 tablestep
= n
// size
241 print ("size %d halfsize %d tablestep %d" % \
242 (size
, halfsize
, tablestep
))
243 y_r
= list(range(0, halfsize
))
245 prefix
= "i %d\t" % i
246 jr
= list(range(i
+halfsize
, i
+n
-halfsize
, size
))
248 (jl
, je
), (jh
, he
) = schedule
[idx
]
249 print (" %-3d\t%s j=%-2d jh=%-2d "
250 "j[jl=%-2d] j[jh=%-2d]" % \
251 (idx
, prefix
, j
, j
+halfsize
,
254 "end", bin(je
)[2:], bin(je
)[2:])
259 # totally cool *in-place* DCT algorithm using yield REMAPs
265 print ("transform2", n
)
266 levels
= n
.bit_length() - 1
268 # reference (read/write) the in-place data in *reverse-bit-order*
270 ri
= [ri
[reverse_bits(i
, levels
)] for i
in range(n
)]
272 # and pretend we LDed data in half-swapped *and* bit-reversed order as well
273 # TODO: merge these two
274 vec
= halfrev2(vec
, False)
275 vec
= [vec
[ri
[i
]] for i
in range(n
)]
277 # create a cos table: not strictly necessary but here for illustrative
278 # purposes, to demonstrate the point that it really *is* iterative.
279 # this table could be cached and used multiple times rather than
280 # computed every time.
285 for i
in range(n
//size
):
286 for ci
in range(halfsize
):
287 ctable
.append((math
.cos((ci
+ 0.5) * math
.pi
/ size
) * 2.0))
302 SVSHAPE0
.lims
= [xdim
, ydim
, zdim
]
303 SVSHAPE0
.order
= [0,1,2] # experiment with different permutations, here
305 SVSHAPE0
.submode2
= 0b01
307 SVSHAPE0
.offset
= 0 # experiment with different offset, here
308 SVSHAPE0
.invxyz
= [1,0,0] # inversion if desired
309 # j+halfstep schedule
311 SVSHAPE1
.lims
= [xdim
, ydim
, zdim
]
312 SVSHAPE1
.order
= [0,1,2] # experiment with different permutations, here
314 SVSHAPE1
.submode2
= 0b01
316 SVSHAPE1
.offset
= 0 # experiment with different offset, here
317 SVSHAPE1
.invxyz
= [1,0,0] # inversion if desired
319 # enumerate over the iterator function, getting new indices
320 i0
= iterate_dct_inner_butterfly_indices(SVSHAPE0
)
321 i1
= iterate_dct_inner_butterfly_indices(SVSHAPE1
)
322 for k
, ((jl
, jle
), (jh
, jhe
)) in enumerate(zip(i0
, i1
)):
323 t1
, t2
= vec
[jl
], vec
[jh
]
326 vec
[jh
] = (t1
- t2
) * (1/coeff
)
327 print ("coeff", size
, i
, "ci", ci
,
329 "i/n", (ci
+0.5)/size
, coeff
, vec
[jl
],
331 "end", bin(jle
), bin(jhe
))
332 if jle
== 0b111: # all loops end
335 print("transform2 pre-itersum", vec
)
337 # now things are in the right order for the outer butterfly.
341 SVSHAPE0
.lims
= [xdim
, ydim
, zdim
]
342 SVSHAPE0
.order
= [0,1,2] # experiment with different permutations, here
343 SVSHAPE0
.submode2
= 0b10
346 SVSHAPE0
.offset
= 0 # experiment with different offset, here
347 SVSHAPE0
.invxyz
= [0,0,0] # inversion if desired
348 # j+halfstep schedule
350 SVSHAPE1
.lims
= [xdim
, ydim
, zdim
]
351 SVSHAPE1
.order
= [0,1,2] # experiment with different permutations, here
353 SVSHAPE1
.submode2
= 0b10
355 SVSHAPE1
.offset
= 0 # experiment with different offset, here
356 SVSHAPE1
.invxyz
= [0,0,0] # inversion if desired
358 # enumerate over the iterator function, getting new indices
359 i0
= iterate_dct_outer_butterfly_indices(SVSHAPE0
)
360 i1
= iterate_dct_outer_butterfly_indices(SVSHAPE1
)
361 for k
, ((jl
, jle
), (jh
, jhe
)) in enumerate(zip(i0
, i1
)):
362 print ("itersum jr", jl
, jh
,
363 "end", bin(jle
), bin(jhe
))
366 if jle
== 0b111: # all loops end
369 print("transform2 result", vec
)
375 # set the dimension sizes here
378 ydim
= 0 # not needed
379 zdim
= 0 # again, not needed
391 SVSHAPE0
.lims
= [xdim
, ydim
, zdim
]
392 SVSHAPE0
.order
= [0,1,2] # experiment with different permutations, here
395 SVSHAPE0
.offset
= 0 # experiment with different offset, here
396 SVSHAPE0
.invxyz
= [0,0,0] # inversion if desired
397 # j+halfstep schedule
399 SVSHAPE1
.lims
= [xdim
, ydim
, zdim
]
400 SVSHAPE1
.order
= [0,1,2] # experiment with different permutations, here
403 SVSHAPE1
.offset
= 0 # experiment with different offset, here
404 SVSHAPE1
.invxyz
= [0,0,0] # inversion if desired
406 # enumerate over the iterator function, getting new indices
408 i0
= iterate_dct_inner_butterfly_indices(SVSHAPE0
)
409 i1
= iterate_dct_inner_butterfly_indices(SVSHAPE1
)
410 for idx
, (jl
, jh
) in enumerate(zip(i0
, i1
)):
411 schedule
.append((jl
, jh
))
412 if jl
[1] == 0b111: # end
415 # ok now pretty-print the results, with some debug output
416 print ("inner butterfly")
417 pprint_schedule(schedule
, n
)
426 SVSHAPE0
.lims
= [xdim
, ydim
, zdim
]
427 SVSHAPE0
.order
= [0,1,2] # experiment with different permutations, here
429 SVSHAPE0
.submode2
= 0b100
431 SVSHAPE0
.offset
= 0 # experiment with different offset, here
432 SVSHAPE0
.invxyz
= [1,0,0] # inversion if desired
433 # j+halfstep schedule
435 SVSHAPE1
.lims
= [xdim
, ydim
, zdim
]
436 SVSHAPE1
.order
= [0,1,2] # experiment with different permutations, here
438 SVSHAPE1
.submode2
= 0b100
440 SVSHAPE1
.offset
= 0 # experiment with different offset, here
441 SVSHAPE1
.invxyz
= [1,0,0] # inversion if desired
443 # enumerate over the iterator function, getting new indices
445 i0
= iterate_dct_outer_butterfly_indices(SVSHAPE0
)
446 i1
= iterate_dct_outer_butterfly_indices(SVSHAPE1
)
447 for idx
, (jl
, jh
) in enumerate(zip(i0
, i1
)):
448 schedule
.append((jl
, jh
))
449 if jl
[1] == 0b111: # end
452 # ok now pretty-print the results, with some debug output
453 print ("outer butterfly")
454 pprint_schedule_outer(schedule
, n
)
457 if __name__
== '__main__':