summaryrefslogtreecommitdiff
path: root/src/MSL_C.PPCEABI.bare.H/qsort.c
blob: 98e86242e436c0b2252a9244390ff5fa4b012537 (plain)
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
#include <stdlib.h>

#define table_ptr(i) (((char*)table_base) + (member_size * ((i)-1)))

#define swap(dst, src, cnt)                                                                        \
  do {                                                                                             \
    char* p;                                                                                       \
    char* q;                                                                                       \
    size_t n = cnt;                                                                                \
                                                                                                   \
    unsigned long tmp;                                                                             \
                                                                                                   \
    for (p = (char*)src - 1, q = (char*)dst - 1, n++; --n;) {                                      \
      tmp = *++q;                                                                                  \
      *q = *++p;                                                                                   \
      *p = tmp;                                                                                    \
    }                                                                                              \
  } while (0)

void qsort(void* table_base, size_t num_members, size_t member_size,
           _compare_function compare_members) {
  size_t l, r, j;
  char* lp;
  char* rp;
  char* ip;
  char* jp;
  char* kp;

  if (num_members < 2)
    return;

  r = num_members;
  l = (r / 2) + 1;

  lp = table_ptr(l);
  rp = table_ptr(r);

  for (;;) {
    if (l > 1) {
      l--;
      lp -= member_size;
    } else {
      swap(lp, rp, member_size);

      if (--r == 1)
        return;

      rp -= member_size;
    }

    j = l;

    jp = table_ptr(j);

    while (j * 2 <= r) {
      j *= 2;

      ip = jp;
      jp = table_ptr(j);

      if (j < r) {
        kp = jp + member_size;

        if (compare_members(jp, kp) < 0) {
          j++;
          jp = kp;
        }
      }

      if (compare_members(ip, jp) < 0)
        swap(ip, jp, member_size);
      else
        break;
    }
  }
}