-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsbu4i.c
306 lines (294 loc) · 10.5 KB
/
msbu4i.c
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
/*----------------------------------------------------------------------*/
/* msbu4i.c hybrid insertion + merge sort */
/* */
/* Jeff Reid 2022AUG12 09:15 */
/*----------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
typedef unsigned long long uint64_t;
#define QP 0 /* if != 0, use queryperformance for timer */
#if QP
#include <math.h>
#include <windows.h>
#pragma comment(lib, "winmm.lib")
typedef LARGE_INTEGER LI64;
#else
#include <time.h>
#endif
#if QP
static LI64 liQPFrequency; /* cpu counter values */
static LI64 liStartTime;
static LI64 liStopTime;
static double dQPFrequency;
static double dStartTime;
static double dStopTime;
static double dElapsedTime;
#else
clock_t ctTimeStart; /* clock values */
clock_t ctTimeStop;
#endif
size_t GetPassCount(size_t n);
void MergeSort(uint64_t a[], uint64_t b[], size_t n)
{
uint64_t *p0r; /* ptr to current element run 0 */
uint64_t *p0e; /* ptr to end run 0 */
uint64_t *p1r; /* ptr to current element run 1 */
uint64_t *p1e; /* ptr to end run 1 */
uint64_t *p2r; /* ptr to current element run 2 */
uint64_t *p2e; /* ptr to end run 2 */
uint64_t *p3r; /* ptr to current element run 3 */
uint64_t *p3e; /* ptr to end run 3 */
uint64_t *pax; /* ptr to a[] or b[] */
uint64_t *pbx; /* ptr to b[] or a[] */
size_t rsz; /* run size */
{ /* check if size < 4 */
uint64_t t;
if(n < 2)
return;
if(n == 2){
if(a[0] > a[1]){
t = a[0];
a[0] = a[1];
a[1] = t;
}
return;
}
if(n == 3){
if(a[0] > a[2]){
t = a[0];
a[0] = a[2];
a[2] = t;
}
if(a[0] > a[1]){
t = a[0];
a[0] = a[1];
a[1] = t;
}
if(a[1] > a[2]){
t = a[1];
a[1] = a[2];
a[2] = t;
}
return;
}
}
/* set run size so merge sort is even number of passes */
rsz = ((GetPassCount(n) & 1) != 0) ? 64 : 16;
{ /* insertion sort */
size_t l, r;
size_t i, j;
uint64_t t;
for (l = 0; l < n; l = r) {
r = l + rsz;
if (r > n)r = n;
l--;
for (j = l + 2; j < r; j++) {
t = a[j];
i = j-1;
while(i != l && a[i] > t){
a[i+1] = a[i];
i--;
}
a[i+1] = t;
}
}
}
while(rsz < n){ /* merge sort */
pbx = &b[0];
pax = &a[0];
while(pax < &a[n]){
p0e = rsz + (p0r = pax);
if(p0e >= &a[n]){
p0e = &a[n];
goto cpy10;}
p1e = rsz + (p1r = p0e);
if(p1e >= &a[n]){
p1e = &a[n];
goto mrg201;}
p2e = rsz + (p2r = p1e);
if(p2e >= &a[n]){
p2e = &a[n];
goto mrg3012;}
p3e = rsz + (p3r = p2e);
if(p3e >= &a[n])
p3e = &a[n];
/* 4 way merge */
while(1){
if(*p0r <= *p1r){
if(*p2r <= *p3r){
if(*p0r <= *p2r){
mrg40: *pbx++ = *p0r++; /* run 0 smallest */
if(p0r < p0e) /* if not end run continue */
continue;
goto mrg3123; /* merge 1,2,3 */
} else {
mrg42: *pbx++ = *p2r++; /* run 2 smallest */
if(p2r < p2e) /* if not end run continue */
continue;
goto mrg3013; /* merge 0,1,3 */
}
} else {
if(*p0r <= *p3r){
goto mrg40; /* run 0 smallext */
} else {
mrg43: *pbx++ = *p3r++; /* run 3 smallest */
if(p3r < p3e) /* if not end run continue */
continue;
goto mrg3012; /* merge 0,1,2 */
}
}
} else {
if(*p2r <= *p3r){
if(*p1r <= *p2r){
mrg41: *pbx++ = *p1r++; /* run 1 smallest */
if(p1r < p1e) /* if not end run continue */
continue;
goto mrg3023; /* merge 0,2,3 */
} else {
goto mrg42; /* run 2 smallest */
}
} else {
if(*p1r <= *p3r){
goto mrg41; /* run 1 smallest */
} else {
goto mrg43; /* run 3 smallest */
}
}
}
}
/* 3 way merge */
mrg3123: p0r = p1r;
p0e = p1e;
mrg3023: p1r = p2r;
p1e = p2e;
mrg3013: p2r = p3r;
p2e = p3e;
mrg3012: while(1){
if(*p0r <= *p1r){
if(*p0r <= *p2r){
*pbx++ = *p0r++; /* run 0 smallest */
if(p0r < p0e) /* if not end run continue */
continue;
goto mrg212; /* merge 1,2 */
} else {
mrg32: *pbx++ = *p2r++; /* run 2 smallest */
if(p2r < p2e) /* if not end run continue */
continue;
goto mrg201; /* merge 0,1 */
}
} else {
if(*p1r <= *p2r){
*pbx++ = *p1r++; /* run 1 smallest */
if(p1r < p1e) /* if not end run continue */
continue;
goto mrg202; /* merge 0,2 */
} else {
goto mrg32; /* run 2 smallest */
}
}
}
mrg212: p0r = p1r;
p0e = p1e;
mrg202: p1r = p2r;
p1e = p2e;
/* 2 way merge */
mrg201: while(1){
if(*p0r <= *p1r){
*pbx++ = *p0r++; /* run 0 smallest */
if(p0r < p0e) /* if not end run continue */
continue;
goto cpy11;
} else {
*pbx++ = *p1r++; /* run 1 smallest */
if(p1r < p1e) /* if not end run continue */
continue;
goto cpy10;
}
}
cpy11: p0r = p1r;
p0e = p1e;
/* 1 way copy */
cpy10: while (1) {
*pbx++ = *p0r++; /* copy element */
if (p0r < p0e) /* if not end of run continue */
continue;
break;
}
pax += rsz << 2; /* setup for next set of runs */
}
pax = a; /* swap ptrs */
a = b;
b = pax;
rsz <<= 2; /* quadruple run size */
}
}
size_t GetPassCount(size_t n) /* return # passes */
{
size_t i = 0;
size_t s;
for(s = 1; s < n; s <<= 2)
i += 1;
return(i);
}
uint64_t rnd64() /* random 64 bit integer */
{
static uint64_t r = 0ull;
#if 1
r = r * 6364136223846793005ull + 1442695040888963407ull;
#endif
#if 0
r = (((uint64_t)((rand()>>4) & 0xff)) << 0);
r += (((uint64_t)((rand()>>4) & 0xff)) << 8);
r += (((uint64_t)((rand()>>4) & 0xff)) <<16);
r += (((uint64_t)((rand()>>4) & 0xff)) <<24);
r += (((uint64_t)((rand()>>4) & 0xff)) <<32);
r += (((uint64_t)((rand()>>4) & 0xff)) <<40);
r += (((uint64_t)((rand()>>4) & 0xff)) <<48);
r += (((uint64_t)((rand()>>4) & 0xff)) <<56);
#endif
#if 0
r += 1ull;
#endif
return r;
}
#define COUNT (16*1024*1024)
int main()
{
uint64_t * a = malloc(COUNT * sizeof(uint64_t));
uint64_t * b = malloc(COUNT * sizeof(uint64_t));
size_t i;
for(i = 0; i < COUNT; i++)
a[i] = rnd64();
#if QP
QueryPerformanceFrequency(&liQPFrequency);
dQPFrequency = (double)liQPFrequency.QuadPart;
timeBeginPeriod(256); /* set ticker to 4 hz */
Sleep(512); /* wait for it to settle */
QueryPerformanceCounter(&liStartTime);
#else
ctTimeStart = clock();
#endif
MergeSort(a, b, COUNT);
#if QP
QueryPerformanceCounter(&liStopTime);
dStartTime = (double)liStartTime.QuadPart;
dStopTime = (double)liStopTime.QuadPart;
dElapsedTime = (dStopTime - dStartTime) / dQPFrequency;
timeEndPeriod(256); /* restore ticker to default */
printf("# of seconds %f\n", dElapsedTime);
#else
ctTimeStop = clock();
printf("# of ticks %d\n", ctTimeStop - ctTimeStart);
#endif
for(i = 1; i < COUNT; i++){
if(a[i] < a[i-1])
break;
}
if(i == COUNT)
printf("passed\n");
else
printf("failed\n");
free(b);
free(a);
return 0;
}