1 //**************************************************************************
3 //--------------------------------------------------------------------------
5 // This benchmark uses quicksort to sort an array of integers. The
6 // implementation is largely adapted from Numerical Recipes for C. The
7 // input data (and reference data) should be generated using the
8 // qsort_gendata.pl perl script and dumped to a file named
9 // dataset1.h The smips-gcc toolchain does not support system calls
10 // so printf's can only be used on a host system, not on the smips
11 // processor simulator itself. You should not change anything except
12 // the HOST_DEBUG and PREALLOCATE macros for your timing run.
16 // The INSERTION_THRESHOLD is the size of the subarray when the
17 // algorithm switches to using an insertion sort instead of
20 #define INSERTION_THRESHOLD 7
22 // NSTACK is the required auxiliary storage.
23 // It must be at least 2*lg(DATA_SIZE)
27 // Swap macro for swapping two values.
29 #define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
31 //--------------------------------------------------------------------------
32 // Input/Reference Data
36 //--------------------------------------------------------------------------
39 void sort( int n
, int arr
[] )
52 printArray( "", n
, arr
);
55 // Insertion sort when subarray small enough.
56 if ( ir
-l
< INSERTION_THRESHOLD
) {
58 for ( j
= l
+1; j
<= ir
; j
++ ) {
60 for ( i
= j
-1; i
>= l
; i
-- ) {
61 if ( arr
[i
-1] <= a
) break;
67 if ( jstack
== 0 ) break;
69 // Pop stack and begin a new round of partitioning.
70 ir
= istack
[jstack
--];
76 // Choose median of left, center, and right elements as
77 // partitioning element a. Also rearrange so that a[l-1] <= a[l] <= a[ir-].
81 if ( arr
[l
-1] > arr
[ir
-1] ) {
82 SWAP(arr
[l
-1],arr
[ir
-1])
84 if ( arr
[l
] > arr
[ir
-1] ) {
85 SWAP(arr
[l
],arr
[ir
-1])
87 if ( arr
[l
-1] > arr
[l
] ) {
91 // Initialize pointers for partitioning.
95 // Partitioning element.
98 for (;;) { // Beginning of innermost loop.
99 do i
++; while (arr
[i
-1] < a
); // Scan up to find element > a.
100 do j
--; while (arr
[j
-1] > a
); // Scan down to find element < a.
101 if (j
< i
) break; // Pointers crossed. Partitioning complete.
102 SWAP(arr
[i
-1],arr
[j
-1]); // Exchange elements.
103 } // End of innermost loop.
105 // Insert partitioning element.
110 // Push pointers to larger subarray on stack,
111 // process smaller subarray immediately.
114 if ( jstack
> NSTACK
) { printf("NSTACK too small in sort.\n"); exit(1); }
117 if ( ir
-i
+1 >= j
-l
) {
119 istack
[jstack
-1] = i
;
123 istack
[jstack
] = j
-1;
124 istack
[jstack
-1] = l
;
133 //--------------------------------------------------------------------------
136 int main( int argc
, char* argv
[] )
138 // Output the input array
139 printArray( "input", DATA_SIZE
, input_data
);
140 printArray( "verify", DATA_SIZE
, verify_data
);
143 // If needed we preallocate everything in the caches
144 sort( DATA_SIZE
, input_data
);
149 sort( DATA_SIZE
, input_data
);
152 // Print out the results
153 printArray( "test", DATA_SIZE
, input_data
);
156 return verify( DATA_SIZE
, input_data
, verify_data
);