//-------------------------------------------------------------------------------------------------- // // @ CopyRight Roberti & Parau Enterprises, Inc. 2021-2023 // // This work is licensed under the Creative Commons Attribution 4.0 International License. // To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/ // or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. // //-------------------------------------------------------------------------------------------------- // // Driver to test and compare performance of the HEAPSORT macro // provided with the RISC-V module of DVASM // // This program sort a vector of pointer to null terminated strings. // in ascending order. The strings themselves are never moved, // only the pointers in the vector are moved. // // The following definition are used to control compilation // // #define SIZE numnber_of_strings // // It must always be defined. For example if // // #define SIZE 8 // // The number of strings will be 8 // // #define STRSIZE string_size // // It must always be defined. It specifies the length of // each string. For example if // // #define STRSIZE 80 // // THe length of each string will be 80 including the null terminator. // // #define FUNCTION // // It must always be defined. Valid values are: // // dvasmsortstring // linuxsortstring // optsortstring // // #define REPEAT number_of_repeat // // It must always be defined. It is used to test performance // For example: // // #define REPEAT 1000 // // will repeat the sort of the same vector 1000 times // // #define PRINT // // When this definition is included the sorted strins are printed // to check for sort correctness // #include #include #include #include //----------------------- Compilation definitions ----------------- #define SIZE (1 << 16) #define STRSIZE (80) #define FUNCTION dvasmsortstring #define REPEAT 10000 //#define PRINT //----------------------- End of compilation definitions ---------- void dvasmsortstring(void *base, size_t num); void sortlinux(void *base, size_t num, size_t size, long (*cmp_func)(const long *, const long *), void (*swap_func)(void *, void *, int size)); #define linuxsortstring(v, l) sortlinux(v, l, sizeof(long), &comp_func, NULL) long comp_func(const long *a, const long *b) { return strcmp((char *) (*a), (char *) (*b)); } void optsortstring(void *base, size_t num); int main(int argc, char **argv) { // // Allocate vector and buffer // char **v= (char **) malloc(SIZE * sizeof(char *)); char *buff= (char *) malloc(SIZE * STRSIZE); // // Seed the random number generator // srandom(17); // // Generate random characters in strings // char *c= buff; for (int i= 0; i < SIZE * STRSIZE; i++) { *c= (random() % 75)+48; c++; } // // Insert null character at the end of each string // c= buff; for (int i= 0; i < SIZE; i++) { v[i]= c; c[STRSIZE-1]= 0; c+= STRSIZE; } // // Sort the vector as amny times as specified in REPAT // for (int i= 0; i < REPEAT; i++) FUNCTION(v, SIZE); // Sort vector // // Generate print code if PRINT is defined // #ifdef PRINT for (int i= 0; i < SIZE; i++) printf("Heap entry is: %6d %s\n", i, v[i]); #endif }