Skip to content

Add a qsort implementation to Mibench/qsort #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 61 additions & 30 deletions build/MiBench/patches/MiBench/automotive/qsort/qsort_large.c
Original file line number Diff line number Diff line change
@@ -1,57 +1,88 @@
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define UNLIMIT
#define MAXARRAY 999999999 // ED: was 60000 /* this number, if too large, will cause a seg. fault!! */
#define MAXARRAY \
999999999 // ED: was 60000 /* this number, if too large, will cause a seg.
// fault!! */

struct my3DVertexStruct {
int x, y, z;
double distance;
};
typedef struct my3DVertexStruct vertex_t;

int compare(const void *elem1, const void *elem2)
{
int vertex_compare(const vertex_t *restrict elem1,
const vertex_t *restrict elem2) {
/* D = [(x1 - x2)^2 + (y1 - y2)^2 + (z1 - z2)^2]^(1/2) */
/* sort based on distances from the origin... */

double distance1, distance2;

distance1 = (*((struct my3DVertexStruct *)elem1)).distance;
distance2 = (*((struct my3DVertexStruct *)elem2)).distance;
distance1 = (*((vertex_t *)elem1)).distance;
distance2 = (*((vertex_t *)elem2)).distance;

return (distance1 > distance2) ? 1 : ((distance1 == distance2) ? 0 : -1);
}

void quicksort(off_t bottom, off_t top, vertex_t *restrict data) {
off_t lower, upper;
vertex_t temp;
if (bottom >= top)
return;

vertex_t *pivot = &data[bottom];

int
main(int argc, char *argv[]) {
//struct my3DVertexStruct array[MAXARRAY];
struct my3DVertexStruct *array = (struct my3DVertexStruct *) malloc(MAXARRAY*sizeof(struct my3DVertexStruct)); // ED: let's allocate this monster on the heap, rather on the stack
for (lower = bottom, upper = top; lower < upper;) {
while (lower <= upper && vertex_compare(&data[lower], pivot) < 0) {
lower++;
}
while (lower <= upper && vertex_compare(&data[upper], pivot) > 0) {
upper--;
}
if (lower < upper) {
temp = data[lower];
data[lower] = data[upper];
data[upper] = temp;
}
}

temp = data[bottom];
data[bottom] = data[upper];
data[upper] = temp;

quicksort(bottom, upper - 1, data);
quicksort(upper + 1, top, data);
}

int main(int argc, char *argv[]) {
// vertex_t array[MAXARRAY];
vertex_t *array = (vertex_t *)malloc(
MAXARRAY * sizeof(vertex_t)); // ED: let's allocate this monster on
// the heap, rather on the stack

FILE *fp;
int i,count=0;
int i, count = 0;
int x, y, z;
if (argc<2) {
fprintf(stderr,"Usage: qsort_large <file>\n");

if (argc < 2) {
fprintf(stderr, "Usage: qsort_large <file>\n");
exit(-1);
}
else {
fp = fopen(argv[1],"r");

while((fscanf(fp, "%d", &x) == 1) && (fscanf(fp, "%d", &y) == 1) && (fscanf(fp, "%d", &z) == 1) && (count < MAXARRAY)) {
array[count].x = x;
array[count].y = y;
array[count].z = z;
array[count].distance = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
count++;
} else {
fp = fopen(argv[1], "r");

while ((fscanf(fp, "%d", &x) == 1) && (fscanf(fp, "%d", &y) == 1) &&
(fscanf(fp, "%d", &z) == 1) && (count < MAXARRAY)) {
array[count].x = x;
array[count].y = y;
array[count].z = z;
array[count].distance = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
count++;
}
}
printf("\nSorting %d vectors based on distance from the origin.\n\n",count);
qsort(array,count,sizeof(struct my3DVertexStruct),compare);

for(i=0;i<count;i++)
printf("%d %d %d\n", array[i].x, array[i].y, array[i].z);
printf("\nSorting %d vectors based on distance from the origin.\n\n", count);
quicksort(0, count, array);

return 0;
}