Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
20 | pmbaty | 1 | #include "brqsort.h" |
2 | #include <stdlib.h> |
||
3 | |||
4 | // Global variables |
||
5 | |||
6 | // IDA: void __cdecl BrQsort(void *basep, unsigned int nelems, unsigned int size, br_qsort_cbfn *comp) |
||
7 | void BrQsort(void* basep, unsigned int nelems, unsigned int size, br_qsort_cbfn comp) { |
||
8 | qsort(basep, nelems, size, comp); |
||
9 | // char* stack[40]; |
||
10 | // char** sp; |
||
11 | // char* i; |
||
12 | // char* j; |
||
13 | // char* limit; |
||
14 | // unsigned int thresh; |
||
15 | // char* base; |
||
16 | // unsigned int width; |
||
17 | // void (*swap_func)(char*, char*, unsigned int); |
||
18 | |||
19 | // width = size; |
||
20 | // swap_func = swap_chars; |
||
21 | |||
22 | // if (size == 4) { // Do we have exactly 1 32bit in worth of data at a time? |
||
23 | // swap_func = swap_int_1; |
||
24 | // } else if (!(size & 3)) { // Is our data divisible by 4 so can be swapped 32bit at a time? |
||
25 | // width >>= 2; |
||
26 | // swap_func = swap_ints; |
||
27 | // } |
||
28 | |||
29 | // base = (char*)basep; |
||
30 | // thresh = 7 * size; |
||
31 | // sp = stack; |
||
32 | |||
33 | // for (limit = (char*)basep + size * nelems;; limit = sp[1]) { |
||
34 | // while (limit - base > thresh) { |
||
35 | // swap_func( |
||
36 | // &base[size * ((limit - base) / size >> 1)], |
||
37 | // base, |
||
38 | // width); |
||
39 | // i = &base[size]; |
||
40 | // j = &limit[-size]; |
||
41 | |||
42 | // if (comp(&base[size], &limit[-size]) > 0) { |
||
43 | // swap_func(i, j, width); |
||
44 | // } |
||
45 | |||
46 | // if (comp(base, j) > 0) { |
||
47 | // swap_func(base, j, width); |
||
48 | // } |
||
49 | |||
50 | // if (comp(i, base) > 0) { |
||
51 | // swap_func(i, base, width); |
||
52 | // } |
||
53 | |||
54 | // while (1) { |
||
55 | // do { |
||
56 | // i += size; |
||
57 | // } while (comp(i, base) < 0); |
||
58 | |||
59 | // do { |
||
60 | // j -= size; |
||
61 | // } while (comp(j, base) > 0); |
||
62 | |||
63 | // if (i > j) { |
||
64 | // break; |
||
65 | // } |
||
66 | |||
67 | // swap_func(i, j, width); |
||
68 | // } |
||
69 | |||
70 | // swap_func(base, j, width); |
||
71 | |||
72 | // if (j - base <= limit - i) { |
||
73 | // *sp = i; |
||
74 | // sp[1] = limit; |
||
75 | // limit = j; |
||
76 | // } else { |
||
77 | // *sp = base; |
||
78 | // sp[1] = j; |
||
79 | // base = i; |
||
80 | // } |
||
81 | |||
82 | // sp += 2; |
||
83 | // } |
||
84 | |||
85 | // j = base; |
||
86 | |||
87 | // for (i = &base[size]; i < limit; i += size) { |
||
88 | // while (comp(j, &j[size]) > 0) { |
||
89 | // swap_func(j, &j[size], width); |
||
90 | |||
91 | // if (j == base) { |
||
92 | // break; |
||
93 | // } |
||
94 | |||
95 | // j -= size; |
||
96 | // } |
||
97 | |||
98 | // j = i; |
||
99 | // } |
||
100 | |||
101 | // if (stack == sp) { |
||
102 | // break; |
||
103 | // } |
||
104 | |||
105 | // sp -= 2; |
||
106 | // base = *sp; |
||
107 | // } |
||
108 | } |
||
109 | |||
110 | // IDA: void __usercall swap_chars(char *a@<EAX>, char *b@<EDX>, unsigned int nbytes@<EBX>) |
||
111 | void swap_chars(char* a, char* b, unsigned int nbytes) { |
||
112 | char tmp; |
||
113 | |||
114 | do { |
||
115 | tmp = *a; |
||
116 | *a++ = *b; |
||
117 | *b++ = tmp; |
||
118 | } while (--nbytes); |
||
119 | } |
||
120 | |||
121 | // IDA: void __usercall swap_ints(char *ap@<EAX>, char *bp@<EDX>, unsigned int nints@<EBX>) |
||
122 | void swap_ints(char* ap, char* bp, unsigned int nints) { |
||
123 | int* a = (int*)ap; |
||
124 | int* b = (int*)bp; |
||
125 | int tmp; |
||
126 | |||
127 | do { |
||
128 | tmp = *a; |
||
129 | *a++ = *b; |
||
130 | *b++ = tmp; |
||
131 | } while (--nints); |
||
132 | } |
||
133 | |||
134 | // IDA: void __usercall swap_int_1(char *ap@<EAX>, char *bp@<EDX>, unsigned int nints@<EBX>) |
||
135 | void swap_int_1(char* ap, char* bp, unsigned int nints) { |
||
136 | int* a = (int*)ap; |
||
137 | int* b = (int*)bp; |
||
138 | int tmp; |
||
139 | |||
140 | tmp = *a; |
||
141 | *a = *b; |
||
142 | *b = tmp; |
||
143 | } |