VHDL Bitonic Sort Network Generator
library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; package bitonic_sort_pkg is -- Generic for the number of elements to sort (must be a power of 2). -- For example, N=8 means sorting 8 elements. constant N : integer := 8; -- Type for the data elements being sort...
This VHDL package defines a parameterized bitonic sort network generator. A bitonic sort network is a parallel sorting algorithm that uses a fixed number of comparators arranged in stages to sort data. It's particularly...
The bitonic sort algorithm works by first creating a bitonic sequence from the input data and then repeatedly merging these bitonic sequences until the entire array is sorted. A bitonic sequence is a sequence that is either monotonically increasing or decreasing, or it can be split into two halves, one bitonic increasing and the other bitonic decreasing, such that the elements in the first half are all less than or equal to the elements in the second half. The `generate_bitonic_sequence` procedure recursively builds such a sequence. It splits the input into two halves, recursively sorts them into bitonic sequences (one ascending, one descending), and then merges them. The `generate_bitonic_merge` procedure takes two bitonic sequences and merges them into a single sorted sequence by applying compare-and-swap operations. The `generate_bitonic_sort_network` procedure orchestrates the entire process, first calling `generate_bitonic_sequence` and then iteratively calling `generate_bitonic_merge` for increasing merge sizes until the entire array is sorted. The time complexity of a bitonic sort network for N elements is O(N log^2 N), and its space complexity is O(N log N) for the network structure itself. Edge cases include the base case of N=2, which is handled directly by a compare-swap. The recursive structure ensures correctness by maintaining the bitonic property at each step and progressively reducing the disorder until full sorting is achieved.
PACKAGE bitonic_sort_pkg IS CONSTANT N : integer -- Number of elements (power of 2) TYPE data_t IS UNSIGNED(31 DOWNTO 0) TYPE data_array_t IS ARRAY(0 TO N-1) OF data_t PROCEDURE generate_bitonic_sort_network(input_data :...