1*04e0dc4aSTimo Kreuzer //
2*04e0dc4aSTimo Kreuzer // qsort.cpp
3*04e0dc4aSTimo Kreuzer //
4*04e0dc4aSTimo Kreuzer // Copyright (c) Microsoft Corporation. All rights reserved.
5*04e0dc4aSTimo Kreuzer //
6*04e0dc4aSTimo Kreuzer // Defines qsort(), a routine for sorting arrays.
7*04e0dc4aSTimo Kreuzer //
8*04e0dc4aSTimo Kreuzer #include <corecrt_internal.h>
9*04e0dc4aSTimo Kreuzer #include <search.h>
10*04e0dc4aSTimo Kreuzer
11*04e0dc4aSTimo Kreuzer
12*04e0dc4aSTimo Kreuzer /* Temporarily define optimization macros (to be removed by the build team: RsmqblCompiler alias) */
13*04e0dc4aSTimo Kreuzer #if !defined(BEGIN_PRAGMA_OPTIMIZE_DISABLE)
14*04e0dc4aSTimo Kreuzer #define BEGIN_PRAGMA_OPTIMIZE_DISABLE(flags, bug, reason) \
15*04e0dc4aSTimo Kreuzer __pragma(optimize(flags, off))
16*04e0dc4aSTimo Kreuzer #define BEGIN_PRAGMA_OPTIMIZE_ENABLE(flags, bug, reason) \
17*04e0dc4aSTimo Kreuzer __pragma(optimize(flags, on))
18*04e0dc4aSTimo Kreuzer #define END_PRAGMA_OPTIMIZE() \
19*04e0dc4aSTimo Kreuzer __pragma(optimize("", on))
20*04e0dc4aSTimo Kreuzer #endif
21*04e0dc4aSTimo Kreuzer
22*04e0dc4aSTimo Kreuzer
23*04e0dc4aSTimo Kreuzer // Always compile this module for speed, not size
24*04e0dc4aSTimo Kreuzer BEGIN_PRAGMA_OPTIMIZE_ENABLE("t", MSFT:4499497, "This file is performance-critical and should always be optimized for speed")
25*04e0dc4aSTimo Kreuzer
26*04e0dc4aSTimo Kreuzer
27*04e0dc4aSTimo Kreuzer
28*04e0dc4aSTimo Kreuzer #ifdef _M_CEE
29*04e0dc4aSTimo Kreuzer #define __fileDECL __clrcall
30*04e0dc4aSTimo Kreuzer #else
31*04e0dc4aSTimo Kreuzer #define __fileDECL __cdecl
32*04e0dc4aSTimo Kreuzer #endif
33*04e0dc4aSTimo Kreuzer
34*04e0dc4aSTimo Kreuzer
35*04e0dc4aSTimo Kreuzer
36*04e0dc4aSTimo Kreuzer #ifdef __USE_CONTEXT
37*04e0dc4aSTimo Kreuzer #define __COMPARE(context, p1, p2) comp(context, p1, p2)
38*04e0dc4aSTimo Kreuzer #define __SHORTSORT(lo, hi, width, comp, context) shortsort_s(lo, hi, width, comp, context);
39*04e0dc4aSTimo Kreuzer #else
40*04e0dc4aSTimo Kreuzer #define __COMPARE(context, p1, p2) comp(p1, p2)
41*04e0dc4aSTimo Kreuzer #define __SHORTSORT(lo, hi, width, comp, context) shortsort(lo, hi, width, comp);
42*04e0dc4aSTimo Kreuzer #endif
43*04e0dc4aSTimo Kreuzer
44*04e0dc4aSTimo Kreuzer
45*04e0dc4aSTimo Kreuzer
46*04e0dc4aSTimo Kreuzer // Swaps the objects of size 'width' that are pointed to by 'a' and 'b'
47*04e0dc4aSTimo Kreuzer #ifndef _QSORT_SWAP_DEFINED
48*04e0dc4aSTimo Kreuzer #define _QSORT_SWAP_DEFINED
49*04e0dc4aSTimo Kreuzer _CRT_SECURITYSAFECRITICAL_ATTRIBUTE
swap(_Inout_updates_ (width)char * a,_Inout_updates_ (width)char * b,size_t width)50*04e0dc4aSTimo Kreuzer static void __fileDECL swap(_Inout_updates_(width) char* a, _Inout_updates_(width) char* b, size_t width)
51*04e0dc4aSTimo Kreuzer {
52*04e0dc4aSTimo Kreuzer if (a != b)
53*04e0dc4aSTimo Kreuzer {
54*04e0dc4aSTimo Kreuzer // Do the swap one character at a time to avoid potential alignment
55*04e0dc4aSTimo Kreuzer // problems:
56*04e0dc4aSTimo Kreuzer while (width--)
57*04e0dc4aSTimo Kreuzer {
58*04e0dc4aSTimo Kreuzer char const tmp = *a;
59*04e0dc4aSTimo Kreuzer *a++ = *b;
60*04e0dc4aSTimo Kreuzer *b++ = tmp;
61*04e0dc4aSTimo Kreuzer }
62*04e0dc4aSTimo Kreuzer }
63*04e0dc4aSTimo Kreuzer }
64*04e0dc4aSTimo Kreuzer #endif // _QSORT_SWAP_DEFINED
65*04e0dc4aSTimo Kreuzer
66*04e0dc4aSTimo Kreuzer
67*04e0dc4aSTimo Kreuzer
68*04e0dc4aSTimo Kreuzer // An insertion sort for sorting short arrays. Sorts the sub-array of elements
69*04e0dc4aSTimo Kreuzer // between lo and hi (inclusive). Assumes lo < hi. lo and hi are pointers to
70*04e0dc4aSTimo Kreuzer // the first and last elements in the range to be sorted (note: hi does not
71*04e0dc4aSTimo Kreuzer // point one-past-the-end). The comp is a comparer with the same behavior as
72*04e0dc4aSTimo Kreuzer // specified for qsort.
73*04e0dc4aSTimo Kreuzer _CRT_SECURITYSAFECRITICAL_ATTRIBUTE
74*04e0dc4aSTimo Kreuzer #ifdef __USE_CONTEXT
75*04e0dc4aSTimo Kreuzer static void __fileDECL shortsort_s(
76*04e0dc4aSTimo Kreuzer _Inout_updates_(hi - lo + 1) char* lo,
77*04e0dc4aSTimo Kreuzer _Inout_updates_(width) char* hi,
78*04e0dc4aSTimo Kreuzer size_t const width,
79*04e0dc4aSTimo Kreuzer int (__fileDECL* comp)(void*, void const*, void const*),
80*04e0dc4aSTimo Kreuzer void* const context
81*04e0dc4aSTimo Kreuzer )
82*04e0dc4aSTimo Kreuzer #else // __USE_CONTEXT
83*04e0dc4aSTimo Kreuzer static void __fileDECL shortsort(
84*04e0dc4aSTimo Kreuzer _Inout_updates_(hi - lo + 1) char* lo,
85*04e0dc4aSTimo Kreuzer _Inout_updates_(width) char* hi,
86*04e0dc4aSTimo Kreuzer size_t const width,
87*04e0dc4aSTimo Kreuzer int (__fileDECL* comp)(void const*, void const*)
88*04e0dc4aSTimo Kreuzer )
89*04e0dc4aSTimo Kreuzer #endif // __USE_CONTEXT
90*04e0dc4aSTimo Kreuzer {
91*04e0dc4aSTimo Kreuzer // Note: in assertions below, i and j are alway inside original bound of
92*04e0dc4aSTimo Kreuzer // array to sort.
93*04e0dc4aSTimo Kreuzer
94*04e0dc4aSTimo Kreuzer // Reentrancy diligence: Save (and unset) global-state mode to the stack before making callout to 'compare'
95*04e0dc4aSTimo Kreuzer __crt_state_management::scoped_global_state_reset saved_state;
96*04e0dc4aSTimo Kreuzer
97*04e0dc4aSTimo Kreuzer while (hi > lo)
98*04e0dc4aSTimo Kreuzer {
99*04e0dc4aSTimo Kreuzer // A[i] <= A[j] for i <= j, j > hi
100*04e0dc4aSTimo Kreuzer char* max = lo;
101*04e0dc4aSTimo Kreuzer for (char* p = lo+width; p <= hi; p += width)
102*04e0dc4aSTimo Kreuzer {
103*04e0dc4aSTimo Kreuzer // A[i] <= A[max] for lo <= i < p
104*04e0dc4aSTimo Kreuzer if (__COMPARE(context, p, max) > 0)
105*04e0dc4aSTimo Kreuzer {
106*04e0dc4aSTimo Kreuzer max = p;
107*04e0dc4aSTimo Kreuzer }
108*04e0dc4aSTimo Kreuzer // A[i] <= A[max] for lo <= i <= p
109*04e0dc4aSTimo Kreuzer }
110*04e0dc4aSTimo Kreuzer
111*04e0dc4aSTimo Kreuzer // A[i] <= A[max] for lo <= i <= hi
112*04e0dc4aSTimo Kreuzer
113*04e0dc4aSTimo Kreuzer swap(max, hi, width);
114*04e0dc4aSTimo Kreuzer
115*04e0dc4aSTimo Kreuzer // A[i] <= A[hi] for i <= hi, so A[i] <= A[j] for i <= j, j >= hi
116*04e0dc4aSTimo Kreuzer
117*04e0dc4aSTimo Kreuzer hi -= width;
118*04e0dc4aSTimo Kreuzer
119*04e0dc4aSTimo Kreuzer // A[i] <= A[j] for i <= j, j > hi, loop top condition established
120*04e0dc4aSTimo Kreuzer }
121*04e0dc4aSTimo Kreuzer
122*04e0dc4aSTimo Kreuzer // A[i] <= A[j] for i <= j, j > lo, which implies A[i] <= A[j] for i < j,
123*04e0dc4aSTimo Kreuzer // so array is sorted
124*04e0dc4aSTimo Kreuzer }
125*04e0dc4aSTimo Kreuzer
126*04e0dc4aSTimo Kreuzer
127*04e0dc4aSTimo Kreuzer
128*04e0dc4aSTimo Kreuzer // This macro defines the cutoff between using QuickSort and insertion sort for
129*04e0dc4aSTimo Kreuzer // arrays; arrays with lengths shorter or equal to the below value use insertion
130*04e0dc4aSTimo Kreuzer // sort.
131*04e0dc4aSTimo Kreuzer #define CUTOFF 8 // Testing shows that this is a good value.
132*04e0dc4aSTimo Kreuzer
133*04e0dc4aSTimo Kreuzer // Note: The theoretical number of stack entries required is no more than 1 +
134*04e0dc4aSTimo Kreuzer // log2(num). But we switch to insertion sort for CUTOFF elements or less, so
135*04e0dc4aSTimo Kreuzer // we really only need 1 + log2(num) - log(CUTOFF) stack entries. For a CUTOFF
136*04e0dc4aSTimo Kreuzer // of 8, that means we need no more than 30 stack entries for 32-bit platforms
137*04e0dc4aSTimo Kreuzer // and 62 for 64-bit platforms.
138*04e0dc4aSTimo Kreuzer #define STKSIZ (8 * sizeof(void*) - 2)
139*04e0dc4aSTimo Kreuzer
140*04e0dc4aSTimo Kreuzer
141*04e0dc4aSTimo Kreuzer
142*04e0dc4aSTimo Kreuzer // QuickSort function for sorting arrays. The array is sorted in place.
143*04e0dc4aSTimo Kreuzer // Parameters:
144*04e0dc4aSTimo Kreuzer // * base: Pointer to the initial element of the array
145*04e0dc4aSTimo Kreuzer // * num: Number of elements in the array
146*04e0dc4aSTimo Kreuzer // * width: Width of each element in the array, in bytes
147*04e0dc4aSTimo Kreuzer // * comp: Pointer to a function returning analog of strcmp for strings, but
148*04e0dc4aSTimo Kreuzer // supplied by the caller for comparing the array elements. It
149*04e0dc4aSTimo Kreuzer // accepts two pointers to elements; returns negative if 1 < 2;
150*04e0dc4aSTimo Kreuzer // zero if 1 == 2, and positive if 1 > 2.
151*04e0dc4aSTimo Kreuzer #ifndef _M_CEE
152*04e0dc4aSTimo Kreuzer extern "C"
153*04e0dc4aSTimo Kreuzer #endif
154*04e0dc4aSTimo Kreuzer _CRT_SECURITYSAFECRITICAL_ATTRIBUTE
155*04e0dc4aSTimo Kreuzer #ifdef __USE_CONTEXT
qsort_s(void * const base,size_t const num,size_t const width,int (__fileDECL * const comp)(void *,void const *,void const *),void * const context)156*04e0dc4aSTimo Kreuzer void __fileDECL qsort_s(
157*04e0dc4aSTimo Kreuzer void* const base,
158*04e0dc4aSTimo Kreuzer size_t const num,
159*04e0dc4aSTimo Kreuzer size_t const width,
160*04e0dc4aSTimo Kreuzer int (__fileDECL* const comp)(void*, void const*, void const*),
161*04e0dc4aSTimo Kreuzer void* const context
162*04e0dc4aSTimo Kreuzer )
163*04e0dc4aSTimo Kreuzer #else // __USE_CONTEXT
164*04e0dc4aSTimo Kreuzer void __fileDECL qsort(
165*04e0dc4aSTimo Kreuzer void* const base,
166*04e0dc4aSTimo Kreuzer size_t const num,
167*04e0dc4aSTimo Kreuzer size_t const width,
168*04e0dc4aSTimo Kreuzer int (__fileDECL* const comp)(void const*, void const*)
169*04e0dc4aSTimo Kreuzer )
170*04e0dc4aSTimo Kreuzer #endif // __USE_CONTEXT
171*04e0dc4aSTimo Kreuzer {
172*04e0dc4aSTimo Kreuzer _VALIDATE_RETURN_VOID(base != nullptr || num == 0, EINVAL);
173*04e0dc4aSTimo Kreuzer _VALIDATE_RETURN_VOID(width > 0, EINVAL);
174*04e0dc4aSTimo Kreuzer _VALIDATE_RETURN_VOID(comp != nullptr, EINVAL);
175*04e0dc4aSTimo Kreuzer
176*04e0dc4aSTimo Kreuzer // A stack for saving the sub-arrays yet to be processed:
177*04e0dc4aSTimo Kreuzer char* lostk[STKSIZ];
178*04e0dc4aSTimo Kreuzer char* histk[STKSIZ];
179*04e0dc4aSTimo Kreuzer int stkptr = 0;
180*04e0dc4aSTimo Kreuzer
181*04e0dc4aSTimo Kreuzer if (num < 2)
182*04e0dc4aSTimo Kreuzer return; // Nothing to do:
183*04e0dc4aSTimo Kreuzer
184*04e0dc4aSTimo Kreuzer // The ends of the sub-array currently being sorted (note that 'hi' points
185*04e0dc4aSTimo Kreuzer // to the last element, not one-past-the-end):
186*04e0dc4aSTimo Kreuzer char* lo = static_cast<char*>(base);
187*04e0dc4aSTimo Kreuzer char* hi = static_cast<char*>(base) + width * (num-1);
188*04e0dc4aSTimo Kreuzer
189*04e0dc4aSTimo Kreuzer // This entry point is for pseudo-recursion calling: setting
190*04e0dc4aSTimo Kreuzer // lo and hi and jumping to here is like recursion, but stkptr is
191*04e0dc4aSTimo Kreuzer // preserved, locals aren't, so we preserve stuff on the stack.
192*04e0dc4aSTimo Kreuzer recurse:
193*04e0dc4aSTimo Kreuzer
194*04e0dc4aSTimo Kreuzer // The number of elements in the sub-array currently being sorted:
195*04e0dc4aSTimo Kreuzer size_t const size = (hi - lo) / width + 1;
196*04e0dc4aSTimo Kreuzer
197*04e0dc4aSTimo Kreuzer // Below a certain size, it is faster to use a O(n^2) sorting method:
198*04e0dc4aSTimo Kreuzer if (size <= CUTOFF)
199*04e0dc4aSTimo Kreuzer {
200*04e0dc4aSTimo Kreuzer __SHORTSORT(lo, hi, width, comp, context);
201*04e0dc4aSTimo Kreuzer }
202*04e0dc4aSTimo Kreuzer else
203*04e0dc4aSTimo Kreuzer {
204*04e0dc4aSTimo Kreuzer // First we pick a partitioning element. The efficiency of the
205*04e0dc4aSTimo Kreuzer // algorithm demands that we find one that is approximately the median
206*04e0dc4aSTimo Kreuzer // of the values, but also that we select one fast. We choose the
207*04e0dc4aSTimo Kreuzer // median of the first, middle, and last elements, to avoid bad
208*04e0dc4aSTimo Kreuzer // performance in the face of already sorted data, or data that is made
209*04e0dc4aSTimo Kreuzer // up of multiple sorted runs appended together. Testing shows that a
210*04e0dc4aSTimo Kreuzer // median-of-three algorithm provides better performance than simply
211*04e0dc4aSTimo Kreuzer // picking the middle element for the latter case.
212*04e0dc4aSTimo Kreuzer
213*04e0dc4aSTimo Kreuzer // Find the middle element:
214*04e0dc4aSTimo Kreuzer char* mid = lo + (size / 2) * width;
215*04e0dc4aSTimo Kreuzer
216*04e0dc4aSTimo Kreuzer // Sort the first, middle, last elements into order:
217*04e0dc4aSTimo Kreuzer if (__COMPARE(context, lo, mid) > 0)
218*04e0dc4aSTimo Kreuzer swap(lo, mid, width);
219*04e0dc4aSTimo Kreuzer
220*04e0dc4aSTimo Kreuzer if (__COMPARE(context, lo, hi) > 0)
221*04e0dc4aSTimo Kreuzer swap(lo, hi, width);
222*04e0dc4aSTimo Kreuzer
223*04e0dc4aSTimo Kreuzer if (__COMPARE(context, mid, hi) > 0)
224*04e0dc4aSTimo Kreuzer swap(mid, hi, width);
225*04e0dc4aSTimo Kreuzer
226*04e0dc4aSTimo Kreuzer // We now wish to partition the array into three pieces, one consisting
227*04e0dc4aSTimo Kreuzer // of elements <= partition element, one of elements equal to the
228*04e0dc4aSTimo Kreuzer // partition element, and one of elements > than it. This is done
229*04e0dc4aSTimo Kreuzer // below; comments indicate conditions established at every step.
230*04e0dc4aSTimo Kreuzer
231*04e0dc4aSTimo Kreuzer char* loguy = lo;
232*04e0dc4aSTimo Kreuzer char* higuy = hi;
233*04e0dc4aSTimo Kreuzer
234*04e0dc4aSTimo Kreuzer // Note that higuy decreases and loguy increases on every iteration,
235*04e0dc4aSTimo Kreuzer // so loop must terminate.
236*04e0dc4aSTimo Kreuzer for (;;)
237*04e0dc4aSTimo Kreuzer {
238*04e0dc4aSTimo Kreuzer // lo <= loguy < hi, lo < higuy <= hi,
239*04e0dc4aSTimo Kreuzer // A[i] <= A[mid] for lo <= i <= loguy,
240*04e0dc4aSTimo Kreuzer // A[i] > A[mid] for higuy <= i < hi,
241*04e0dc4aSTimo Kreuzer // A[hi] >= A[mid]
242*04e0dc4aSTimo Kreuzer
243*04e0dc4aSTimo Kreuzer // The doubled loop is to avoid calling comp(mid,mid), since some
244*04e0dc4aSTimo Kreuzer // existing comparison funcs don't work when passed the same
245*04e0dc4aSTimo Kreuzer // value for both pointers.
246*04e0dc4aSTimo Kreuzer
247*04e0dc4aSTimo Kreuzer if (mid > loguy)
248*04e0dc4aSTimo Kreuzer {
249*04e0dc4aSTimo Kreuzer do
250*04e0dc4aSTimo Kreuzer {
251*04e0dc4aSTimo Kreuzer loguy += width;
252*04e0dc4aSTimo Kreuzer }
253*04e0dc4aSTimo Kreuzer while (loguy < mid && __COMPARE(context, loguy, mid) <= 0);
254*04e0dc4aSTimo Kreuzer }
255*04e0dc4aSTimo Kreuzer if (mid <= loguy)
256*04e0dc4aSTimo Kreuzer {
257*04e0dc4aSTimo Kreuzer do
258*04e0dc4aSTimo Kreuzer {
259*04e0dc4aSTimo Kreuzer loguy += width;
260*04e0dc4aSTimo Kreuzer }
261*04e0dc4aSTimo Kreuzer while (loguy <= hi && __COMPARE(context, loguy, mid) <= 0);
262*04e0dc4aSTimo Kreuzer }
263*04e0dc4aSTimo Kreuzer
264*04e0dc4aSTimo Kreuzer // lo < loguy <= hi+1, A[i] <= A[mid] for lo <= i < loguy,
265*04e0dc4aSTimo Kreuzer // either loguy > hi or A[loguy] > A[mid]
266*04e0dc4aSTimo Kreuzer
267*04e0dc4aSTimo Kreuzer do
268*04e0dc4aSTimo Kreuzer {
269*04e0dc4aSTimo Kreuzer higuy -= width;
270*04e0dc4aSTimo Kreuzer }
271*04e0dc4aSTimo Kreuzer while (higuy > mid && __COMPARE(context, higuy, mid) > 0);
272*04e0dc4aSTimo Kreuzer
273*04e0dc4aSTimo Kreuzer // lo <= higuy < hi, A[i] > A[mid] for higuy < i < hi,
274*04e0dc4aSTimo Kreuzer // either higuy == lo or A[higuy] <= A[mid]
275*04e0dc4aSTimo Kreuzer
276*04e0dc4aSTimo Kreuzer if (higuy < loguy)
277*04e0dc4aSTimo Kreuzer break;
278*04e0dc4aSTimo Kreuzer
279*04e0dc4aSTimo Kreuzer // if loguy > hi or higuy == lo, then we would have exited, so
280*04e0dc4aSTimo Kreuzer // A[loguy] > A[mid], A[higuy] <= A[mid],
281*04e0dc4aSTimo Kreuzer // loguy <= hi, higuy > lo
282*04e0dc4aSTimo Kreuzer
283*04e0dc4aSTimo Kreuzer swap(loguy, higuy, width);
284*04e0dc4aSTimo Kreuzer
285*04e0dc4aSTimo Kreuzer // If the partition element was moved, follow it. Only need
286*04e0dc4aSTimo Kreuzer // to check for mid == higuy, since before the swap,
287*04e0dc4aSTimo Kreuzer // A[loguy] > A[mid] implies loguy != mid.
288*04e0dc4aSTimo Kreuzer
289*04e0dc4aSTimo Kreuzer if (mid == higuy)
290*04e0dc4aSTimo Kreuzer mid = loguy;
291*04e0dc4aSTimo Kreuzer
292*04e0dc4aSTimo Kreuzer // A[loguy] <= A[mid], A[higuy] > A[mid]; so condition at top
293*04e0dc4aSTimo Kreuzer // of loop is re-established
294*04e0dc4aSTimo Kreuzer }
295*04e0dc4aSTimo Kreuzer
296*04e0dc4aSTimo Kreuzer // A[i] <= A[mid] for lo <= i < loguy,
297*04e0dc4aSTimo Kreuzer // A[i] > A[mid] for higuy < i < hi,
298*04e0dc4aSTimo Kreuzer // A[hi] >= A[mid]
299*04e0dc4aSTimo Kreuzer // higuy < loguy
300*04e0dc4aSTimo Kreuzer // implying:
301*04e0dc4aSTimo Kreuzer // higuy == loguy-1
302*04e0dc4aSTimo Kreuzer // or higuy == hi - 1, loguy == hi + 1, A[hi] == A[mid]
303*04e0dc4aSTimo Kreuzer
304*04e0dc4aSTimo Kreuzer // Find adjacent elements equal to the partition element. The
305*04e0dc4aSTimo Kreuzer // doubled loop is to avoid calling comp(mid,mid), since some
306*04e0dc4aSTimo Kreuzer // existing comparison funcs don't work when passed the same value
307*04e0dc4aSTimo Kreuzer // for both pointers.
308*04e0dc4aSTimo Kreuzer
309*04e0dc4aSTimo Kreuzer higuy += width;
310*04e0dc4aSTimo Kreuzer if (mid < higuy)
311*04e0dc4aSTimo Kreuzer {
312*04e0dc4aSTimo Kreuzer do
313*04e0dc4aSTimo Kreuzer {
314*04e0dc4aSTimo Kreuzer higuy -= width;
315*04e0dc4aSTimo Kreuzer }
316*04e0dc4aSTimo Kreuzer while (higuy > mid && __COMPARE(context, higuy, mid) == 0);
317*04e0dc4aSTimo Kreuzer }
318*04e0dc4aSTimo Kreuzer if (mid >= higuy)
319*04e0dc4aSTimo Kreuzer {
320*04e0dc4aSTimo Kreuzer do
321*04e0dc4aSTimo Kreuzer {
322*04e0dc4aSTimo Kreuzer higuy -= width;
323*04e0dc4aSTimo Kreuzer }
324*04e0dc4aSTimo Kreuzer while (higuy > lo && __COMPARE(context, higuy, mid) == 0);
325*04e0dc4aSTimo Kreuzer }
326*04e0dc4aSTimo Kreuzer
327*04e0dc4aSTimo Kreuzer // OK, now we have the following:
328*04e0dc4aSTimo Kreuzer // higuy < loguy
329*04e0dc4aSTimo Kreuzer // lo <= higuy <= hi
330*04e0dc4aSTimo Kreuzer // A[i] <= A[mid] for lo <= i <= higuy
331*04e0dc4aSTimo Kreuzer // A[i] == A[mid] for higuy < i < loguy
332*04e0dc4aSTimo Kreuzer // A[i] > A[mid] for loguy <= i < hi
333*04e0dc4aSTimo Kreuzer // A[hi] >= A[mid] */
334*04e0dc4aSTimo Kreuzer
335*04e0dc4aSTimo Kreuzer // We've finished the partition, now we want to sort the subarrays
336*04e0dc4aSTimo Kreuzer // [lo, higuy] and [loguy, hi].
337*04e0dc4aSTimo Kreuzer // We do the smaller one first to minimize stack usage.
338*04e0dc4aSTimo Kreuzer // We only sort arrays of length 2 or more.
339*04e0dc4aSTimo Kreuzer
340*04e0dc4aSTimo Kreuzer if (higuy - lo >= hi - loguy)
341*04e0dc4aSTimo Kreuzer {
342*04e0dc4aSTimo Kreuzer if (lo < higuy)
343*04e0dc4aSTimo Kreuzer {
344*04e0dc4aSTimo Kreuzer // Save the big recursion for later:
345*04e0dc4aSTimo Kreuzer lostk[stkptr] = lo;
346*04e0dc4aSTimo Kreuzer histk[stkptr] = higuy;
347*04e0dc4aSTimo Kreuzer ++stkptr;
348*04e0dc4aSTimo Kreuzer }
349*04e0dc4aSTimo Kreuzer
350*04e0dc4aSTimo Kreuzer if (loguy < hi)
351*04e0dc4aSTimo Kreuzer {
352*04e0dc4aSTimo Kreuzer // Do the small recursion:
353*04e0dc4aSTimo Kreuzer lo = loguy;
354*04e0dc4aSTimo Kreuzer goto recurse;
355*04e0dc4aSTimo Kreuzer }
356*04e0dc4aSTimo Kreuzer }
357*04e0dc4aSTimo Kreuzer else
358*04e0dc4aSTimo Kreuzer {
359*04e0dc4aSTimo Kreuzer if (loguy < hi)
360*04e0dc4aSTimo Kreuzer {
361*04e0dc4aSTimo Kreuzer // Save the big recursion for later:
362*04e0dc4aSTimo Kreuzer lostk[stkptr] = loguy;
363*04e0dc4aSTimo Kreuzer histk[stkptr] = hi;
364*04e0dc4aSTimo Kreuzer ++stkptr;
365*04e0dc4aSTimo Kreuzer }
366*04e0dc4aSTimo Kreuzer
367*04e0dc4aSTimo Kreuzer if (lo < higuy)
368*04e0dc4aSTimo Kreuzer {
369*04e0dc4aSTimo Kreuzer // Do the small recursion:
370*04e0dc4aSTimo Kreuzer hi = higuy;
371*04e0dc4aSTimo Kreuzer goto recurse;
372*04e0dc4aSTimo Kreuzer }
373*04e0dc4aSTimo Kreuzer }
374*04e0dc4aSTimo Kreuzer }
375*04e0dc4aSTimo Kreuzer
376*04e0dc4aSTimo Kreuzer // We have sorted the array, except for any pending sorts on the stack.
377*04e0dc4aSTimo Kreuzer // Check if there are any, and sort them:
378*04e0dc4aSTimo Kreuzer --stkptr;
379*04e0dc4aSTimo Kreuzer if (stkptr >= 0)
380*04e0dc4aSTimo Kreuzer {
381*04e0dc4aSTimo Kreuzer // Pop sub-array from the stack:
382*04e0dc4aSTimo Kreuzer lo = lostk[stkptr];
383*04e0dc4aSTimo Kreuzer hi = histk[stkptr];
384*04e0dc4aSTimo Kreuzer goto recurse;
385*04e0dc4aSTimo Kreuzer }
386*04e0dc4aSTimo Kreuzer else
387*04e0dc4aSTimo Kreuzer {
388*04e0dc4aSTimo Kreuzer // Otherwise, all sub-arrays have been sorted:
389*04e0dc4aSTimo Kreuzer return;
390*04e0dc4aSTimo Kreuzer }
391*04e0dc4aSTimo Kreuzer }
392*04e0dc4aSTimo Kreuzer
393*04e0dc4aSTimo Kreuzer #undef __COMPARE
394*04e0dc4aSTimo Kreuzer #undef __SHORTSORT
395*04e0dc4aSTimo Kreuzer
396*04e0dc4aSTimo Kreuzer END_PRAGMA_OPTIMIZE()
397