SDL_qsort.c File Reference
`#include "../SDL_internal.h"`
`#include "SDL_stdinc.h"`
`#include "SDL_assert.h"`
Include dependency graph for SDL_qsort.c:

Go to the source code of this file.

## Data Structures

struct | stack_entry

## Macros

#define | assert(X) SDL_assert(X)
#define | malloc SDL_malloc
#define | free SDL_free
#define | memcpy SDL_memcpy
#define | memmove SDL_memmove
#define | qsort SDL_qsort
#define | WORD_BYTES sizeof(int)
#define | STACK_SIZE (8*sizeof(size_t))
#define | TRUNC_nonaligned 12
#define | TRUNC_aligned 12
#define | TRUNC_words 12*WORD_BYTES /* nb different meaning */
#define | PIVOT_THRESHOLD 40
#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
#define | Recurse(Trunc)
#define | Pivot(swapper, sz)
#define | Partition(swapper, sz)
#define | PreInsertion(swapper, limit, sz)
#define | Insertion(swapper)
#define | SWAP_nonaligned(a, b)
#define | SWAP_aligned(a, b)
#define | SWAP_words(a, b)

## Functions

static char * | pivot_big (char *first, char *mid, char *last, size_t size, int compare(const void *, const void *))
static void | qsort_nonaligned (void *base, size_t nmemb, size_t size, int(*compare)(const void *, const void *))
static void | qsort_aligned (void *base, size_t nmemb, size_t size, int(*compare)(const void *, const void *))
static void | qsort_words (void *base, size_t nmemb, int(*compare)(const void *, const void *))
void | qsort (void *base, size_t nmemb, size_t size, int(*compare)(const void *, const void *))

## Variables

static const char | _ID [] = "<qsort.c gjm 1.12 1998-03-19>"

## Macro Definition Documentation

#define assert | ( |  | X | ) | SDL_assert(X)

Definition at line 70 of file SDL_qsort.c.

Referenced by qsort_aligned(), qsort_nonaligned(), and qsort_words().

#define doLeft {first=ffirst;llast=last;continue;}

Definition at line 128 of file SDL_qsort.c.

#define doRight {ffirst=first;last=llast;continue;}

Definition at line 129 of file SDL_qsort.c.

#define free SDL_free

Definition at line 78 of file SDL_qsort.c.

Referenced by qsort_aligned(), qsort_nonaligned(), and qsort_words().

#define Insertion | ( |  | swapper | ) |

Value:
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); \
} \
}
first
const GLint * first
Definition: SDL_opengl_glext.h:368
memcpy
#define memcpy
Definition: SDL_qsort.c:82
memmove
#define memmove
Definition: SDL_qsort.c:86
size
GLsizeiptr size
Definition: SDL_opengl_glext.h:537

Definition at line 264 of file SDL_qsort.c.

Referenced by qsort_aligned(), and qsort_nonaligned().

#define malloc SDL_malloc

Definition at line 74 of file SDL_qsort.c.

Referenced by qsort_aligned(), qsort_nonaligned(), and qsort_words().

#define memcpy SDL_memcpy

Definition at line 82 of file SDL_qsort.c.

Referenced by qsort_aligned(), and qsort_nonaligned().

#define memmove SDL_memmove

Definition at line 86 of file SDL_qsort.c.

Referenced by SDL_memmove().

#define Partition | ( |  | swapper,
|  |  | sz
| ) |  |

Value:
{ \
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 \
}
first
const GLint * first
Definition: SDL_opengl_glext.h:368
pop
#define pop
Definition: SDL_qsort.c:130

Definition at line 237 of file SDL_qsort.c.

Referenced by qsort_aligned(), qsort_nonaligned(), and qsort_words().

#define Pivot | ( |  | swapper,
|  |  | sz
| ) |  |

Value:
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; \
}
first
const GLint * first
Definition: SDL_opengl_glext.h:368
pivot_big
static char * pivot_big(char *first, char *mid, char *last, size_t size, int compare(const void *, const void *))
Definition: SDL_qsort.c:298
PIVOT_THRESHOLD
#define PIVOT_THRESHOLD
Definition: SDL_qsort.c:119

Definition at line 213 of file SDL_qsort.c.

Referenced by qsort_aligned(), qsort_nonaligned(), and qsort_words().

#define PIVOT_THRESHOLD 40

Definition at line 119 of file SDL_qsort.c.

#define pop

Value:
{if (--stacktop<0) break;\
first=ffirst=stack[stacktop].first;\
last=llast=stack[stacktop].last;\
continue;}
first
const GLint * first
Definition: SDL_opengl_glext.h:368

Definition at line 130 of file SDL_qsort.c.

Referenced by CPU_haveCPUID(), SDL_SoftStretch(), and SDL_tolower().

#define PreInsertion | ( |  | swapper,
|  |  | limit,
|  |  | sz
| ) |  |

Value:
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);
limit
GLint limit
Definition: SDL_opengl_glext.h:8766
first
const GLint * first
Definition: SDL_opengl_glext.h:368

Definition at line 255 of file SDL_qsort.c.

Referenced by qsort_aligned(), qsort_nonaligned(), and qsort_words().

#define pushLeft {stack[stacktop].first=ffirst;stack[stacktop++].last=last;}

Definition at line 126 of file SDL_qsort.c.

#define pushRight {stack[stacktop].first=first;stack[stacktop++].last=llast;}

Definition at line 127 of file SDL_qsort.c.

#define qsort SDL_qsort

Definition at line 90 of file SDL_qsort.c.

#define Recurse | ( |  | Trunc | ) |

Value:
{ 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 \
}
pushRight
#define pushRight
Definition: SDL_qsort.c:127
r
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2072
first
const GLint * first
Definition: SDL_opengl_glext.h:368
pop
#define pop
Definition: SDL_qsort.c:130
doRight
#define doRight
Definition: SDL_qsort.c:129
doLeft
#define doLeft
Definition: SDL_qsort.c:128
pushLeft
#define pushLeft
Definition: SDL_qsort.c:126

Definition at line 201 of file SDL_qsort.c.

Referenced by qsort_aligned(), qsort_nonaligned(), and qsort_words().

#define STACK_SIZE (8*sizeof(size_t))

Definition at line 102 of file SDL_qsort.c.

Referenced by qsort_aligned(), qsort_nonaligned(), and qsort_words().

#define SWAP_aligned | ( |  | a,
|  |  | b
| ) |  |

Value:
{ \
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); }
WORD_BYTES
#define WORD_BYTES
Definition: SDL_qsort.c:97
size
GLsizeiptr size
Definition: SDL_opengl_glext.h:537
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1109
b
GLboolean GLboolean GLboolean b
Definition: SDL_opengl_glext.h:1109
t
GLdouble GLdouble t
Definition: SDL_opengl.h:2064

Definition at line 287 of file SDL_qsort.c.

Referenced by qsort_aligned().

#define SWAP_nonaligned | ( |  | a,
|  |  | b
| ) |  |

Value:
{ \
register char *aa=(a),*bb=(b); \
register size_t sz=size; \
do { register char t=*aa; *aa++=*bb; *bb++=t; } while (--sz); }
size
GLsizeiptr size
Definition: SDL_opengl_glext.h:537
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1109
b
GLboolean GLboolean GLboolean b
Definition: SDL_opengl_glext.h:1109
t
GLdouble GLdouble t
Definition: SDL_opengl.h:2064

Definition at line 282 of file SDL_qsort.c.

Referenced by qsort_nonaligned().

#define SWAP_words | ( |  | a,
|  |  | b
| ) |  |

Value:
{ \
register int t=*((int*)a); *((int*)a)=*((int*)b); *((int*)b)=t; }
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1109
b
GLboolean GLboolean GLboolean b
Definition: SDL_opengl_glext.h:1109
t
GLdouble GLdouble t
Definition: SDL_opengl.h:2064

Definition at line 292 of file SDL_qsort.c.

Referenced by qsort_words().

#define TRUNC_aligned 12

Definition at line 112 of file SDL_qsort.c.

Referenced by qsort_aligned().

#define TRUNC_nonaligned 12

Definition at line 111 of file SDL_qsort.c.

Referenced by qsort_nonaligned().

#define TRUNC_words 12*WORD_BYTES /* nb different meaning */

Definition at line 113 of file SDL_qsort.c.

Referenced by qsort_words().

#define WORD_BYTES sizeof(int)

Definition at line 97 of file SDL_qsort.c.

Referenced by qsort(), and qsort_words().

## Function Documentation

static char* pivot_big | ( | char * | first,
|  | char * | mid,
|  | char * | last,
|  | size_t | size,
|  | int | compareconst void *, const void *
| ) |  |

static

Definition at line 298 of file SDL_qsort.c.

References d.

{
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));
}
first
const GLint * first
Definition: SDL_opengl_glext.h:368
size
GLsizeiptr size
Definition: SDL_opengl_glext.h:537
d
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 ** d
Definition: SDL_dynapi_procs.h:121
c
const GLubyte * c
Definition: SDL_opengl_glext.h:11093
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1109
b
GLboolean GLboolean GLboolean b
Definition: SDL_opengl_glext.h:1109

void qsort | ( | void * | base,
|  | size_t | nmemb,
|  | size_t | size,
|  | int(*)(const void *, const void *) | compare
| ) |  |

Definition at line 465 of file SDL_qsort.c.

References qsort_aligned(), qsort_nonaligned(), qsort_words(), and WORD_BYTES.

{
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);
}
WORD_BYTES
#define WORD_BYTES
Definition: SDL_qsort.c:97
size
GLsizeiptr size
Definition: SDL_opengl_glext.h:537
qsort_nonaligned
static void qsort_nonaligned(void *base, size_t nmemb, size_t size, int(*compare)(const void *, const void *))
Definition: SDL_qsort.c:341
uintptr_t
unsigned int uintptr_t
Definition: SDL_config_windows.h:69
qsort_words
static void qsort_words(void *base, size_t nmemb, int(*compare)(const void *, const void *))
Definition: SDL_qsort.c:409
qsort_aligned
static void qsort_aligned(void *base, size_t nmemb, size_t size, int(*compare)(const void *, const void *))
Definition: SDL_qsort.c:375

static void qsort_aligned | ( | void * | base,
|  | size_t | nmemb,
|  | size_t | size,
|  | int(*)(const void *, const void *) | compare
| ) |  |

static

Definition at line 375 of file SDL_qsort.c.

References assert, free, Insertion, malloc, memcpy, Partition, Pivot, PreInsertion, Recurse, STACK_SIZE, SWAP_aligned, and TRUNC_aligned.

Referenced by qsort().

{
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 */
{
char *mid = first + size * ((last - first) / size >> 1);
Pivot(SWAP_aligned, size);
memcpy(pivot, mid, size);
}
/* Partition. */
Partition(SWAP_aligned, size);
/* Prepare to recurse/iterate. */
Recurse(trunc)}
}
PreInsertion(SWAP_aligned, TRUNC_aligned, size);
Insertion(SWAP_aligned);
free(pivot);
}
SWAP_aligned
#define SWAP_aligned(a, b)
Definition: SDL_qsort.c:287
TRUNC_aligned
#define TRUNC_aligned
Definition: SDL_qsort.c:112
stack_entry
Definition: SDL_qsort.c:121
first
const GLint * first
Definition: SDL_opengl_glext.h:368
PreInsertion
#define PreInsertion(swapper, limit, sz)
Definition: SDL_qsort.c:255
memcpy
#define memcpy
Definition: SDL_qsort.c:82
Pivot
#define Pivot(swapper, sz)
Definition: SDL_qsort.c:213
size
GLsizeiptr size
Definition: SDL_opengl_glext.h:537
STACK_SIZE
#define STACK_SIZE
Definition: SDL_qsort.c:102
Recurse
#define Recurse(Trunc)
Definition: SDL_qsort.c:201
assert
#define assert(X)
Definition: SDL_qsort.c:70
Insertion
#define Insertion(swapper)
Definition: SDL_qsort.c:264
free
#define free
Definition: SDL_qsort.c:78
Partition
#define Partition(swapper, sz)
Definition: SDL_qsort.c:237
malloc
#define malloc
Definition: SDL_qsort.c:74

static void qsort_nonaligned | ( | void * | base,
|  | size_t | nmemb,
|  | size_t | size,
|  | int(*)(const void *, const void *) | compare
| ) |  |

static

Definition at line 341 of file SDL_qsort.c.

References assert, free, Insertion, malloc, memcpy, Partition, Pivot, PreInsertion, Recurse, STACK_SIZE, SWAP_nonaligned, and TRUNC_nonaligned.

Referenced by qsort().

{
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 *mid = first + size * ((last - first) / size >> 1);
Pivot(SWAP_nonaligned, size);
memcpy(pivot, mid, size);
}
/* Partition. */
Partition(SWAP_nonaligned, size);
/* Prepare to recurse/iterate. */
Recurse(trunc)}
}
PreInsertion(SWAP_nonaligned, TRUNC_nonaligned, size);
Insertion(SWAP_nonaligned);
free(pivot);
}
stack_entry
Definition: SDL_qsort.c:121
first
const GLint * first
Definition: SDL_opengl_glext.h:368
PreInsertion
#define PreInsertion(swapper, limit, sz)
Definition: SDL_qsort.c:255
memcpy
#define memcpy
Definition: SDL_qsort.c:82
Pivot
#define Pivot(swapper, sz)
Definition: SDL_qsort.c:213
size
GLsizeiptr size
Definition: SDL_opengl_glext.h:537
STACK_SIZE
#define STACK_SIZE
Definition: SDL_qsort.c:102
Recurse
#define Recurse(Trunc)
Definition: SDL_qsort.c:201
TRUNC_nonaligned
#define TRUNC_nonaligned
Definition: SDL_qsort.c:111
assert
#define assert(X)
Definition: SDL_qsort.c:70
SWAP_nonaligned
#define SWAP_nonaligned(a, b)
Definition: SDL_qsort.c:282
Insertion
#define Insertion(swapper)
Definition: SDL_qsort.c:264
free
#define free
Definition: SDL_qsort.c:78
Partition
#define Partition(swapper, sz)
Definition: SDL_qsort.c:237
malloc
#define malloc
Definition: SDL_qsort.c:74

static void qsort_words | ( | void * | base,
|  | size_t | nmemb,
|  | int(*)(const void *, const void *) | compare
| ) |  |

static

Definition at line 409 of file SDL_qsort.c.

References assert, free, malloc, Partition, Pivot, PreInsertion, Recurse, STACK_SIZE, SWAP_words, TRUNC_words, and WORD_BYTES.

Referenced by qsort().

{
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 */
{
char *mid =
first + WORD_BYTES * ((last - first) / (2 * WORD_BYTES));
Pivot(SWAP_words, WORD_BYTES);
*(int *) pivot = *(int *) mid;
}
#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);
}
WORD_BYTES
#define WORD_BYTES
Definition: SDL_qsort.c:97
stack_entry
Definition: SDL_qsort.c:121
first
const GLint * first
Definition: SDL_opengl_glext.h:368
PreInsertion
#define PreInsertion(swapper, limit, sz)
Definition: SDL_qsort.c:255
Pivot
#define Pivot(swapper, sz)
Definition: SDL_qsort.c:213
SWAP_words
#define SWAP_words(a, b)
Definition: SDL_qsort.c:292
STACK_SIZE
#define STACK_SIZE
Definition: SDL_qsort.c:102
Recurse
#define Recurse(Trunc)
Definition: SDL_qsort.c:201
assert
#define assert(X)
Definition: SDL_qsort.c:70
TRUNC_words
#define TRUNC_words
Definition: SDL_qsort.c:113
free
#define free
Definition: SDL_qsort.c:78
Partition
#define Partition(swapper, sz)
Definition: SDL_qsort.c:237
malloc
#define malloc
Definition: SDL_qsort.c:74

## Variable Documentation

const char _ID[] = "<qsort.c gjm 1.12 1998-03-19>"

static

Definition at line 92 of file SDL_qsort.c.