Commit 014956ac1dda5f0baf45eb8c2a79e219308e9ed2

Ryan C. Gordon 2016-02-15T03:16:46

Replaced SDL_qsort with public domain code from PDCLib: http://pdclib.e43.eu/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
diff --git a/debian/copyright b/debian/copyright
index 8ce26d1..99c3d44 100644
--- a/debian/copyright
+++ b/debian/copyright
@@ -31,10 +31,6 @@ Copyright: 1995 Erik Corry
            1995 Brown University
 License: BrownUn_UnCalifornia_ErikCorry
 
-Files: src/stdlib/SDL_qsort.c
-Copyright: 1998 Gareth McCaughan
-License: Gareth_McCaughan
-
 Files: src/test/SDL_test_md5.c
 Copyright: 1997-2016 Sam Lantinga <slouken@libsdl.org>
            1990 RSA Data Security, Inc.
@@ -270,13 +266,6 @@ License: BrownUn_UnCalifornia_ErikCorry
   * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
   */
 
-License: Gareth_McCaughan
-  You may use it in anything you like; you may make money
-  out of it; you may distribute it in object form or as
-  part of an executable without including source code;
-  you don't have to credit me. (But it would be nice if
-  you did.)
-
 License: Johnson_M._Hart
   Permission is granted for any and all use providing that this
   copyright is properly acknowledged.
diff --git a/src/stdlib/SDL_qsort.c b/src/stdlib/SDL_qsort.c
index 0d19784..b0a662f 100644
--- a/src/stdlib/SDL_qsort.c
+++ b/src/stdlib/SDL_qsort.c
@@ -1,58 +1,5 @@
-/* qsort.c
- * (c) 1998 Gareth McCaughan
- *
- * This is a drop-in replacement for the C library's |qsort()| routine.
- *
- * Features:
- *   - Median-of-three pivoting (and more)
- *   - Truncation and final polishing by a single insertion sort
- *   - Early truncation when no swaps needed in pivoting step
- *   - Explicit recursion, guaranteed not to overflow
- *   - A few little wrinkles stolen from the GNU |qsort()|.
- *   - separate code for non-aligned / aligned / word-size objects
- *
- * This code may be reproduced freely provided
- *   - this file is retained unaltered apart from minor
- *     changes for portability and efficiency
- *   - no changes are made to this comment
- *   - any changes that *are* made are clearly flagged
- *   - the _ID string below is altered by inserting, after
- *     the date, the string " altered" followed at your option
- *     by other material. (Exceptions: you may change the name
- *     of the exported routine without changing the ID string.
- *     You may change the values of the macros TRUNC_* and
- *     PIVOT_THRESHOLD without changing the ID string, provided
- *     they remain constants with TRUNC_nonaligned, TRUNC_aligned
- *     and TRUNC_words/WORD_BYTES between 8 and 24, and
- *     PIVOT_THRESHOLD between 32 and 200.)
- *
- * You may use it in anything you like; you may make money
- * out of it; you may distribute it in object form or as
- * part of an executable without including source code;
- * you don't have to credit me. (But it would be nice if
- * you did.)
- *
- * If you find problems with this code, or find ways of
- * making it significantly faster, please let me know!
- * My e-mail address, valid as of early 1998 and certainly
- * OK for at least the next 18 months, is
- *    gjm11@dpmms.cam.ac.uk
- * Thanks!
- *
- * Gareth McCaughan   Peterhouse   Cambridge   1998
- */
-
-#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
-#define SDL_DISABLE_ANALYZE_MACROS 1
-#endif
-
 #include "../SDL_internal.h"
 
-/*
-#include <assert.h>
-#include <stdlib.h>
-#include <string.h>
-*/
 #include "SDL_stdinc.h"
 #include "SDL_assert.h"
 
@@ -64,418 +11,203 @@ SDL_qsort(void *base, size_t nmemb, size_t size, int (*compare) (const void *, c
 }
 #else
 
-#ifdef assert
-#undef assert
-#endif
-#define assert(X) SDL_assert(X)
-#ifdef malloc
-#undef malloc
-#endif
-#define malloc	SDL_malloc
-#ifdef free
-#undef free
-#endif
-#define free	SDL_free
-#ifdef memcpy
-#undef memcpy
+#ifdef REGTEST
+#undef REGTEST
 #endif
-#define memcpy	SDL_memcpy
-#ifdef memmove
-#undef memmove
-#endif
-#define memmove	SDL_memmove
-#ifdef qsort
-#undef qsort
-#endif
-#define qsort	SDL_qsort
-
-static const char _ID[] = "<qsort.c gjm 1.12 1998-03-19>";
-
-/* How many bytes are there per word? (Must be a power of 2,
- * and must in fact equal sizeof(int).)
- */
-#define WORD_BYTES sizeof(int)
-
-/* How big does our stack need to be? Answer: one entry per
- * bit in a |size_t|.
- */
-#define STACK_SIZE (8*sizeof(size_t))
 
-/* Different situations have slightly different requirements,
- * and we make life epsilon easier by using different truncation
- * points for the three different cases.
- * So far, I have tuned TRUNC_words and guessed that the same
- * value might work well for the other two cases. Of course
- * what works well on my machine might work badly on yours.
- */
-#define TRUNC_nonaligned	12
-#define TRUNC_aligned		12
-#define TRUNC_words		12*WORD_BYTES   /* nb different meaning */
+#ifdef TEST
+#undef TEST
+#endif
 
-/* We use a simple pivoting algorithm for shortish sub-arrays
- * and a more complicated one for larger ones. The threshold
- * is PIVOT_THRESHOLD.
- */
-#define PIVOT_THRESHOLD 40
+#ifndef _PDCLIB_memswp
+#define _PDCLIB_memswp( i, j, size ) char tmp; do { tmp = *i; *i++ = *j; *j++ = tmp; } while ( --size );
+#endif
 
-typedef struct
-{
-    char *first;
-    char *last;
-} stack_entry;
-#define pushLeft {stack[stacktop].first=ffirst;stack[stacktop++].last=last;}
-#define pushRight {stack[stacktop].first=first;stack[stacktop++].last=llast;}
-#define doLeft {first=ffirst;llast=last;continue;}
-#define doRight {ffirst=first;last=llast;continue;}
-#define pop {if (--stacktop<0) break;\
-  first=ffirst=stack[stacktop].first;\
-  last=llast=stack[stacktop].last;\
-  continue;}
+#ifndef _PDCLIB_size_t
+#define _PDCLIB_size_t size_t
+#endif
 
-/* Some comments on the implementation.
- * 1. When we finish partitioning the array into "low"
- *    and "high", we forget entirely about short subarrays,
- *    because they'll be done later by insertion sort.
- *    Doing lots of little insertion sorts might be a win
- *    on large datasets for locality-of-reference reasons,
- *    but it makes the code much nastier and increases
- *    bookkeeping overhead.
- * 2. We always save the shorter and get to work on the
- *    longer. This guarantees that every time we push
- *    an item onto the stack its size is <= 1/2 of that
- *    of its parent; so the stack can't need more than
- *    log_2(max-array-size) entries.
- * 3. We choose a pivot by looking at the first, last
- *    and middle elements. We arrange them into order
- *    because it's easy to do that in conjunction with
- *    choosing the pivot, and it makes things a little
- *    easier in the partitioning step. Anyway, the pivot
- *    is the middle of these three. It's still possible
- *    to construct datasets where the algorithm takes
- *    time of order n^2, but it simply never happens in
- *    practice.
- * 3' Newsflash: On further investigation I find that
- *    it's easy to construct datasets where median-of-3
- *    simply isn't good enough. So on large-ish subarrays
- *    we do a more sophisticated pivoting: we take three
- *    sets of 3 elements, find their medians, and then
- *    take the median of those.
- * 4. We copy the pivot element to a separate place
- *    because that way we can always do our comparisons
- *    directly against a pointer to that separate place,
- *    and don't have to wonder "did we move the pivot
- *    element?". This makes the inner loop better.
- * 5. It's possible to make the pivoting even more
- *    reliable by looking at more candidates when n
- *    is larger. (Taking this to its logical conclusion
- *    results in a variant of quicksort that doesn't
- *    have that n^2 worst case.) However, the overhead
- *    from the extra bookkeeping means that it's just
- *    not worth while.
- * 6. This is pretty clean and portable code. Here are
- *    all the potential portability pitfalls and problems
- *    I know of:
- *      - In one place (the insertion sort) I construct
- *        a pointer that points just past the end of the
- *        supplied array, and assume that (a) it won't
- *        compare equal to any pointer within the array,
- *        and (b) it will compare equal to a pointer
- *        obtained by stepping off the end of the array.
- *        These might fail on some segmented architectures.
- *      - I assume that there are 8 bits in a |char| when
- *        computing the size of stack needed. This would
- *        fail on machines with 9-bit or 16-bit bytes.
- *      - I assume that if |((int)base&(sizeof(int)-1))==0|
- *        and |(size&(sizeof(int)-1))==0| then it's safe to
- *        get at array elements via |int*|s, and that if
- *        actually |size==sizeof(int)| as well then it's
- *        safe to treat the elements as |int|s. This might
- *        fail on systems that convert pointers to integers
- *        in non-standard ways.
- *      - I assume that |8*sizeof(size_t)<=INT_MAX|. This
- *        would be false on a machine with 8-bit |char|s,
- *        16-bit |int|s and 4096-bit |size_t|s. :-)
- */
+#define qsort SDL_qsort
 
-/* The recursion logic is the same in each case: */
-#define Recurse(Trunc)				\
-      { size_t l=last-ffirst,r=llast-first;	\
-        if (l<Trunc) {				\
-          if (r>=Trunc) doRight			\
-          else pop				\
-        }					\
-        else if (l<=r) { pushLeft; doRight }	\
-        else if (r>=Trunc) { pushRight; doLeft }\
-        else doLeft				\
-      }
+#define inline SDL_INLINE
 
-/* and so is the pivoting logic: */
-#define Pivot(swapper,sz)			\
-  if ((size_t)(last-first)>PIVOT_THRESHOLD*sz) mid=pivot_big(first,mid,last,sz,compare);\
-  else {	\
-    if (compare(first,mid)<0) {			\
-      if (compare(mid,last)>0) {		\
-        swapper(mid,last);			\
-        if (compare(first,mid)>0) swapper(first,mid);\
-      }						\
-    }						\
-    else {					\
-      if (compare(mid,last)>0) swapper(first,last)\
-      else {					\
-        swapper(first,mid);			\
-        if (compare(mid,last)>0) swapper(mid,last);\
-      }						\
-    }						\
-    first+=sz; last-=sz;			\
-  }
+/*
+This code came from PDCLib, under the public domain. Specifically this:
+https://bitbucket.org/pdclib/pdclib/raw/a82b02d0c7d4ed633b97f2a7639d9a10b1c92ec8/functions/stdlib/qsort.c
+The _PDCLIB_memswp macro was from
+https://bitbucket.org/pdclib/pdclib/src/a82b02d0c7d4ed633b97f2a7639d9a10b1c92ec8/platform/posix/internals/_PDCLIB_config.h?at=default&fileviewer=file-view-default#_PDCLIB_config.h-28
 
-#ifdef DEBUG_QSORT
-#include <stdio.h>
-#endif
+Everything below this comment until the HAVE_QSORT #endif was from PDCLib.
+--ryan.
+*/
 
-/* and so is the partitioning logic: */
-#define Partition(swapper,sz) {			\
-  int swapped=0;				\
-  do {						\
-    while (compare(first,pivot)<0) first+=sz;	\
-    while (compare(pivot,last)<0) last-=sz;	\
-    if (first<last) {				\
-      swapper(first,last); swapped=1;		\
-      first+=sz; last-=sz; }			\
-    else if (first==last) { first+=sz; last-=sz; break; }\
-  } while (first<=last);			\
-  if (!swapped) pop				\
-}
+/* $Id$ */
 
-/* and so is the pre-insertion-sort operation of putting
- * the smallest element into place as a sentinel.
- * Doing this makes the inner loop nicer. I got this
- * idea from the GNU implementation of qsort().
- */
-#define PreInsertion(swapper,limit,sz)		\
-  first=base;					\
-  last=first + (nmemb>limit ? limit : nmemb-1)*sz;\
-  while (last!=base) {				\
-    if (compare(first,last)>0) first=last;	\
-    last-=sz; }					\
-  if (first!=base) swapper(first,(char*)base);
+/* qsort( void *, size_t, size_t, int(*)( const void *, const void * ) )
 
-/* and so is the insertion sort, in the first two cases: */
-#define Insertion(swapper)			\
-  last=((char*)base)+nmemb*size;		\
-  for (first=((char*)base)+size;first!=last;first+=size) {	\
-    char *test;					\
-    /* Find the right place for |first|.	\
-     * My apologies for var reuse. */		\
-    for (test=first-size;compare(test,first)>0;test-=size) ;	\
-    test+=size;					\
-    if (test!=first) {				\
-      /* Shift everything in [test,first)	\
-       * up by one, and place |first|		\
-       * where |test| is. */			\
-      memcpy(pivot,first,size);			\
-      memmove(test+size,test,first-test);	\
-      memcpy(test,pivot,size);			\
-    }						\
-  }
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
 
-#define SWAP_nonaligned(a,b) { \
-  register char *aa=(a),*bb=(b); \
-  register size_t sz=size; \
-  do { register char t=*aa; *aa++=*bb; *bb++=t; } while (--sz); }
+#include <stdlib.h>
 
-#define SWAP_aligned(a,b) { \
-  register int *aa=(int*)(a),*bb=(int*)(b); \
-  register size_t sz=size; \
-  do { register int t=*aa;*aa++=*bb; *bb++=t; } while (sz-=WORD_BYTES); }
+#ifndef REGTEST
 
-#define SWAP_words(a,b) { \
-  register int t=*((int*)a); *((int*)a)=*((int*)b); *((int*)b)=t; }
+/* This implementation is taken from Paul Edward's PDPCLIB.
 
-/* ---------------------------------------------------------------------- */
+   Original code is credited to Raymond Gardner, Englewood CO.
+   Minor mods are credited to Paul Edwards.
+   Some reformatting and simplification done by Martin Baute.
+   All code is still Public Domain.
+*/
 
-static char *
-pivot_big(char *first, char *mid, char *last, size_t size,
-          int compare(const void *, const void *))
+/* Wrapper for _PDCLIB_memswp protects against multiple argument evaluation. */
+static inline void memswp( char * i, char * j, size_t size )
 {
-    size_t d = (((last - first) / size) >> 3) * size;
-    char *m1, *m2, *m3;
-    {
-        char *a = first, *b = first + d, *c = first + 2 * d;
-#ifdef DEBUG_QSORT
-        fprintf(stderr, "< %d %d %d\n", *(int *) a, *(int *) b, *(int *) c);
-#endif
-        m1 = compare(a, b) < 0 ?
-            (compare(b, c) < 0 ? b : (compare(a, c) < 0 ? c : a))
-            : (compare(a, c) < 0 ? a : (compare(b, c) < 0 ? c : b));
-    }
-    {
-        char *a = mid - d, *b = mid, *c = mid + d;
-#ifdef DEBUG_QSORT
-        fprintf(stderr, ". %d %d %d\n", *(int *) a, *(int *) b, *(int *) c);
-#endif
-        m2 = compare(a, b) < 0 ?
-            (compare(b, c) < 0 ? b : (compare(a, c) < 0 ? c : a))
-            : (compare(a, c) < 0 ? a : (compare(b, c) < 0 ? c : b));
-    }
-    {
-        char *a = last - 2 * d, *b = last - d, *c = last;
-#ifdef DEBUG_QSORT
-        fprintf(stderr, "> %d %d %d\n", *(int *) a, *(int *) b, *(int *) c);
-#endif
-        m3 = compare(a, b) < 0 ?
-            (compare(b, c) < 0 ? b : (compare(a, c) < 0 ? c : a))
-            : (compare(a, c) < 0 ? a : (compare(b, c) < 0 ? c : b));
-    }
-#ifdef DEBUG_QSORT
-    fprintf(stderr, "-> %d %d %d\n", *(int *) m1, *(int *) m2, *(int *) m3);
-#endif
-    return compare(m1, m2) < 0 ?
-        (compare(m2, m3) < 0 ? m2 : (compare(m1, m3) < 0 ? m3 : m1))
-        : (compare(m1, m3) < 0 ? m1 : (compare(m2, m3) < 0 ? m3 : m2));
+    _PDCLIB_memswp( i, j, size );
 }
 
-/* ---------------------------------------------------------------------- */
+/* For small sets, insertion sort is faster than quicksort.
+   T is the threshold below which insertion sort will be used.
+   Must be 3 or larger.
+*/
+#define T 7
+
+/* Macros for handling the QSort stack */
+#define PREPARE_STACK char * stack[STACKSIZE]; char * * stackptr = stack
+#define PUSH( base, limit ) stackptr[0] = base; stackptr[1] = limit; stackptr += 2
+#define POP( base, limit ) stackptr -= 2; base = stackptr[0]; limit = stackptr[1]
+/* TODO: Stack usage is log2( nmemb ) (minus what T shaves off the worst case).
+         Worst-case nmemb is platform dependent and should probably be 
+         configured through _PDCLIB_config.h.
+*/
+#define STACKSIZE 64
 
-static void
-qsort_nonaligned(void *base, size_t nmemb, size_t size,
-                 int (*compare) (const void *, const void *))
+void qsort( void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) )
 {
-
-    stack_entry stack[STACK_SIZE];
-    int stacktop = 0;
-    char *first, *last;
-    char *pivot = malloc(size);
-    size_t trunc = TRUNC_nonaligned * size;
-    assert(pivot != 0);
-
-    first = (char *) base;
-    last = first + (nmemb - 1) * size;
-
-    if ((size_t) (last - first) > trunc) {
-        char *ffirst = first, *llast = last;
-        while (1) {
-            /* Select pivot */
+    char * i;
+    char * j;
+    _PDCLIB_size_t thresh = T * size;
+    char * base_          = (char *)base;
+    char * limit          = base_ + nmemb * size;
+    PREPARE_STACK;
+
+    for ( ;; )
+    {
+        if ( (size_t)( limit - base_ ) > thresh ) /* QSort for more than T elements. */
+        {
+            /* We work from second to last - first will be pivot element. */
+            i = base_ + size;
+            j = limit - size;
+            /* We swap first with middle element, then sort that with second
+               and last element so that eventually first element is the median
+               of the three - avoiding pathological pivots.
+               TODO: Instead of middle element, chose one randomly.
+            */
+            memswp( ( ( ( (size_t)( limit - base_ ) ) / size ) / 2 ) * size + base_, base_, size );
+            if ( compar( i, j ) > 0 ) memswp( i, j, size );
+            if ( compar( base_, j ) > 0 ) memswp( base_, j, size );
+            if ( compar( i, base_ ) > 0 ) memswp( i, base_, size );
+            /* Now we have the median for pivot element, entering main Quicksort. */
+            for ( ;; )
             {
-                char *mid = first + size * ((last - first) / size >> 1);
-                Pivot(SWAP_nonaligned, size);
-                memcpy(pivot, mid, size);
+                do
+                {
+                    /* move i right until *i >= pivot */
+                    i += size;
+                } while ( compar( i, base_ ) < 0 );
+                do
+                {
+                    /* move j left until *j <= pivot */
+                    j -= size;
+                } while ( compar( j, base_ ) > 0 );
+                if ( i > j )
+                {
+                    /* break loop if pointers crossed */
+                    break;
+                }
+                /* else swap elements, keep scanning */
+                memswp( i, j, size );
             }
-            /* Partition. */
-            Partition(SWAP_nonaligned, size);
-            /* Prepare to recurse/iterate. */
-        Recurse(trunc)}
-    }
-    PreInsertion(SWAP_nonaligned, TRUNC_nonaligned, size);
-    Insertion(SWAP_nonaligned);
-    free(pivot);
-}
-
-static void
-qsort_aligned(void *base, size_t nmemb, size_t size,
-              int (*compare) (const void *, const void *))
-{
-
-    stack_entry stack[STACK_SIZE];
-    int stacktop = 0;
-    char *first, *last;
-    char *pivot = malloc(size);
-    size_t trunc = TRUNC_aligned * size;
-    assert(pivot != 0);
-
-    first = (char *) base;
-    last = first + (nmemb - 1) * size;
-
-    if ((size_t) (last - first) > trunc) {
-        char *ffirst = first, *llast = last;
-        while (1) {
-            /* Select pivot */
+            /* move pivot into correct place */
+            memswp( base_, j, size );
+            /* larger subfile base / limit to stack, sort smaller */
+            if ( j - base_ > limit - i )
             {
-                char *mid = first + size * ((last - first) / size >> 1);
-                Pivot(SWAP_aligned, size);
-                memcpy(pivot, mid, size);
+                /* left is larger */
+                PUSH( base_, j );
+                base_ = i;
             }
-            /* Partition. */
-            Partition(SWAP_aligned, size);
-            /* Prepare to recurse/iterate. */
-        Recurse(trunc)}
-    }
-    PreInsertion(SWAP_aligned, TRUNC_aligned, size);
-    Insertion(SWAP_aligned);
-    free(pivot);
-}
-
-static void
-qsort_words(void *base, size_t nmemb,
-            int (*compare) (const void *, const void *))
-{
-
-    stack_entry stack[STACK_SIZE];
-    int stacktop = 0;
-    char *first, *last;
-    char *pivot = malloc(WORD_BYTES);
-    assert(pivot != 0);
-
-    first = (char *) base;
-    last = first + (nmemb - 1) * WORD_BYTES;
-
-    if (last - first > TRUNC_words) {
-        char *ffirst = first, *llast = last;
-        while (1) {
-#ifdef DEBUG_QSORT
-            fprintf(stderr, "Doing %d:%d: ",
-                    (first - (char *) base) / WORD_BYTES,
-                    (last - (char *) base) / WORD_BYTES);
-#endif
-            /* Select pivot */
+            else
             {
-                char *mid =
-                    first + WORD_BYTES * ((last - first) / (2 * WORD_BYTES));
-                Pivot(SWAP_words, WORD_BYTES);
-                *(int *) pivot = *(int *) mid;
+                /* right is larger */
+                PUSH( i, limit );
+                limit = j;
+            }
+        }
+        else /* insertion sort for less than T elements              */
+        {
+            for ( j = base_, i = j + size; i < limit; j = i, i += size )
+            {
+                for ( ; compar( j, j + size ) > 0; j -= size )
+                {
+                    memswp( j, j + size, size );
+                    if ( j == base_ )
+                    {
+                        break;
+                    }
+                }
+            }
+            if ( stackptr != stack )           /* if any entries on stack  */
+            {
+                POP( base_, limit );
+            }
+            else                       /* else stack empty, done   */
+            {
+                break;
             }
-#ifdef DEBUG_QSORT
-            fprintf(stderr, "pivot=%d\n", *(int *) pivot);
-#endif
-            /* Partition. */
-            Partition(SWAP_words, WORD_BYTES);
-            /* Prepare to recurse/iterate. */
-        Recurse(TRUNC_words)}
-    }
-    PreInsertion(SWAP_words, (TRUNC_words / WORD_BYTES), WORD_BYTES);
-    /* Now do insertion sort. */
-    last = ((char *) base) + nmemb * WORD_BYTES;
-    for (first = ((char *) base) + WORD_BYTES; first != last;
-         first += WORD_BYTES) {
-        /* Find the right place for |first|. My apologies for var reuse */
-        int *pl = (int *) (first - WORD_BYTES), *pr = (int *) first;
-        *(int *) pivot = *(int *) first;
-        for (; compare(pl, pivot) > 0; pr = pl, --pl) {
-            *pr = *pl;
         }
-        if (pr != (int *) first)
-            *pr = *(int *) pivot;
     }
-    free(pivot);
 }
 
-/* ---------------------------------------------------------------------- */
+#endif
 
-void
-qsort(void *base, size_t nmemb, size_t size,
-      int (*compare) (const void *, const void *))
+#ifdef TEST
+#include <_PDCLIB_test.h>
+#include <string.h>
+#include <limits.h>
+
+static int compare( const void * left, const void * right )
 {
+    return *( (unsigned char *)left ) - *( (unsigned char *)right );
+}
 
-    if (nmemb <= 1)
-        return;
-    if (((uintptr_t) base | size) & (WORD_BYTES - 1))
-        qsort_nonaligned(base, nmemb, size, compare);
-    else if (size != WORD_BYTES)
-        qsort_aligned(base, nmemb, size, compare);
-    else
-        qsort_words(base, nmemb, compare);
+int main( void )
+{
+    char presort[] = { "shreicnyjqpvozxmbt" };
+    char sorted1[] = { "bcehijmnopqrstvxyz" };
+    char sorted2[] = { "bticjqnyozpvreshxm" };
+    char s[19];
+    strcpy( s, presort );
+    qsort( s, 18, 1, compare );
+    TESTCASE( strcmp( s, sorted1 ) == 0 );
+    strcpy( s, presort );
+    qsort( s, 9, 2, compare );
+    TESTCASE( strcmp( s, sorted2 ) == 0 );
+    strcpy( s, presort );
+    qsort( s, 1, 1, compare );
+    TESTCASE( strcmp( s, presort ) == 0 );
+#if defined(REGTEST) && (__BSD_VISIBLE || __APPLE__)
+    puts( "qsort.c: Skipping test #4 for BSD as it goes into endless loop here." );
+#else
+    qsort( s, 100, 0, compare );
+    TESTCASE( strcmp( s, presort ) == 0 );
+#endif
+    return TEST_RESULTS;
 }
 
-#endif /* !SDL_qsort */
+#endif
+
+#endif /* HAVE_QSORT */
 
 /* vi: set ts=4 sw=4 expandtab: */