A MLNS is a
For small values of
Alternatively, an MLNS can also be used as the foundational building block of a
Pairwise comparison of each distinct pair of input elements. This should result in
⋯ |
|
|||||||
⋯ |
|
|||||||
⋯ |
|
|||||||
⋯ |
|
|||||||
⋯ |
|
|||||||
︙ | ︙ | ︙ | ︙ | ︙ | ⋱ | |||
|
||||||||
|
||||||||
For example:
- for
$N=3$ and inputsi0
,i1
, andi2
, this will result in 3 comparisons of(i0,i1)
,(i0,i2)
,(i1,i2)
. - for
$N=4$ and inputsi0
,i1
,i2
, andi3
, this will result in 6 comparisons of(i0,i1)
,(i0,i2)
,(i0,i3)
,(i1,i2)
,(i1,i3)
,(i2,i3)
.
The
In Verilog, the
/* C comparisons in parallel */
wire c0 = i0 < i1;
wire c1 = i0 < i2;
wire c2 = i1 < i2;
In this stage, we map the results of the comparisons performed in the previous stage to an input ordering so that the outputs are in the desired, sorted, order.
This means mapping
- distinct cases can map to the same permuation
- cases may be impossible (no set of input values can yield that set of comparison results)
- all
$N!$ ordering must be mapped
For the case
case ({c0, c1, c2})
3'b111: out = '{i0,i1,i2}; // i0<i1, i0<i2, i1<i2 -> i0 < i1 < i2
3'b110: out = '{i0,i2,i1}; // i0<i1, i0<i2, i1>=i2 -> i0 < i2 <= i1
3'b101: out = '{X, X, X }; // i0<i1, i0>=i2, i1<i2 -> i2 <= i0 < i1 < i2 -> NC
3'b100: out = '{i2,i0,i1}; // i0<i1, i0>=i2, i1>=i2 -> i2 <= i0 < i1
3'b011: out = '{i1,i0,i2}; // i0>=i1, i0<i2, i1<i2 -> i1 <= i0 < i2
3'b010: out = '{X, X, X }; // i0>=i1, i0<i2, i1>=i2 -> i1 <= i0 < i2 <= i1 -> NC
3'b001: out = '{i1,i2,i0}; // i0>=i1, i0>=i2, i1<i2 -> i1 < i2 <= i0
3'b000: out = '{i2,i1,i0}; // i0>=i1, i0>=i2, i1>=i2 -> i2 <= i1 <= i0
endcase
Obviously, for larger values of
- omit all NC cases, and simply have a
default
case that returnsout = '{X,X,X}
- merge (using comma-separated lists and don't care bits as needed) all cases that yield the same ordering
So for example,
case ({c0, c1, c2, c3, c4, c5})
6'b111111: out = '{i0,i1,i2,i3,i4,i5};
...
endcase
MLNS size1 |
MLNS depth | MLNS cases |
MLNS illegal cases | MLNS valid cases | Possible orderings |
SN2 depth | SN2 size1 | |
---|---|---|---|---|---|---|---|---|
23 | 1 | 1 | 2 | 0 | 2 | 2 | 1 | 1 |
3 | 3 | 1 | 8 | 2 | 6 | 6 | 3 | 3 |
4 | 6 | 1 | 64 | ? | ? | 24 | 3 | 5 |
5 | 10 | 1 | 1K | ? | ? | 120 | 5 | 9 |
6 | 15 | 1 | 32K | ? | ? | 720 | 5 | 12 |
7 | 21 | 1 | 2M | ? | ? | 5040 | 6 | 16 |
8 | 28 | 1 | 256M | ? | ? | 40320 | 6 | 19 |
While the number of MLNS valid cases, especially for higher values of
- In the example above we only handle unsigned integers, but it extends naturally to signed integers and floating point inputs by replacing the
$C$ comparisons with comparators that can handle multiple formats. - In the examples above we only handle ascending order, but it would be trivial to extend them to handle both ascending and descending orders.