reduce¶
Performs reduction operations on Queue data like calculating a sum of all Queue elements, or like calculating XOR checksum.
- 
reduce(din: Queue, init, *, f) → init¶ Calculates a reduction of an input
Queue, using the binary operationfand the initial valueinit. The output data type, as well as the internal register, are the same as the type of theinitinput.The example shows how to calculate XOR checksum of the input
Queue. The inputinithas been fixed to0, but has also been given a data typeUint[8](0)to make sure that internal register width and output value is alsoUint[8]drv(t=Queue[Uint[8]], seq=[[0xff, 0xff, 0xff, 0xff]]) \ | reduce(init=Uint[8](0), f=lambda x, y: x ^ y) \ | check(ref=[0])
Next example shows how to pack bits received as a
Queueinto a parallel data of the typeUint. It also shows thatinitinput can be supplied at run time.(drv(t=Queue[Uint[8]], seq=[[0, 1, 0, 1, 0, 1, 0]]), drv(t=Uint[8], seq=[1])) \ | reduce(f=lambda x, y: (x << 1) | y) \ | check(ref=[0xaa])
- 
accum(din: Queue[Integer], init: Integer) → init¶ The
accum()gear is a convenience gear for calculating the sum of all elements of the inputQueue. It relies on thereduce()gear underneath:@gear def accum(din: Queue[Integer], init: Integer) -> b'init': return reduce(din, init, f=lambda x, y: x + y)
drv(t=Queue[Uint[4]], seq=[[0, 1, 2, 3, 4]]) \ | accum(init=Uint[8](0)) \ | check(ref=[10])