Rizin
unix-like reverse engineering framework and cli tools
qsort.h File Reference
#include <libkern/libkern.h>

Go to the source code of this file.

Macros

#define min(a, b)   (a) < (b) ? a : b
 
#define swapcode(TYPE, parmi, parmj, n)
 
#define SWAPINIT(a, es)
 
#define swap(a, b)
 
#define vecswap(a, b, n)   if ((n) > 0) swapfunc(a, b, n, swaptype)
 

Functions

void qsort (void *a, size_t n, size_t es, int(*cmp)(const void *, const void *))
 
static char * med3 (char *, char *, char *, int(*)(const void *, const void *))
 
static void swapfunc (char *, char *, int, int)
 

Macro Definition Documentation

◆ min

#define min (   a,
  b 
)    (a) < (b) ? a : b

Definition at line 83 of file qsort.h.

◆ swap

#define swap (   a,
  b 
)
Value:
if (swaptype == 0) { \
long t = *(long *)(a); \
*(long *)(a) = *(long *)(b); \
*(long *)(b) = t; \
swapfunc(a, b, es, swaptype)
static void swapfunc(char *, char *, int, int)
Definition: qsort.h:103
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41

Definition at line 111 of file qsort.h.

◆ swapcode

#define swapcode (   TYPE,
  parmi,
  parmj,
  n 
)
Value:
{ \
long i = (n) / sizeof (TYPE); \
TYPE *pi = (TYPE *) (parmi); \
TYPE *pj = (TYPE *) (parmj); \
do { \
TYPE t = *pi; \
*pi++ = *pj; \
*pj++ = t; \
} while (--i > 0); \
}
lzma_index ** i
Definition: index.h:629
@ TYPE
Definition: inflate9.h:13
int n
Definition: mipsasm.c:19

Definition at line 88 of file qsort.h.

◆ SWAPINIT

#define SWAPINIT (   a,
  es 
)
Value:
swaptype = ((char *)a - (char *)0) % sizeof(long) || \
es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags long
Definition: sflib.h:79

Definition at line 99 of file qsort.h.

◆ vecswap

#define vecswap (   a,
  b,
  n 
)    if ((n) > 0) swapfunc(a, b, n, swaptype)

Definition at line 119 of file qsort.h.

Function Documentation

◆ med3()

static char * med3 ( char *  a,
char *  b,
char *  c,
int(*)(const void *, const void *)  cmp 
)
inlinestatic

Definition at line 122 of file qsort.h.

123 {
124  return cmp(a, b) < 0 ?
125  (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
126  :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
127 }
static RzILOpEffect * cmp(cs_insn *insn, bool is_thumb)
Definition: arm_il32.c:942
#define c(i)
Definition: sha256.c:43

References a, b, c, and cmp().

Referenced by qsort().

◆ qsort()

void qsort ( void *  a,
size_t  n,
size_t  es,
int(*)(const void *, const void *)  cmp 
)

Definition at line 130 of file qsort.h.

131 {
132  char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
133  int d, swaptype, swap_cnt;
134  int r;
135 
136 loop: SWAPINIT(a, es);
137  swap_cnt = 0;
138  if (n < 7) {
139  for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
140  for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
141  pl -= es)
142  swap(pl, pl - es);
143  return;
144  }
145  pm = (char *)a + (n / 2) * es;
146  if (n > 7) {
147  pl = a;
148  pn = (char *)a + (n - 1) * es;
149  if (n > 40) {
150  d = (n / 8) * es;
151  pl = med3(pl, pl + d, pl + 2 * d, cmp);
152  pm = med3(pm - d, pm, pm + d, cmp);
153  pn = med3(pn - 2 * d, pn - d, pn, cmp);
154  }
155  pm = med3(pl, pm, pn, cmp);
156  }
157  swap(a, pm);
158  pa = pb = (char *)a + es;
159 
160  pc = pd = (char *)a + (n - 1) * es;
161  for (;;) {
162  while (pb <= pc && (r = cmp(pb, a)) <= 0) {
163  if (r == 0) {
164  swap_cnt = 1;
165  swap(pa, pb);
166  pa += es;
167  }
168  pb += es;
169  }
170  while (pb <= pc && (r = cmp(pc, a)) >= 0) {
171  if (r == 0) {
172  swap_cnt = 1;
173  swap(pc, pd);
174  pd -= es;
175  }
176  pc -= es;
177  }
178  if (pb > pc)
179  break;
180  swap(pb, pc);
181  swap_cnt = 1;
182  pb += es;
183  pc -= es;
184  }
185  if (swap_cnt == 0) { /* Switch to insertion sort */
186  for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
187  for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
188  pl -= es)
189  swap(pl, pl - es);
190  return;
191  }
192 
193  pn = (char *)a + n * es;
194  r = min(pa - (char *)a, pb - pa);
195  vecswap(a, pb - r, r);
196  r = min((size_t)(pd - pc), pn - pd - es);
197  vecswap(pb, pn - r, r);
198  if ((size_t)(r = pb - pa) > es)
199  qsort(a, r / es, es, cmp);
200  if ((size_t)(r = pd - pc) > es) {
201  /* Iterate rather than recurse to save stack space */
202  a = pn - r;
203  n = r / es;
204  goto loop;
205  }
206 /* qsort(pn - r, r / es, es, cmp);*/
207 }
#define r
Definition: crypto_rc6.c:12
#define swap(a, b)
Definition: qsort.h:111
#define vecswap(a, b, n)
Definition: qsort.h:119
void qsort(void *a, size_t n, size_t es, int(*cmp)(const void *, const void *))
Definition: qsort.h:130
#define min(a, b)
Definition: qsort.h:83
#define SWAPINIT(a, es)
Definition: qsort.h:99
static char * med3(char *, char *, char *, int(*)(const void *, const void *))
Definition: qsort.h:122
#define d(i)
Definition: sha256.c:44
uv_loop_t * loop
Definition: main.c:7

References a, cmp(), d, loop, med3(), min, n, pc, pl, r, swap, SWAPINIT, and vecswap.

Referenced by apprentice_load(), build_immediate_table(), build_opcode_table(), compare_zip(), ef_read(), main(), make_program_env(), print_insn_lanai(), print_insn_sparc(), scandir(), sdb_array_add_sorted(), sdb_array_sort(), sdb_array_sort_num(), and xtensa_isa_init().

◆ swapfunc()

static void swapfunc ( char *  a,
char *  b,
int  n,
int  swaptype 
)
inlinestatic

Definition at line 103 of file qsort.h.

104 {
105  if(swaptype <= 1)
106  swapcode(long, a, b, n)
107  else
108  swapcode(char, a, b, n)
109 }
#define swapcode(TYPE, parmi, parmj, n)
Definition: qsort.h:88

References a, b, n, and swapcode.