since it has more code to optimize. Given enough memory we are going to sort 100 million random 32-bit numbers. So far I have:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
int MAX_ENTRIES;
#define QSORT 0 // 0=C | 1=ASM
#define DEBUG 0
#define ITEMS 100000000 // items to sort (100 million)
struct hash
{ unsigned long key1;
unsigned long key2;
unsigned char byte1;
unsigned char byte2; };
struct hash *cht;
static int time1,time2;
// Create random 32 bit keys
// =========================
static unsigned long x_random[55] = {
1410651636UL, 3012776752UL, 3497475623UL, 2892145026UL, 1571949714UL,
3253082284UL, 3489895018UL, 387949491UL, 2597396737UL, 1981903553UL,
3160251843UL, 129444464UL, 1851443344UL, 4156445905UL, 224604922UL,
1455067070UL, 3953493484UL, 1460937157UL, 2528362617UL, 317430674UL,
3229354360UL, 117491133UL, 832845075UL, 1961600170UL, 1321557429UL,
747750121UL, 545747446UL, 810476036UL, 503334515UL, 4088144633UL,
2824216555UL, 3738252341UL, 3493754131UL, 3672533954UL, 29494241UL,
1180928407UL, 4213624418UL, 33062851UL, 3221315737UL, 1145213552UL,
2957984897UL, 4078668503UL, 2262661702UL, 65478801UL, 2527208841UL,
1960622036UL, 315685891UL, 1196037864UL, 804614524UL, 1421733266UL,
2017105031UL, 3882325900UL, 810735053UL, 384606609UL, 2393861397UL };
static int init_random = 1;
static unsigned long y_random[55];
static int j_random, k_random;
static unsigned long ul_random;
void zobrist(void);
void quicksort(void);
int main (int argc,char *argv[])
{ int x;
// Get free memory
// ===============
static size_t ms;
static char *p_memory,*bgn;
for (ms = (size_t)(3ull*1024*1048576); ms >= 2000; ms -= 100000) // search till 12 Gb
{ p_memory = (char*)malloc(ms); if (p_memory) break; }
printf("Free memory = %d Mb\n\n",ms>>20);
free(p_memory);
p_memory=(char*)malloc(ms);
cht=(struct hash *)p_memory;
bgn=p_memory;
p_memory=p_memory+ms;
MAX_ENTRIES=(int)((p_memory-bgn)/10); // length structure *cht
MAX_ENTRIES=MAX_ENTRIES-100; // create some slack space
printf("Allocated space for %d Mb entries\n\n",MAX_ENTRIES>>20);
if (MAX_ENTRIES < ITEMS) { printf("Not enough memory\n\n"); exit(1); }
MAX_ENTRIES=ITEMS;
printf("Creating %d Zobrist keys\n\n",MAX_ENTRIES);
zobrist(); // make 100 million random 32-bit keys
printf("Sorting now... ");
time1=clock();
quicksort();
time2=clock();
printf("Done in %d\n\n",time2-time1);
#if DEBUG
for (x=0; x<20; x++) printf("%8x\t %8x\t (%2x) (%2x)\n",
cht[x].key1,cht[x].key2,cht[x].byte1,cht[x].byte2);
#endif
}
void zobrist()
// Create random 32-bit integers
// =============================
{ int x;
if (init_random)
{ int i;
init_random = 0;
for (i = 0; i < 55; i++) y_random[i] = x_random[i];
j_random = 24 - 1;
k_random = 55 - 1; }
for (x=0; x<MAX_ENTRIES; x++)
{ ul_random = (y_random[k_random] += y_random[j_random]);
if (--j_random < 0) j_random = 55 - 1;
if (--k_random < 0) k_random = 55 - 1;
cht[x].key1=0;
cht[x].key1=cht[x].key1 ^ ul_random; // key1
cht[x].key2=cht[x].key1; // key2 (copy to check proper swapping)
cht[x].byte1=cht[x].key1; // byte1 (lower 8 bits to check proper swapping)
cht[x].byte2=cht[x].key1>>24; // byte1 (first 8 bits to check proper swapping)
}
#if DEBUG
for (x=0; x<20; x++) printf("%8x\t %8x\t (%2x) (%2x)\n",
cht[x].key1,cht[x].key2,cht[x].byte1,cht[x].byte2);
printf("\n");
#endif
}
void quicksort()
{ unsigned int h,zz; unsigned char ch;
int t1,l,i,j,r;
int st1[3000],st2[3000];
t1=1; st1[1]=0; st2[1]=MAX_ENTRIES-1;
lab8: l=st1[t1]; r=st2[t1]; t1=t1-1;
lab9: i=l; j=r;
h=cht[(l+r)>>1].key1; // delen door 2
lab10: if (cht[i].key1>=h || i >= r) goto lab20;
i=i+1; goto lab10;
lab20: if (cht[j].key1<=h || j <= l) goto lab30;
j=j-1; goto lab20;
lab30: if (i > j) goto lab40;
zz=cht[i].key1; cht[i].key1=cht[j].key1; cht[j].key1=zz;
zz=cht[i].key2; cht[i].key2=cht[j].key2; cht[j].key2=zz;
ch=cht[i].byte1; cht[i].byte1=cht[j].byte1; cht[j].byte1=ch;
ch=cht[i].byte2; cht[i].byte2=cht[j].byte2; cht[j].byte2=ch;
i=i+1; j=j-1;
lab40: if (i <= j) goto lab10;
if (r-i <= j-l) goto lab60;
if (l >= j) goto lab50;
t1=t1+1; st1[t1]=l; st2[t1]=j;
lab50: l=i; goto lab80;
lab60: if (i >= r) goto lab70;
t1=t1+1; st1[t1]=i; st2[t1]=r;
lab70: r=j;
lab80: if (r > l) goto lab9;
if (t1) goto lab8;
}