Mill Computing, Inc. Forums The Mill Architecture Vector ops Reply To: Vector ops

goldbug
Participant
Post count: 53

My shot at this using alternate:

``````
// %0 contains [0,0,1,0]
// %1 contains [A,B,C,D]
F("pick_reduce_4") %0, %1;
pick      (%0, %1)        %2;

alternate (%2, %2)        %3 %4,
recur     (%3, %4, %3)    %5;

alternate (%5, %5)        %6 %7,
recur     (%6, %7, %6)    %8;

extract   (%8, w(0ul))   %9
ret       (%9);
``````

Some example runs:

``````
input: %0 = [1,0,0,0] %1 = [a,b,c,d]

%2 = [a,,,]
%3 = [a,a,,]   %4=[,,,]
%5 = [a,a,,]
%6 = [a,a,a,a] %7=[,,,]
%8 = [a,a,a,a]
%9 = a
``````
``````
input: %0 = [0,1,0,0] %1 = [a,b,c,d]

%2 = [,b,,]
%3 = [,,b,b]      %4=[,,,]
%5 = [,,b,b]
%6 = [,,,]        %7=[b,b,b,b]
%8 = [b,b,b,b]
%9 = b
``````
``````
input: %0 = [0,0,1,0] %1 = [a,b,c,d]

%2 = [,,c,]
%3 = [,,,]      %4=[c,c,,]
%5 = [c,c,,]
%6 = [c,c,c,c]  %7=[,,,]
%8 = [c,c,c,c]
%9 = c
``````
``````
input: %0 = [0,0,0,1] %1 = [a,b,c,d]

%2 = [,,,d]
%3 = [,,,]      %4=[,,d,d]
%5 = [,,d,d]
%6 = [,,,]      %7=[d,d,d,d]
%8 = [d,d,d,d]
%9 = d
``````

Shuffle is to reorder a vector? If I understand it correctly:

``````
shuffle ([a,b,c,d],  [3,2,1,0])  =  [d,c,b,a]
shuffle ([a,b,c,d],  [1,1,1,1])  =  [b,b,b,b]
shuffle ([a,b,c,d], [0, None, None, None]) = [a, None, None, None]
``````

Then perhaps we can use the left operation to find the index of the selected element, this should work for vectors of any size and it would be O(1):

``````
F("pick_reduce") %0, %1;
left       (%0)           %2;

// this just builds a vector the same width as the data vector
// the first element is the index of the element we want
// the rest do not matter and can be junk
inject     (%1, %2, 0)    %3;

shuffle    (%1, %3)       %4;

extract    (%4, 0)        %5,
ret        (%5);
``````

If I understand correctly, the left operation will return the amount of leading zeroes. So if %0 = [0,0,1,0], then %2 will be 2.
Then I create a vector with that result as the first element in %3 and use that to shuffle the input vector, the first element in the result will be the element at position 2.

Is this correct Ivan?

• This reply was modified 6 years, 10 months ago by  goldbug.
• This reply was modified 6 years, 10 months ago by  goldbug.
• This reply was modified 6 years, 10 months ago by  goldbug.